| 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 |
fn bootstrap(&mut self) -> &mut Self { |
| 41 |
let built_root = get_built_root(); |
| 42 |
let features = if cfg!(feature = "git2") { |
| 43 |
r#"["git2"]"# |
| 44 |
} else { |
| 45 |
"[]" |
| 46 |
}; |
| 47 |
|
| 48 |
self.add_file( |
| 49 |
"Cargo.toml", |
| 50 |
format!( |
| 51 |
r#" |
| 52 |
[package] |
| 53 |
name = "testbox" |
| 54 |
version = "0.0.1" |
| 55 |
build = "build.rs" |
| 56 |
|
| 57 |
[build-dependencies] |
| 58 |
built = {{ path = "{}", features = {} }}"#, |
| 59 |
built_root.display().to_string().escape_default(), |
| 60 |
&features, |
| 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 |
/// Tries to find built's Cargo.toml, panics if it ends up in / |
| 152 |
fn get_built_root() -> path::PathBuf { |
| 153 |
env::current_exe() |
| 154 |
.map(|path| { |
| 155 |
let mut path = path; |
| 156 |
loop { |
| 157 |
if path.join("Cargo.toml").exists() { |
| 158 |
break; |
| 159 |
} |
| 160 |
path = path::PathBuf::from(path.parent().unwrap()); |
| 161 |
} |
| 162 |
path |
| 163 |
}) |
| 164 |
.unwrap() |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
#[ignore = "requires target x86_64-unknown-none"] |
| 169 |
fn nostd_testbox() { |
| 170 |
let mut p = Project::new(); |
| 171 |
|
| 172 |
let built_root = get_built_root(); |
| 173 |
|
| 174 |
p.add_file( |
| 175 |
"Cargo.toml", |
| 176 |
format!( |
| 177 |
r#" |
| 178 |
[package] |
| 179 |
name = "nostd_testbox" |
| 180 |
version = "1.2.3-rc1" |
| 181 |
authors = ["Joe", "Bob"] |
| 182 |
build = "build.rs" |
| 183 |
description = "xobtset" |
| 184 |
homepage = "localhost" |
| 185 |
repository = "https://dev.example.com/sources/testbox/" |
| 186 |
license = "MIT" |
| 187 |
|
| 188 |
[build-dependencies] |
| 189 |
built = {{ path = "{}", default_features=false }} |
| 190 |
|
| 191 |
[profile.dev] |
| 192 |
panic = "abort" |
| 193 |
|
| 194 |
[profile.release] |
| 195 |
panic = "abort" |
| 196 |
|
| 197 |
[features] |
| 198 |
default = ["SuperAwesome", "MegaAwesome"] |
| 199 |
SuperAwesome = [] |
| 200 |
MegaAwesome = []"#, |
| 201 |
built_root.display().to_string().escape_default(), |
| 202 |
), |
| 203 |
) |
| 204 |
.add_file( |
| 205 |
"build.rs", |
| 206 |
r#" |
| 207 |
fn main() { |
| 208 |
built::write_built_file().unwrap(); |
| 209 |
}"#, |
| 210 |
) |
| 211 |
.add_file( |
| 212 |
"src/main.rs", |
| 213 |
r#" |
| 214 |
#![no_main] |
| 215 |
#![no_std] |
| 216 |
|
| 217 |
use core::panic::PanicInfo; |
| 218 |
|
| 219 |
mod built_info { |
| 220 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 221 |
} |
| 222 |
|
| 223 |
#[panic_handler] |
| 224 |
fn panic(_panic: &PanicInfo<'_>) -> ! { |
| 225 |
loop {} |
| 226 |
} |
| 227 |
|
| 228 |
#[no_mangle] |
| 229 |
pub unsafe extern "C" fn main() -> ! { |
| 230 |
loop {} |
| 231 |
} |
| 232 |
"#, |
| 233 |
); |
| 234 |
|
| 235 |
p.create_and_build(&["--target", "x86_64-unknown-none"]); |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn unused_override() { |
| 240 |
let mut p = Project::new(); |
| 241 |
p.bootstrap() |
| 242 |
.add_file( |
| 243 |
"src/main.rs", |
| 244 |
r#" |
| 245 |
mod built_info { |
| 246 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 247 |
} |
| 248 |
|
| 249 |
fn main() { |
| 250 |
println!("builttestsuccess"); |
| 251 |
} |
| 252 |
"#, |
| 253 |
) |
| 254 |
.set_env("BUILT_OVERRIDE_testbox_FOO", "BAR") |
| 255 |
.set_env("BUILT_OVERRIDE_testbox1_FOO", "BAR"); |
| 256 |
let (stderr, _) = p.create_and_run(&[]); |
| 257 |
assert!(stderr.contains("but was ignored by built: `BUILT_OVERRIDE_testbox_FOO`.")); |
| 258 |
assert!(!stderr.contains("but was ignored by built: `BUILT_OVERRIDE_testbox1_FOO`.")); |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn minimal_testbox() { |
| 263 |
let mut p = Project::new(); |
| 264 |
|
| 265 |
let built_root = get_built_root(); |
| 266 |
|
| 267 |
p.add_file( |
| 268 |
"Cargo.toml", |
| 269 |
format!( |
| 270 |
r#" |
| 271 |
[package] |
| 272 |
name = "minimal_testbox" |
| 273 |
version = "1.2.3-rc1" |
| 274 |
authors = ["Joe", "Bob"] |
| 275 |
build = "build.rs" |
| 276 |
description = "xobtset" |
| 277 |
homepage = "localhost" |
| 278 |
repository = "https://dev.example.com/sources/testbox/" |
| 279 |
license = "MIT" |
| 280 |
|
| 281 |
[dependencies] |
| 282 |
built = {{ path = "{built_root}", default_features=false }} |
| 283 |
|
| 284 |
[build-dependencies] |
| 285 |
built = {{ path = "{built_root}", default_features=false }} |
| 286 |
|
| 287 |
[features] |
| 288 |
default = ["SuperAwesome", "MegaAwesome"] |
| 289 |
SuperAwesome = [] |
| 290 |
MegaAwesome = []"#, |
| 291 |
built_root = built_root.display().to_string().escape_default() |
| 292 |
), |
| 293 |
); |
| 294 |
|
| 295 |
p.add_file( |
| 296 |
"build.rs", |
| 297 |
r#" |
| 298 |
|
| 299 |
fn main() { |
| 300 |
built::write_built_file().unwrap(); |
| 301 |
}"#, |
| 302 |
); |
| 303 |
|
| 304 |
p.add_file( |
| 305 |
"src/main.rs", |
| 306 |
r#" |
| 307 |
//! The minimal testbox. |
| 308 |
|
| 309 |
mod built_info { |
| 310 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 311 |
} |
| 312 |
|
| 313 |
fn main() { |
| 314 |
assert_eq!(built_info::PKG_VERSION, "1.2.3-rc1"); |
| 315 |
assert_eq!(built_info::PKG_VERSION_MAJOR, "1"); |
| 316 |
assert_eq!(built_info::PKG_VERSION_MINOR, "2"); |
| 317 |
assert_eq!(built_info::PKG_VERSION_PATCH, "3"); |
| 318 |
assert_eq!(built_info::PKG_VERSION_PRE, "rc1"); |
| 319 |
assert_eq!(built_info::PKG_AUTHORS, "Joe:Bob"); |
| 320 |
assert_eq!(built_info::PKG_NAME, "minimal_testbox"); |
| 321 |
assert_eq!(built_info::PKG_DESCRIPTION, "xobtset"); |
| 322 |
assert_eq!(built_info::PKG_HOMEPAGE, "localhost"); |
| 323 |
assert_eq!(built_info::PKG_LICENSE, "MIT"); |
| 324 |
assert_eq!(built_info::PKG_REPOSITORY, "https://dev.example.com/sources/testbox/"); |
| 325 |
assert!(built_info::NUM_JOBS > 0); |
| 326 |
assert!(built_info::OPT_LEVEL == "0"); |
| 327 |
assert!(built_info::DEBUG); |
| 328 |
assert_eq!(built_info::PROFILE, "debug"); |
| 329 |
assert_eq!(built_info::FEATURES, |
| 330 |
["DEFAULT", "MEGAAWESOME", "SUPERAWESOME"]); |
| 331 |
assert_eq!(built_info::FEATURES_STR, |
| 332 |
"DEFAULT, MEGAAWESOME, SUPERAWESOME"); |
| 333 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 334 |
["default", "megaawesome", "superawesome"]); |
| 335 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 336 |
"default, megaawesome, superawesome"); |
| 337 |
assert_ne!(built_info::RUSTC_VERSION, ""); |
| 338 |
assert_ne!(built_info::RUSTDOC_VERSION, ""); |
| 339 |
assert_ne!(built_info::HOST, ""); |
| 340 |
assert_ne!(built_info::TARGET, ""); |
| 341 |
assert_ne!(built_info::RUSTC, ""); |
| 342 |
assert_ne!(built_info::RUSTDOC, ""); |
| 343 |
assert_ne!(built_info::CFG_TARGET_ARCH, ""); |
| 344 |
assert_ne!(built_info::CFG_ENDIAN, ""); |
| 345 |
assert_ne!(built_info::CFG_FAMILY, ""); |
| 346 |
assert_ne!(built_info::CFG_OS, ""); |
| 347 |
assert_ne!(built_info::CFG_POINTER_WIDTH, ""); |
| 348 |
// For CFG_ENV, empty string is a possible value. |
| 349 |
let _: &'static str = built_info::CFG_ENV; |
| 350 |
println!("builttestsuccess"); |
| 351 |
}"#, |
| 352 |
); |
| 353 |
|
| 354 |
p.create_and_run(&[]); |
| 355 |
} |
| 356 |
|
| 357 |
#[test] |
| 358 |
fn simple_workspace() { |
| 359 |
let mut p = Project::new(); |
| 360 |
let built_root = get_built_root(); |
| 361 |
|
| 362 |
p.add_file("Cargo.toml", "[workspace]\nmembers = ['foobar']"); |
| 363 |
p.add_file( |
| 364 |
"foobar/Cargo.toml", |
| 365 |
format!( |
| 366 |
r#"[package] |
| 367 |
name = "foobar" |
| 368 |
version = "5.6.7" |
| 369 |
build = "build.rs" |
| 370 |
|
| 371 |
[build-dependencies] |
| 372 |
built = {{ path = "{}" }}"#, |
| 373 |
built_root.display().to_string().escape_default() |
| 374 |
), |
| 375 |
); |
| 376 |
p.add_file( |
| 377 |
"foobar/build.rs", |
| 378 |
"fn main() { built::write_built_file().unwrap(); }", |
| 379 |
); |
| 380 |
p.add_file( |
| 381 |
"foobar/src/main.rs", |
| 382 |
r#" |
| 383 |
mod built_info { |
| 384 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 385 |
} |
| 386 |
fn main() { |
| 387 |
assert_eq!(built_info::PKG_VERSION, "5.6.7"); |
| 388 |
println!("builttestsuccess"); |
| 389 |
} |
| 390 |
"#, |
| 391 |
); |
| 392 |
p.create_and_run(&[]); |
| 393 |
} |
| 394 |
|
| 395 |
#[test] |
| 396 |
#[cfg(all( |
| 397 |
feature = "cargo-lock", |
| 398 |
feature = "dependency-tree", |
| 399 |
feature = "git2", |
| 400 |
feature = "chrono", |
| 401 |
feature = "semver" |
| 402 |
))] |
| 403 |
fn full_testbox() { |
| 404 |
let mut p = Project::new(); |
| 405 |
|
| 406 |
let built_root = get_built_root(); |
| 407 |
|
| 408 |
p.add_file( |
| 409 |
"Cargo.toml", |
| 410 |
format!( |
| 411 |
r#" |
| 412 |
[package] |
| 413 |
name = "testbox" |
| 414 |
version = "1.2.3-rc1" |
| 415 |
authors = ["Joe", "Bob", "Harry:Potter"] |
| 416 |
build = "build.rs" |
| 417 |
description = "xobtset" |
| 418 |
homepage = "localhost" |
| 419 |
repository = "https://dev.example.com/sources/testbox/" |
| 420 |
license = "MIT" |
| 421 |
|
| 422 |
[dependencies] |
| 423 |
built = {{ path = "{built_root}", features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }} |
| 424 |
|
| 425 |
[build-dependencies] |
| 426 |
built = {{ path = "{built_root}", features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }} |
| 427 |
|
| 428 |
[features] |
| 429 |
default = ["SuperAwesome", "MegaAwesome"] |
| 430 |
SuperAwesome = [] |
| 431 |
MegaAwesome = []"#, |
| 432 |
built_root = built_root.display().to_string().escape_default() |
| 433 |
), |
| 434 |
) |
| 435 |
.add_file( |
| 436 |
"build.rs", |
| 437 |
r#" |
| 438 |
fn main() { |
| 439 |
built::write_built_file().unwrap(); |
| 440 |
}"#, |
| 441 |
) |
| 442 |
.set_env("CONTINUOUS_INTEGRATION", "1") |
| 443 |
.add_file( |
| 444 |
"src/main.rs", |
| 445 |
r#" |
| 446 |
//! The testbox. |
| 447 |
|
| 448 |
mod built_info { |
| 449 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 450 |
} |
| 451 |
|
| 452 |
fn main() { |
| 453 |
assert_eq!(built_info::GIT_VERSION, None); |
| 454 |
assert_eq!(built_info::GIT_DIRTY, None); |
| 455 |
assert_eq!(built_info::GIT_COMMIT_HASH, None); |
| 456 |
assert_eq!(built_info::GIT_COMMIT_HASH_SHORT, None); |
| 457 |
assert_eq!(built_info::GIT_HEAD_REF, None); |
| 458 |
assert!(built_info::CI_PLATFORM.is_some()); |
| 459 |
assert_eq!(built_info::PKG_VERSION, "1.2.3-rc1"); |
| 460 |
assert_eq!(built_info::PKG_VERSION_MAJOR, "1"); |
| 461 |
assert_eq!(built_info::PKG_VERSION_MINOR, "2"); |
| 462 |
assert_eq!(built_info::PKG_VERSION_PATCH, "3"); |
| 463 |
assert_eq!(built_info::PKG_VERSION_PRE, "rc1"); |
| 464 |
assert_eq!(built_info::PKG_AUTHORS, "Joe:Bob:Harry:Potter"); |
| 465 |
assert_eq!(built_info::PKG_NAME, "testbox"); |
| 466 |
assert_eq!(built_info::PKG_DESCRIPTION, "xobtset"); |
| 467 |
assert_eq!(built_info::PKG_HOMEPAGE, "localhost"); |
| 468 |
assert_eq!(built_info::PKG_LICENSE, "MIT"); |
| 469 |
assert_eq!(built_info::PKG_REPOSITORY, "https://dev.example.com/sources/testbox/"); |
| 470 |
assert!(built_info::NUM_JOBS > 0); |
| 471 |
assert!(built_info::OPT_LEVEL == "0"); |
| 472 |
assert!(built_info::DEBUG); |
| 473 |
assert_eq!(built_info::PROFILE, "debug"); |
| 474 |
assert_eq!(built_info::FEATURES, |
| 475 |
["DEFAULT", "MEGAAWESOME", "SUPERAWESOME"]); |
| 476 |
assert_eq!(built_info::FEATURES_STR, |
| 477 |
"DEFAULT, MEGAAWESOME, SUPERAWESOME"); |
| 478 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 479 |
["default", "megaawesome", "superawesome"]); |
| 480 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 481 |
"default, megaawesome, superawesome"); |
| 482 |
assert_ne!(built_info::RUSTC_VERSION, ""); |
| 483 |
assert_ne!(built_info::RUSTDOC_VERSION, ""); |
| 484 |
assert_ne!(built_info::DEPENDENCIES_STR, ""); |
| 485 |
assert_ne!(built_info::DIRECT_DEPENDENCIES_STR, ""); |
| 486 |
assert_ne!(built_info::INDIRECT_DEPENDENCIES_STR, ""); |
| 487 |
assert_ne!(built_info::HOST, ""); |
| 488 |
assert_ne!(built_info::TARGET, ""); |
| 489 |
assert_ne!(built_info::RUSTC, ""); |
| 490 |
assert_ne!(built_info::RUSTDOC, ""); |
| 491 |
assert_ne!(built_info::CFG_TARGET_ARCH, ""); |
| 492 |
assert_ne!(built_info::CFG_ENDIAN, ""); |
| 493 |
assert_ne!(built_info::CFG_FAMILY, ""); |
| 494 |
assert_ne!(built_info::CFG_OS, ""); |
| 495 |
assert_ne!(built_info::CFG_POINTER_WIDTH, ""); |
| 496 |
// For CFG_ENV, empty string is a possible value. |
| 497 |
let _: &'static str = built_info::CFG_ENV; |
| 498 |
|
| 499 |
assert!(built::util::parse_versions(built_info::DEPENDENCIES.iter()) |
| 500 |
.any(|(name, ver)| name == "toml" && ver >= built::semver::Version::parse("0.1.0").unwrap())); |
| 501 |
|
| 502 |
assert_eq!(built_info::DIRECT_DEPENDENCIES.len(), 1); |
| 503 |
assert_eq!(built_info::DIRECT_DEPENDENCIES[0].0, "built"); |
| 504 |
|
| 505 |
assert!((built::chrono::offset::Utc::now() - built::util::strptime(built_info::BUILT_TIME_UTC)).num_days() <= 1); |
| 506 |
|
| 507 |
assert!(built_info::OVERRIDE_VARIABLES_USED.is_empty()); |
| 508 |
|
| 509 |
println!("builttestsuccess"); |
| 510 |
}"#, |
| 511 |
); |
| 512 |
p.create_and_run(&[]); |
| 513 |
} |
| 514 |
|
| 515 |
#[test] |
| 516 |
#[cfg(all(feature = "git2", feature = "chrono", feature = "semver"))] |
| 517 |
fn overridden_testbox() { |
| 518 |
let mut p = Project::new(); |
| 519 |
|
| 520 |
let built_root = get_built_root(); |
| 521 |
|
| 522 |
p.add_file( |
| 523 |
"Cargo.toml", |
| 524 |
format!( |
| 525 |
r#" |
| 526 |
[package] |
| 527 |
name = "testbox" |
| 528 |
version = "1.2.3-rc1" |
| 529 |
authors = ["Joe", "Bob", "Harry:Potter"] |
| 530 |
build = "build.rs" |
| 531 |
description = "xobtset" |
| 532 |
homepage = "localhost" |
| 533 |
repository = "https://dev.example.com/sources/testbox/" |
| 534 |
license = "MIT" |
| 535 |
|
| 536 |
[dependencies] |
| 537 |
built = {{ path = "{built_root}", features=["git2", "chrono", "semver"] }} |
| 538 |
|
| 539 |
[build-dependencies] |
| 540 |
built = {{ path = "{built_root}", features=["git2", "chrono", "semver"] }} |
| 541 |
|
| 542 |
[features] |
| 543 |
default = ["SuperAwesome", "MegaAwesome"] |
| 544 |
SuperAwesome = [] |
| 545 |
MegaAwesome = []"#, |
| 546 |
built_root = built_root.display().to_string().escape_default() |
| 547 |
), |
| 548 |
) |
| 549 |
.add_file( |
| 550 |
"build.rs", |
| 551 |
r#" |
| 552 |
fn main() { |
| 553 |
built::write_built_file().unwrap(); |
| 554 |
}"#, |
| 555 |
) |
| 556 |
.set_env("BUILT_OVERRIDE_testbox_GIT_VERSION", "GIT_VERSION1") |
| 557 |
.set_env("BUILT_OVERRIDE_testbox_GIT_DIRTY", "false") |
| 558 |
.set_env("BUILT_OVERRIDE_testbox_GIT_COMMIT_HASH", "1234567890") |
| 559 |
.set_env("BUILT_OVERRIDE_testbox_GIT_HEAD_REF", "GIT_REF") |
| 560 |
.set_env("BUILT_OVERRIDE_testbox_CI_PLATFORM", "TESTBOXCI") |
| 561 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION", "1.2.3.4") |
| 562 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_MAJOR", "abc") |
| 563 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_MINOR", "def") |
| 564 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_PATCH", "ghi") |
| 565 |
.set_env("BUILT_OVERRIDE_testbox_PKG_VERSION_PRE", "jkl") |
| 566 |
.set_env("BUILT_OVERRIDE_testbox_PKG_AUTHORS", "The council") |
| 567 |
.set_env("BUILT_OVERRIDE_testbox_PKG_NAME", "OVERRIDEBOX") |
| 568 |
.set_env("BUILT_OVERRIDE_testbox_PKG_DESCRIPTION", "TEST") |
| 569 |
.set_env("BUILT_OVERRIDE_testbox_PKG_HOMEPAGE", "foreignhost") |
| 570 |
.set_env("BUILT_OVERRIDE_testbox_PKG_LICENSE", "MITv2") |
| 571 |
.set_env("BUILT_OVERRIDE_testbox_PKG_REPOSITORY", "8.8.8.8") |
| 572 |
.set_env("BUILT_OVERRIDE_testbox_NUM_JOBS", "999") |
| 573 |
.set_env( |
| 574 |
"BUILT_OVERRIDE_testbox_OPT_LEVEL", |
| 575 |
"not_too_fast_not_too_slow", |
| 576 |
) |
| 577 |
.set_env("BUILT_OVERRIDE_testbox_DEBUG", "false") |
| 578 |
.set_env("BUILT_OVERRIDE_testbox_PROFILE", "MEDIUM") |
| 579 |
.set_env( |
| 580 |
"BUILT_OVERRIDE_testbox_FEATURES", |
| 581 |
"cup_holder,Stereo Sound, dynamic range", |
| 582 |
) |
| 583 |
.set_env("BUILT_OVERRIDE_testbox_RUSTC", "overridec") |
| 584 |
.set_env("BUILT_OVERRIDE_testbox_RUSTC_VERSION", "overridec v1") |
| 585 |
.set_env("BUILT_OVERRIDE_testbox_RUSTDOC", "overridedoc") |
| 586 |
.set_env("BUILT_OVERRIDE_testbox_RUSTDOC_VERSION", "overridedoc v1") |
| 587 |
.set_env("BUILT_OVERRIDE_testbox_HOST", "overridehost") |
| 588 |
.set_env("BUILT_OVERRIDE_testbox_TARGET", "potato") |
| 589 |
.set_env("BUILT_OVERRIDE_testbox_CFG_TARGET_ARCH", "potatoes") |
| 590 |
.set_env("BUILT_OVERRIDE_testbox_CFG_ENDIAN", "random") |
| 591 |
.set_env("BUILT_OVERRIDE_testbox_CFG_FAMILY", "v0") |
| 592 |
.set_env("BUILT_OVERRIDE_testbox_CFG_OS", "none") |
| 593 |
.set_env("BUILT_OVERRIDE_testbox_CFG_POINTER_WIDTH", "63.9998") |
| 594 |
.set_env("BUILT_OVERRIDE_testbox_CFG_ENV", "calm") |
| 595 |
.set_env( |
| 596 |
"BUILT_OVERRIDE_testbox_BUILT_TIME_UTC", |
| 597 |
"Sat, 25 May 2024 12:15:59 +0000", |
| 598 |
) |
| 599 |
.add_file( |
| 600 |
"src/main.rs", |
| 601 |
r#" |
| 602 |
//! The testbox. |
| 603 |
|
| 604 |
mod built_info { |
| 605 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 606 |
} |
| 607 |
|
| 608 |
fn main() { |
| 609 |
assert_eq!(built_info::GIT_VERSION, Some("GIT_VERSION1")); |
| 610 |
assert_eq!(built_info::GIT_DIRTY, Some(false)); |
| 611 |
assert_eq!(built_info::GIT_COMMIT_HASH, Some("1234567890")); |
| 612 |
assert_eq!(built_info::GIT_COMMIT_HASH_SHORT, Some("12345678")); |
| 613 |
assert_eq!(built_info::GIT_HEAD_REF, Some("GIT_REF")); |
| 614 |
assert_eq!(built_info::CI_PLATFORM, Some("TESTBOXCI")); |
| 615 |
assert_eq!(built_info::PKG_VERSION, "1.2.3.4"); |
| 616 |
assert_eq!(built_info::PKG_VERSION_MAJOR, "abc"); |
| 617 |
assert_eq!(built_info::PKG_VERSION_MINOR, "def"); |
| 618 |
assert_eq!(built_info::PKG_VERSION_PATCH, "ghi"); |
| 619 |
assert_eq!(built_info::PKG_VERSION_PRE, "jkl"); |
| 620 |
assert_eq!(built_info::PKG_AUTHORS, "The council"); |
| 621 |
assert_eq!(built_info::PKG_NAME, "OVERRIDEBOX"); |
| 622 |
assert_eq!(built_info::PKG_DESCRIPTION, "TEST"); |
| 623 |
assert_eq!(built_info::PKG_HOMEPAGE, "foreignhost"); |
| 624 |
assert_eq!(built_info::PKG_LICENSE, "MITv2"); |
| 625 |
assert_eq!(built_info::PKG_REPOSITORY, "8.8.8.8"); |
| 626 |
assert_eq!(built_info::NUM_JOBS, 999); |
| 627 |
assert_eq!(built_info::OPT_LEVEL, "not_too_fast_not_too_slow"); |
| 628 |
assert!(!built_info::DEBUG); |
| 629 |
assert_eq!(built_info::PROFILE, "MEDIUM"); |
| 630 |
assert_eq!(built_info::FEATURES, |
| 631 |
["Stereo Sound", "cup_holder", "dynamic range"]); |
| 632 |
assert_eq!(built_info::FEATURES_STR, |
| 633 |
"Stereo Sound, cup_holder, dynamic range"); |
| 634 |
assert_eq!(built_info::FEATURES_LOWERCASE, |
| 635 |
["cup_holder", "dynamic range", "stereo sound"]); |
| 636 |
assert_eq!(built_info::FEATURES_LOWERCASE_STR, |
| 637 |
"cup_holder, dynamic range, stereo sound"); |
| 638 |
assert_eq!(built_info::RUSTC, "overridec"); |
| 639 |
assert_eq!(built_info::RUSTC_VERSION, "overridec v1"); |
| 640 |
assert_eq!(built_info::RUSTDOC, "overridedoc"); |
| 641 |
assert_eq!(built_info::RUSTDOC_VERSION, "overridedoc v1"); |
| 642 |
assert_eq!(built_info::HOST, "overridehost"); |
| 643 |
assert_eq!(built_info::TARGET, "potato"); |
| 644 |
assert_eq!(built_info::CFG_TARGET_ARCH, "potatoes"); |
| 645 |
assert_eq!(built_info::CFG_ENDIAN, "random"); |
| 646 |
assert_eq!(built_info::CFG_FAMILY, "v0"); |
| 647 |
assert_eq!(built_info::CFG_OS, "none"); |
| 648 |
assert_eq!(built_info::CFG_POINTER_WIDTH, "63.9998"); |
| 649 |
assert_eq!(built_info::CFG_ENV, "calm"); |
| 650 |
|
| 651 |
assert_eq!(built::util::strptime(built_info::BUILT_TIME_UTC).to_rfc2822(), |
| 652 |
"Sat, 25 May 2024 12:15:59 +0000"); |
| 653 |
assert_eq!(built_info::OVERRIDE_VARIABLES_USED, [ |
| 654 |
"BUILT_TIME_UTC", |
| 655 |
"CFG_ENDIAN", |
| 656 |
"CFG_ENV", |
| 657 |
"CFG_FAMILY", |
| 658 |
"CFG_OS", |
| 659 |
"CFG_POINTER_WIDTH", |
| 660 |
"CFG_TARGET_ARCH", |
| 661 |
"CI_PLATFORM", |
| 662 |
"DEBUG", |
| 663 |
"FEATURES", |
| 664 |
"GIT_COMMIT_HASH", |
| 665 |
"GIT_DIRTY", |
| 666 |
"GIT_HEAD_REF", |
| 667 |
"GIT_VERSION", |
| 668 |
"HOST", |
| 669 |
"NUM_JOBS", |
| 670 |
"OPT_LEVEL", |
| 671 |
"PKG_AUTHORS", |
| 672 |
"PKG_DESCRIPTION", |
| 673 |
"PKG_HOMEPAGE", |
| 674 |
"PKG_LICENSE", |
| 675 |
"PKG_NAME", |
| 676 |
"PKG_REPOSITORY", |
| 677 |
"PKG_VERSION", |
| 678 |
"PKG_VERSION_MAJOR", |
| 679 |
"PKG_VERSION_MINOR", |
| 680 |
"PKG_VERSION_PATCH", |
| 681 |
"PKG_VERSION_PRE", |
| 682 |
"PROFILE", |
| 683 |
"RUSTC", |
| 684 |
"RUSTC_VERSION", |
| 685 |
"RUSTDOC", |
| 686 |
"RUSTDOC_VERSION", |
| 687 |
"TARGET", |
| 688 |
]); |
| 689 |
println!("builttestsuccess"); |
| 690 |
}"#, |
| 691 |
); |
| 692 |
p.create_and_run(&[]); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
#[cfg(feature = "chrono")] |
| 697 |
fn source_date_epoch() { |
| 698 |
let mut p = Project::new(); |
| 699 |
let built_root = get_built_root(); |
| 700 |
|
| 701 |
p.add_file( |
| 702 |
"Cargo.toml", |
| 703 |
format!( |
| 704 |
r#" |
| 705 |
[package] |
| 706 |
name = "testbox" |
| 707 |
version = "1.2.3-rc1" |
| 708 |
authors = ["Joe", "Bob", "Harry:Potter"] |
| 709 |
build = "build.rs" |
| 710 |
description = "xobtset" |
| 711 |
|
| 712 |
[dependencies] |
| 713 |
built = {{ path = "{built_root}", features=["chrono"] }} |
| 714 |
|
| 715 |
[build-dependencies] |
| 716 |
built = {{ path = "{built_root}", features=["chrono"] }}"#, |
| 717 |
built_root = built_root.display().to_string().escape_default() |
| 718 |
), |
| 719 |
) |
| 720 |
.add_file( |
| 721 |
"build.rs", |
| 722 |
r#" |
| 723 |
fn main() { |
| 724 |
built::write_built_file().unwrap(); |
| 725 |
}"#, |
| 726 |
) |
| 727 |
.add_file( |
| 728 |
"src/main.rs", |
| 729 |
r#" |
| 730 |
mod built_info { |
| 731 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 732 |
} |
| 733 |
|
| 734 |
fn main() { |
| 735 |
assert_eq!(built::util::strptime(built_info::BUILT_TIME_UTC).to_rfc2822(), |
| 736 |
"Sat, 25 May 2024 12:15:59 +0000"); |
| 737 |
assert_eq!(built_info::NUM_JOBS, 1); |
| 738 |
println!("builttestsuccess"); |
| 739 |
}"#, |
| 740 |
) |
| 741 |
.set_env("SOURCE_DATE_EPOCH", "1716639359"); |
| 742 |
p.create_and_run(&[]); |
| 743 |
} |
| 744 |
|
| 745 |
#[test] |
| 746 |
#[cfg(feature = "git2")] |
| 747 |
fn git_no_git() { |
| 748 |
// `root` isn't even a git-repo |
| 749 |
let mut p = Project::new(); |
| 750 |
p.bootstrap().add_file( |
| 751 |
"src/main.rs", |
| 752 |
r#" |
| 753 |
mod built_info { |
| 754 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 755 |
} |
| 756 |
|
| 757 |
fn main() { |
| 758 |
assert_eq!(built_info::GIT_DIRTY, None); |
| 759 |
println!("builttestsuccess"); |
| 760 |
} |
| 761 |
"#, |
| 762 |
); |
| 763 |
|
| 764 |
p.create_and_run(&[]); |
| 765 |
} |
| 766 |
|
| 767 |
#[test] |
| 768 |
#[cfg(feature = "git2")] |
| 769 |
fn clean_then_dirty_git() { |
| 770 |
let mut p = Project::new(); |
| 771 |
p.bootstrap().add_file( |
| 772 |
"src/main.rs", |
| 773 |
r#" |
| 774 |
mod built_info { |
| 775 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 776 |
} |
| 777 |
|
| 778 |
fn main() { |
| 779 |
assert_eq!(built_info::GIT_DIRTY, Some(false)); |
| 780 |
println!("builttestsuccess"); |
| 781 |
} |
| 782 |
"#, |
| 783 |
); |
| 784 |
let repo = p.init_git(); |
| 785 |
let root = p.create().expect("Creating the project failed"); |
| 786 |
|
| 787 |
let sig = git2::Signature::now("foo", "bar").unwrap(); |
| 788 |
|
| 789 |
let mut idx = repo.index().unwrap(); |
| 790 |
for p in &["src/main.rs", "build.rs"] { |
| 791 |
idx.add_path(path::Path::new(p)).unwrap(); |
| 792 |
} |
| 793 |
idx.write().unwrap(); |
| 794 |
repo.commit( |
| 795 |
Some("HEAD"), |
| 796 |
&sig, |
| 797 |
&sig, |
| 798 |
"Testing testing 1 2 3", |
| 799 |
&repo.find_tree(idx.write_tree().unwrap()).unwrap(), |
| 800 |
&[], |
| 801 |
) |
| 802 |
.unwrap(); |
| 803 |
Project::run( |
| 804 |
root.as_ref(), |
| 805 |
&[], |
| 806 |
std::iter::empty::<(ffi::OsString, ffi::OsString)>(), |
| 807 |
); |
| 808 |
|
| 809 |
let mut f = std::fs::OpenOptions::new() |
| 810 |
.write(true) |
| 811 |
.truncate(true) |
| 812 |
.open(root.path().join("src/main.rs")) |
| 813 |
.unwrap(); |
| 814 |
f.write_all( |
| 815 |
r#" |
| 816 |
mod built_info { |
| 817 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 818 |
} |
| 819 |
|
| 820 |
fn main() { |
| 821 |
assert_eq!(built_info::GIT_DIRTY, Some(true)); |
| 822 |
assert!(built_info::GIT_COMMIT_HASH.is_some()); |
| 823 |
assert!(built_info::GIT_COMMIT_HASH_SHORT.is_some()); |
| 824 |
assert!(built_info::GIT_COMMIT_HASH.unwrap().starts_with(built_info::GIT_COMMIT_HASH_SHORT.unwrap())); |
| 825 |
println!("builttestsuccess"); |
| 826 |
} |
| 827 |
"# |
| 828 |
.as_bytes(), |
| 829 |
) |
| 830 |
.unwrap(); |
| 831 |
|
| 832 |
Project::run( |
| 833 |
root.as_ref(), |
| 834 |
&[], |
| 835 |
std::iter::empty::<(ffi::OsString, ffi::OsString)>(), |
| 836 |
); |
| 837 |
} |
| 838 |
|
| 839 |
#[test] |
| 840 |
#[cfg(feature = "git2")] |
| 841 |
fn empty_git() { |
| 842 |
// Issue #7, git can be there and still fail |
| 843 |
let mut p = Project::new(); |
| 844 |
p.bootstrap().add_file( |
| 845 |
"src/main.rs", |
| 846 |
r#" |
| 847 |
mod built_info { |
| 848 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 849 |
} |
| 850 |
|
| 851 |
fn main() { |
| 852 |
println!("builttestsuccess"); |
| 853 |
} |
| 854 |
"#, |
| 855 |
); |
| 856 |
p.init_git(); |
| 857 |
p.create_and_run(&[]); |
| 858 |
} |
| 859 |
|
| 860 |
#[cfg(target_os = "windows")] |
| 861 |
#[test] |
| 862 |
fn absolute_paths() { |
| 863 |
// Issue #35. Usually binaries we refer to are simply executables names but sometimes they are |
| 864 |
// absolute paths, containing backslashes, and everything gets sad on this devilish platform. |
| 865 |
|
| 866 |
let mut p = Project::new(); |
| 867 |
p.bootstrap().add_file( |
| 868 |
"src/main.rs", |
| 869 |
r#" |
| 870 |
mod built_info { |
| 871 |
include!(concat!(env!("OUT_DIR"), "/built.rs")); |
| 872 |
} |
| 873 |
|
| 874 |
fn main() { |
| 875 |
println!("builttestsuccess"); |
| 876 |
} |
| 877 |
"#, |
| 878 |
); |
| 879 |
|
| 880 |
let rustc_exe_buf = String::from_utf8( |
| 881 |
process::Command::new("where") |
| 882 |
.arg("rustc") |
| 883 |
.output() |
| 884 |
.expect("Unable to locate absolute path to rustc using `where`") |
| 885 |
.stdout, |
| 886 |
) |
| 887 |
.unwrap(); |
| 888 |
let rustc_exe = rustc_exe_buf.split("\r\n").next().unwrap(); |
| 889 |
|
| 890 |
// There should at least be `C:\` |
| 891 |
assert!(rustc_exe.contains('\\')); |
| 892 |
|
| 893 |
let root = p.create().expect("Creating the project failed"); |
| 894 |
let cargo_result = process::Command::new("cargo") |
| 895 |
.current_dir(&root) |
| 896 |
.arg("run") |
| 897 |
.env("RUSTC", &rustc_exe) |
| 898 |
.output() |
| 899 |
.expect("cargo failed"); |
| 900 |
if !cargo_result.status.success() { |
| 901 |
panic!( |
| 902 |
"cargo failed with {}", |
| 903 |
String::from_utf8_lossy(&cargo_result.stderr) |
| 904 |
); |
| 905 |
} |
| 906 |
} |