-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42d4f97
commit 840f346
Showing
8 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "repo-schemas" | ||
version = "0.0.0" | ||
authors = ["Maxwell Huang-Hobbs <mhuan13@gmail.com>"] | ||
license = "Apache-2.0" | ||
description = """ | ||
Crate for introspective tests on this repo's schemas | ||
""" | ||
edition = "2018" | ||
|
||
[dev-dependencies] | ||
dockerfile-parser = "0.8.0" | ||
path-slash.workspace = true | ||
pretty_assertions.workspace = true | ||
regex.workspace = true | ||
repo-root = { path = "../repo-root" } | ||
serde-hjson = { version = "1.1.0", features = [] } | ||
toml = "0.8.19" | ||
schemars.workspace = true | ||
serde_json.workspace = true | ||
unused_finder = { version = "0.2.0", path = "../unused_finder" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(test)] | ||
mod schemas_up_to_date; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use path_slash::PathBufExt; | ||
use schemars::schema_for; | ||
use std::path::{Path, PathBuf}; | ||
use unused_finder::UnusedFinderJSONConfig; | ||
|
||
fn enforce_schema(file_path: &Path, schema: &schemars::schema::RootSchema) { | ||
// Check if the file exists | ||
if !file_path.exists() { | ||
// Just write the intended schema | ||
let schema_str = serde_json::to_string_pretty(schema).unwrap(); | ||
std::fs::create_dir_all(file_path.parent().unwrap()).unwrap(); | ||
std::fs::write(file_path, schema_str).unwrap(); | ||
} else { | ||
// read the schmea from the file | ||
let file_str = std::fs::read_to_string(file_path).unwrap(); | ||
let file_schema: schemars::schema::RootSchema = serde_json::from_str(&file_str).unwrap(); | ||
|
||
if file_schema != *schema { | ||
// overwrite with the intended schema if the original is out of date | ||
let schema_str = serde_json::to_string_pretty(schema).unwrap(); | ||
std::fs::write(file_path, schema_str).unwrap(); | ||
|
||
// fail the test with a pretty_eq comparison | ||
panic!( | ||
"schema mismatch for {:?}. The schema file has been updated with the intended comments, which you should commit", | ||
file_path | ||
); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_unused_config_schema() { | ||
let repo_root = repo_root::find_git_root(); | ||
let schemadir_path = repo_root.join(PathBuf::from_slash("schemas/")); | ||
|
||
// Check unused-config.schema.json | ||
enforce_schema( | ||
&schemadir_path.join("unused-config.schema.json"), | ||
&schema_for!(UnusedFinderJSONConfig), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "UnusedFinderJSONConfig", | ||
"description": "A JSON serializable proxy for the UnusedFinderConfig struct\n\nThis struct is used to deserialize the UnusedFinderConfig struct from a config file to with serde / over the debug bridge for napi", | ||
"type": "object", | ||
"required": [ | ||
"entryPackages", | ||
"rootPaths" | ||
], | ||
"properties": { | ||
"allowUnusedTypes": { | ||
"description": "If true, type-only exports will not be reported as used. However, the transitive dependencies of unused types will still be reported as unused.", | ||
"default": false, | ||
"type": "boolean" | ||
}, | ||
"entryPackages": { | ||
"description": "List of packages that should be considered \"entry\" packages All transitive imports from the exposed exports of these packages will be considered used\n\nNote that the only files that are considered roots are the ones that are _explicitly exported_, either as an entry in the package's \"exports\" config, or as a main/module export\n\nItems are parsed in one of three ways: 1. If the item starts with \"./\", it is treated as a path glob, and evaluated against the paths of package folders, relative to the repo root. 2. If the item contains any of \"~)('!*\", it is treated as a name-glob, and evaluated as a glob against the names of packages. 3. Otherwise, the item is treated as the name of an individual package, and matched literally.", | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"repoRoot": { | ||
"description": "Path to the root directory of the repository.", | ||
"default": "", | ||
"type": "string" | ||
}, | ||
"reportExportedSymbols": { | ||
"description": "If true, individual exported symbols are also tracked", | ||
"default": false, | ||
"type": "boolean" | ||
}, | ||
"rootPaths": { | ||
"description": "Root paths to walk as source files\n\nThese can be either absolute paths, or paths relative to the repo root", | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"skip": { | ||
"description": "A List of globs. Matching files and directories won't be scanned during the file walk\n\nMatches are made against the names of the individual directories, NOT the full directory paths", | ||
"default": [], | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"testFiles": { | ||
"description": "List of glob patterns to mark as \"tests\". These files will be marked as used, and all of their transitive dependencies will also be marked as used\n\nglob patterns are matched against the relative file path from the root of the repository", | ||
"default": [], | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} |