use std::path::{Path, PathBuf};
struct Scratch(PathBuf);
impl Scratch {
fn new(name: &str) -> Self {
let path = std::env::temp_dir().join(format!("origin-kit-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).expect("cannot create the scratch directory");
Self(path)
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn no_template_file_is_named_cargo_toml() {
let offenders: Vec<&str> = origin_kit::template_files()
.iter()
.map(|(path, _)| *path)
.filter(|path| {
Path::new(path)
.file_name()
.is_some_and(|name| name == "Cargo.toml")
})
.collect();
assert!(
offenders.is_empty(),
"these template manifests must be stored with the `.in` suffix, or `cargo \
package` will drop their directories: {offenders:?}"
);
}
#[test]
fn the_template_is_embedded() {
let paths: Vec<&str> = origin_kit::template_files()
.iter()
.map(|(path, _)| *path)
.collect();
for expected in [
"Cargo.toml.in",
"app.toml",
"src-tauri/Cargo.toml.in",
"src-tauri/src/lib.rs",
"src-tauri/icons/icon.icns",
"xtask/Cargo.toml.in",
"ui/src/App.svelte",
".github/workflows/ci.yml",
".github/workflows/release.yml",
".github/dependabot.yml",
".cargo/config.toml",
".gitignore",
] {
assert!(paths.contains(&expected), "{expected} is not embedded");
}
}
#[test]
fn a_scaffolded_project_has_no_placeholders_left() {
let scratch = Scratch::new("scaffold");
let options = origin_kit::Options {
slug: "sample-app".to_owned(),
name: "Sample App".to_owned(),
id: "dev.example.sampleapp".to_owned(),
into: scratch.0.clone(),
local: None,
};
origin_kit::run(&options).expect("scaffolding failed");
let project = scratch.0.join("sample-app");
for manifest in ["Cargo.toml", "src-tauri/Cargo.toml", "xtask/Cargo.toml"] {
assert!(project.join(manifest).is_file(), "{manifest} is missing");
assert!(
!project.join(format!("{manifest}.in")).exists(),
"{manifest}.in was written as well"
);
}
let icon = project.join("src-tauri").join("icons").join("icon.icns");
assert!(
std::fs::metadata(&icon).expect("no icon").len() > 0,
"the icon is empty"
);
for file in text_files(&project) {
let contents = std::fs::read_to_string(&file).expect("cannot read a generated file");
assert!(
leftover_placeholder(&contents).is_none(),
"{} still contains the placeholder {}",
file.display(),
leftover_placeholder(&contents).unwrap_or_default()
);
}
let manifest = std::fs::read_to_string(project.join("Cargo.toml")).expect("no Cargo.toml");
assert!(manifest.contains("origin-app"));
assert!(
manifest.parse::<toml_edit::DocumentMut>().is_ok(),
"the generated Cargo.toml is not valid TOML"
);
let app = std::fs::read_to_string(project.join("app.toml")).expect("no app.toml");
assert!(app.contains("dev.example.sampleapp"));
assert!(app.contains("\"Sample App\""));
}
fn text_files(directory: &Path) -> Vec<PathBuf> {
let binary = ["png", "ico", "icns", "jpg", "jpeg", "webp"];
let mut files = Vec::new();
for entry in std::fs::read_dir(directory)
.expect("cannot read the project")
.flatten()
{
let path = entry.path();
if path.is_dir() {
files.extend(text_files(&path));
continue;
}
let is_binary = path
.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| binary.contains(&extension));
if !is_binary {
files.push(path);
}
}
files
}
fn leftover_placeholder(contents: &str) -> Option<String> {
let bytes = contents.as_bytes();
let mut start = 0;
while let Some(index) = contents[start..].find("__") {
let open = start + index + 2;
let length = contents[open..].find("__")?;
let token = &bytes[open..open + length];
let shouting = length >= 2
&& token
.iter()
.all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit() || *byte == b'_');
if shouting {
return Some(format!("__{}__", &contents[open..open + length]));
}
start = open;
}
None
}