-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
- Loading branch information
1 parent
18054da
commit d133bdc
Showing
9 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
6 changes: 6 additions & 0 deletions
6
crates/jsonschema-cli/tests/snapshots/cli__invalid_instance.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
5 changes: 5 additions & 0 deletions
5
crates/jsonschema-cli/tests/snapshots/cli__invalid_schema.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
crates/jsonschema-cli/tests/snapshots/cli__multiple_instances.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
--- | ||
|
5 changes: 5 additions & 0 deletions
5
crates/jsonschema-cli/tests/snapshots/cli__valid_instance.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |