Skip to content

Commit ebfe8ee

Browse files
committed
test(xtask/preset_index): cover run's create and write failure paths — surfacing the create context when a directory component is a regular file and the write context when the index path is pre-created as a directory, with a shared unique_temp_dir helper tagging each case
1 parent 037092b commit ebfe8ee

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

‎xtask/src/preset_index.rs‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,21 @@ pub fn run(out_dir: &Path) -> Result<()> {
5555
mod tests {
5656
use super::*;
5757

58-
#[test]
59-
fn run_writes_an_entry_for_every_template() {
58+
fn unique_temp_dir(tag: &str) -> std::path::PathBuf {
6059
use std::time::{SystemTime, UNIX_EPOCH};
6160
let unique = SystemTime::now()
6261
.duration_since(UNIX_EPOCH)
6362
.unwrap()
6463
.as_nanos();
65-
let dir = std::env::temp_dir().join(format!(
66-
"splashboard-preset-index-{unique}-{}",
64+
std::env::temp_dir().join(format!(
65+
"splashboard-preset-index-{tag}-{unique}-{}",
6766
std::process::id()
68-
));
67+
))
68+
}
69+
70+
#[test]
71+
fn run_writes_an_entry_for_every_template() {
72+
let dir = unique_temp_dir("write");
6973

7074
run(&dir).unwrap();
7175
let body = fs::read_to_string(dir.join("_presets.json")).unwrap();
@@ -78,4 +82,34 @@ mod tests {
7882
}
7983
let _ = fs::remove_dir_all(&dir);
8084
}
85+
86+
#[test]
87+
fn run_surfaces_create_dir_failure() {
88+
// A regular file where a directory component is expected — `create_dir_all` on a
89+
// child path fails, and `run` should surface it with the `create` context.
90+
let blocker = unique_temp_dir("create-fail");
91+
fs::write(&blocker, "not a directory").unwrap();
92+
93+
let err = run(&blocker.join("child")).unwrap_err();
94+
assert!(
95+
err.to_string().contains("create"),
96+
"unexpected error: {err}"
97+
);
98+
99+
let _ = fs::remove_file(&blocker);
100+
}
101+
102+
#[test]
103+
fn run_surfaces_write_failure() {
104+
// Pre-create the index path as a directory so `fs::write` cannot replace it — `run`
105+
// should surface the failure with the `write` context.
106+
let dir = unique_temp_dir("write-fail");
107+
fs::create_dir_all(&dir).unwrap();
108+
fs::create_dir(dir.join("_presets.json")).unwrap();
109+
110+
let err = run(&dir).unwrap_err();
111+
assert!(err.to_string().contains("write"), "unexpected error: {err}");
112+
113+
let _ = fs::remove_dir_all(&dir);
114+
}
81115
}

0 commit comments

Comments
 (0)