Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/artifacts
/target
/result
11 changes: 8 additions & 3 deletions nix/load.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ args:

let
src = toString (
args.src or (warn
"namaka.load: `flake` and `dir` have been deprecated, use `src` directly instead"
(args.flake + "/${args.dir or "tests"}"))
if args ? src then
args.src
else if args ? flake then
warn
"namaka.load: `flake` and `dir` have been deprecated, use `src` directly instead"
(args.flake + "/${args.dir or "tests"}")
else
throw "namaka.load: missing mandatory `src' argument"
);

tests = haumea.load (removeAttrs args [ "flake" "dir" ] // {
Expand Down
27 changes: 20 additions & 7 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs::{self, create_dir_all, remove_dir_all, File},
io::{stderr, BufRead, Write},
path::Path,
process::exit,
};

Expand All @@ -14,7 +15,7 @@ use crate::{
proto::{TestOutput, TestResult},
};

pub fn check(opts: Opts, cfg: Option<Config>) -> Result<()> {
pub fn check(root: &Path, opts: Opts, cfg: Option<Config>) -> Result<()> {
let output = nix_check(opts, cfg)?;
let success = output.status.success();

Expand All @@ -26,13 +27,14 @@ pub fn check(opts: Opts, cfg: Option<Config>) -> Result<()> {

let output = serde_json::from_str::<TestOutput>(line)?;

let pending = output.dir.join("_snapshots").join(".pending");
let pending = root.join(output.dir).join("_snapshots/.pending");
let _ = remove_dir_all(&pending);
create_dir_all(&pending)?;
fs::write(pending.join(".gitignore"), "*")?;

let total = output.results.len();
let mut failures = 0;
let mut additions = 0;
for (name, res) in output.results {
let new = pending.join(&name);
match res {
Expand All @@ -41,24 +43,35 @@ pub fn check(opts: Opts, cfg: Option<Config>) -> Result<()> {
}

TestResult::Failure { snapshot, old } => {
failures += 1;
println!("{} {name}", if old { "✘" } else { "🞥" }.red());
if old {
failures += 1;
println!("{} {name}", "✘".red());
} else {
additions += 1;
println!("{} {name}", "🞥".blue());
}
snapshot.to_writer(File::create(new)?)?;
}
}
}

if failures == 0 {
if failures == 0 && additions == 0 {
if success {
eprintln!("All {total} tests succeeded");
return Ok(());
} else {
break;
}
} else {
eprintln!("{failures} out of {total} tests failed");
if failures != 0 {
let existing = total - additions;
eprintln!("{failures} out of {existing} tests failed");
}
if additions != 0 {
eprintln!("{additions} new tests found");
}
eprintln!("run `namaka review` to review the pending snapshots");
exit(1);
exit(if failures != 0 { 1 } else { 2 });
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/cmd/clean.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
fs::{read_dir, remove_dir_all, remove_file},
path::Path,
io::{stderr, BufRead, Write},
};

Expand All @@ -8,7 +9,7 @@ use owo_colors::OwoColorize;

use crate::{cfg::Config, cli::Opts, cmd::run::nix_eval, proto::TestOutput};

pub fn clean(opts: Opts, cfg: Option<Config>) -> Result<()> {
pub fn clean(root: &Path, opts: Opts, cfg: Option<Config>) -> Result<()> {
let output = nix_eval(opts, cfg)?;

for line in output.stderr.lines() {
Expand All @@ -19,7 +20,7 @@ pub fn clean(opts: Opts, cfg: Option<Config>) -> Result<()> {

let mut out = stderr().lock();
let output = serde_json::from_str::<TestOutput>(line)?;
let snapshots = output.dir.join("_snapshots");
let snapshots = root.join(output.dir).join("_snapshots");

for entry in read_dir(snapshots)? {
let entry = entry?;
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
proto::{Snapshot, TestOutput},
};

pub fn review(opts: Opts, cfg: Option<Config>) -> Result<()> {
pub fn review(root: &Path, opts: Opts, cfg: Option<Config>) -> Result<()> {
let output = nix_eval(opts, cfg)?;
let _ = ctrlc::set_handler(|| {
let mut term = Term::stderr();
Expand All @@ -36,7 +36,7 @@ pub fn review(opts: Opts, cfg: Option<Config>) -> Result<()> {
};

let output = serde_json::from_str::<TestOutput>(line)?;
let snapshots = output.dir.join("_snapshots");
let snapshots = root.join(output.dir).join("_snapshots");

for entry in read_dir(snapshots.join(".pending"))? {
use bstr::ByteSlice;
Expand Down
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ fn main() -> Result<()> {

let cfg = cfg::load()?;

let root = repo_root()?;

match opts.subcmd {
Subcommand::Check => check(opts, cfg),
Subcommand::Clean => clean(opts, cfg),
Subcommand::Review => review(opts, cfg),
Subcommand::Check => check(&root, opts, cfg),
Subcommand::Clean => clean(&root, opts, cfg),
Subcommand::Review => review(&root, opts, cfg),
}
}

fn repo_root() -> Result<std::path::PathBuf> {
let mut cmd = std::process::Command::new("git");
cmd.args(["rev-parse", "--show-toplevel"]);

let out = cmd.output()?;
let str = std::str::from_utf8(&out.stdout)?;
let str = str.trim_end();

Ok(str.to_owned().into())
}