msvc-kit 0.2.10

A portable MSVC Build Tools installer and manager for Rust development
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! CLI exit code behavior tests
//!
//! Validates that the msvc-kit CLI exits with correct codes for winget compatibility:
//! - Exit code 0 when no subcommand is provided (prints help)
//! - Exit code 0 for successful operations
//! - Exit code 1 for errors

use rstest::rstest;
use std::process::Command;

/// Helper function to get the path to the msvc-kit binary
fn get_binary_path() -> std::path::PathBuf {
    let mut path = std::env::current_exe()
        .expect("Failed to get current executable path")
        .parent()
        .expect("Failed to get parent directory")
        .to_path_buf();

    // Navigate from target/{debug|release}/deps to target/{debug|release}
    if path.ends_with("deps") {
        path.pop();
    }

    path.push(if cfg!(windows) {
        "msvc-kit.exe"
    } else {
        "msvc-kit"
    });

    path
}

/// Helper function to run msvc-kit command and capture output
fn run_command(args: &[&str]) -> std::io::Result<std::process::Output> {
    Command::new(get_binary_path()).args(args).output()
}

#[test]
fn test_no_subcommand_exits_zero() {
    // Running without any subcommand should print help and exit with code 0
    // This is critical for winget validation
    let output = run_command(&[]).expect("Failed to run msvc-kit");

    assert!(
        output.status.success(),
        "Expected exit code 0 when no subcommand is provided, got: {:?}",
        output.status.code()
    );

    // Verify help text is printed
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("msvc-kit") || stdout.contains("Usage:"),
        "Expected help output to be printed"
    );
}

#[test]
fn test_help_flag_exits_zero() {
    // --help should exit with code 0
    let output = run_command(&["--help"]).expect("Failed to run msvc-kit --help");

    assert!(
        output.status.success(),
        "Expected exit code 0 for --help, got: {:?}",
        output.status.code()
    );
}

#[test]
fn test_verbose_help_exits_zero() {
    // --verbose --help should also exit with code 0 and exercise debug filter path
    let output =
        run_command(&["--verbose", "--help"]).expect("Failed to run msvc-kit --verbose --help");

    assert!(
        output.status.success(),
        "Expected exit code 0 for --verbose --help, got: {:?}",
        output.status.code()
    );
}

#[test]
fn test_version_flag_exits_zero() {
    // --version should exit with code 0
    let output = run_command(&["--version"]).expect("Failed to run msvc-kit --version");

    assert!(
        output.status.success(),
        "Expected exit code 0 for --version, got: {:?}",
        output.status.code()
    );

    // Verify version is printed
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("msvc-kit") || !stdout.is_empty(),
        "Expected version output to be printed"
    );
}

#[test]
fn test_subcommand_help_exits_zero() {
    // Subcommand help should exit with code 0
    let commands = [
        "download", "setup", "list", "clean", "config", "env", "bundle", "update",
    ];

    for cmd in commands {
        let output = run_command(&[cmd, "--help"])
            .unwrap_or_else(|_| panic!("Failed to run msvc-kit {} --help", cmd));

        assert!(
            output.status.success(),
            "Expected exit code 0 for {} --help, got: {:?}",
            cmd,
            output.status.code()
        );
    }
}

#[rstest]
#[case(&["config"])]
#[case(&["config", "--reset"])]
fn test_config_command_exits_zero(#[case] args: &[&str]) {
    // Config command with valid arguments should exit with code 0
    let output = run_command(args)
        .unwrap_or_else(|_| panic!("Failed to run msvc-kit config with args {:?}", args));

    assert!(
        output.status.success(),
        "Expected exit code 0 for config command with args {:?}, got: {:?}",
        args,
        output.status.code()
    );
}

#[test]
fn test_invalid_subcommand_exits_nonzero() {
    // Invalid subcommand should exit with non-zero code
    let output = run_command(&["invalid-command"]).expect("Failed to run msvc-kit");

    assert!(
        !output.status.success(),
        "Expected non-zero exit code for invalid subcommand, got: {:?}",
        output.status.code()
    );
}

#[test]
fn test_bundle_without_license_exits_nonzero() {
    // Bundle command without --accept-license should exit with non-zero code
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
    let output_path = temp_dir.path().join("bundle");

    let output = run_command(&[
        "bundle",
        "--output",
        output_path.to_str().unwrap(),
        "--arch",
        "x64",
    ])
    .expect("Failed to run msvc-kit bundle");

    assert!(
        !output.status.success(),
        "Expected non-zero exit code for bundle without license acceptance, got: {:?}",
        output.status.code()
    );

    // Verify error message mentions license
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let output_text = format!("{}{}", stdout, stderr);
    assert!(
        output_text.contains("license") || output_text.contains("License"),
        "Expected license-related error message"
    );
}

#[test]
fn test_setup_without_installation_exits_nonzero() {
    // Setup command without prior installation should exit with non-zero code
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let output = run_command(&["setup", "--dir", temp_dir.path().to_str().unwrap()])
        .expect("Failed to run msvc-kit setup");

    assert!(
        !output.status.success(),
        "Expected non-zero exit code for setup without installation, got: {:?}",
        output.status.code()
    );

    // Verify error message mentions missing installation
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("No MSVC installation found") || stderr.contains("not found"),
        "Expected error about missing installation"
    );
}

#[test]
fn test_clean_nonexistent_version_exits_zero() {
    // Clean command with nonexistent version should not fail (idempotent)
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let output = run_command(&[
        "clean",
        "--dir",
        temp_dir.path().to_str().unwrap(),
        "--msvc-version",
        "99.99.99999",
    ])
    .expect("Failed to run msvc-kit clean");

    // Clean should be idempotent and exit successfully even if version doesn't exist
    assert!(
        output.status.success(),
        "Expected exit code 0 for clean with nonexistent version, got: {:?}",
        output.status.code()
    );
}

#[test]
fn test_list_empty_dir_exits_zero() {
    // List command with empty directory should exit with code 0
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let output = run_command(&["list", "--dir", temp_dir.path().to_str().unwrap()])
        .expect("Failed to run msvc-kit list");

    assert!(
        output.status.success(),
        "Expected exit code 0 for list with empty directory, got: {:?}",
        output.status.code()
    );

    // Verify appropriate message is printed
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("No installations found") || stdout.contains("Installed versions"),
        "Expected appropriate list output"
    );
}

#[test]
fn test_invalid_architecture_exits_nonzero() {
    // Commands with invalid architecture should exit with non-zero code
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let output = run_command(&[
        "download",
        "--target",
        temp_dir.path().to_str().unwrap(),
        "--arch",
        "invalid-arch",
        "--no-msvc",
        "--no-sdk",
    ])
    .expect("Failed to run msvc-kit download");

    assert!(
        !output.status.success(),
        "Expected non-zero exit code for invalid architecture, got: {:?}",
        output.status.code()
    );

    // Verify error message mentions architecture
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("architecture") || stderr.contains("arch"),
        "Expected error about invalid architecture"
    );
}

// Note: update command test is intentionally omitted because:
// 1. It depends on network availability which makes tests flaky
// 2. The self-update feature may not always be compiled in
// 3. Exit codes can vary (0=success, 1=error, 2=unknown command, 101=panic)
// Manual testing of update command is recommended instead.

#[test]
fn test_env_command_without_installation_exits_nonzero() {
    // Env command without installation should exit with non-zero code
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let output = run_command(&["env", "--dir", temp_dir.path().to_str().unwrap()])
        .expect("Failed to run msvc-kit env");

    assert!(
        !output.status.success(),
        "Expected non-zero exit code for env without installation, got: {:?}",
        output.status.code()
    );
}

#[rstest]
#[case("json")]
fn test_env_output_format(#[case] format: &str) {
    // Test that different output formats are accepted (though may fail without installation)
    let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");

    let output = run_command(&[
        "env",
        "--dir",
        temp_dir.path().to_str().unwrap(),
        "--format",
        format,
    ])
    .expect("Failed to run msvc-kit env");

    // Without installation, should exit with non-zero
    assert!(
        !output.status.success(),
        "Expected non-zero exit code for env without installation (format: {}), got: {:?}",
        format,
        output.status.code()
    );
}

// ============================================================================
// WinGet Release Workflow Validation Tests
// ============================================================================

/// Verify the release workflow contains the winget-releaser configuration
/// and uses a strict regex that matches exactly one binary file.
/// This prevents the "Duplicate installer entry found" winget validation error.
#[test]
fn test_release_workflow_has_winget_updater() {
    let workflow_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join(".github")
        .join("workflows")
        .join("release.yml");

    let content = std::fs::read_to_string(&workflow_path).expect("Failed to read release.yml");

    // Verify winget-releaser action is present
    assert!(
        content.contains("vedantmgoyal2009/winget-releaser@v2"),
        "release.yml must contain vedantmgoyal2009/winget-releaser@v2"
    );

    // Verify package identifier
    assert!(
        content.contains("identifier: loonghao.msvc-kit"),
        "release.yml must specify the correct winget package identifier"
    );

    // Verify strict regex: must anchor with ^ and $ to match exactly one file
    assert!(
        content.contains("'^msvc-kit-x86_64-windows\\.exe$'"),
        "release.yml installers-regex must use anchored pattern to prevent duplicate entries"
    );
}

/// Verify that the release workflow builds exactly one binary (x64 only)
/// to avoid creating duplicate installer entries in winget manifest.
#[test]
fn test_release_workflow_single_architecture_binary() {
    let workflow_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join(".github")
        .join("workflows")
        .join("release.yml");

    let content = std::fs::read_to_string(&workflow_path).expect("Failed to read release.yml");

    // Verify only x64 binary is built (no x86 or arm64 builds)
    assert!(
        content.contains("msvc-kit-x86_64-windows"),
        "release.yml must build the x86_64 Windows binary"
    );

    // Ensure we don't upload multiple architectures that could cause duplicate entries
    let x86_count = content.matches("msvc-kit-i686-windows").count();
    let arm64_count = content.matches("msvc-kit-aarch64-windows").count();

    assert_eq!(
        x86_count, 0,
        "release.yml must NOT upload i686 binary to avoid duplicate winget entries"
    );
    assert_eq!(
        arm64_count, 0,
        "release.yml must NOT upload aarch64 binary to avoid duplicate winget entries"
    );
}

/// Verify the update-winget job runs after github-release to ensure
/// assets are available when winget-releaser fetches them.
#[test]
fn test_release_workflow_winget_job_ordering() {
    let workflow_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join(".github")
        .join("workflows")
        .join("release.yml");

    let content = std::fs::read_to_string(&workflow_path).expect("Failed to read release.yml");

    // Verify the update-winget job depends on github-release
    assert!(
        content.contains("update-winget:"),
        "release.yml must contain the update-winget job"
    );

    // Verify there's a sleep/wait step before winget update
    assert!(
        content.contains("Waiting for release assets to be fully available"),
        "release.yml must wait for release assets before updating winget"
    );
}