From 6011d1bdbd979bf8397267a797f674aed359e7ce Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Wed, 31 Jul 2024 18:51:57 -0400 Subject: [PATCH] Turn schema tests into unit tests 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. --- .ci/test-cover | 2 +- {tests/corpus => corpus}/v1/howto.json | 0 {tests/corpus => corpus}/v1/howto2.json | 0 {tests/corpus => corpus}/v1/widget.json | 0 {tests/corpus => corpus}/v2/minimal.json | 0 {tests/corpus => corpus}/v2/pg_partman.json | 0 {tests/corpus => corpus}/v2/postgresml.json | 0 {tests/corpus => corpus}/v2/typical-c.json | 0 {tests/corpus => corpus}/v2/typical-pgrx.json | 0 {tests/corpus => corpus}/v2/typical-sql.json | 0 src/lib.rs | 6 +++--- tests/common/mod.rs => src/tests/common.rs | 6 +++--- src/tests/mod.rs | 3 +++ tests/v1_schema_test.rs => src/tests/v1.rs | 3 +-- tests/v2_schema_test.rs => src/tests/v2.rs | 3 +-- src/{ => valid}/compiler/mod.rs | 3 +-- src/valid/mod.rs | 12 +++++++++--- 17 files changed, 22 insertions(+), 16 deletions(-) rename {tests/corpus => corpus}/v1/howto.json (100%) rename {tests/corpus => corpus}/v1/howto2.json (100%) rename {tests/corpus => corpus}/v1/widget.json (100%) rename {tests/corpus => corpus}/v2/minimal.json (100%) rename {tests/corpus => corpus}/v2/pg_partman.json (100%) rename {tests/corpus => corpus}/v2/postgresml.json (100%) rename {tests/corpus => corpus}/v2/typical-c.json (100%) rename {tests/corpus => corpus}/v2/typical-pgrx.json (100%) rename {tests/corpus => corpus}/v2/typical-sql.json (100%) rename tests/common/mod.rs => src/tests/common.rs (98%) create mode 100644 src/tests/mod.rs rename tests/v1_schema_test.rs => src/tests/v1.rs (99%) rename tests/v2_schema_test.rs => src/tests/v2.rs (99%) rename src/{ => valid}/compiler/mod.rs (98%) diff --git a/.ci/test-cover b/.ci/test-cover index d0ebca9..693658a 100755 --- a/.ci/test-cover +++ b/.ci/test-cover @@ -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 \ diff --git a/tests/corpus/v1/howto.json b/corpus/v1/howto.json similarity index 100% rename from tests/corpus/v1/howto.json rename to corpus/v1/howto.json diff --git a/tests/corpus/v1/howto2.json b/corpus/v1/howto2.json similarity index 100% rename from tests/corpus/v1/howto2.json rename to corpus/v1/howto2.json diff --git a/tests/corpus/v1/widget.json b/corpus/v1/widget.json similarity index 100% rename from tests/corpus/v1/widget.json rename to corpus/v1/widget.json diff --git a/tests/corpus/v2/minimal.json b/corpus/v2/minimal.json similarity index 100% rename from tests/corpus/v2/minimal.json rename to corpus/v2/minimal.json diff --git a/tests/corpus/v2/pg_partman.json b/corpus/v2/pg_partman.json similarity index 100% rename from tests/corpus/v2/pg_partman.json rename to corpus/v2/pg_partman.json diff --git a/tests/corpus/v2/postgresml.json b/corpus/v2/postgresml.json similarity index 100% rename from tests/corpus/v2/postgresml.json rename to corpus/v2/postgresml.json diff --git a/tests/corpus/v2/typical-c.json b/corpus/v2/typical-c.json similarity index 100% rename from tests/corpus/v2/typical-c.json rename to corpus/v2/typical-c.json diff --git a/tests/corpus/v2/typical-pgrx.json b/corpus/v2/typical-pgrx.json similarity index 100% rename from tests/corpus/v2/typical-pgrx.json rename to corpus/v2/typical-pgrx.json diff --git a/tests/corpus/v2/typical-sql.json b/corpus/v2/typical-sql.json similarity index 100% rename from tests/corpus/v2/typical-sql.json rename to corpus/v2/typical-sql.json diff --git a/src/lib.rs b/src/lib.rs index c3df338..4bc6826 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/tests/common/mod.rs b/src/tests/common.rs similarity index 98% rename from tests/common/mod.rs rename to src/tests/common.rs index c77a3b4..a7edbe3 100644 --- a/tests/common/mod.rs +++ b/src/tests/common.rs @@ -1,4 +1,3 @@ -use pgxn_meta::compiler; use std::{ collections::HashMap, error::Error, @@ -6,6 +5,7 @@ use std::{ path::Path, }; +use crate::valid::compiler; use boon::{Compiler, Schemas}; use serde_json::{json, Value}; use wax::Glob; @@ -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", @@ -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", diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..7f11b1e --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1,3 @@ +mod common; +mod v1; +mod v2; diff --git a/tests/v1_schema_test.rs b/src/tests/v1.rs similarity index 99% rename from tests/v1_schema_test.rs rename to src/tests/v1.rs index 07a8ae8..df1b3e4 100644 --- a/tests/v1_schema_test.rs +++ b/src/tests/v1.rs @@ -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; diff --git a/tests/v2_schema_test.rs b/src/tests/v2.rs similarity index 99% rename from tests/v2_schema_test.rs rename to src/tests/v2.rs index 7be7813..f18ac34 100644 --- a/tests/v2_schema_test.rs +++ b/src/tests/v2.rs @@ -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; diff --git a/src/compiler/mod.rs b/src/valid/compiler/mod.rs similarity index 98% rename from src/compiler/mod.rs rename to src/valid/compiler/mod.rs index 5e982a4..533ddba 100644 --- a/src/compiler/mod.rs +++ b/src/valid/compiler/mod.rs @@ -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(); } @@ -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); diff --git a/src/valid/mod.rs b/src/valid/mod.rs index eb9fb4f..f63bc73 100644 --- a/src/valid/mod.rs +++ b/src/valid/mod.rs @@ -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, @@ -46,7 +53,7 @@ impl Validator { /// `dir`. pub fn new() -> Validator { Validator { - compiler: super::compiler::new(), + compiler: compiler::new(), schemas: Schemas::new(), } } @@ -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")?; @@ -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);