Skip to content

Commit 582a811

Browse files
committed
test(xtask/themes): cover pretty_name's None arm for empty underscore segments rendering as blank words, theme_for's unwrap_or_default fallback materialising the default theme for an unknown slug, and render_one's happy path plus both failure contexts: a missing demo config failing render_config_html_with_theme's read_to_string, and an output path pre-created as a directory surfacing the write context after a successful render
1 parent 545d511 commit 582a811

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

‎xtask/src/themes.rs‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ mod tests {
149149
assert_eq!(pretty_name("github_light"), "GitHub Light");
150150
}
151151

152+
#[test]
153+
fn pretty_name_keeps_empty_underscore_segments_as_blank_words() {
154+
// A leading / trailing `_` yields an empty `split('_')` segment whose `chars().next()`
155+
// is `None`, exercising the `None => String::new()` arm rather than the capitalize path.
156+
assert_eq!(pretty_name("foo_"), "Foo ");
157+
assert_eq!(pretty_name("_bar"), " Bar");
158+
}
159+
152160
#[test]
153161
fn ordered_slugs_lists_default_first_then_remaining_known_themes() {
154162
let order = ordered_slugs();
@@ -173,4 +181,94 @@ mod tests {
173181
let _ = theme_for(slug);
174182
}
175183
}
184+
185+
#[test]
186+
fn theme_for_falls_back_to_default_for_unknown_slug() {
187+
// `presets::by_name` misses → `unwrap_or_default()` materialises the default theme
188+
// rather than panicking, so a stale slug in the gallery still renders something.
189+
let _ = theme_for("not-a-real-theme");
190+
}
191+
192+
fn unique_temp_dir(tag: &str) -> std::path::PathBuf {
193+
use std::time::{SystemTime, UNIX_EPOCH};
194+
let unique = SystemTime::now()
195+
.duration_since(UNIX_EPOCH)
196+
.unwrap()
197+
.as_nanos();
198+
std::env::temp_dir().join(format!(
199+
"splashboard-themes-{tag}-{unique}-{}",
200+
std::process::id()
201+
))
202+
}
203+
204+
/// A minimal one-widget dashboard written to a temp file — enough for
205+
/// `render_config_html_with_theme` to parse, lay out, and snapshot without network or disk
206+
/// fetchers. Returns the path so the caller can pass it to `render_one` as the demo config.
207+
fn write_demo_dashboard(tag: &str) -> std::path::PathBuf {
208+
let path = unique_temp_dir(tag).with_extension("toml");
209+
fs::write(
210+
&path,
211+
r#"
212+
[[widget]]
213+
id = "hello"
214+
fetcher = "basic_static"
215+
format = "Hi"
216+
render = "list_plain"
217+
218+
[[row]]
219+
height = { length = 1 }
220+
[[row.child]]
221+
widget = "hello"
222+
"#,
223+
)
224+
.unwrap();
225+
path
226+
}
227+
228+
#[test]
229+
fn render_one_writes_theme_html_and_returns_entry() {
230+
let demo = write_demo_dashboard("render-ok");
231+
let out_dir = unique_temp_dir("render-ok-out");
232+
fs::create_dir_all(&out_dir).unwrap();
233+
234+
let entry = render_one("nord", &demo, &out_dir, 20, 6).unwrap();
235+
assert_eq!(entry.slug, "nord");
236+
assert_eq!(entry.name, "Nord");
237+
assert!(matches!(entry.kind, ThemeKind::Dark));
238+
239+
let html = fs::read_to_string(out_dir.join("nord.html")).unwrap();
240+
assert!(html.starts_with("<pre"), "unexpected html: {html}");
241+
242+
let _ = fs::remove_file(&demo);
243+
let _ = fs::remove_dir_all(&out_dir);
244+
}
245+
246+
#[test]
247+
fn render_one_surfaces_render_failure_from_missing_demo_config() {
248+
// A demo path that doesn't exist makes `render_config_html_with_theme`'s `read_to_string`
249+
// fail; `render_one` should propagate that error rather than writing a stray html file.
250+
let missing = unique_temp_dir("render-missing").with_extension("toml");
251+
let out_dir = unique_temp_dir("render-missing-out");
252+
fs::create_dir_all(&out_dir).unwrap();
253+
254+
let err = render_one("nord", &missing, &out_dir, 20, 6).unwrap_err();
255+
assert!(err.to_string().contains("read"), "unexpected error: {err}");
256+
257+
let _ = fs::remove_dir_all(&out_dir);
258+
}
259+
260+
#[test]
261+
fn render_one_surfaces_write_failure_when_output_path_is_a_directory() {
262+
// Pre-create `nord.html` as a directory so `fs::write` cannot replace it — `render_one`
263+
// should surface the failure with its `write` context after a successful render.
264+
let demo = write_demo_dashboard("render-write-fail");
265+
let out_dir = unique_temp_dir("render-write-fail-out");
266+
fs::create_dir_all(out_dir.join("nord.html")).unwrap();
267+
268+
let err = render_one("nord", &demo, &out_dir, 20, 6).unwrap_err();
269+
assert!(err.to_string().contains("write"), "unexpected error: {err}");
270+
271+
let _ = fs::remove_file(&demo);
272+
let _ = fs::remove_dir_all(&out_dir);
273+
}
176274
}

0 commit comments

Comments
 (0)