Skip to content

Commit

Permalink
Move directory-loading into common
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Jul 31, 2024
1 parent 490f4f3 commit d716b55
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 68 deletions.
19 changes: 1 addition & 18 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,23 +25,9 @@ pub fn new() -> Compiler {
compiler
}

pub fn for_test<P: AsRef<Path>>(dir: P) -> Result<Compiler, Box<dyn Error>> {
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 {
Expand Down
25 changes: 23 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -87,6 +93,21 @@ pub fn id_for(version: u8, schema: &str) -> String {
format!("{SCHEMA_BASE}{version}/{schema}.schema.json")
}

pub fn new_compiler<P: AsRef<Path>>(dir: P) -> Result<Compiler, Box<dyn Error>> {
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<dyn Error>> {
let mut schemas = Schemas::new();
let id = id_for(version, "term");
Expand Down
35 changes: 17 additions & 18 deletions tests/v1_schema_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,21 +17,21 @@ fn test_schema_v1() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_term() -> Result<(), Box<dyn Error>> {
// 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<dyn Error>> {
// 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<dyn Error>> {
// 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)?;
Expand All @@ -57,7 +56,7 @@ fn test_v1_version() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_version_range() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -128,7 +127,7 @@ fn test_v1_version_range() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_license() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -197,7 +196,7 @@ fn test_v1_license() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_provides() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -296,7 +295,7 @@ fn test_v1_provides() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_extension() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -472,7 +471,7 @@ fn test_v1_extension() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_maintainer() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -522,7 +521,7 @@ fn test_v1_maintainer() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_meta_spec() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -576,7 +575,7 @@ fn test_v1_meta_spec() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_bugtracker() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -630,7 +629,7 @@ fn test_v1_bugtracker() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_no_index() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -697,7 +696,7 @@ fn test_v1_no_index() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_prereq_relationship() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -750,7 +749,7 @@ fn test_v1_prereq_relationship() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_prereq_phase() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -862,7 +861,7 @@ fn test_v1_prereq_phase() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_prereqs() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -1010,7 +1009,7 @@ fn test_v1_prereqs() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_repository() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -1087,7 +1086,7 @@ fn test_v1_repository() -> Result<(), Box<dyn Error>> {
#[test]
fn test_v1_resources() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down Expand Up @@ -1224,7 +1223,7 @@ fn valid_distribution() -> Value {
#[test]
fn test_v1_distribution() -> Result<(), Box<dyn Error>> {
// 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)?;
Expand Down
Loading

0 comments on commit d716b55

Please sign in to comment.