|
| 1 | +//! Integration tests of the bundles crate. |
| 2 | +
|
| 3 | +use std::env; |
| 4 | +use std::path::PathBuf; |
| 5 | +use std::process::Command; |
| 6 | + |
| 7 | +const PRE_FORMAT_VERSION: u32 = 31; |
| 8 | +const TEST_FORMAT_VERSION: u32 = 32; |
| 9 | +const TEST_BUNDLE_LOCKED: &str = "https://example.com/locked_bundle.tar"; |
| 10 | +const TEST_BUNDLE_PREFIX: &str = "https://custom.example.com"; |
| 11 | + |
| 12 | +/// Runs the test project and gets the output |
| 13 | +fn run_test_program(format_version: u32, env_vars: &[(&str, &str)]) -> String { |
| 14 | + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 15 | + let test_dir = PathBuf::from(&manifest_dir) |
| 16 | + .join("tests") |
| 17 | + .join("bundle_env_overrides_test"); |
| 18 | + let target_platform = env::var("TARGET").unwrap(); |
| 19 | + |
| 20 | + let mut cmd = Command::new("cargo"); |
| 21 | + cmd.arg("run") |
| 22 | + .arg("--quiet") |
| 23 | + .arg("--target") |
| 24 | + .arg(target_platform) |
| 25 | + .arg("--") |
| 26 | + .arg(format_version.to_string()) |
| 27 | + .current_dir(&test_dir); |
| 28 | + |
| 29 | + for (key, value) in env_vars { |
| 30 | + cmd.env(key, value); |
| 31 | + } |
| 32 | + |
| 33 | + let output = cmd.output().expect("Failed to run test program"); |
| 34 | + assert!( |
| 35 | + output.status.success(), |
| 36 | + "Test program failed: {}", |
| 37 | + String::from_utf8_lossy(&output.stderr) |
| 38 | + ); |
| 39 | + String::from_utf8(output.stdout).unwrap().trim().to_string() |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn test_bundle_locked() { |
| 44 | + let url_locked = run_test_program( |
| 45 | + PRE_FORMAT_VERSION, |
| 46 | + &[("TECTONIC_BUNDLE_LOCKED", TEST_BUNDLE_LOCKED)], |
| 47 | + ); |
| 48 | + let url_locked_versioned = run_test_program( |
| 49 | + TEST_FORMAT_VERSION, |
| 50 | + &[("TECTONIC_BUNDLE_LOCKED", TEST_BUNDLE_LOCKED)], |
| 51 | + ); |
| 52 | + |
| 53 | + assert_eq!(url_locked, TEST_BUNDLE_LOCKED); |
| 54 | + assert_eq!(url_locked_versioned, TEST_BUNDLE_LOCKED); |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn test_bundle_prefix() { |
| 59 | + let url_prefixed = run_test_program( |
| 60 | + PRE_FORMAT_VERSION, |
| 61 | + &[("TECTONIC_BUNDLE_PREFIX", TEST_BUNDLE_PREFIX)], |
| 62 | + ); |
| 63 | + let url_prefixed_versioned = run_test_program( |
| 64 | + TEST_FORMAT_VERSION, |
| 65 | + &[("TECTONIC_BUNDLE_PREFIX", TEST_BUNDLE_PREFIX)], |
| 66 | + ); |
| 67 | + |
| 68 | + assert_eq!( |
| 69 | + url_prefixed, |
| 70 | + format!("{TEST_BUNDLE_PREFIX}/default_bundle.tar") |
| 71 | + ); |
| 72 | + assert_eq!( |
| 73 | + url_prefixed_versioned, |
| 74 | + format!("{TEST_BUNDLE_PREFIX}/default_bundle_v{TEST_FORMAT_VERSION}.tar") |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn test_precedence_locked_over_prefix() { |
| 80 | + let url_both_env_set = run_test_program( |
| 81 | + TEST_FORMAT_VERSION, |
| 82 | + &[ |
| 83 | + ("TECTONIC_BUNDLE_LOCKED", TEST_BUNDLE_LOCKED), |
| 84 | + ("TECTONIC_BUNDLE_PREFIX", TEST_BUNDLE_PREFIX), |
| 85 | + ], |
| 86 | + ); |
| 87 | + assert_eq!(url_both_env_set, TEST_BUNDLE_LOCKED); |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn test_empty_locked_bundle_ignored() { |
| 92 | + let url_empty_locked = run_test_program( |
| 93 | + TEST_FORMAT_VERSION, |
| 94 | + &[ |
| 95 | + ("TECTONIC_BUNDLE_LOCKED", ""), |
| 96 | + ("TECTONIC_BUNDLE_PREFIX", TEST_BUNDLE_PREFIX), |
| 97 | + ], |
| 98 | + ); |
| 99 | + assert_eq!( |
| 100 | + url_empty_locked, |
| 101 | + format!("{TEST_BUNDLE_PREFIX}/default_bundle_v{TEST_FORMAT_VERSION}.tar",) |
| 102 | + ); |
| 103 | +} |
0 commit comments