|
| 1 | +//! Tests for multiple build scripts feature. |
| 2 | +
|
| 3 | +use cargo_test_support::git; |
| 4 | +use cargo_test_support::prelude::*; |
| 5 | +use cargo_test_support::str; |
| 6 | +use cargo_test_support::{project, Project}; |
| 7 | + |
| 8 | +#[cargo_test] |
| 9 | +fn build_without_feature_enabled_aborts_with_error() { |
| 10 | + let p = project() |
| 11 | + .file( |
| 12 | + "Cargo.toml", |
| 13 | + r#" |
| 14 | + [package] |
| 15 | + name = "foo" |
| 16 | + version = "0.1.0" |
| 17 | + edition = "2024" |
| 18 | + build = ["build1.rs", "build2.rs"] |
| 19 | + "#, |
| 20 | + ) |
| 21 | + .file("src/main.rs", "fn main() {}") |
| 22 | + .file("build1.rs", "fn main() {}") |
| 23 | + .file("build2.rs", "fn main() {}") |
| 24 | + .build(); |
| 25 | + p.cargo("check") |
| 26 | + .masquerade_as_nightly_cargo(&["multiple-build-scripts"]) |
| 27 | + .with_status(101) |
| 28 | + .with_stderr_data(str![[r#" |
| 29 | +[ERROR] invalid type: sequence, expected a boolean or string |
| 30 | + --> Cargo.toml:6:25 |
| 31 | + | |
| 32 | +6 | build = ["build1.rs", "build2.rs"] |
| 33 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 34 | + | |
| 35 | +
|
| 36 | +"#]]) |
| 37 | + .run(); |
| 38 | +} |
| 39 | + |
| 40 | +fn basic_empty_project() -> Project { |
| 41 | + project() |
| 42 | + .file( |
| 43 | + "Cargo.toml", |
| 44 | + r#" |
| 45 | + cargo-features = ["multiple-build-scripts"] |
| 46 | +
|
| 47 | + [package] |
| 48 | + name = "foo" |
| 49 | + version = "0.1.0" |
| 50 | + edition = "2024" |
| 51 | + build = ["build1.rs", "build2.rs"] |
| 52 | + "#, |
| 53 | + ) |
| 54 | + .file("src/main.rs", "fn main() {}") |
| 55 | + .file("build1.rs", "fn main() {}") |
| 56 | + .file("build2.rs", "fn main() {}") |
| 57 | + .build() |
| 58 | +} |
| 59 | + |
| 60 | +#[cargo_test] |
| 61 | +fn empty_multiple_build_script_project() { |
| 62 | + let p = basic_empty_project(); |
| 63 | + p.cargo("check") |
| 64 | + .masquerade_as_nightly_cargo(&["multiple-build-scripts"]) |
| 65 | + .with_status(101) |
| 66 | + .with_stderr_data(str![[r#" |
| 67 | +[ERROR] invalid type: sequence, expected a boolean or string |
| 68 | + --> Cargo.toml:8:25 |
| 69 | + | |
| 70 | +8 | build = ["build1.rs", "build2.rs"] |
| 71 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 | + | |
| 73 | +
|
| 74 | +"#]]) |
| 75 | + .run(); |
| 76 | +} |
| 77 | + |
| 78 | +#[cargo_test] |
| 79 | +fn multiple_build_scripts_metadata() { |
| 80 | + let p = basic_empty_project(); |
| 81 | + p.cargo("metadata --format-version=1") |
| 82 | + .masquerade_as_nightly_cargo(&["multiple-build-scripts"]) |
| 83 | + .with_status(101) |
| 84 | + .with_stderr_data(str![[r#" |
| 85 | +[ERROR] invalid type: sequence, expected a boolean or string |
| 86 | + --> Cargo.toml:8:25 |
| 87 | + | |
| 88 | +8 | build = ["build1.rs", "build2.rs"] |
| 89 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 90 | + | |
| 91 | +
|
| 92 | +"#]]) |
| 93 | + .run(); |
| 94 | +} |
| 95 | + |
| 96 | +#[cargo_test] |
| 97 | +fn verify_package_multiple_build_scripts() { |
| 98 | + let p = basic_empty_project(); |
| 99 | + |
| 100 | + p.cargo("package") |
| 101 | + .with_status(101) |
| 102 | + .with_stderr_data(str![[r#" |
| 103 | +[ERROR] invalid type: sequence, expected a boolean or string |
| 104 | + --> Cargo.toml:8:25 |
| 105 | + | |
| 106 | +8 | build = ["build1.rs", "build2.rs"] |
| 107 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 108 | + | |
| 109 | +
|
| 110 | +"#]]) |
| 111 | + .run(); |
| 112 | +} |
| 113 | + |
| 114 | +#[cargo_test] |
| 115 | +fn verify_vendor_multiple_build_scripts() { |
| 116 | + let git_project = git::new("dep", |project| { |
| 117 | + project |
| 118 | + .file( |
| 119 | + "Cargo.toml", |
| 120 | + r#" |
| 121 | + cargo-features = ["multiple-build-scripts"] |
| 122 | +
|
| 123 | + [package] |
| 124 | + name = "foo" |
| 125 | + version = "0.1.0" |
| 126 | + edition = "2024" |
| 127 | + build = ["build1.rs", "build2.rs"] |
| 128 | + "#, |
| 129 | + ) |
| 130 | + .file("src/main.rs", "fn main() {}") |
| 131 | + .file("build1.rs", "fn main() {}") |
| 132 | + .file("build2.rs", "fn main() {}") |
| 133 | + }); |
| 134 | + |
| 135 | + let p = project() |
| 136 | + .file( |
| 137 | + "Cargo.toml", |
| 138 | + &format!( |
| 139 | + r#" |
| 140 | + [package] |
| 141 | + name = "foo" |
| 142 | + version = "0.1.0" |
| 143 | + edition = "2024" |
| 144 | +
|
| 145 | + [dependencies.dep] |
| 146 | + git = '{}' |
| 147 | + "#, |
| 148 | + git_project.url() |
| 149 | + ), |
| 150 | + ) |
| 151 | + .file("src/main.rs", "fn main() {}") |
| 152 | + .build(); |
| 153 | + |
| 154 | + p.cargo("vendor --respect-source-config") |
| 155 | + .with_status(101) |
| 156 | + .with_stderr_data(str![[r#" |
| 157 | +[UPDATING] git repository `[ROOTURL]/dep` |
| 158 | +[ERROR] invalid type: sequence, expected a boolean or string |
| 159 | + --> ../home/.cargo/git/checkouts/dep-[HASH]/[..]/Cargo.toml:8:25 |
| 160 | + | |
| 161 | +8 | build = ["build1.rs", "build2.rs"] |
| 162 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 163 | + | |
| 164 | +[ERROR] failed to sync |
| 165 | +
|
| 166 | +Caused by: |
| 167 | + failed to load lockfile for [ROOT]/foo |
| 168 | +
|
| 169 | +Caused by: |
| 170 | + failed to get `dep` as a dependency of package `foo v0.1.0 ([ROOT]/foo)` |
| 171 | +
|
| 172 | +Caused by: |
| 173 | + failed to load source for dependency `dep` |
| 174 | +
|
| 175 | +Caused by: |
| 176 | + Unable to update [ROOTURL]/dep |
| 177 | +
|
| 178 | +"#]]) |
| 179 | + .run(); |
| 180 | +} |
| 181 | + |
| 182 | +#[cargo_test] |
| 183 | +fn rerun_untracks_other_files() { |
| 184 | + let p = project() |
| 185 | + .file( |
| 186 | + "Cargo.toml", |
| 187 | + r#" |
| 188 | + [package] |
| 189 | + name = "foo" |
| 190 | + version = "0.1.0" |
| 191 | + edition = "2024" |
| 192 | + "#, |
| 193 | + ) |
| 194 | + .file("src/main.rs", "fn main() {}") |
| 195 | + .file( |
| 196 | + "build.rs", |
| 197 | + r#" |
| 198 | +fn main() { |
| 199 | + foo(); |
| 200 | + bar(); |
| 201 | +} |
| 202 | +fn foo() { |
| 203 | + let _path = "assets/foo.txt"; |
| 204 | +} |
| 205 | +fn bar() { |
| 206 | + let path = "assets/bar.txt"; |
| 207 | + println!("cargo::rerun-if-changed={path}"); |
| 208 | +}"#, |
| 209 | + ) |
| 210 | + .file("assets/foo.txt", "foo") |
| 211 | + .file("assets/bar.txt", "bar") |
| 212 | + .build(); |
| 213 | + p.cargo("build").run(); |
| 214 | + |
| 215 | + // Editing foo.txt won't recompile, leading to unnoticed changes |
| 216 | + |
| 217 | + p.change_file("assets/foo.txt", "foo updated"); |
| 218 | + p.cargo("build -v") |
| 219 | + .with_stderr_data(str![[r#" |
| 220 | +[FRESH] foo v0.1.0 ([ROOT]/foo) |
| 221 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 222 | +
|
| 223 | +"#]]) |
| 224 | + .run(); |
| 225 | + |
| 226 | + // Editing bar.txt will recompile |
| 227 | + |
| 228 | + p.change_file("assets/bar.txt", "bar updated"); |
| 229 | + p.cargo("build -v") |
| 230 | + .with_stderr_data(str![[r#" |
| 231 | +[DIRTY] foo v0.1.0 ([ROOT]/foo): the file `assets/bar.txt` has changed ([TIME_DIFF_AFTER_LAST_BUILD]) |
| 232 | +[COMPILING] foo v0.1.0 ([ROOT]/foo) |
| 233 | +[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build` |
| 234 | +[RUNNING] `rustc --crate-name foo --edition=2024 src/main.rs [..] --crate-type bin [..] |
| 235 | +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s |
| 236 | +
|
| 237 | +"#]]) |
| 238 | + .run(); |
| 239 | +} |
0 commit comments