diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 6608bb8..5e982a4 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -6,9 +6,6 @@ use std::error::Error; use boon::Compiler; use serde_json::Value; -use std::{fs::File, path::Path}; -use wax::Glob; - /// new returns a new boon::Compiler with the schema files loaded from `dir` /// and configured to validate `path` and `license` formats. pub fn new() -> Compiler { @@ -28,23 +25,9 @@ pub fn new() -> Compiler { compiler } -pub fn for_test>(dir: P) -> Result> { - let mut compiler = spec_compiler(); - let glob = Glob::new("**/*.schema.json")?; - for path in glob.walk(dir) { - let schema: Value = serde_json::from_reader(File::open(path?.into_path())?)?; - let id = &schema["$id"] - .as_str() - .ok_or(super::valid::ValidationError::UnknownID)?; - compiler.add_resource(id, schema.to_owned())?; - } - - Ok(compiler) -} - /// Creates a new boon::compiler with format assertions enabled and validation /// for the custom `path` and `license` formats. -fn spec_compiler() -> Compiler { +pub fn spec_compiler() -> Compiler { let mut compiler = Compiler::new(); compiler.enable_format_assertions(); compiler.register_format(boon::Format { diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 5fdd14f..c77a3b4 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,8 +1,14 @@ -use std::fs::{self, File}; -use std::{collections::HashMap, error::Error}; +use pgxn_meta::compiler; +use std::{ + collections::HashMap, + error::Error, + fs::{self, File}, + path::Path, +}; use boon::{Compiler, Schemas}; use serde_json::{json, Value}; +use wax::Glob; const SCHEMA_BASE: &str = "https://pgxn.org/meta/v"; @@ -87,6 +93,21 @@ pub fn id_for(version: u8, schema: &str) -> String { format!("{SCHEMA_BASE}{version}/{schema}.schema.json") } +pub fn new_compiler>(dir: P) -> Result> { + let mut compiler = compiler::spec_compiler(); + let glob = Glob::new("**/*.schema.json")?; + for path in glob.walk(dir) { + let path = path?.into_path(); + let schema: Value = serde_json::from_reader(File::open(&path)?)?; + let id = &schema["$id"] + .as_str() + .ok_or(format!("Missing $id from {}", &path.display()))?; + compiler.add_resource(id, schema.to_owned())?; + } + + Ok(compiler) +} + pub fn test_term_schema(mut compiler: Compiler, version: u8) -> Result<(), Box> { let mut schemas = Schemas::new(); let id = id_for(version, "term"); diff --git a/tests/v1_schema_test.rs b/tests/v1_schema_test.rs index 7274306..07a8ae8 100644 --- a/tests/v1_schema_test.rs +++ b/tests/v1_schema_test.rs @@ -6,7 +6,6 @@ use serde_json::{json, Map, Value}; // importing common module. mod common; use common::*; -use pgxn_meta::compiler; const SCHEMA_VERSION: u8 = 1; @@ -18,21 +17,21 @@ fn test_schema_v1() -> Result<(), Box> { #[test] fn test_v1_term() -> Result<(), Box> { // Load the schemas and compile the term schema. - let compiler = compiler::for_test("schema/v1")?; + let compiler = new_compiler("schema/v1")?; test_term_schema(compiler, SCHEMA_VERSION) } #[test] fn test_v1_tags() -> Result<(), Box> { // Load the schemas and compile the tags schema. - let compiler = compiler::for_test("schema/v1")?; + let compiler = new_compiler("schema/v1")?; test_tags_schema(compiler, SCHEMA_VERSION) } #[test] fn test_v1_version() -> Result<(), Box> { // Load the schemas and compile the version schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "version"); let idx = compiler.compile(&id, &mut schemas)?; @@ -57,7 +56,7 @@ fn test_v1_version() -> Result<(), Box> { #[test] fn test_v1_version_range() -> Result<(), Box> { // Load the schemas and compile the version_range schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "version_range"); let idx = compiler.compile(&id, &mut schemas)?; @@ -128,7 +127,7 @@ fn test_v1_version_range() -> Result<(), Box> { #[test] fn test_v1_license() -> Result<(), Box> { // Load the schemas and compile the license schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "license"); let idx = compiler.compile(&id, &mut schemas)?; @@ -197,7 +196,7 @@ fn test_v1_license() -> Result<(), Box> { #[test] fn test_v1_provides() -> Result<(), Box> { // Load the schemas and compile the provides schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "provides"); let idx = compiler.compile(&id, &mut schemas)?; @@ -296,7 +295,7 @@ fn test_v1_provides() -> Result<(), Box> { #[test] fn test_v1_extension() -> Result<(), Box> { // Load the schemas and compile the extension schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "extension"); let idx = compiler.compile(&id, &mut schemas)?; @@ -472,7 +471,7 @@ fn test_v1_extension() -> Result<(), Box> { #[test] fn test_v1_maintainer() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "maintainer"); let idx = compiler.compile(&id, &mut schemas)?; @@ -522,7 +521,7 @@ fn test_v1_maintainer() -> Result<(), Box> { #[test] fn test_v1_meta_spec() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "meta-spec"); let idx = compiler.compile(&id, &mut schemas)?; @@ -576,7 +575,7 @@ fn test_v1_meta_spec() -> Result<(), Box> { #[test] fn test_v1_bugtracker() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "bugtracker"); let idx = compiler.compile(&id, &mut schemas)?; @@ -630,7 +629,7 @@ fn test_v1_bugtracker() -> Result<(), Box> { #[test] fn test_v1_no_index() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "no_index"); let idx = compiler.compile(&id, &mut schemas)?; @@ -697,7 +696,7 @@ fn test_v1_no_index() -> Result<(), Box> { #[test] fn test_v1_prereq_relationship() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "prereq_relationship"); let idx = compiler.compile(&id, &mut schemas)?; @@ -750,7 +749,7 @@ fn test_v1_prereq_relationship() -> Result<(), Box> { #[test] fn test_v1_prereq_phase() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "prereq_phase"); let idx = compiler.compile(&id, &mut schemas)?; @@ -862,7 +861,7 @@ fn test_v1_prereq_phase() -> Result<(), Box> { #[test] fn test_v1_prereqs() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "prereqs"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1010,7 +1009,7 @@ fn test_v1_prereqs() -> Result<(), Box> { #[test] fn test_v1_repository() -> Result<(), Box> { // Load the schemas and compile the repository schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "repository"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1087,7 +1086,7 @@ fn test_v1_repository() -> Result<(), Box> { #[test] fn test_v1_resources() -> Result<(), Box> { // Load the schemas and compile the resources schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "resources"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1224,7 +1223,7 @@ fn valid_distribution() -> Value { #[test] fn test_v1_distribution() -> Result<(), Box> { // Load the schemas and compile the distribution schema. - let mut compiler = compiler::for_test("schema/v1")?; + let mut compiler = new_compiler("schema/v1")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "distribution"); let idx = compiler.compile(&id, &mut schemas)?; diff --git a/tests/v2_schema_test.rs b/tests/v2_schema_test.rs index 730d06b..7be7813 100644 --- a/tests/v2_schema_test.rs +++ b/tests/v2_schema_test.rs @@ -6,7 +6,6 @@ use serde_json::{json, Map, Value}; // importing common module. mod common; use common::*; -use pgxn_meta::compiler; const SCHEMA_VERSION: u8 = 2; @@ -18,21 +17,21 @@ fn test_schema_v2() -> Result<(), Box> { #[test] fn test_v2_term() -> Result<(), Box> { // Load the schemas and compile the term schema. - let compiler = compiler::for_test("schema/v2")?; + let compiler = new_compiler("schema/v2")?; test_term_schema(compiler, SCHEMA_VERSION) } #[test] fn test_v2_tags() -> Result<(), Box> { // Load the schemas and compile the tags schema. - let compiler = compiler::for_test("schema/v2")?; + let compiler = new_compiler("schema/v2")?; test_tags_schema(compiler, SCHEMA_VERSION) } #[test] fn test_v2_semver() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "semver"); let idx = compiler.compile(&id, &mut schemas)?; @@ -62,7 +61,7 @@ fn test_v2_semver() -> Result<(), Box> { #[test] fn test_v2_path() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "path"); let idx = compiler.compile(&id, &mut schemas)?; @@ -106,7 +105,7 @@ fn test_v2_path() -> Result<(), Box> { #[test] fn test_v2_glob() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "glob"); let idx = compiler.compile(&id, &mut schemas)?; @@ -147,7 +146,7 @@ fn test_v2_glob() -> Result<(), Box> { #[test] fn test_v2_version_range() -> Result<(), Box> { // Load the schemas and compile the version_range schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "version_range"); let idx = compiler.compile(&id, &mut schemas)?; @@ -217,7 +216,7 @@ fn test_v2_version_range() -> Result<(), Box> { #[test] fn test_v2_license() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "license"); let idx = compiler.compile(&id, &mut schemas)?; @@ -267,7 +266,7 @@ fn test_v2_license() -> Result<(), Box> { #[test] fn test_v2_purl() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "purl"); let idx = compiler.compile(&id, &mut schemas)?; @@ -311,7 +310,7 @@ fn test_v2_purl() -> Result<(), Box> { #[test] fn test_v2_platform() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "platform"); let idx = compiler.compile(&id, &mut schemas)?; @@ -397,7 +396,7 @@ fn test_v2_platform() -> Result<(), Box> { #[test] fn test_v2_platforms() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "platforms"); let idx = compiler.compile(&id, &mut schemas)?; @@ -445,7 +444,7 @@ fn test_v2_platforms() -> Result<(), Box> { #[test] fn test_v2_maintainers() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "maintainers"); let idx = compiler.compile(&id, &mut schemas)?; @@ -583,7 +582,7 @@ fn test_v2_maintainers() -> Result<(), Box> { #[test] fn test_v2_extension() -> Result<(), Box> { // Load the schemas and compile the extension schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "extension"); let idx = compiler.compile(&id, &mut schemas)?; @@ -760,7 +759,7 @@ fn test_v2_extension() -> Result<(), Box> { #[test] fn test_v2_module() -> Result<(), Box> { // Load the schemas and compile the extension schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "module"); let idx = compiler.compile(&id, &mut schemas)?; @@ -929,7 +928,7 @@ fn test_v2_module() -> Result<(), Box> { #[test] fn test_v2_app() -> Result<(), Box> { // Load the schemas and compile the extension schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "app"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1043,7 +1042,7 @@ fn test_v2_app() -> Result<(), Box> { #[test] fn test_v2_contents() -> Result<(), Box> { // Load the schemas and compile the extension schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "contents"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1182,7 +1181,7 @@ fn test_v2_contents() -> Result<(), Box> { #[test] fn test_v2_meta_spec() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "meta-spec"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1236,7 +1235,7 @@ fn test_v2_meta_spec() -> Result<(), Box> { #[test] fn test_v2_categories() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "categories"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1305,7 +1304,7 @@ fn test_v2_categories() -> Result<(), Box> { #[test] fn test_v2_classifications() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "classifications"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1376,7 +1375,7 @@ fn test_v2_classifications() -> Result<(), Box> { #[test] fn test_v2_ignore() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "ignore"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1434,7 +1433,7 @@ fn test_v2_ignore() -> Result<(), Box> { #[test] fn test_v2_phase() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "phase"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1561,7 +1560,7 @@ fn test_v2_phase() -> Result<(), Box> { #[test] fn test_v2_packages() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "packages"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1715,7 +1714,7 @@ fn test_v2_packages() -> Result<(), Box> { #[test] fn test_v2_postgres() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "postgres"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1775,7 +1774,7 @@ fn test_v2_postgres() -> Result<(), Box> { #[test] fn test_v2_pipeline() -> Result<(), Box> { // Load the schemas and compile the semver schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "pipeline"); let idx = compiler.compile(&id, &mut schemas)?; @@ -1819,7 +1818,7 @@ fn test_v2_pipeline() -> Result<(), Box> { #[test] fn test_v2_dependencies() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "dependencies"); let idx = compiler.compile(&id, &mut schemas)?; @@ -2074,7 +2073,7 @@ fn test_v2_dependencies() -> Result<(), Box> { #[test] fn test_v2_variations() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "variations"); let idx = compiler.compile(&id, &mut schemas)?; @@ -2204,7 +2203,7 @@ fn test_v2_variations() -> Result<(), Box> { #[test] fn test_v2_badges() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "badges"); let idx = compiler.compile(&id, &mut schemas)?; @@ -2317,7 +2316,7 @@ fn test_v2_badges() -> Result<(), Box> { #[test] fn test_v2_resources() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "resources"); let idx = compiler.compile(&id, &mut schemas)?; @@ -2443,7 +2442,7 @@ fn test_v2_resources() -> Result<(), Box> { #[test] fn test_v2_artifacts() -> Result<(), Box> { // Load the schemas and compile the maintainer schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "artifacts"); let idx = compiler.compile(&id, &mut schemas)?; @@ -2889,7 +2888,7 @@ fn valid_v2_distribution() -> Value { #[test] fn test_v2_distribution() -> Result<(), Box> { // Load the schemas and compile the distribution schema. - let mut compiler = compiler::for_test("schema/v2")?; + let mut compiler = new_compiler("schema/v2")?; let mut schemas = Schemas::new(); let id = id_for(SCHEMA_VERSION, "distribution"); let idx = compiler.compile(&id, &mut schemas)?;