Skip to content

Commit

Permalink
test: Add file for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Aug 7, 2023
1 parent c835d42 commit ff2ec57
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Cargo.lock linguist-generated=true
flake.lock linguist-generated=true

tests/*.txt linguist-generated=true
resources/test/* linguist-generated=true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ target/
.vscode/

.direnv/

resources/test/tmp/
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "man_completions"
name = "man-completions"
version = "0.1.0"
edition = "2021"

Expand All @@ -12,5 +12,5 @@ flate2 = "1.0"
regex = "1.9"

[[bin]]
name = "man_completions"
name = "man-completions"
path = "src/main.rs"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# man_completions
# man-completions

Parse manpages to get completions for Zsh, Bash, and Nu

Expand Down
7 changes: 7 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tests

In the [`in`](./resources/in) folder are manpages for whom completions will be
generated, as part of the integration tests. The expected outputs will be in the
[`expected`](./resources/expected) folder. The [`tmp`](./resources/tmp) folder
will hold the completions generated by the tests, which will then be compared to
the files in the `expected` folder.
61 changes: 61 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::{env, fs, path::PathBuf, process::Command};

#[test]
fn test() {
// The project's root directory
let root = env::var("CARGO_MANIFEST_DIR").unwrap();

let test_resources = PathBuf::from(root).join("tests/resources");
let in_dir = test_resources.join("in");
let expected_dir = test_resources.join("expected");
let out_dir = test_resources.join("tmp");

// The man-completions binary to test
let bin = env::var("CARGO_BIN_EXE_man-completions").unwrap();
let status = Command::new(bin).env("MANPATH", in_dir).status().unwrap();
assert!(status.success());

// Files that didn't get generated
let mut not_generated = Vec::new();
// Files that don't match the expected contents
let mut not_match = Vec::new();
for exp_file in fs::read_dir(&expected_dir).unwrap() {
let exp_file = exp_file.unwrap();
let file_name = exp_file.file_name().to_string_lossy().to_string();
let got_file = out_dir.join(&file_name);
if !got_file.exists() {
not_generated.push(file_name);
continue;
}

let expected = fs::read(exp_file.path()).unwrap();
let got = fs::read(got_file).unwrap();
if expected != got {
not_match.push(file_name);
}
}

if !not_generated.is_empty() {
println!("The following files weren't generated:");
for file_name in &not_generated {
println!("- {file_name}");
}
}

if !not_match.is_empty() {
println!("The following files didn't match what was expected:");
for file_name in &not_match {
let exp = expected_dir.join(&file_name);
let exp = exp.to_string_lossy();
let got = out_dir.join(&file_name);
let got = got.to_string_lossy();
println!("Test for {file_name} failed: contents of {got} did not match those of {exp}");
println!("To see the diff, run `diff {exp} {got}`");
println!("To overwrite the expected file, run `cp {got} {exp}`")
}
}

if !not_generated.is_empty() || !not_match.is_empty() {
assert!(false);
}
}
File renamed without changes.

0 comments on commit ff2ec57

Please sign in to comment.