|
| 1 | +use std::process::Command; |
| 2 | + |
| 3 | +/// This test ensures that flags like --help after the crate spec are passed to the target binary |
| 4 | +/// and not intercepted by cargox's own argument parser. |
| 5 | +/// |
| 6 | +/// Regression test for: cargox bat --help should show bat's help, not cargox's help |
| 7 | +#[test] |
| 8 | +fn test_help_flag_passed_to_binary() { |
| 9 | + // Build the binary first |
| 10 | + let binary_path = env!("CARGO_BIN_EXE_cargox"); |
| 11 | + |
| 12 | + // Run: cargox bat --help |
| 13 | + // We can't actually install bat in the test, but we can verify that: |
| 14 | + // 1. cargox doesn't show its own help (which would exit with code 0) |
| 15 | + // 2. The --help flag gets passed through to the execution logic |
| 16 | + // |
| 17 | + // Since bat isn't installed, this will fail trying to find/install bat, |
| 18 | + // but importantly it won't show cargox's help text |
| 19 | + let output = Command::new(binary_path) |
| 20 | + .args(["bat", "--help"]) |
| 21 | + .output() |
| 22 | + .expect("Failed to execute cargox"); |
| 23 | + |
| 24 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 25 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 26 | + |
| 27 | + // The key assertion: cargox's help should NOT appear |
| 28 | + // If the help was intercepted, we'd see "Run Cargo binaries on demand" in the output |
| 29 | + assert!( |
| 30 | + !stdout.contains("Run Cargo binaries on demand"), |
| 31 | + "cargox help text appeared in stdout, indicating --help was intercepted.\nStdout:\n{}", |
| 32 | + stdout |
| 33 | + ); |
| 34 | + assert!( |
| 35 | + !stderr.contains("Run Cargo binaries on demand"), |
| 36 | + "cargox help text appeared in stderr, indicating --help was intercepted.\nStderr:\n{}", |
| 37 | + stderr |
| 38 | + ); |
| 39 | + |
| 40 | + // We should see some error about bat not being found/installed |
| 41 | + // or if it's already on PATH, it would run bat's help |
| 42 | + // Either way, we shouldn't see cargox's help |
| 43 | +} |
| 44 | + |
| 45 | +/// Test that cargox's own help still works when invoked without a crate spec |
| 46 | +#[test] |
| 47 | +fn test_cargox_help_still_works() { |
| 48 | + let binary_path = env!("CARGO_BIN_EXE_cargox"); |
| 49 | + |
| 50 | + let output = Command::new(binary_path) |
| 51 | + .args(["--help"]) |
| 52 | + .output() |
| 53 | + .expect("Failed to execute cargox"); |
| 54 | + |
| 55 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 56 | + |
| 57 | + // When --help comes before the crate spec, it should show cargox's help |
| 58 | + assert!( |
| 59 | + stdout.contains("Run Cargo binaries on demand"), |
| 60 | + "cargox help should appear when --help is used without a crate spec.\nStdout:\n{}", |
| 61 | + stdout |
| 62 | + ); |
| 63 | + assert!(output.status.success(), "cargox --help should exit successfully"); |
| 64 | +} |
| 65 | + |
| 66 | +/// Test that flags before the crate spec are still parsed by cargox |
| 67 | +#[test] |
| 68 | +fn test_cargox_flags_before_crate_spec() { |
| 69 | + let binary_path = env!("CARGO_BIN_EXE_cargox"); |
| 70 | + |
| 71 | + // Run: cargox --force bat |
| 72 | + // The --force flag should be recognized by cargox |
| 73 | + let output = Command::new(binary_path) |
| 74 | + .args(["--force", "bat"]) |
| 75 | + .output() |
| 76 | + .expect("Failed to execute cargox"); |
| 77 | + |
| 78 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 79 | + |
| 80 | + // If --force was parsed correctly, cargox would proceed with installation |
| 81 | + // We should not see cargox's help text |
| 82 | + assert!( |
| 83 | + !stderr.contains("Run Cargo binaries on demand"), |
| 84 | + "cargox help text appeared, indicating argument parsing failed.\nStderr:\n{}", |
| 85 | + stderr |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +/// Test that --bin flag works correctly |
| 90 | +#[test] |
| 91 | +fn test_bin_flag_parsing() { |
| 92 | + let binary_path = env!("CARGO_BIN_EXE_cargox"); |
| 93 | + |
| 94 | + // Run: cargox --bin custom mycrate --help |
| 95 | + // The --help should be passed to the binary, not cargox |
| 96 | + let output = Command::new(binary_path) |
| 97 | + .args(["--bin", "custom", "mycrate", "--help"]) |
| 98 | + .output() |
| 99 | + .expect("Failed to execute cargox"); |
| 100 | + |
| 101 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 102 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 103 | + |
| 104 | + // Should not show cargox help |
| 105 | + assert!( |
| 106 | + !stdout.contains("Run Cargo binaries on demand"), |
| 107 | + "cargox help appeared when it shouldn't.\nStdout:\n{}", |
| 108 | + stdout |
| 109 | + ); |
| 110 | + assert!( |
| 111 | + !stderr.contains("Run Cargo binaries on demand"), |
| 112 | + "cargox help appeared when it shouldn't.\nStderr:\n{}", |
| 113 | + stderr |
| 114 | + ); |
| 115 | +} |
0 commit comments