返回 CodeWhale
binary.rs
根目录 / crates / tui / tests / support / binary.rs
1 //! Canonical executable lookup shared by both mounted acceptance harnesses.
2 //!
3 //! The Engine crate is a library. Build `codewhale-cli --bin codewhale` before
4 //! acceptance tests, or supply QA_TUI_BIN for a deliberately selected QA build.
5
6 use std::path::{Path, PathBuf};
7
8 pub fn codewhale() -> PathBuf {
9 let qa = std::env::var_os("QA_TUI_BIN")
10 .filter(|value| !value.is_empty())
11 .map(PathBuf::from);
12 let cargo = std::env::var_os("CARGO_BIN_EXE_codewhale")
13 .filter(|value| !value.is_empty())
14 .map(PathBuf::from)
15 .or_else(|| option_env!("CARGO_BIN_EXE_codewhale").map(PathBuf::from));
16 let workspace = Path::new(env!("CARGO_MANIFEST_DIR"))
17 .parent()
18 .and_then(Path::parent)
19 .expect("Engine workspace root");
20 let target = std::env::var_os("CARGO_TARGET_DIR")
21 .filter(|value| !value.is_empty())
22 .map(PathBuf::from)
23 .unwrap_or_else(|| workspace.join("target"));
24 let target = if target.is_absolute() {
25 target
26 } else {
27 workspace.join(target)
28 };
29 resolve(
30 qa,
31 cargo,
32 &std::env::current_exe().expect("current test executable"),
33 &target,
34 )
35 .unwrap_or_else(|message| panic!("{message}"))
36 }
37
38 fn resolve(
39 qa: Option<PathBuf>,
40 cargo: Option<PathBuf>,
41 test_executable: &Path,
42 target_directory: &Path,
43 ) -> Result<PathBuf, String> {
44 if let Some(path) = qa.or(cargo) {
45 return require_file(path);
46 }
47 let directory = test_executable
48 .parent()
49 .ok_or("test executable has no parent")?;
50 let directory = if directory.ends_with("deps") {
51 directory
52 .parent()
53 .ok_or("test deps directory has no parent")?
54 } else if directory.ends_with("out") {
55 directory
56 .ancestors()
57 .skip(1)
58 .find(|ancestor| ancestor.ends_with("build"))
59 .and_then(Path::parent)
60 .unwrap_or(directory)
61 } else {
62 directory
63 };
64 let name = format!("codewhale{}", std::env::consts::EXE_SUFFIX);
65 let adjacent = directory.join(&name);
66 if adjacent.is_file() {
67 return Ok(adjacent);
68 }
69 // Cargo's split build-dir stores test deps separately from final binaries.
70 // Only inspect the known workspace/explicit target directory; never scan
71 // PATH, another checkout, or a legacy executable name.
72 let profile = directory
73 .file_name()
74 .ok_or("test profile directory has no name")?;
75 // Explicit --target builds add one triple directory to both layouts.
76 // Prefer that exact projection over a coexisting host executable.
77 if let Some(triple) = directory.parent().and_then(Path::file_name) {
78 let cross_target = target_directory.join(triple).join(profile).join(&name);
79 if cross_target.is_file() {
80 return Ok(cross_target);
81 }
82 }
83 require_file(target_directory.join(profile).join(&name))
84 }
85
86 // This synchronous resolver belongs only to Cargo acceptance test binaries.
87 #[cfg(test)]
88 fn require_file(path: PathBuf) -> Result<PathBuf, String> {
89 if path.is_file() {
90 if path.is_absolute() {
91 Ok(path)
92 } else {
93 path.canonicalize().map_err(|error| {
94 format!(
95 "cannot resolve selected Codewhale executable {} before changing workspace: {error}",
96 path.display()
97 )
98 })
99 }
100 } else {
101 Err(format!(
102 "canonical Codewhale executable is missing at {}: build `cargo build -p codewhale-cli --bin codewhale --locked` before Engine acceptance tests, or set QA_TUI_BIN to the intended QA binary",
103 path.display()
104 ))
105 }
106 }
107
108 #[cfg(test)]
109 mod tests {
110 use super::*;
111
112 #[test]
113 fn relative_explicit_selection_survives_an_acceptance_workspace_change() {
114 let current = std::env::current_dir().unwrap();
115 let root = tempfile::TempDir::new_in(&current).unwrap();
116 let binary = root.path().join("selected QA build");
117 std::fs::write(&binary, b"canonical").unwrap();
118 let relative = binary.strip_prefix(&current).unwrap().to_path_buf();
119 for (qa, cargo) in [
120 (Some(relative.clone()), None),
121 (None, Some(relative.clone())),
122 ] {
123 let selected = resolve(qa, cargo, &binary, root.path()).unwrap();
124 assert!(selected.is_absolute());
125 assert_eq!(selected, binary.canonicalize().unwrap());
126 assert!(
127 root.path()
128 .join("another workspace")
129 .join(selected)
130 .is_file()
131 );
132 }
133 }
134
135 #[test]
136 fn canonical_lookup_uses_the_test_target_directory_and_never_legacy_binary() {
137 let root = tempfile::TempDir::new().unwrap();
138 let debug = root.path().join("target with spaces").join("debug");
139 std::fs::create_dir_all(debug.join("deps")).unwrap();
140 let canonical = debug.join(format!("codewhale{}", std::env::consts::EXE_SUFFIX));
141 let legacy = debug.join(format!("codewhale-tui{}", std::env::consts::EXE_SUFFIX));
142 std::fs::write(&legacy, b"legacy").unwrap();
143 let executable = debug.join("deps").join("integration-hash");
144 assert!(
145 resolve(
146 None,
147 None,
148 &executable,
149 &root.path().join("target with spaces")
150 )
151 .is_err()
152 );
153 std::fs::write(&canonical, b"canonical").unwrap();
154 assert_eq!(
155 resolve(
156 None,
157 None,
158 &executable,
159 &root.path().join("target with spaces")
160 )
161 .unwrap(),
162 canonical
163 );
164 }
165
166 #[test]
167 fn explicit_qa_selection_wins_and_a_missing_selection_never_falls_back() {
168 let root = tempfile::TempDir::new().unwrap();
169 let qa = root.path().join("selected release");
170 let cargo = root.path().join("codewhale");
171 std::fs::write(&qa, b"qa").unwrap();
172 std::fs::write(&cargo, b"cargo").unwrap();
173 assert_eq!(
174 resolve(Some(qa.clone()), Some(cargo.clone()), &cargo, root.path()).unwrap(),
175 qa
176 );
177 assert!(
178 resolve(
179 Some(root.path().join("missing")),
180 Some(cargo),
181 &qa,
182 root.path()
183 )
184 .is_err()
185 );
186 }
187
188 #[test]
189 fn split_build_cache_resolves_the_known_final_target_and_cross_profile() {
190 let root = tempfile::TempDir::new().unwrap();
191 let target = root.path().join("final target");
192 let cache = root.path().join("build cache");
193 for triple in [None, Some("aarch64-unknown-linux-gnu")] {
194 let final_profile = match triple {
195 Some(triple) => target.join(triple).join("debug"),
196 None => target.join("debug"),
197 };
198 std::fs::create_dir_all(&final_profile).unwrap();
199 let canonical =
200 final_profile.join(format!("codewhale{}", std::env::consts::EXE_SUFFIX));
201 std::fs::write(&canonical, b"canonical").unwrap();
202 let test = match triple {
203 Some(triple) => cache.join(triple).join("debug/deps/integration-hash"),
204 None => cache.join("debug/deps/integration-hash"),
205 };
206 // The requested cross target must not select coexisting host output.
207 assert_eq!(resolve(None, None, &test, &target).unwrap(), canonical);
208 }
209 }
210
211 #[test]
212 fn per_unit_build_layout_resolves_the_final_target_and_cross_profile() {
213 let root = tempfile::TempDir::new().unwrap();
214 let target = root.path().join("final target");
215 let cache = root.path().join("build cache");
216 for triple in [None, Some("aarch64-unknown-linux-gnu")] {
217 let final_profile = match triple {
218 Some(triple) => target.join(triple).join("debug"),
219 None => target.join("debug"),
220 };
221 std::fs::create_dir_all(&final_profile).unwrap();
222 let canonical =
223 final_profile.join(format!("codewhale{}", std::env::consts::EXE_SUFFIX));
224 std::fs::write(&canonical, b"canonical").unwrap();
225 let build_profile = match triple {
226 Some(triple) => cache.join(triple).join("debug"),
227 None => cache.join("debug"),
228 };
229 for profile_dir in [&final_profile, &build_profile] {
230 let unit = profile_dir
231 .join("build")
232 .join("codewhale-tui")
233 .join("0123456789abcdef")
234 .join("out");
235 std::fs::create_dir_all(&unit).unwrap();
236 let test = unit.join("integration-hash");
237 assert_eq!(resolve(None, None, &test, &target).unwrap(), canonical);
238 }
239 }
240 }
241 }
242
242 lines RUST