| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use std::path::{Path, PathBuf}; |
| 9 |
|
| 10 |
use makeover::Rgb; |
| 11 |
use skelgen::{Palette, render, theme_directive}; |
| 12 |
|
| 13 |
fn repo() -> PathBuf { |
| 14 |
|
| 15 |
Path::new(env!("CARGO_MANIFEST_DIR")) |
| 16 |
.ancestors() |
| 17 |
.nth(2) |
| 18 |
.expect("skelgen lives two levels under the repo root") |
| 19 |
.to_path_buf() |
| 20 |
} |
| 21 |
|
| 22 |
fn palette(id: &str) -> Palette { |
| 23 |
let dir = makeover::bundled_themes_dir().expect("makeover bundles its themes"); |
| 24 |
let theme = makeover::load_theme(&[(dir, false)], id).unwrap_or_else(|e| panic!("{id}: {e}")); |
| 25 |
Palette::new(id, &theme).unwrap_or_else(|e| panic!("{id}: {e}")) |
| 26 |
} |
| 27 |
|
| 28 |
fn templates() -> Vec<PathBuf> { |
| 29 |
fn walk(dir: &Path, out: &mut Vec<PathBuf>) { |
| 30 |
for entry in std::fs::read_dir(dir).expect("template tree is readable") { |
| 31 |
let path = entry.expect("readable entry").path(); |
| 32 |
if path.is_dir() { |
| 33 |
walk(&path, out); |
| 34 |
} else if path.extension().is_some_and(|e| e == "in") { |
| 35 |
out.push(path); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
let mut out = Vec::new(); |
| 40 |
walk(&repo().join("templates"), &mut out); |
| 41 |
out.sort(); |
| 42 |
assert!(!out.is_empty(), "found no templates under templates/"); |
| 43 |
out |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#[test] |
| 52 |
fn the_whole_skeleton_renders() { |
| 53 |
let default = palette("akari-dawn"); |
| 54 |
let night = palette("akari-night"); |
| 55 |
|
| 56 |
for path in templates() { |
| 57 |
let text = std::fs::read_to_string(&path).expect("template is readable"); |
| 58 |
let (directive, body) = |
| 59 |
theme_directive(&text).unwrap_or_else(|e| panic!("{}: {e:#}", path.display())); |
| 60 |
let target = path.to_string_lossy().trim_end_matches(".in").to_string(); |
| 61 |
let renders = directive |
| 62 |
.renders(&target) |
| 63 |
.unwrap_or_else(|e| panic!("{}: {e:#}", path.display())); |
| 64 |
|
| 65 |
for (theme, out_path) in renders { |
| 66 |
let palette = match theme.as_str() { |
| 67 |
"default" => &default, |
| 68 |
"night" => &night, |
| 69 |
other => panic!("{}: unknown theme `{other}`", path.display()), |
| 70 |
}; |
| 71 |
let out = render(&body, palette).unwrap_or_else(|e| panic!("{out_path}: {e:#}")); |
| 72 |
assert!( |
| 73 |
!out.contains("@{"), |
| 74 |
"{out_path}: an expression survived rendering" |
| 75 |
); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#[test] |
| 95 |
fn no_template_assigns_a_hex_literal() { |
| 96 |
for path in templates() { |
| 97 |
let text = std::fs::read_to_string(&path).expect("template is readable"); |
| 98 |
for (n, line) in text.lines().enumerate() { |
| 99 |
let code = line.split('#').next().unwrap_or(""); |
| 100 |
let is_comment = |
| 101 |
line.trim_start().starts_with('#') || line.trim_start().starts_with("/*"); |
| 102 |
if is_comment || !code.contains('=') { |
| 103 |
continue; |
| 104 |
} |
| 105 |
let word = |w: &str| { |
| 106 |
w.trim_matches(|c: char| !c.is_ascii_alphanumeric() && c != '#') |
| 107 |
.to_string() |
| 108 |
}; |
| 109 |
let prefixed = line |
| 110 |
.split_whitespace() |
| 111 |
.find(|w| word(w).len() == 7 && w.contains('#')); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
let bare = code.split_whitespace().map(word).find(|w| { |
| 117 |
matches!(w.len(), 6 | 8) |
| 118 |
&& w.bytes().all(|b| b.is_ascii_hexdigit()) |
| 119 |
&& w.bytes().any(|b| b.is_ascii_alphabetic()) |
| 120 |
}); |
| 121 |
assert!( |
| 122 |
prefixed.is_none() && bare.is_none(), |
| 123 |
"{}:{}: hex literal in an assignment: {line}", |
| 124 |
path.display(), |
| 125 |
n + 1 |
| 126 |
); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[test] |
| 138 |
fn every_helix_foreground_reads_against_its_background() { |
| 139 |
for (theme, file) in [ |
| 140 |
("akari-dawn", "akari-dawn.toml.in"), |
| 141 |
("akari-night", "akari-night.toml.in"), |
| 142 |
] { |
| 143 |
let path = repo() |
| 144 |
.join("templates/etc/skel/.config/helix/themes") |
| 145 |
.join(file); |
| 146 |
let text = std::fs::read_to_string(&path).expect("helix template is readable"); |
| 147 |
let (_, body) = theme_directive(&text).expect("helix template has a valid directive"); |
| 148 |
let out = render(&body, &palette(theme)).expect("helix template renders"); |
| 149 |
|
| 150 |
let entries = palette_entries(&out); |
| 151 |
let background = entries |
| 152 |
.iter() |
| 153 |
.find(|(k, _)| k == "background") |
| 154 |
.map(|(_, v)| *v) |
| 155 |
.expect("the palette declares a background"); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
let used = foreground_names(&out); |
| 163 |
assert!( |
| 164 |
used.len() > 10, |
| 165 |
"{theme}: only found {} foreground names; the parser is wrong", |
| 166 |
used.len() |
| 167 |
); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
let decorative = ["border", "yellow", "amber"]; |
| 185 |
|
| 186 |
for (key, value) in &entries { |
| 187 |
if !used.contains(key) || decorative.contains(&key.as_str()) { |
| 188 |
continue; |
| 189 |
} |
| 190 |
let contrast = makeover::wcag_contrast(*value, background); |
| 191 |
assert!( |
| 192 |
contrast >= 3.0, |
| 193 |
"{theme}: `{key}` = {} is only {contrast:.2}:1 on the background", |
| 194 |
value.to_hex() |
| 195 |
); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
fn foreground_names(rendered: &str) -> Vec<String> { |
| 207 |
let rules = rendered |
| 208 |
.split_once("[palette]") |
| 209 |
.map_or(rendered, |(rules, _)| rules); |
| 210 |
let mut names = Vec::new(); |
| 211 |
for line in rules.lines() { |
| 212 |
let line = line.trim(); |
| 213 |
if line.starts_with('#') || !line.contains('=') { |
| 214 |
continue; |
| 215 |
} |
| 216 |
let Some((_, value)) = line.split_once('=') else { |
| 217 |
continue; |
| 218 |
}; |
| 219 |
let value = value.trim(); |
| 220 |
if let Some(inner) = value.strip_prefix('{').and_then(|v| v.strip_suffix('}')) { |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
if inner.contains("bg") { |
| 227 |
continue; |
| 228 |
} |
| 229 |
for field in inner.split(',') { |
| 230 |
let Some((key, name)) = field.split_once('=') else { |
| 231 |
continue; |
| 232 |
}; |
| 233 |
if key.trim() == "fg" |
| 234 |
&& let Some(name) = name.split('"').nth(1) |
| 235 |
{ |
| 236 |
names.push(name.to_string()); |
| 237 |
} |
| 238 |
} |
| 239 |
} else if let Some(name) = value.split('"').nth(1) { |
| 240 |
|
| 241 |
names.push(name.to_string()); |
| 242 |
} |
| 243 |
} |
| 244 |
names.sort(); |
| 245 |
names.dedup(); |
| 246 |
names |
| 247 |
} |
| 248 |
|
| 249 |
fn palette_entries(rendered: &str) -> Vec<(String, Rgb)> { |
| 250 |
let (_, palette) = rendered |
| 251 |
.split_once("[palette]") |
| 252 |
.expect("a helix theme ends with its palette"); |
| 253 |
palette |
| 254 |
.lines() |
| 255 |
.filter_map(|line| { |
| 256 |
let line = line.trim(); |
| 257 |
if line.starts_with('#') { |
| 258 |
return None; |
| 259 |
} |
| 260 |
let (key, value) = line.split_once('=')?; |
| 261 |
let value = value.trim().trim_matches('"'); |
| 262 |
Some((key.trim().to_string(), Rgb::from_hex(value)?)) |
| 263 |
}) |
| 264 |
.collect() |
| 265 |
} |
| 266 |
|