Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: init more scalable unit tests #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ license = "MIT"
description = "Some functions to modify NixOS configuration files"
repository = "https://github.com/vlinkz/nix-editor/"
readme = "README.md"
include = [
"src/*",
"Cargo.toml",
"LICENSE*",
"README.md",
]
include = ["src/*", "Cargo.toml", "LICENSE*", "README.md"]

[dependencies]
clap = { version = "4.1", features = ["derive"] }
clap = { version = "4.1", features = ["derive"] }
rnix = "0.11"
owo-colors = "3.5"
thiserror = "1.0"
nixpkgs-fmt = "1.3"

[dev-dependencies]
expect-test = "1.4.0"
# serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub mod parse;
pub mod read;
#[cfg(test)]
mod tests;
pub mod write;

#[cfg(test)]
mod tests;
mod unit_test;
50 changes: 50 additions & 0 deletions src/unit_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::write::write;
use expect_test::expect_file;
use std::{ffi::OsStr, fs, path::PathBuf};

fn dir_tests<F>(dir: &str, get_actual: F)
where
F: Fn(String, Vec<String>) -> String,
{
let base_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "test_data", dir]
.iter()
.collect();
let tests = base_path.read_dir().unwrap();
for test_case in tests {
let case_folder = test_case.unwrap().path();
let entries = base_path.join(&case_folder).read_dir().unwrap();

for entry in entries {
let path = entry.unwrap().path();

if path.extension() != Some(OsStr::new("nix")) {
continue;
}

println!("testing: {}", path.display());

let mut code = fs::read_to_string(&path).unwrap();
if code.ends_with('\n') {
code.truncate(code.len() - 1);
}
let args_path = path.with_extension("args");
let args_raw = fs::read_to_string(&args_path).unwrap();
let args: Vec<String> = serde_json::from_str(&args_raw).unwrap();

let actual = get_actual(code, args);
expect_file![path.with_extension("expect")].assert_eq(&actual);
}
}
}

#[test]
fn write_dir_tests() {
dir_tests("write", |code: String, args| {
let actual = match write(&code, &args[0], &args[1]) {
Ok(s) => s,
Err(_) => panic!("Failed to write to file"),
};

actual
})
}
1 change: 1 addition & 0 deletions test_data/write/attrs/empty_attrs.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a","1"]
3 changes: 3 additions & 0 deletions test_data/write/attrs/empty_attrs.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
a = 1;
}
2 changes: 2 additions & 0 deletions test_data/write/attrs/empty_attrs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions test_data/write/attrs/existing_attrs.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a","1"]
4 changes: 4 additions & 0 deletions test_data/write/attrs/existing_attrs.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
b = 2;
a = 1;
}
3 changes: 3 additions & 0 deletions test_data/write/attrs/existing_attrs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
b = 2;
}
1 change: 1 addition & 0 deletions test_data/write/attrs/nested_attrs.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a.b","{}"]
3 changes: 3 additions & 0 deletions test_data/write/attrs/nested_attrs.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
a.b = {};
}
2 changes: 2 additions & 0 deletions test_data/write/attrs/nested_attrs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions test_data/write/func/empty_func.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a","1"]
3 changes: 3 additions & 0 deletions test_data/write/func/empty_func.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{lib, config, ...}:{
a = 1;
}
2 changes: 2 additions & 0 deletions test_data/write/func/empty_func.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{lib, config, ...}:{
}
1 change: 1 addition & 0 deletions test_data/write/let_in/let.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a","1"]
5 changes: 5 additions & 0 deletions test_data/write/let_in/let.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let
res = {
a = 1;
};
in res
4 changes: 4 additions & 0 deletions test_data/write/let_in/let.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let
res = {
};
in res
1 change: 1 addition & 0 deletions test_data/write/values/int.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a","1"]
3 changes: 3 additions & 0 deletions test_data/write/values/int.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
a = 1;
}
2 changes: 2 additions & 0 deletions test_data/write/values/int.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}