Skip to content

Commit

Permalink
Turn schema tests into unit tests
Browse files Browse the repository at this point in the history
It was a bit fussy to have them as integration tests, and that's not
really what they are anyway. So move them into `src/tests` and otherwise
rejigger things only enough for them to have access to the private stuff
they need.

Also move the test corpus into the top-level `corpus` directory.

While at it, move the compiler module under valid, as it's only needed
by the valid module and the unit tests. Remove it from the root module
and instead load it publicly only for tests.
  • Loading branch information
theory committed Aug 1, 2024
1 parent 0eaa138 commit 6011d1b
Show file tree
Hide file tree
Showing 17 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .ci/test-cover
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ grcov "${DESTDIR}" \
--ignore '**/clang-sys*/**' \
--ignore "$HOME/.cargo/**" \
--ignore-not-existing \
--ignore 'tests/**' \
--ignore '**/tests/**' \
--ignore 'build.rs' \
--excl-start '#(\[cfg\(test\)\]|\[test\])' \
--llvm \
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ assert!(validator.validate(&meta).is_ok());
*/

#[doc(hidden)]
pub mod compiler;

mod valid;
pub use valid::{ValidationError, Validator};

#[cfg(test)]
mod tests;
6 changes: 3 additions & 3 deletions tests/common/mod.rs → src/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use pgxn_meta::compiler;
use std::{
collections::HashMap,
error::Error,
fs::{self, File},
path::Path,
};

use crate::valid::compiler;
use boon::{Compiler, Schemas};
use serde_json::{json, Value};
use wax::Glob;
Expand All @@ -27,7 +27,7 @@ pub const VALID_SEMVERS: &[&str] = &[
"1.0.0-alpha.1",
"1.0.0-alpha0.valid",
"1.0.0-alpha.0valid",
"1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay",
"1.0.0-alpha-a.b-c-something-long+build.1-aef.1-its-okay",
"1.0.0-rc.1+build.1",
"2.0.0-rc.1+build.123",
"1.2.3-beta",
Expand Down Expand Up @@ -83,7 +83,7 @@ pub const INVALID_SEMVERS: &[&str] = &[
"1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788",
"1.2-RC-SNAPSHOT",
"-1.0.3-gamma+b7718",
"+justmeta",
"+just-meta",
"9.8.7+meta+meta",
"9.8.7-whatever+meta+meta",
"99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12",
Expand Down
3 changes: 3 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod common;
mod v1;
mod v2;
3 changes: 1 addition & 2 deletions tests/v1_schema_test.rs → src/tests/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use boon::Schemas;
use serde_json::{json, Map, Value};

// importing common module.
mod common;
use common::*;
use super::common::*;

const SCHEMA_VERSION: u8 = 1;

Expand Down
3 changes: 1 addition & 2 deletions tests/v2_schema_test.rs → src/tests/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use boon::Schemas;
use serde_json::{json, Map, Value};

// importing common module.
mod common;
use common::*;
use super::common::*;

const SCHEMA_VERSION: u8 = 2;

Expand Down
3 changes: 1 addition & 2 deletions src/compiler/mod.rs → src/valid/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn new() -> Compiler {
let schema: Value = serde_json::from_str(str).unwrap();
let id = &schema["$id"]
.as_str()
.ok_or(super::valid::ValidationError::UnknownID)
.ok_or(super::ValidationError::UnknownID)
.unwrap();
compiler.add_resource(id, schema.to_owned()).unwrap();
}
Expand Down Expand Up @@ -161,7 +161,6 @@ mod tests {
let idx = compiler.compile(&id, &mut schemas)?;

let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("corpus")
.join(tc.0)
.join(tc.1);
Expand Down
12 changes: 9 additions & 3 deletions src/valid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ use std::{error::Error, fmt};
use boon::{Compiler, Schemas};
use serde_json::Value;

// Export compiler publicly only for tests.
#[cfg(test)]
pub mod compiler;

#[cfg(not(test))]
mod compiler;

/// PGXN Meta validator.
pub struct Validator {
compiler: Compiler,
Expand Down Expand Up @@ -46,7 +53,7 @@ impl Validator {
/// `dir`.
pub fn new() -> Validator {
Validator {
compiler: super::compiler::new(),
compiler: compiler::new(),
schemas: Schemas::new(),
}
}
Expand Down Expand Up @@ -99,7 +106,7 @@ mod tests {
let mut validator = Validator::default();

for v_dir in ["v1", "v2"] {
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "corpus", v_dir]
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "corpus", v_dir]
.iter()
.collect();
let glob = Glob::new("*.json")?;
Expand All @@ -122,7 +129,6 @@ mod tests {

for tc in [("v1", "widget.json"), ("v2", "typical-sql.json")] {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("corpus")
.join(tc.0)
.join(tc.1);
Expand Down

0 comments on commit 6011d1b

Please sign in to comment.