返回 CodeWhale
build_script_relocation.rs
根目录 / crates / build-support / tests / build_script_relocation.rs
1 use std::path::{Path, PathBuf};
2 use std::process::{Command, Output};
3 use tempfile::TempDir;
4
5 struct Fixture(TempDir);
6
7 impl Fixture {
8 fn new() -> Self {
9 let fixture = Self(tempfile::tempdir().expect("create build-script fixture"));
10 let support = fixture.0.path().join("support.rs");
11 std::fs::write(&support, include_str!("../src/lib.rs")).unwrap();
12 checked(
13 Command::new(rustc())
14 .args([
15 "--edition=2024",
16 "--crate-name=codewhale_build_support",
17 "--crate-type=rlib",
18 ])
19 .arg(support)
20 .arg("-o")
21 .arg(fixture.library()),
22 );
23 fixture
24 }
25
26 fn library(&self) -> PathBuf {
27 self.0.path().join("libcodewhale_build_support.rlib")
28 }
29
30 fn compile(&self, name: &str, source: &str, manifest: &Path) -> PathBuf {
31 let source_path = self.0.path().join(format!("{name}.rs"));
32 std::fs::write(&source_path, source).unwrap();
33 let executable = self
34 .0
35 .path()
36 .join(format!("{name}{}", std::env::consts::EXE_SUFFIX));
37 checked(
38 Command::new(rustc())
39 .args(["--edition=2024", "--extern"])
40 .arg(format!(
41 "codewhale_build_support={}",
42 self.library().display()
43 ))
44 .arg(source_path)
45 .arg("-o")
46 .arg(&executable)
47 .env("CARGO_MANIFEST_DIR", manifest)
48 .env("CARGO_PKG_VERSION", "1.2.3"),
49 );
50 executable
51 }
52 }
53
54 fn rustc() -> std::ffi::OsString {
55 std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into())
56 }
57
58 fn checked(command: &mut Command) -> Output {
59 let output = command.output().expect("execute build fixture");
60 assert!(
61 output.status.success(),
62 "{}",
63 String::from_utf8_lossy(&output.stderr)
64 );
65 output
66 }
67
68 fn run_script(executable: &Path, manifest: &Path, cwd: &Path) -> Command {
69 let mut command = Command::new(executable);
70 command
71 .current_dir(cwd)
72 .env("CARGO_MANIFEST_DIR", manifest)
73 .env("CARGO_CFG_TARGET_OS", "linux")
74 .env_remove("CODEWHALE_BUILD_SHA")
75 .env_remove("DEEPSEEK_BUILD_SHA")
76 .env_remove("GITHUB_SHA")
77 .env_remove("CODEWHALE_LOCAL_BUILD_SHA");
78 command
79 }
80
81 #[test]
82 fn cached_build_scripts_classify_the_current_manifest_and_require_it() {
83 let fixture = Fixture::new();
84 for (name, source) in [
85 ("cli", include_str!("../../cli/build.rs")),
86 ("engine", include_str!("../../engine/build.rs")),
87 ("tui", include_str!("../../tui/build.rs")),
88 ] {
89 let original = fixture.0.path().join(format!("{name} original"));
90 let relocated = fixture.0.path().join(format!("{name} relocated"));
91 std::fs::create_dir(&original).unwrap();
92 let executable = fixture.compile(name, source, &original);
93 let initial = checked(&mut run_script(&executable, &original, fixture.0.path()));
94 assert!(
95 String::from_utf8_lossy(&initial.stdout)
96 .contains("cargo:rustc-env=CODEWHALE_BUILD_VERSION=1.2.3 (dev)\n")
97 );
98
99 // Reuse the same executable after the path baked in at compilation is gone.
100 std::fs::rename(&original, &relocated).unwrap();
101 std::fs::write(relocated.join("Cargo.toml.orig"), "packaged source").unwrap();
102 let output = checked(&mut run_script(&executable, &relocated, fixture.0.path()));
103 let directives = String::from_utf8_lossy(&output.stdout);
104 assert!(directives.contains("cargo:rustc-env=CODEWHALE_BUILD_VERSION=1.2.3\n"));
105 assert!(directives.contains("cargo:rerun-if-env-changed=CARGO_MANIFEST_DIR\n"));
106
107 // Do not guess the current directory or fall back to the removed checkout.
108 let missing = run_script(&executable, &relocated, fixture.0.path())
109 .env_remove("CARGO_MANIFEST_DIR")
110 .output()
111 .unwrap();
112 assert!(!missing.status.success());
113 }
114 }
115
116 #[test]
117 fn tui_build_does_not_compile_or_embed_computer_use() {
118 let fixture = Fixture::new();
119 let manifest = fixture.0.path().join("manifest");
120 std::fs::create_dir(&manifest).unwrap();
121 let executable = fixture.compile("tui", include_str!("../../tui/build.rs"), &manifest);
122 let out = fixture.0.path().join("out");
123 std::fs::create_dir(&out).unwrap();
124 let output = checked(
125 run_script(&executable, &manifest, fixture.0.path())
126 .env("CARGO_CFG_TARGET_OS", "macos")
127 .env("CARGO_CFG_TARGET_ARCH", "aarch64")
128 .env("OUT_DIR", &out),
129 );
130 assert_eq!(std::fs::read_dir(out).unwrap().count(), 0);
131 let directives = String::from_utf8_lossy(&output.stdout);
132 assert!(!directives.contains("computer-use"));
133 assert!(!directives.contains("CODEWHALE_CU_SIGN_IDENTITY"));
134 }
135
135 lines RUST