| 1 |
use std::{ |
| 2 |
collections, env, ffi, fs, |
| 3 |
io::{self, Write}, |
| 4 |
path, process, |
| 5 |
}; |
| 6 |
|
| 7 |
struct Project { |
| 8 |
root: tempfile::TempDir, |
| 9 |
files: Vec<(path::PathBuf, Vec<u8>)>, |
| 10 |
env: collections::HashMap<ffi::OsString, ffi::OsString>, |
| 11 |
} |
| 12 |
|
| 13 |
impl Project { |
| 14 |
fn new() -> Project { |
| 15 |
Project { |
| 16 |
root: tempfile::tempdir().unwrap(), |
| 17 |
files: Default::default(), |
| 18 |
env: Default::default(), |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
fn add_file<N: Into<path::PathBuf>, C: Into<Vec<u8>>>( |
| 23 |
&mut self, |
| 24 |
name: N, |
| 25 |
content: C, |
| 26 |
) -> &mut Self { |
| 27 |
self.files.push((name.into(), content.into())); |
| 28 |
self |
| 29 |
} |
| 30 |
|
| 31 |
fn set_env<K, V>(&mut self, k: K, v: V) -> &mut Self |
| 32 |
where |
| 33 |
K: Into<ffi::OsString>, |
| 34 |
V: Into<ffi::OsString>, |
| 35 |
{ |
| 36 |
self.env.insert(k.into(), v.into()); |
| 37 |
self |
| 38 |
} |
| 39 |
|
| 40 |
/// Use `git_feature` to control which Git implementation to use. |
| 41 |
fn bootstrap(&mut self, git_feature: Option<&str>) -> &mut Self { |
| 42 |
let built_root = get_built_root(); |
| 43 |
let features = if let Some(git_feature) = git_feature { |
| 44 |
format!(r#"["{git_feature}"]"#) |
| 45 |
} else { |
| 46 |
"[]".into() |
| 47 |
}; |
| 48 |
|
| 49 |
self.add_file( |
| 50 |
"Cargo.toml", |
| 51 |
format!( |
| 52 |
r#" |
| 53 |
[package] |
| 54 |
name = "testbox" |
| 55 |
version = "0.0.1" |
| 56 |
build = "build.rs" |
| 57 |
|
| 58 |
[build-dependencies] |
| 59 |
built = {{ path = "{build_root}", features = {features} }}"#, |
| 60 |
build_root = built_root.display().to_string().escape_default(), |
| 61 |
), |
| 62 |
) |
| 63 |
.add_file( |
| 64 |
"build.rs", |
| 65 |
r#" |
| 66 |
fn main() { |
| 67 |
built::write_built_file().expect("writing failed"); |
| 68 |
}"#, |
| 69 |
); |
| 70 |
self |
| 71 |
} |
| 72 |
|
| 73 |
/// Hold on to the tempdir, it will be removed when dropped! |
| 74 |
fn create(self) -> io::Result<tempfile::TempDir> { |
| 75 |
fs::DirBuilder::new() |
| 76 |
.create(self.root.path().join("src")) |
| 77 |
.unwrap(); |
| 78 |
for (name, content) in self.files { |
| 79 |
let fname = self.root.path().join(name); |
| 80 |
assert!(fname.is_absolute()); |
| 81 |
fs::create_dir_all(fname.parent().unwrap()).unwrap(); |
| 82 |
let mut file = fs::File::create(fname)?; |
| 83 |
file.write_all(&content)?; |
| 84 |
} |
| 85 |
Ok(self.root) |
| 86 |
} |
| 87 |
|
| 88 |
fn create_and_run(mut self, extra_args: &[&str]) -> (String, String) { |
| 89 |
let env = std::mem::take(&mut self.env); |
| 90 |
let root = self.create().expect("Creating the project failed"); |
| 91 |
Self::run(root.as_ref(), extra_args, env) |
| 92 |
} |
| 93 |
|
| 94 |
fn create_and_build(self, extra_args: &[&str]) { |
| 95 |
let root = self.create().expect("Creating the project failed"); |
| 96 |
Self::build(root.as_ref(), extra_args); |
| 97 |
} |
| 98 |
|
| 99 |
fn run<I, K, V>(root: &std::path::Path, extra_args: &[&str], env: I) -> (String, String) |
| 100 |
where |
| 101 |
I: IntoIterator<Item = (K, V)>, |
| 102 |
K: AsRef<ffi::OsStr>, |
| 103 |
V: AsRef<ffi::OsStr>, |
| 104 |
{ |
| 105 |
let cargo_result = process::Command::new("cargo") |
| 106 |
.current_dir(root) |
| 107 |
.arg("run") |
| 108 |
.args(extra_args) |
| 109 |
.envs(env) |
| 110 |
.output() |
| 111 |
.expect("cargo failed"); |
| 112 |
let stderr = String::from_utf8_lossy(&cargo_result.stderr); |
| 113 |
let stdout = String::from_utf8_lossy(&cargo_result.stdout); |
| 114 |
assert!( |
| 115 |
cargo_result.status.success(), |
| 116 |
"cargo failed with {}", |
| 117 |
stderr |
| 118 |
); |
| 119 |
assert!(stdout.contains("builttestsuccess")); |
| 120 |
(stderr.into_owned(), stdout.into_owned()) |
| 121 |
} |
| 122 |
|
| 123 |
fn build(root: &std::path::Path, extra_args: &[&str]) { |
| 124 |
let cargo_result = process::Command::new("cargo") |
| 125 |
.current_dir(root) |
| 126 |
.arg("build") |
| 127 |
.args(extra_args) |
| 128 |
.output() |
| 129 |
.expect("cargo failed"); |
| 130 |
assert!( |
| 131 |
cargo_result.status.success(), |
| 132 |
"cargo failed with {}", |
| 133 |
String::from_utf8_lossy(&cargo_result.stderr) |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
#[cfg(feature = "git2")] |
| 138 |
fn init_git(&self) -> git2::Repository { |
| 139 |
git2::Repository::init_opts( |
| 140 |
&self.root, |
| 141 |
git2::RepositoryInitOptions::new() |
| 142 |
.external_template(false) |
| 143 |
.mkdir(false) |
| 144 |
.no_reinit(true) |
| 145 |
.mkpath(false), |
| 146 |
) |
| 147 |
.expect("git-init failed") |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
fn get_built_root() -> path::PathBuf { |
| •152 |
path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
#[ignore = "requires target x86_64-unknown-none"] |
| 157 |
fn nostd_testbox() { |
| 158 |
let mut p = Project::new(); |
| 159 |
|
| 160 |
let built_root = get_built_root(); |
| 161 |
|
| 162 |
p.add_file( |
| 163 |
"Cargo.toml", |
| 164 |
format!( |
| 165 |
r#" |
| 166 |
[package] |
| 167 |
name = "nostd_testbox" |
| 168 |
version = "1.2.3-rc1" |
| 169 |
authors = ["Joe", "Bob"] |
| 170 |
build = "build.rs" |
| 171 |
description = "xobtset" |
| 172 |
homepage = "localhost" |
| 173 |
repository = "https://dev.example.com/sources/testbox/" |
| 174 |
license = "MIT" |
| 175 |
|
| 176 |
[build-dependencies] |
| 177 |
built = {{ path = "{}", default_features=false }} |
| 178 |
|
| 179 |
[profile.dev] |
| 180 |
panic = "abort" |
| 181 |
|
| 182 |
[profile.release] |
| 183 |
panic = "abort" |
| 184 |
|
| 185 |
[features] |
| 186 |
default = ["SuperAwesome", "MegaAwesome"] |
| 187 |
SuperAwesome = [] |
| 188 |
MegaAwesome = []"#, |
| 189 |
built_root.display().to_string().escape_default(), |
| 190 |
), |
| 191 |
) |
| 192 |
.add_file( |
| 193 |
"build.rs", |
| 194 |
r#" |
| 195 |
fn main() { |
| 196 |
built::write_built_file().unwrap(); |
| 197 |
}"#, |
| 198 |
) |
| 199 |
.add_file( |
| 200 |
"src/main.rs", |
| 201 |
r#" |
| 202 |
#![no_main] |
| 203 |
#![no_std] |
| 204 |
|
| 205 |
use core::panic::PanicInfo; |
| 206 |
|
| 207 |
mod built_info { |
| 208 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 209 |
} |
| 210 |
|
| 211 |
#[panic_handler] |
| 212 |
fn panic(_panic: &PanicInfo<'_>) -> ! { |
| 213 |
loop {} |
| 214 |
} |
| 215 |
|
| 216 |
#[no_mangle] |
| 217 |
pub unsafe extern "C" fn main() -> ! { |
| 218 |
loop {} |
| 219 |
} |
| 220 |
"#, |
| 221 |
); |
| 222 |
|
| 223 |
p.create_and_build(&["--target", "x86_64-unknown-none"]); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| •227 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 228 |
fn unused_override() { |
| 229 |
for git_feature in git_features_for_bootstrap() { |
| 230 |
let mut p = Project::new(); |
| 231 |
p.bootstrap(git_feature) |
| 232 |
.add_file( |
| 233 |
"src/main.rs", |
| 234 |
r#" |
| 235 |
mod built_info { |
| 236 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 237 |
} |
| 238 |
|
| 239 |
fn main() { |
| 240 |
println!("builttestsuccess"); |
| 241 |
} |
| 242 |
"#, |
| 243 |
) |
| 244 |
.set_env("BUILT_OVERRIDE_testbox_FOO", "BAR") |
| 245 |
.set_env("BUILT_OVERRIDE_testbox1_FOO", "BAR"); |
| 246 |
let (stderr, _) = p.create_and_run(&[]); |
| 247 |
assert!(stderr.contains("but was ignored by built: `BUILT_OVERRIDE_testbox_FOO`.")); |
| 248 |
assert!(!stderr.contains("but was ignored by built: `BUILT_OVERRIDE_testbox1_FOO`.")); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| •253 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 254 |
fn minimal_testbox() { |
| 255 |
let mut p = Project::new(); |
| 256 |
|
| 257 |
let built_root = get_built_root(); |
| 258 |
|
| 259 |
p.add_file( |
| 260 |
"Cargo.toml", |
| 261 |
format!( |
| 262 |
r#" |
| 263 |
[package] |
| 264 |
name = "minimal_testbox" |
| 265 |
version = "1.2.3-rc1" |
| 266 |
authors = ["Joe", "Bob"] |
| 267 |
build = "build.rs" |
| 268 |
description = "xobtset" |
| 269 |
homepage = "localhost" |
| 270 |
repository = "https://dev.example.com/sources/testbox/" |
| 271 |
license = "MIT" |
| 272 |
|
| 273 |
[dependencies] |
| 274 |
built = {{ path = "{built_root}", default_features=false }} |
| 275 |
|
| 276 |
[build-dependencies] |
| 277 |
built = {{ path = "{built_root}", default_features=false }} |
| 278 |
|
| 279 |
[features] |
| 280 |
default = ["SuperAwesome", "MegaAwesome"] |
| 281 |
SuperAwesome = [] |
| 282 |
MegaAwesome = []"#, |
| 283 |
built_root = built_root.display().to_string().escape_default() |
| 284 |
), |
| 285 |
); |
| 286 |
|
| 287 |
p.add_file( |
| 288 |
"build.rs", |
| 289 |
r#" |
| 290 |
|
| 291 |
fn main() { |
| 292 |
built::write_built_file().unwrap(); |
| 293 |
}"#, |
| 294 |
); |
| 295 |
|
| 296 |
const FEATURES: &str = r#"["MegaAwesome", "SuperAwesome", "default"]"#; |
| 297 |
const FEATURES_STR: &str = r#""MegaAwesome, SuperAwesome, default""#; |
| 298 |
|
| 299 |
p.add_file( |
| 300 |
"src/main.rs", |
| 301 |
format!( |
| 302 |
r#" |
| 303 |
//! The minimal testbox. |
| 304 |
|
| 305 |
mod built_info {{ |
| 306 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 307 |
}} |
| 308 |
|
| 309 |
fn main() {{ |
| 310 |
assert_eq!(built_info::PKG_VERSION, "1.2.3-rc1"); |
| 311 |
assert_eq!(built_info::PKG_VERSION_MAJOR, "1"); |
| 312 |
assert_eq!(built_info::PKG_VERSION_MINOR, "2"); |
| 313 |
assert_eq!(built_info::PKG_VERSION_PATCH, "3"); |
| 314 |
assert_eq!(built_info::PKG_VERSION_PRE, "rc1"); |
| 315 |
assert_eq!(built_info::PKG_AUTHORS, "Joe:Bob"); |
| 316 |
assert_eq!(built_info::PKG_NAME, "minimal_testbox"); |
| 317 |
assert_eq!(built_info::PKG_DESCRIPTION, "xobtset"); |
| 318 |
assert_eq!(built_info::PKG_HOMEPAGE, "localhost"); |
| 319 |
assert_eq!(built_info::PKG_LICENSE, "MIT"); |
| 320 |
assert_eq!(built_info::PKG_REPOSITORY, "https://dev.example.com/sources/testbox/"); |
| 321 |
assert!(built_info::NUM_JOBS > 0); |
| 322 |
assert!(built_info::OPT_LEVEL == "0"); |
| 323 |
assert!(built_info::DEBUG); |
| 324 |
assert_eq!(built_info::PROFILE, "debug"); |
| 325 |
assert_eq!(built_info::FEATURES, {features}); |
| 326 |
assert_eq!(built_info::FEATURES_STR, {features_str}); |
| 327 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 328 |
["default", "megaawesome", "superawesome"]); |
| 329 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 330 |
"default, megaawesome, superawesome"); |
| 331 |
assert_ne!(built_info::RUSTC_VERSION, ""); |
| 332 |
assert_ne!(built_info::RUSTDOC_VERSION, ""); |
| 333 |
assert_ne!(built_info::HOST, ""); |
| 334 |
assert_ne!(built_info::TARGET, ""); |
| 335 |
assert_ne!(built_info::RUSTC, ""); |
| 336 |
assert_ne!(built_info::RUSTDOC, ""); |
| 337 |
assert_ne!(built_info::CFG_TARGET_ARCH, ""); |
| 338 |
assert_ne!(built_info::CFG_ENDIAN, ""); |
| 339 |
assert_ne!(built_info::CFG_FAMILY, ""); |
| 340 |
assert_ne!(built_info::CFG_OS, ""); |
| 341 |
assert_ne!(built_info::CFG_POINTER_WIDTH, ""); |
| 342 |
// For CFG_ENV, empty string is a possible value. |
| 343 |
let _: &'static str = built_info::CFG_ENV; |
| 344 |
println!("builttestsuccess"); |
| 345 |
}}"#, |
| 346 |
features = FEATURES, |
| 347 |
features_str = FEATURES_STR |
| 348 |
), |
| 349 |
); |
| 350 |
|
| 351 |
p.create_and_run(&[]); |
| 352 |
} |
| 353 |
|
| 354 |
#[test] |
| 355 |
fn cargo_cfg_feature() { |
| 356 |
let mut p = Project::new(); |
| 357 |
|
| 358 |
let built_root = get_built_root(); |
| 359 |
|
| 360 |
p.add_file( |
| 361 |
"Cargo.toml", |
| 362 |
format!( |
| 363 |
r#" |
| 364 |
[package] |
| 365 |
name = "cargo_cfg_feature_testbox" |
| 366 |
version = "1.2.3-rc1" |
| 367 |
build = "build.rs" |
| 368 |
|
| 369 |
[build-dependencies] |
| 370 |
built = {{ path = "{built_root}", default_features=false }} |
| 371 |
|
| 372 |
[features] |
| 373 |
default = ["This-Feature", "Other-Feature"] |
| 374 |
This-Feature = [] |
| 375 |
Other-Feature = [] |
| 376 |
"#, |
| 377 |
built_root = built_root.display().to_string().escape_default() |
| 378 |
), |
| 379 |
); |
| 380 |
|
| 381 |
p.add_file( |
| 382 |
"build.rs", |
| 383 |
r#" |
| 384 |
|
| 385 |
fn main() { |
| 386 |
built::write_built_file().unwrap(); |
| 387 |
}"#, |
| 388 |
); |
| 389 |
|
| 390 |
p.add_file( |
| 391 |
"src/main.rs", |
| 392 |
r#" |
| 393 |
mod built_info { |
| 394 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 395 |
} |
| 396 |
|
| 397 |
fn main() { |
| 398 |
assert_eq!(built_info::FEATURES, |
| 399 |
["Other-Feature", "This-Feature", "default"]); |
| 400 |
assert_eq!(built_info::FEATURES_STR, |
| 401 |
"Other-Feature, This-Feature, default"); |
| 402 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 403 |
["default", "other-feature", "this-feature"]); |
| 404 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 405 |
"default, other-feature, this-feature"); |
| 406 |
println!("builttestsuccess"); |
| 407 |
}"#, |
| 408 |
); |
| 409 |
|
| 410 |
p.create_and_run(&[]); |
| 411 |
} |
| 412 |
|
| 413 |
#[test] |
| •414 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 415 |
fn simple_workspace() { |
| 416 |
let mut p = Project::new(); |
| 417 |
let built_root = get_built_root(); |
| 418 |
|
| 419 |
p.add_file("Cargo.toml", "[workspace]\nmembers = ['foobar']"); |
| 420 |
p.add_file( |
| 421 |
"foobar/Cargo.toml", |
| 422 |
format!( |
| 423 |
r#"[package] |
| 424 |
name = "foobar" |
| 425 |
version = "5.6.7" |
| 426 |
build = "build.rs" |
| 427 |
|
| 428 |
[build-dependencies] |
| 429 |
built = {{ path = "{}" }}"#, |
| 430 |
built_root.display().to_string().escape_default() |
| 431 |
), |
| 432 |
); |
| 433 |
p.add_file( |
| 434 |
"foobar/build.rs", |
| 435 |
"fn main() { built::write_built_file().unwrap(); }", |
| 436 |
); |
| 437 |
p.add_file( |
| 438 |
"foobar/src/main.rs", |
| 439 |
r#" |
| 440 |
mod built_info { |
| 441 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 442 |
} |
| 443 |
fn main() { |
| 444 |
assert_eq!(built_info::PKG_VERSION, "5.6.7"); |
| 445 |
println!("builttestsuccess"); |
| 446 |
} |
| 447 |
"#, |
| 448 |
); |
| 449 |
p.create_and_run(&[]); |
| 450 |
} |
| 451 |
|
| 452 |
#[test] |
| 453 |
#[cfg(all( |
| 454 |
feature = "cargo-lock", |
| 455 |
feature = "dependency-tree", |
| 456 |
any(feature = "git2", feature = "gix"), |
| 457 |
feature = "chrono", |
| 458 |
feature = "semver" |
| 459 |
))] |
| •460 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 461 |
fn full_testbox() { |
| 462 |
for git_feature in git_features() { |
| 463 |
let mut p = Project::new(); |
| 464 |
|
| 465 |
let built_root = get_built_root(); |
| 466 |
|
| 467 |
const FEATURES: &str = r#"["MegaAwesome", "SuperAwesome", "default"]"#; |
| 468 |
const FEATURES_STR: &str = r#""MegaAwesome, SuperAwesome, default""#; |
| 469 |
|
| 470 |
p.add_file( |
| 471 |
"Cargo.toml", |
| 472 |
format!( |
| 473 |
r#" |
| 474 |
[package] |
| 475 |
name = "testbox" |
| 476 |
version = "1.2.3-rc1" |
| 477 |
authors = ["Joe", "Bob", "Harry:Potter"] |
| 478 |
build = "build.rs" |
| 479 |
description = "xobtset" |
| 480 |
homepage = "localhost" |
| 481 |
repository = "https://dev.example.com/sources/testbox/" |
| 482 |
license = "MIT" |
| 483 |
|
| 484 |
[dependencies] |
| 485 |
built = {{ path = "{built_root}", features=["cargo-lock", "dependency-tree", "{git_feature}", "chrono", "semver"] }} |
| 486 |
|
| 487 |
[build-dependencies] |
| 488 |
built = {{ path = "{built_root}", features=["cargo-lock", "dependency-tree", "{git_feature}", "chrono", "semver"] }} |
| 489 |
|
| 490 |
[features] |
| 491 |
default = ["SuperAwesome", "MegaAwesome"] |
| 492 |
SuperAwesome = [] |
| 493 |
MegaAwesome = []"#, |
| 494 |
built_root = built_root.display().to_string().escape_default() |
| 495 |
), |
| 496 |
) |
| 497 |
.add_file( |
| 498 |
"build.rs", |
| 499 |
r#" |
| 500 |
fn main() { |
| 501 |
built::write_built_file().unwrap(); |
| 502 |
}"#, |
| 503 |
) |
| 504 |
.set_env("CONTINUOUS_INTEGRATION", "1") |
| 505 |
.add_file( |
| 506 |
"src/main.rs", |
| 507 |
format!(r#" |
| 508 |
//! The testbox. |
| 509 |
|
| 510 |
mod built_info {{ |
| 511 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 512 |
}} |
| 513 |
|
| 514 |
fn main() {{ |
| 515 |
assert_eq!(built_info::GIT_VERSION, None); |
| 516 |
assert_eq!(built_info::GIT_DIRTY, None); |
| 517 |
assert_eq!(built_info::GIT_COMMIT_HASH, None); |
| 518 |
assert_eq!(built_info::GIT_COMMIT_HASH_SHORT, None); |
| 519 |
assert_eq!(built_info::GIT_HEAD_REF, None); |
| 520 |
assert!(built_info::CI_PLATFORM.is_some()); |
| 521 |
assert_eq!(built_info::PKG_VERSION, "1.2.3-rc1"); |
| 522 |
assert_eq!(built_info::PKG_VERSION_MAJOR, "1"); |
| 523 |
assert_eq!(built_info::PKG_VERSION_MINOR, "2"); |
| 524 |
assert_eq!(built_info::PKG_VERSION_PATCH, "3"); |
| 525 |
assert_eq!(built_info::PKG_VERSION_PRE, "rc1"); |
| 526 |
assert_eq!(built_info::PKG_AUTHORS, "Joe:Bob:Harry:Potter"); |
| 527 |
assert_eq!(built_info::PKG_NAME, "testbox"); |
| 528 |
assert_eq!(built_info::PKG_DESCRIPTION, "xobtset"); |
| 529 |
assert_eq!(built_info::PKG_HOMEPAGE, "localhost"); |
| 530 |
assert_eq!(built_info::PKG_LICENSE, "MIT"); |
| 531 |
assert_eq!(built_info::PKG_REPOSITORY, "https://dev.example.com/sources/testbox/"); |
| 532 |
assert!(built_info::NUM_JOBS > 0); |
| 533 |
assert!(built_info::OPT_LEVEL == "0"); |
| 534 |
assert!(built_info::DEBUG); |
| 535 |
assert_eq!(built_info::PROFILE, "debug"); |
| 536 |
assert_eq!(built_info::FEATURES, {features}); |
| 537 |
assert_eq!(built_info::FEATURES_STR, {features_str}); |
| 538 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 539 |
["default", "megaawesome", "superawesome"]); |
| 540 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 541 |
"default, megaawesome, superawesome"); |
| 542 |
assert_ne!(built_info::RUSTC_VERSION, ""); |
| 543 |
assert_ne!(built_info::RUSTDOC_VERSION, ""); |
| 544 |
assert_ne!(built_info::DEPENDENCIES_STR, ""); |
| 545 |
assert_ne!(built_info::DIRECT_DEPENDENCIES_STR, ""); |
| 546 |
assert_ne!(built_info::INDIRECT_DEPENDENCIES_STR, ""); |
| 547 |
assert_ne!(built_info::HOST, ""); |
| 548 |
assert_ne!(built_info::TARGET, ""); |
| 549 |
assert_ne!(built_info::RUSTC, ""); |
| 550 |
assert_ne!(built_info::RUSTDOC, ""); |
| 551 |
assert_ne!(built_info::CFG_TARGET_ARCH, ""); |
| 552 |
assert_ne!(built_info::CFG_ENDIAN, ""); |
| 553 |
assert_ne!(built_info::CFG_FAMILY, ""); |
| 554 |
assert_ne!(built_info::CFG_OS, ""); |
| 555 |
assert_ne!(built_info::CFG_POINTER_WIDTH, ""); |
| 556 |
// For CFG_ENV, empty string is a possible value. |
| 557 |
let _: &'static str = built_info::CFG_ENV; |
| 558 |
|
| 559 |
assert!(built::util::parse_versions(built_info::DEPENDENCIES.iter()) |
| 560 |
.any(|(name, ver)| name == "toml" && ver >= built::semver::Version::parse("0.1.0").unwrap())); |
| 561 |
|
| 562 |
assert_eq!(built_info::DIRECT_DEPENDENCIES.len(), 1); |
| 563 |
assert_eq!(built_info::DIRECT_DEPENDENCIES[0].0, "built"); |
| 564 |
|
| 565 |
assert!((built::chrono::offset::Utc::now() - built::util::strptime(built_info::BUILT_TIME_UTC)).num_days() <= 1); |
| 566 |
|
| 567 |
assert!(built_info::OVERRIDE_VARIABLES_USED.is_empty()); |
| 568 |
|
| 569 |
println!("builttestsuccess"); |
| 570 |
}}"#, features=FEATURES, features_str=FEATURES_STR), |
| 571 |
); |
| 572 |
p.create_and_run(&[]); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
#[test] |
| 577 |
#[cfg(all( |
| 578 |
any(feature = "git2", feature = "gix"), |
| 579 |
feature = "chrono", |
| 580 |
feature = "semver" |
| 581 |
))] |
| •582 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 583 |
fn overridden_testbox() { |
| 584 |
for git_feature in git_features() { |
| 585 |
let mut p = Project::new(); |
| 586 |
|
| 587 |
let built_root = get_built_root(); |
| 588 |
|
| 589 |
p.add_file( |
| 590 |
"Cargo.toml", |
| 591 |
format!( |
| 592 |
r#" |
| 593 |
[package] |
| 594 |
name = "testbox" |
| 595 |
version = "1.2.3-rc1" |
| 596 |
authors = ["Joe", "Bob", "Harry:Potter"] |
| 597 |
build = "build.rs" |
| 598 |
description = "xobtset" |
| 599 |
homepage = "localhost" |
| 600 |
repository = "https://dev.example.com/sources/testbox/" |
| 601 |
license = "MIT" |
| 602 |
|
| 603 |
[dependencies] |
| 604 |
built = {{ path = "{built_root}", features=["{git_feature}", "chrono", "semver"] }} |
| 605 |
|
| 606 |
[build-dependencies] |
| 607 |
built = {{ path = "{built_root}", features=["{git_feature}", "chrono", "semver"] }} |
| 608 |
|
| 609 |
[features] |
| 610 |
default = ["SuperAwesome", "MegaAwesome"] |
| 611 |
SuperAwesome = [] |
| 612 |
MegaAwesome = []"#, |
| 613 |
built_root = built_root.display().to_string().escape_default() |
| 614 |
), |
| 615 |
) |
| 616 |
.add_file( |
| 617 |
"build.rs", |
| 618 |
r#" |
| 619 |
fn main() { |
| 620 |
built::write_built_file().unwrap(); |
| 621 |
}"#, |
| 622 |
) |
| 623 |
.set_env("BUILT_OVERRIDE_testbox_GIT_VERSION", "GIT_VERSION1") |
| 624 |
.set_env("BUILT_OVERRIDE_testbox_GIT_DIRTY", "false") |
| 625 |
.set_env("BUILT_OVERRIDE_testbox_GIT_COMMIT_HASH", "1234567890") |
| 626 |
.set_env("BUILT_OVERRIDE_testbox_GIT_HEAD_REF", "GIT_REF") |
| 627 |
.set_env("BUILT_OVERRIDE_testbox_CI_PLATFORM", "TESTBOXCI") |
| 628 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION", "1.2.3.4") |
| 629 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_MAJOR", "abc") |
| 630 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_MINOR", "def") |
| 631 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_PATCH", "ghi") |
| 632 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_PRE", "jkl") |
| 633 |
.set_env("BUILT_OVERRIDE_testbox_PKG_AUTHORS", "The council") |
| 634 |
.set_env("BUILT_OVERRIDE_testbox_PKG_NAME", "OVERRIDEBOX") |
| 635 |
.set_env("BUILT_OVERRIDE_testbox_PKG_DESCRIPTION", "TEST") |
| 636 |
.set_env("BUILT_OVERRIDE_testbox_PKG_HOMEPAGE", "foreignhost") |
| 637 |
.set_env("BUILT_OVERRIDE_testbox_PKG_LICENSE", "MITv2") |
| 638 |
.set_env("BUILT_OVERRIDE_testbox_PKG_REPOSITORY", "8.8.8.8") |
| 639 |
.set_env("BUILT_OVERRIDE_testbox_NUM_JOBS", "999") |
| 640 |
.set_env( |
| 641 |
"BUILT_OVERRIDE_testbox_OPT_LEVEL", |
| 642 |
"not_too_fast_not_too_slow", |
| 643 |
) |
| 644 |
.set_env("BUILT_OVERRIDE_testbox_DEBUG", "false") |
| 645 |
.set_env("BUILT_OVERRIDE_testbox_PROFILE", "MEDIUM") |
| 646 |
.set_env( |
| 647 |
"BUILT_OVERRIDE_testbox_FEATURES", |
| 648 |
"cup_holder,Stereo Sound, dynamic range", |
| 649 |
) |
| 650 |
.set_env("BUILT_OVERRIDE_testbox_RUSTC", "overridec") |
| 651 |
.set_env("BUILT_OVERRIDE_testbox_RUSTC_VERSION", "overridec v1") |
| 652 |
.set_env("BUILT_OVERRIDE_testbox_RUSTDOC", "overridedoc") |
| 653 |
.set_env("BUILT_OVERRIDE_testbox_RUSTDOC_VERSION", "overridedoc v1") |
| 654 |
.set_env("BUILT_OVERRIDE_testbox_HOST", "overridehost") |
| 655 |
.set_env("BUILT_OVERRIDE_testbox_TARGET", "potato") |
| 656 |
.set_env("BUILT_OVERRIDE_testbox_CFG_TARGET_ARCH", "potatoes") |
| 657 |
.set_env("BUILT_OVERRIDE_testbox_CFG_ENDIAN", "random") |
| 658 |
.set_env("BUILT_OVERRIDE_testbox_CFG_FAMILY", "v0") |
| 659 |
.set_env("BUILT_OVERRIDE_testbox_CFG_OS", "none") |
| 660 |
.set_env("BUILT_OVERRIDE_testbox_CFG_POINTER_WIDTH", "63.9998") |
| 661 |
.set_env("BUILT_OVERRIDE_testbox_CFG_ENV", "calm") |
| 662 |
.set_env( |
| 663 |
"BUILT_OVERRIDE_testbox_BUILT_TIME_UTC", |
| 664 |
"Sat, 25 May 2024 12:15:59 +0000", |
| 665 |
) |
| 666 |
.add_file( |
| 667 |
"src/main.rs", |
| 668 |
r#" |
| 669 |
//! The testbox. |
| 670 |
|
| 671 |
mod built_info { |
| 672 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 673 |
} |
| 674 |
|
| 675 |
fn main() { |
| 676 |
assert_eq!(built_info::GIT_VERSION, Some("GIT_VERSION1")); |
| 677 |
assert_eq!(built_info::GIT_DIRTY, Some(false)); |
| 678 |
assert_eq!(built_info::GIT_COMMIT_HASH, Some("1234567890")); |
| 679 |
assert_eq!(built_info::GIT_COMMIT_HASH_SHORT, Some("12345678")); |
| 680 |
assert_eq!(built_info::GIT_HEAD_REF, Some("GIT_REF")); |
| 681 |
assert_eq!(built_info::CI_PLATFORM, Some("TESTBOXCI")); |
| 682 |
assert_eq!(built_info::PKG_VERSION, "1.2.3.4"); |
| 683 |
assert_eq!(built_info::PKG_VERSION_MAJOR, "abc"); |
| 684 |
assert_eq!(built_info::PKG_VERSION_MINOR, "def"); |
| 685 |
assert_eq!(built_info::PKG_VERSION_PATCH, "ghi"); |
| 686 |
assert_eq!(built_info::PKG_VERSION_PRE, "jkl"); |
| 687 |
assert_eq!(built_info::PKG_AUTHORS, "The council"); |
| 688 |
assert_eq!(built_info::PKG_NAME, "OVERRIDEBOX"); |
| 689 |
assert_eq!(built_info::PKG_DESCRIPTION, "TEST"); |
| 690 |
assert_eq!(built_info::PKG_HOMEPAGE, "foreignhost"); |
| 691 |
assert_eq!(built_info::PKG_LICENSE, "MITv2"); |
| 692 |
assert_eq!(built_info::PKG_REPOSITORY, "8.8.8.8"); |
| 693 |
assert_eq!(built_info::NUM_JOBS, 999); |
| 694 |
assert_eq!(built_info::OPT_LEVEL, "not_too_fast_not_too_slow"); |
| 695 |
assert!(!built_info::DEBUG); |
| 696 |
assert_eq!(built_info::PROFILE, "MEDIUM"); |
| 697 |
assert_eq!(built_info::FEATURES, |
| 698 |
["Stereo Sound", "cup_holder", "dynamic range"]); |
| 699 |
assert_eq!(built_info::FEATURES_STR, |
| 700 |
"Stereo Sound, cup_holder, dynamic range"); |
| 701 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 702 |
["cup_holder", "dynamic range", "stereo sound"]); |
| 703 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 704 |
"cup_holder, dynamic range, stereo sound"); |
| 705 |
assert_eq!(built_info::RUSTC, "overridec"); |
| 706 |
assert_eq!(built_info::RUSTC_VERSION, "overridec v1"); |
| 707 |
assert_eq!(built_info::RUSTDOC, "overridedoc"); |
| 708 |
assert_eq!(built_info::RUSTDOC_VERSION, "overridedoc v1"); |
| 709 |
assert_eq!(built_info::HOST, "overridehost"); |
| 710 |
assert_eq!(built_info::TARGET, "potato"); |
| 711 |
assert_eq!(built_info::CFG_TARGET_ARCH, "potatoes"); |
| 712 |
assert_eq!(built_info::CFG_ENDIAN, "random"); |
| 713 |
assert_eq!(built_info::CFG_FAMILY, "v0"); |
| 714 |
assert_eq!(built_info::CFG_OS, "none"); |
| 715 |
assert_eq!(built_info::CFG_POINTER_WIDTH, "63.9998"); |
| 716 |
assert_eq!(built_info::CFG_ENV, "calm"); |
| 717 |
|
| 718 |
assert_eq!(built::util::strptime(built_info::BUILT_TIME_UTC).to_rfc2822(), |
| 719 |
"Sat, 25 May 2024 12:15:59 +0000"); |
| 720 |
assert_eq!(built_info::OVERRIDE_VARIABLES_USED, [ |
| 721 |
"BUILT_TIME_UTC", |
| 722 |
"CFG_ENDIAN", |
| 723 |
"CFG_ENV", |
| 724 |
"CFG_FAMILY", |
| 725 |
"CFG_OS", |
| 726 |
"CFG_POINTER_WIDTH", |
| 727 |
"CFG_TARGET_ARCH", |
| 728 |
"CI_PLATFORM", |
| 729 |
"DEBUG", |
| 730 |
"FEATURES", |
| 731 |
"GIT_COMMIT_HASH", |
| 732 |
"GIT_DIRTY", |
| 733 |
"GIT_HEAD_REF", |
| 734 |
"GIT_VERSION", |
| 735 |
"HOST", |
| 736 |
"NUM_JOBS", |
| 737 |
"OPT_LEVEL", |
| 738 |
"PKG_AUTHORS", |
| 739 |
"PKG_DESCRIPTION", |
| 740 |
"PKG_HOMEPAGE", |
| 741 |
"PKG_LICENSE", |
| 742 |
"PKG_NAME", |
| 743 |
"PKG_REPOSITORY", |
| 744 |
"PKG_VERSION", |
| 745 |
"PKG_VERSION_MAJOR", |
| 746 |
"PKG_VERSION_MINOR", |
| 747 |
"PKG_VERSION_PATCH", |
| 748 |
"PKG_VERSION_PRE", |
| 749 |
"PROFILE", |
| 750 |
"RUSTC", |
| 751 |
"RUSTC_VERSION", |
| 752 |
"RUSTDOC", |
| 753 |
"RUSTDOC_VERSION", |
| 754 |
"TARGET", |
| 755 |
]); |
| 756 |
println!("builttestsuccess"); |
| 757 |
}"#, |
| 758 |
); |
| 759 |
p.create_and_run(&[]); |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
#[test] |
| 764 |
#[cfg(all( |
| 765 |
any(feature = "git2", feature = "gix"), |
| 766 |
feature = "chrono", |
| 767 |
feature = "semver" |
| 768 |
))] |
| 769 |
fn overridden_testbox_pkg_name_with_hyphen() { |
| 770 |
for git_feature in git_features() { |
| 771 |
let mut p = Project::new(); |
| 772 |
|
| 773 |
let built_root = get_built_root(); |
| 774 |
|
| 775 |
p.add_file( |
| 776 |
"Cargo.toml", |
| 777 |
format!( |
| 778 |
r#" |
| 779 |
[package] |
| 780 |
name = "great-testbox" |
| 781 |
version = "1.2.3-rc1" |
| 782 |
authors = ["Joe", "Bob", "Harry:Potter"] |
| 783 |
build = "build.rs" |
| 784 |
description = "xobtset" |
| 785 |
homepage = "localhost" |
| 786 |
repository = "https://dev.example.com/sources/great-testbox/" |
| 787 |
license = "MIT" |
| 788 |
|
| 789 |
[dependencies] |
| 790 |
built = {{ path = "{built_root}", features=["{git_feature}", "chrono", "semver"] }} |
| 791 |
|
| 792 |
[build-dependencies] |
| 793 |
built = {{ path = "{built_root}", features=["{git_feature}", "chrono", "semver"] }} |
| 794 |
|
| 795 |
[features] |
| 796 |
default = ["SuperAwesome", "MegaAwesome"] |
| 797 |
SuperAwesome = [] |
| 798 |
MegaAwesome = []"#, |
| 799 |
built_root = built_root.display().to_string().escape_default() |
| 800 |
), |
| 801 |
) |
| 802 |
.add_file( |
| 803 |
"build.rs", |
| 804 |
r#" |
| 805 |
fn main() { |
| 806 |
built::write_built_file().unwrap(); |
| 807 |
}"#, |
| 808 |
) |
| 809 |
.set_env("BUILT_OVERRIDE_great_testbox_GIT_VERSION", "GIT_VERSION1") |
| 810 |
.set_env("BUILT_OVERRIDE_great_testbox_GIT_DIRTY", "false") |
| 811 |
.set_env("BUILT_OVERRIDE_great_testbox_GIT_COMMIT_HASH", "1234567890") |
| 812 |
.set_env("BUILT_OVERRIDE_great_testbox_GIT_HEAD_REF", "GIT_REF") |
| 813 |
.set_env("BUILT_OVERRIDE_great_testbox_CI_PLATFORM", "TESTBOXCI") |
| 814 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_VERSION", "1.2.3.4") |
| 815 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_VERSION_MAJOR", "abc") |
| 816 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_VERSION_MINOR", "def") |
| 817 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_VERSION_PATCH", "ghi") |
| 818 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_VERSION_PRE", "jkl") |
| 819 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_AUTHORS", "The council") |
| 820 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_NAME", "OVERRIDEBOX") |
| 821 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_DESCRIPTION", "TEST") |
| 822 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_HOMEPAGE", "foreignhost") |
| 823 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_LICENSE", "MITv2") |
| 824 |
.set_env("BUILT_OVERRIDE_great_testbox_PKG_REPOSITORY", "8.8.8.8") |
| 825 |
.set_env("BUILT_OVERRIDE_great_testbox_NUM_JOBS", "999") |
| 826 |
.set_env( |
| 827 |
"BUILT_OVERRIDE_great_testbox_OPT_LEVEL", |
| 828 |
"not_too_fast_not_too_slow", |
| 829 |
) |
| 830 |
.set_env("BUILT_OVERRIDE_great_testbox_DEBUG", "false") |
| 831 |
.set_env("BUILT_OVERRIDE_great_testbox_PROFILE", "MEDIUM") |
| 832 |
.set_env( |
| 833 |
"BUILT_OVERRIDE_great_testbox_FEATURES", |
| 834 |
"cup_holder,Stereo Sound, dynamic range", |
| 835 |
) |
| 836 |
.set_env("BUILT_OVERRIDE_great_testbox_RUSTC", "overridec") |
| 837 |
.set_env("BUILT_OVERRIDE_great_testbox_RUSTC_VERSION", "overridec v1") |
| 838 |
.set_env("BUILT_OVERRIDE_great_testbox_RUSTDOC", "overridedoc") |
| 839 |
.set_env( |
| 840 |
"BUILT_OVERRIDE_great_testbox_RUSTDOC_VERSION", |
| 841 |
"overridedoc v1", |
| 842 |
) |
| 843 |
.set_env("BUILT_OVERRIDE_great_testbox_HOST", "overridehost") |
| 844 |
.set_env("BUILT_OVERRIDE_great_testbox_TARGET", "potato") |
| 845 |
.set_env("BUILT_OVERRIDE_great_testbox_CFG_TARGET_ARCH", "potatoes") |
| 846 |
.set_env("BUILT_OVERRIDE_great_testbox_CFG_ENDIAN", "random") |
| 847 |
.set_env("BUILT_OVERRIDE_great_testbox_CFG_FAMILY", "v0") |
| 848 |
.set_env("BUILT_OVERRIDE_great_testbox_CFG_OS", "none") |
| 849 |
.set_env("BUILT_OVERRIDE_great_testbox_CFG_POINTER_WIDTH", "63.9998") |
| 850 |
.set_env("BUILT_OVERRIDE_great_testbox_CFG_ENV", "calm") |
| 851 |
.set_env( |
| 852 |
"BUILT_OVERRIDE_great_testbox_BUILT_TIME_UTC", |
| 853 |
"Sat, 25 May 2024 12:15:59 +0000", |
| 854 |
) |
| 855 |
.add_file( |
| 856 |
"src/main.rs", |
| 857 |
r#" |
| 858 |
//! The testbox. |
| 859 |
|
| 860 |
mod built_info { |
| 861 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 862 |
} |
| 863 |
|
| 864 |
fn main() { |
| 865 |
assert_eq!(built_info::GIT_VERSION, Some("GIT_VERSION1")); |
| 866 |
assert_eq!(built_info::GIT_DIRTY, Some(false)); |
| 867 |
assert_eq!(built_info::GIT_COMMIT_HASH, Some("1234567890")); |
| 868 |
assert_eq!(built_info::GIT_COMMIT_HASH_SHORT, Some("12345678")); |
| 869 |
assert_eq!(built_info::GIT_HEAD_REF, Some("GIT_REF")); |
| 870 |
assert_eq!(built_info::CI_PLATFORM, Some("TESTBOXCI")); |
| 871 |
assert_eq!(built_info::PKG_VERSION, "1.2.3.4"); |
| 872 |
assert_eq!(built_info::PKG_VERSION_MAJOR, "abc"); |
| 873 |
assert_eq!(built_info::PKG_VERSION_MINOR, "def"); |
| 874 |
assert_eq!(built_info::PKG_VERSION_PATCH, "ghi"); |
| 875 |
assert_eq!(built_info::PKG_VERSION_PRE, "jkl"); |
| 876 |
assert_eq!(built_info::PKG_AUTHORS, "The council"); |
| 877 |
assert_eq!(built_info::PKG_NAME, "OVERRIDEBOX"); |
| 878 |
assert_eq!(built_info::PKG_DESCRIPTION, "TEST"); |
| 879 |
assert_eq!(built_info::PKG_HOMEPAGE, "foreignhost"); |
| 880 |
assert_eq!(built_info::PKG_LICENSE, "MITv2"); |
| 881 |
assert_eq!(built_info::PKG_REPOSITORY, "8.8.8.8"); |
| 882 |
assert_eq!(built_info::NUM_JOBS, 999); |
| 883 |
assert_eq!(built_info::OPT_LEVEL, "not_too_fast_not_too_slow"); |
| 884 |
assert!(!built_info::DEBUG); |
| 885 |
assert_eq!(built_info::PROFILE, "MEDIUM"); |
| 886 |
assert_eq!(built_info::FEATURES, |
| 887 |
["Stereo Sound", "cup_holder", "dynamic range"]); |
| 888 |
assert_eq!(built_info::FEATURES_STR, |
| 889 |
"Stereo Sound, cup_holder, dynamic range"); |
| 890 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 891 |
["cup_holder", "dynamic range", "stereo sound"]); |
| 892 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 893 |
"cup_holder, dynamic range, stereo sound"); |
| 894 |
assert_eq!(built_info::RUSTC, "overridec"); |
| 895 |
assert_eq!(built_info::RUSTC_VERSION, "overridec v1"); |
| 896 |
assert_eq!(built_info::RUSTDOC, "overridedoc"); |
| 897 |
assert_eq!(built_info::RUSTDOC_VERSION, "overridedoc v1"); |
| 898 |
assert_eq!(built_info::HOST, "overridehost"); |
| 899 |
assert_eq!(built_info::TARGET, "potato"); |
| 900 |
assert_eq!(built_info::CFG_TARGET_ARCH, "potatoes"); |
| 901 |
assert_eq!(built_info::CFG_ENDIAN, "random"); |
| 902 |
assert_eq!(built_info::CFG_FAMILY, "v0"); |
| 903 |
assert_eq!(built_info::CFG_OS, "none"); |
| 904 |
assert_eq!(built_info::CFG_POINTER_WIDTH, "63.9998"); |
| 905 |
assert_eq!(built_info::CFG_ENV, "calm"); |
| 906 |
|
| 907 |
assert_eq!(built::util::strptime(built_info::BUILT_TIME_UTC).to_rfc2822(), |
| 908 |
"Sat, 25 May 2024 12:15:59 +0000"); |
| 909 |
assert_eq!(built_info::OVERRIDE_VARIABLES_USED, [ |
| 910 |
"BUILT_TIME_UTC", |
| 911 |
"CFG_ENDIAN", |
| 912 |
"CFG_ENV", |
| 913 |
"CFG_FAMILY", |
| 914 |
"CFG_OS", |
| 915 |
"CFG_POINTER_WIDTH", |
| 916 |
"CFG_TARGET_ARCH", |
| 917 |
"CI_PLATFORM", |
| 918 |
"DEBUG", |
| 919 |
"FEATURES", |
| 920 |
"GIT_COMMIT_HASH", |
| 921 |
"GIT_DIRTY", |
| 922 |
"GIT_HEAD_REF", |
| 923 |
"GIT_VERSION", |
| 924 |
"HOST", |
| 925 |
"NUM_JOBS", |
| 926 |
"OPT_LEVEL", |
| 927 |
"PKG_AUTHORS", |
| 928 |
"PKG_DESCRIPTION", |
| 929 |
"PKG_HOMEPAGE", |
| 930 |
"PKG_LICENSE", |
| 931 |
"PKG_NAME", |
| 932 |
"PKG_REPOSITORY", |
| 933 |
"PKG_VERSION", |
| 934 |
"PKG_VERSION_MAJOR", |
| 935 |
"PKG_VERSION_MINOR", |
| 936 |
"PKG_VERSION_PATCH", |
| 937 |
"PKG_VERSION_PRE", |
| 938 |
"PROFILE", |
| 939 |
"RUSTC", |
| 940 |
"RUSTC_VERSION", |
| 941 |
"RUSTDOC", |
| 942 |
"RUSTDOC_VERSION", |
| 943 |
"TARGET", |
| 944 |
]); |
| 945 |
println!("builttestsuccess"); |
| 946 |
}"#, |
| 947 |
); |
| 948 |
p.create_and_run(&[]); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
#[test] |
| 953 |
#[cfg(feature = "chrono")] |
| •954 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 955 |
fn source_date_epoch() { |
| 956 |
let mut p = Project::new(); |
| 957 |
let built_root = get_built_root(); |
| 958 |
|
| 959 |
p.add_file( |
| 960 |
"Cargo.toml", |
| 961 |
format!( |
| 962 |
r#" |
| 963 |
[package] |
| 964 |
name = "testbox" |
| 965 |
version = "1.2.3-rc1" |
| 966 |
authors = ["Joe", "Bob", "Harry:Potter"] |
| 967 |
build = "build.rs" |
| 968 |
description = "xobtset" |
| 969 |
|
| 970 |
[dependencies] |
| 971 |
built = {{ path = "{built_root}", features=["chrono"] }} |
| 972 |
|
| 973 |
[build-dependencies] |
| 974 |
built = {{ path = "{built_root}", features=["chrono"] }}"#, |
| 975 |
built_root = built_root.display().to_string().escape_default() |
| 976 |
), |
| 977 |
) |
| 978 |
.add_file( |
| 979 |
"build.rs", |
| 980 |
r#" |
| 981 |
fn main() { |
| 982 |
built::write_built_file().unwrap(); |
| 983 |
}"#, |
| 984 |
) |
| 985 |
.add_file( |
| 986 |
"src/main.rs", |
| 987 |
r#" |
| 988 |
mod built_info { |
| 989 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 990 |
} |
| 991 |
|
| 992 |
fn main() { |
| 993 |
assert_eq!(built::util::strptime(built_info::BUILT_TIME_UTC).to_rfc2822(), |
| 994 |
"Sat, 25 May 2024 12:15:59 +0000"); |
| 995 |
assert_eq!(built_info::NUM_JOBS, 1); |
| 996 |
println!("builttestsuccess"); |
| 997 |
}"#, |
| 998 |
) |
| 999 |
.set_env("SOURCE_DATE_EPOCH", "1716639359"); |
| 1000 |
p.create_and_run(&[]); |
| 1001 |
} |
| 1002 |
|
| 1003 |
#[test] |
| 1004 |
#[cfg(any(feature = "git2", feature = "gix"))] |
| •1005 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 1006 |
fn git_no_git() { |
| 1007 |
for git_feature in git_features_for_bootstrap() { |
| 1008 |
// `root` isn't even a git-repo |
| 1009 |
let mut p = Project::new(); |
| 1010 |
p.bootstrap(git_feature).add_file( |
| 1011 |
"src/main.rs", |
| 1012 |
r#" |
| 1013 |
mod built_info { |
| 1014 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 1015 |
} |
| 1016 |
|
| 1017 |
fn main() { |
| 1018 |
assert_eq!(built_info::GIT_DIRTY, None); |
| 1019 |
println!("builttestsuccess"); |
| 1020 |
} |
| 1021 |
"#, |
| 1022 |
); |
| 1023 |
|
| 1024 |
p.create_and_run(&[]); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
#[test] |
| 1029 |
#[cfg(feature = "git2")] |
| •1030 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 1031 |
fn clean_then_dirty_git() { |
| 1032 |
for git_feature in git_features_for_bootstrap() { |
| 1033 |
let mut p = Project::new(); |
| 1034 |
p.bootstrap(git_feature).add_file( |
| 1035 |
"src/main.rs", |
| 1036 |
r#" |
| 1037 |
mod built_info { |
| 1038 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 1039 |
} |
| 1040 |
|
| 1041 |
fn main() { |
| 1042 |
assert_eq!(built_info::GIT_DIRTY, Some(false)); |
| 1043 |
println!("builttestsuccess"); |
| 1044 |
} |
| 1045 |
"#, |
| 1046 |
); |
| 1047 |
let repo = p.init_git(); |
| 1048 |
let root = p.create().expect("Creating the project failed"); |
| 1049 |
|
| 1050 |
let sig = git2::Signature::now("foo", "bar").unwrap(); |
| 1051 |
|
| 1052 |
let mut idx = repo.index().unwrap(); |
| 1053 |
for p in &["src/main.rs", "build.rs"] { |
| 1054 |
idx.add_path(path::Path::new(p)).unwrap(); |
| 1055 |
} |
| 1056 |
idx.write().unwrap(); |
| 1057 |
repo.commit( |
| 1058 |
Some("HEAD"), |
| 1059 |
&sig, |
| 1060 |
&sig, |
| 1061 |
"Testing testing 1 2 3", |
| 1062 |
&repo.find_tree(idx.write_tree().unwrap()).unwrap(), |
| 1063 |
&[], |
| 1064 |
) |
| 1065 |
.unwrap(); |
| 1066 |
Project::run( |
| 1067 |
root.as_ref(), |
| 1068 |
&[], |
| 1069 |
std::iter::empty::<(ffi::OsString, ffi::OsString)>(), |
| 1070 |
); |
| 1071 |
|
| 1072 |
let mut f = std::fs::OpenOptions::new() |
| 1073 |
.write(true) |
| 1074 |
.truncate(true) |
| 1075 |
.open(root.path().join("src/main.rs")) |
| 1076 |
.unwrap(); |
| 1077 |
f.write_all( |
| 1078 |
r#" |
| 1079 |
mod built_info { |
| 1080 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 1081 |
} |
| 1082 |
|
| 1083 |
fn main() { |
| 1084 |
assert_eq!(built_info::GIT_DIRTY, Some(true)); |
| 1085 |
assert!(built_info::GIT_COMMIT_HASH.is_some()); |
| 1086 |
assert!(built_info::GIT_COMMIT_HASH_SHORT.is_some()); |
| 1087 |
assert!(built_info::GIT_COMMIT_HASH.unwrap().starts_with(built_info::GIT_COMMIT_HASH_SHORT.unwrap())); |
| 1088 |
println!("builttestsuccess"); |
| 1089 |
} |
| 1090 |
"# |
| 1091 |
.as_bytes(), |
| 1092 |
) |
| 1093 |
.unwrap(); |
| 1094 |
|
| 1095 |
Project::run( |
| 1096 |
root.as_ref(), |
| 1097 |
&[], |
| 1098 |
std::iter::empty::<(ffi::OsString, ffi::OsString)>(), |
| 1099 |
); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
#[test] |
| 1104 |
#[cfg(feature = "git2")] |
| •1105 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 1106 |
fn empty_git() { |
| 1107 |
for git_feature in git_features_for_bootstrap() { |
| 1108 |
// Issue #7, git can be there and still fail |
| 1109 |
let mut p = Project::new(); |
| 1110 |
p.bootstrap(git_feature).add_file( |
| 1111 |
"src/main.rs", |
| 1112 |
r#" |
| 1113 |
mod built_info { |
| 1114 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 1115 |
} |
| 1116 |
|
| 1117 |
fn main() { |
| 1118 |
println!("builttestsuccess"); |
| 1119 |
} |
| 1120 |
"#, |
| 1121 |
); |
| 1122 |
p.init_git(); |
| 1123 |
p.create_and_run(&[]); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
#[cfg(target_os = "windows")] |
| 1128 |
#[test] |
| •1129 |
#[ignore = "require access to the source tree structure and write permissions"] |
| 1130 |
fn absolute_paths() { |
| 1131 |
// Issue #35. Usually binaries we refer to are simply executables names but sometimes they are |
| 1132 |
// absolute paths, containing backslashes, and everything gets sad on this devilish platform. |
| 1133 |
|
| 1134 |
for git_feature in git_features_for_bootstrap() { |
| 1135 |
let mut p = Project::new(); |
| 1136 |
p.bootstrap(git_feature).add_file( |
| 1137 |
"src/main.rs", |
| 1138 |
r#" |
| 1139 |
mod built_info { |
| 1140 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 1141 |
} |
| 1142 |
|
| 1143 |
fn main() { |
| 1144 |
println!("builttestsuccess"); |
| 1145 |
} |
| 1146 |
"#, |
| 1147 |
); |
| 1148 |
|
| 1149 |
let rustc_exe_buf = String::from_utf8( |
| 1150 |
process::Command::new("where") |
| 1151 |
.arg("rustc") |
| 1152 |
.output() |
| 1153 |
.expect("Unable to locate absolute path to rustc using `where`") |
| 1154 |
.stdout, |
| 1155 |
) |
| 1156 |
.unwrap(); |
| 1157 |
let rustc_exe = rustc_exe_buf.split("\r\n").next().unwrap(); |
| 1158 |
|
| 1159 |
// There should at least be `C:\` |
| 1160 |
assert!(rustc_exe.contains('\\')); |
| 1161 |
|
| 1162 |
let root = p.create().expect("Creating the project failed"); |
| 1163 |
let cargo_result = process::Command::new("cargo") |
| 1164 |
.current_dir(&root) |
| 1165 |
.arg("run") |
| 1166 |
.env("RUSTC", &rustc_exe) |
| 1167 |
.output() |
| 1168 |
.expect("cargo failed"); |
| 1169 |
if !cargo_result.status.success() { |
| 1170 |
panic!( |
| 1171 |
"cargo failed with {}", |
| 1172 |
String::from_utf8_lossy(&cargo_result.stderr) |
| 1173 |
); |
| 1174 |
} |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
/// Returns an iterator over the enabled git features ("gix" and/or "git2"). |
| 1179 |
fn git_features() -> impl Iterator<Item = &'static str> { |
| 1180 |
cfg!(feature = "gix") |
| 1181 |
.then_some("gix") |
| 1182 |
.into_iter() |
| 1183 |
.chain(cfg!(feature = "git2").then_some("git2")) |
| 1184 |
} |
| 1185 |
|
| 1186 |
// Produce only `Some("git2"|"gix")` or `None` if there is not a single Git feature set. |
| 1187 |
fn git_features_for_bootstrap() -> Vec<Option<&'static str>> { |
| 1188 |
let mut features = git_features().peekable(); |
| 1189 |
if features.peek().is_none() { |
| 1190 |
vec![None] |
| 1191 |
} else { |
| 1192 |
features.map(Some).collect() |
| 1193 |
} |
| 1194 |
} |