Skip to content

Commit

Permalink
test: Add tests for CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed Sep 18, 2024
1 parent 18054da commit d133bdc
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ lint-py:

lint: lint-rs lint-py

test-rs *FLAGS:
cargo llvm-cov --html test {{FLAGS}}

test-py *FLAGS:
uvx --with="crates/jsonschema-py[tests]" pytest crates/jsonschema-py/tests-py -rs {{FLAGS}}

Expand Down
5 changes: 5 additions & 0 deletions crates/jsonschema-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ path = "src/main.rs"

[lints]
workspace = true

[dev-dependencies]
assert_cmd = "2.0.13"
insta = "1.40.0"
tempfile = "3.12.0"
128 changes: 128 additions & 0 deletions crates/jsonschema-cli/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use assert_cmd::Command;
use insta::assert_snapshot;
use std::fs;
use tempfile::tempdir;

fn cli() -> Command {
Command::cargo_bin("jsonschema-cli").unwrap()
}

fn create_temp_file(dir: &tempfile::TempDir, name: &str, content: &str) -> String {
let file_path = dir.path().join(name);
fs::write(&file_path, content).unwrap();
file_path.to_str().unwrap().to_string()
}

fn sanitize_output(output: String, file_names: &[&str]) -> String {
let mut sanitized = output;
for (i, name) in file_names.iter().enumerate() {
sanitized = sanitized.replace(name, &format!("{{FILE_{}}}", i + 1));
}
sanitized
}

#[test]
fn test_version() {
let mut cmd = cli();
cmd.arg("--version");
let output = cmd.output().unwrap();
assert!(output.status.success());
assert_snapshot!(String::from_utf8_lossy(&output.stdout));
}

#[test]
fn test_valid_instance() {
let dir = tempdir().unwrap();
let schema = create_temp_file(
&dir,
"schema.json",
r#"{"type": "object", "properties": {"name": {"type": "string"}}}"#,
);
let instance = create_temp_file(&dir, "instance.json", r#"{"name": "John Doe"}"#);

let mut cmd = cli();
cmd.arg(&schema).arg("--instance").arg(&instance);
let output = cmd.output().unwrap();
assert!(output.status.success());
let sanitized = sanitize_output(
String::from_utf8_lossy(&output.stdout).to_string(),
&[&instance],
);
assert_snapshot!(sanitized);
}

#[test]
fn test_invalid_instance() {
let dir = tempdir().unwrap();
let schema = create_temp_file(
&dir,
"schema.json",
r#"{"type": "object", "properties": {"name": {"type": "string"}}}"#,
);
let instance = create_temp_file(&dir, "instance.json", r#"{"name": 123}"#);

let mut cmd = cli();
cmd.arg(&schema).arg("--instance").arg(&instance);
let output = cmd.output().unwrap();
assert!(!output.status.success());
let sanitized = sanitize_output(
String::from_utf8_lossy(&output.stdout).to_string(),
&[&instance],
);
assert_snapshot!(sanitized);
}

#[test]
fn test_invalid_schema() {
let dir = tempdir().unwrap();
let schema = create_temp_file(&dir, "schema.json", r#"{"type": "invalid"}"#);
let instance = create_temp_file(&dir, "instance.json", r#"{}"#);

let mut cmd = cli();
cmd.arg(&schema).arg("--instance").arg(&instance);
let output = cmd.output().unwrap();
assert!(!output.status.success());
let sanitized = sanitize_output(
String::from_utf8_lossy(&output.stdout).to_string(),
&[&instance],
);
assert_snapshot!(sanitized);
}

#[test]
fn test_multiple_instances() {
let dir = tempdir().unwrap();
let schema = create_temp_file(
&dir,
"schema.json",
r#"{"type": "object", "properties": {"name": {"type": "string"}}}"#,
);
let instance1 = create_temp_file(&dir, "instance1.json", r#"{"name": "John Doe"}"#);
let instance2 = create_temp_file(&dir, "instance2.json", r#"{"name": 123}"#);

let mut cmd = cli();
cmd.arg(&schema)
.arg("--instance")
.arg(&instance1)
.arg("--instance")
.arg(&instance2);
let output = cmd.output().unwrap();
assert!(!output.status.success());
let sanitized = sanitize_output(
String::from_utf8_lossy(&output.stdout).to_string(),
&[&instance1, &instance2],
);
assert_snapshot!(sanitized);
}

#[test]
fn test_no_instances() {
let dir = tempdir().unwrap();
let schema = create_temp_file(&dir, "schema.json", r#"{"type": "object"}"#);

let mut cmd = cli();
cmd.arg(&schema);
let output = cmd.output().unwrap();
assert!(output.status.success());
assert_snapshot!(String::from_utf8_lossy(&output.stdout));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: crates/jsonschema-cli/tests/cli.rs
expression: sanitized
---
{FILE_1} - INVALID. Errors:
1. 123 is not of type "string"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/jsonschema-cli/tests/cli.rs
expression: sanitized
---
Schema is invalid. Error: "invalid" is not valid under any of the schemas listed in the 'anyOf' keyword
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/jsonschema-cli/tests/cli.rs
expression: sanitized
---
{FILE_1} - VALID
{FILE_2} - INVALID. Errors:
1. 123 is not of type "string"
5 changes: 5 additions & 0 deletions crates/jsonschema-cli/tests/snapshots/cli__no_instances.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/jsonschema-cli/tests/cli.rs
expression: "String::from_utf8_lossy(&output.stdout)"
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/jsonschema-cli/tests/cli.rs
expression: sanitized
---
{FILE_1} - VALID
5 changes: 5 additions & 0 deletions crates/jsonschema-cli/tests/snapshots/cli__version.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/jsonschema-cli/tests/cli.rs
expression: "String::from_utf8_lossy(&output.stdout)"
---
Version: 0.19.1

0 comments on commit d133bdc

Please sign in to comment.