ic_testkit/artifacts/
icp.rs1use std::{ffi::OsString, fs, io, path::Path};
2
3use super::digest::{InputDigest, digest_labeled_paths, read_stamp_with_limit, write_atomic};
4
5const WATCHED_INPUT_STAMP_VERSION: &str = "ic-testkit-watched-input-v1";
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct WatchedInputSnapshot {
14 digest: InputDigest,
15}
16
17impl WatchedInputSnapshot {
18 pub fn capture(workspace_root: &Path, watched_relative_paths: &[&str]) -> io::Result<Self> {
23 let paths = watched_relative_paths
24 .iter()
25 .map(|relative| (Path::new(relative), workspace_root.join(relative)));
26 Ok(Self {
27 digest: digest_labeled_paths("watched-inputs-v1", paths, &[])?,
28 })
29 }
30
31 #[must_use]
33 pub const fn digest(self) -> InputDigest {
34 self.digest
35 }
36
37 pub fn artifact_is_fresh(self, artifact_path: &Path) -> io::Result<bool> {
48 let metadata = fs::metadata(artifact_path)?;
49 if !metadata.is_file() || metadata.len() == 0 {
50 return Ok(false);
51 }
52
53 let expected = self.stamp_contents();
54 match read_stamp_with_limit(&watched_input_stamp_path(artifact_path), expected.len()) {
55 Ok(stamp) => Ok(stamp.is_some_and(|contents| contents == expected)),
56 Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
57 Err(error) => Err(error),
58 }
59 }
60
61 pub fn mark_artifact_fresh(self, artifact_path: &Path) -> io::Result<()> {
67 let metadata = fs::metadata(artifact_path)?;
68 if !metadata.is_file() || metadata.len() == 0 {
69 return Err(io::Error::new(
70 io::ErrorKind::InvalidInput,
71 format!(
72 "cannot stamp missing or empty artifact: {}",
73 artifact_path.display()
74 ),
75 ));
76 }
77
78 write_atomic(
79 &watched_input_stamp_path(artifact_path),
80 self.stamp_contents().as_bytes(),
81 )
82 }
83
84 fn stamp_contents(self) -> String {
85 format!("{WATCHED_INPUT_STAMP_VERSION}\nsha256:{}\n", self.digest)
86 }
87}
88
89#[must_use]
91pub fn icp_artifact_ready_for_build(
92 workspace_root: &Path,
93 artifact_relative_path: &str,
94 watched_relative_paths: &[&str],
95) -> bool {
96 let Ok(watched_inputs) = WatchedInputSnapshot::capture(workspace_root, watched_relative_paths)
97 else {
98 return false;
99 };
100
101 icp_artifact_ready_with_snapshot(workspace_root, artifact_relative_path, watched_inputs)
102}
103
104#[must_use]
106pub fn icp_artifact_ready_with_snapshot(
107 workspace_root: &Path,
108 artifact_relative_path: &str,
109 watched_inputs: WatchedInputSnapshot,
110) -> bool {
111 let artifact_path = workspace_root.join(artifact_relative_path);
112
113 watched_inputs
114 .artifact_is_fresh(&artifact_path)
115 .unwrap_or(false)
116}
117
118fn watched_input_stamp_path(artifact_path: &Path) -> std::path::PathBuf {
119 let mut stamp_name = artifact_path
120 .file_name()
121 .map_or_else(|| OsString::from("artifact"), OsString::from);
122 stamp_name.push(".ic-testkit-input");
123 artifact_path.with_file_name(stamp_name)
124}
125
126#[cfg(test)]
127mod tests {
128 use super::WatchedInputSnapshot;
129 use super::icp_artifact_ready_for_build;
130 use crate::artifacts::test_support::unique_temp_directory;
131 use std::{fs, path::PathBuf};
132
133 fn temp_workspace() -> PathBuf {
134 let path = unique_temp_directory("icp-artifact-test");
135 fs::create_dir_all(path.join(".icp/local/canisters/counter"))
136 .expect("create temp workspace");
137 path
138 }
139
140 #[test]
141 fn icp_artifact_ready_requires_matching_content_stamp() {
142 let workspace_root = temp_workspace();
143 let artifact_relative_path = ".icp/local/canisters/counter/counter.wasm.gz";
144 let artifact_path = workspace_root.join(artifact_relative_path);
145 fs::write(workspace_root.join("Cargo.toml"), "workspace").expect("write watched input");
146 fs::write(&artifact_path, b"wasm").expect("write artifact");
147
148 assert!(!icp_artifact_ready_for_build(
149 &workspace_root,
150 artifact_relative_path,
151 &["Cargo.toml"],
152 ));
153
154 let snapshot = WatchedInputSnapshot::capture(&workspace_root, &["Cargo.toml"])
155 .expect("capture exact watched inputs");
156 snapshot
157 .mark_artifact_fresh(&artifact_path)
158 .expect("stamp artifact inputs");
159 assert!(icp_artifact_ready_for_build(
160 &workspace_root,
161 artifact_relative_path,
162 &["Cargo.toml"],
163 ));
164
165 fs::write(workspace_root.join("Cargo.toml"), "changed").expect("update watched input");
166 assert!(!icp_artifact_ready_for_build(
167 &workspace_root,
168 artifact_relative_path,
169 &["Cargo.toml"],
170 ));
171
172 let changed = WatchedInputSnapshot::capture(&workspace_root, &["Cargo.toml"])
173 .expect("capture changed watched inputs");
174 assert_ne!(snapshot.digest(), changed.digest());
175
176 let _ = fs::remove_dir_all(workspace_root);
177 }
178
179 #[test]
180 fn watched_input_digest_ignores_checkout_root_and_input_order() {
181 let first_root = temp_workspace();
182 let second_root = temp_workspace();
183 for root in [&first_root, &second_root] {
184 fs::create_dir_all(root.join("src")).expect("create watched source directory");
185 fs::write(root.join("Cargo.toml"), "[workspace]").expect("write manifest input");
186 fs::write(root.join("src/lib.rs"), "pub fn value() -> u8 { 7 }")
187 .expect("write source input");
188 }
189
190 let first = WatchedInputSnapshot::capture(&first_root, &["Cargo.toml", "src"])
191 .expect("capture first checkout");
192 let second = WatchedInputSnapshot::capture(&second_root, &["src", "Cargo.toml"])
193 .expect("capture second checkout");
194 assert_eq!(first.digest(), second.digest());
195
196 let _ = fs::remove_dir_all(first_root);
197 let _ = fs::remove_dir_all(second_root);
198 }
199
200 #[test]
201 fn artifact_freshness_rejects_malformed_and_oversized_stamps() {
202 let root = temp_workspace();
203 let artifact = root.join("artifact.wasm");
204 fs::write(root.join("Cargo.toml"), "workspace").expect("write watched input");
205 fs::write(&artifact, b"wasm").expect("write artifact");
206 let snapshot =
207 WatchedInputSnapshot::capture(&root, &["Cargo.toml"]).expect("capture watched inputs");
208 let stamp_path = super::watched_input_stamp_path(&artifact);
209 let expected = snapshot.stamp_contents();
210
211 assert!(!snapshot.artifact_is_fresh(&artifact).unwrap());
212 for contents in [
213 String::new(),
214 expected[..expected.len() - 1].to_owned(),
215 expected.replacen("sha256:", "sha257:", 1),
216 format!("{expected}\n"),
217 ] {
218 fs::write(&stamp_path, contents).expect("write malformed stamp");
219 assert!(!snapshot.artifact_is_fresh(&artifact).unwrap());
220 }
221
222 fs::write(&stamp_path, [0xff]).expect("write invalid UTF-8 stamp");
223 assert_eq!(
224 snapshot.artifact_is_fresh(&artifact).unwrap_err().kind(),
225 std::io::ErrorKind::InvalidData,
226 );
227
228 fs::write(&stamp_path, &expected).expect("write matching prefix");
230 fs::OpenOptions::new()
231 .write(true)
232 .open(&stamp_path)
233 .expect("open oversized stamp")
234 .set_len(1024 * 1024 * 1024)
235 .expect("extend oversized stamp");
236 assert!(!snapshot.artifact_is_fresh(&artifact).unwrap());
237
238 snapshot.mark_artifact_fresh(&artifact).unwrap();
239 assert!(snapshot.artifact_is_fresh(&artifact).unwrap());
240 fs::remove_file(&stamp_path).expect("remove stamp");
241 fs::create_dir(&stamp_path).expect("replace stamp with unreadable directory");
242 assert!(snapshot.artifact_is_fresh(&artifact).is_err());
243 assert!(!icp_artifact_ready_for_build(
244 &root,
245 "artifact.wasm",
246 &["Cargo.toml"],
247 ));
248 let _ = fs::remove_dir_all(root);
249 }
250}