Skip to content

Commit d519373

Browse files
committed
Test all schemas
1 parent 30c2acf commit d519373

File tree

4 files changed

+80
-71
lines changed

4 files changed

+80
-71
lines changed

schema/v2/semver.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"1.3.6",
1111
"10.20.30",
1212
"1.0.0-alpha",
13-
"1.0.0-alpha-a.b-c-something-long+build.1-aef.1-its-okay"
13+
"1.0.0-alpha-a.b-c-something-long"
1414
]
1515
}

tests/common/mod.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::error::Error;
21
use std::fs::{self, File};
2+
use std::{collections::HashMap, error::Error};
33

44
use boon::{Compiler, Schemas};
55
use serde_json::{json, Value};
@@ -186,3 +186,56 @@ pub fn test_tags_schema(mut compiler: Compiler, id: &str) -> Result<(), Box<dyn
186186

187187
Ok(())
188188
}
189+
190+
pub fn test_schema_version(version: i8) -> Result<(), Box<dyn Error>> {
191+
let mut compiler = Compiler::new();
192+
compiler.enable_format_assertions();
193+
let mut loaded: HashMap<String, Vec<Value>> = HashMap::new();
194+
195+
let paths = fs::read_dir(format!("./schema/v{version}"))?;
196+
for path in paths {
197+
let path = path?.path();
198+
let bn = path.file_name().unwrap().to_str().unwrap();
199+
if bn.ends_with(".schema.json") {
200+
let schema: Value = serde_json::from_reader(File::open(path.clone())?)?;
201+
if let Value::String(s) = &schema["$id"] {
202+
// Make sure that the ID is correct.
203+
assert_eq!(format!("https://pgxn.org/meta/v{version}/{bn}"), *s);
204+
205+
// Add the schema to the compiler.
206+
compiler.add_resource(s, schema.to_owned())?;
207+
208+
// Grab the examples, if any, to test later.
209+
if let Value::Array(a) = &schema["examples"] {
210+
loaded.insert(s.clone(), a.to_owned());
211+
} else {
212+
loaded.insert(s.clone(), Vec::new());
213+
}
214+
} else {
215+
panic!("Unable to find ID in {}", path.display());
216+
}
217+
} else {
218+
println!("Skipping {}", path.display());
219+
}
220+
}
221+
222+
// Make sure we found schemas.
223+
assert!(!loaded.is_empty(), "No schemas loaded!");
224+
225+
// Make sure each schema we loaded is valid.
226+
let mut schemas = Schemas::new();
227+
for (id, examples) in loaded {
228+
let index = compiler.compile(id.as_str(), &mut schemas)?;
229+
println!("{} ok", id);
230+
231+
// Test the schema's examples.
232+
for (i, example) in examples.iter().enumerate() {
233+
if let Err(e) = schemas.validate(example, index) {
234+
panic!("Example {i} failed: {e}");
235+
}
236+
// println!(" Example {i} ok");
237+
}
238+
}
239+
240+
Ok(())
241+
}

tests/v1_schema_test.rs

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::fs::{self, File};
1+
use std::error::Error;
2+
use std::fs::File;
23
use std::io::{prelude::*, BufReader};
3-
use std::{collections::HashMap, error::Error};
44

5-
use boon::{Compiler, Schemas};
5+
use boon::Schemas;
66
use serde::{Deserialize, Serialize};
77
use serde_json::{json, Map, Value};
88

@@ -15,56 +15,7 @@ const SCHEMA_ID: &str = "https://pgxn.org/meta/v1/distribution.schema.json";
1515

1616
#[test]
1717
fn test_schema_v1() -> Result<(), Box<dyn Error>> {
18-
let mut compiler = Compiler::new();
19-
compiler.enable_format_assertions();
20-
let mut loaded: HashMap<String, Vec<Value>> = HashMap::new();
21-
22-
let paths = fs::read_dir("./schema/v1")?;
23-
for path in paths {
24-
let path = path?.path();
25-
let bn = path.file_name().unwrap().to_str().unwrap();
26-
if bn.ends_with(".schema.json") {
27-
let schema: Value = serde_json::from_reader(File::open(path.clone())?)?;
28-
if let Value::String(s) = &schema["$id"] {
29-
// Make sure that the ID is correct.
30-
assert_eq!(format!("https://pgxn.org/meta/v1/{bn}"), *s);
31-
32-
// Add the schema to the compiler.
33-
compiler.add_resource(s, schema.to_owned())?;
34-
35-
// Grab the examples, if any, to test later.
36-
if let Value::Array(a) = &schema["examples"] {
37-
loaded.insert(s.clone(), a.to_owned());
38-
} else {
39-
loaded.insert(s.clone(), Vec::new());
40-
}
41-
} else {
42-
panic!("Unable to find ID in {}", path.display());
43-
}
44-
} else {
45-
println!("Skipping {}", path.display());
46-
}
47-
}
48-
49-
// Make sure we found schemas.
50-
assert!(!loaded.is_empty(), "No schemas loaded!");
51-
52-
// Make sure each schema we loaded is valid.
53-
let mut schemas = Schemas::new();
54-
for (id, examples) in loaded {
55-
let index = compiler.compile(id.as_str(), &mut schemas)?;
56-
println!("{} ok", id);
57-
58-
// Test the schema's examples.
59-
for (i, example) in examples.iter().enumerate() {
60-
if let Err(e) = schemas.validate(example, index) {
61-
panic!("Example {i} failed: {e}");
62-
}
63-
// println!(" Example {i} ok");
64-
}
65-
}
66-
67-
Ok(())
18+
test_schema_version(1)
6819
}
6920

7021
#[derive(Deserialize, Serialize)]

tests/v2_schema_test.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ use common::*;
99

1010
const SCHEMA_BASE: &str = "https://pgxn.org/meta/v2";
1111

12+
#[test]
13+
fn test_schema_v1() -> Result<(), Box<dyn Error>> {
14+
test_schema_version(2)
15+
}
16+
17+
#[test]
18+
fn test_v2_term() -> Result<(), Box<dyn Error>> {
19+
// Load the schemas and compile the term schema.
20+
let compiler = new_compiler("schema/v2")?;
21+
let id = format!("{SCHEMA_BASE}/term.schema.json");
22+
test_term_schema(compiler, &id)
23+
}
24+
25+
#[test]
26+
fn test_v2_tags() -> Result<(), Box<dyn Error>> {
27+
// Load the schemas and compile the tags schema.
28+
let compiler = new_compiler("schema/v2")?;
29+
let id = format!("{SCHEMA_BASE}/tags.schema.json");
30+
test_tags_schema(compiler, &id)
31+
}
32+
1233
#[test]
1334
fn test_v2_semver() -> Result<(), Box<dyn Error>> {
1435
// Load the schemas and compile the semver schema.
@@ -38,19 +59,3 @@ fn test_v2_semver() -> Result<(), Box<dyn Error>> {
3859

3960
Ok(())
4061
}
41-
42-
#[test]
43-
fn test_v2_term() -> Result<(), Box<dyn Error>> {
44-
// Load the schemas and compile the term schema.
45-
let compiler = new_compiler("schema/v2")?;
46-
let id = format!("{SCHEMA_BASE}/term.schema.json");
47-
test_term_schema(compiler, &id)
48-
}
49-
50-
#[test]
51-
fn test_v2_tags() -> Result<(), Box<dyn Error>> {
52-
// Load the schemas and compile the tags schema.
53-
let compiler = new_compiler("schema/v2")?;
54-
let id = format!("{SCHEMA_BASE}/tags.schema.json");
55-
test_tags_schema(compiler, &id)
56-
}

0 commit comments

Comments
 (0)