Skip to content

Commit

Permalink
update schemas, cargo udeps fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adjective-Object committed Nov 13, 2024
1 parent 42d4f97 commit 840f346
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 1 deletion.
58 changes: 58 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ swc_ecma_parser = "0.149.0"
swc_ecma_transforms = "0.238.0"
swc_ecma_visit = "0.104.6"
thiserror = "1.0.52"
schemars = "0.8.21"


[profile.release]
Expand Down
21 changes: 21 additions & 0 deletions crates/repo-schemas/Cargo.toml
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" }
2 changes: 2 additions & 0 deletions crates/repo-schemas/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(test)]
mod schemas_up_to_date;
42 changes: 42 additions & 0 deletions crates/repo-schemas/src/schemas_up_to_date.rs
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),
);
}
1 change: 1 addition & 0 deletions crates/unused_finder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ignore = "0.4.23"
abspath = { version = "0.2.0", path = "../abspath" }
itertools = "0.13.0"
debug_print = "1.0.0"
schemars.workspace = true

[dev-dependencies]
stringreader = "0.1.1"
Expand Down
3 changes: 2 additions & 1 deletion crates/unused_finder/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{Display, Formatter};
use itertools::Itertools;
use package_match_rules::PackageMatchRules;
use rayon::iter::Either;
use schemars::JsonSchema;
use serde::Deserialize;

pub mod package_match_rules;
Expand Down Expand Up @@ -62,7 +63,7 @@ pub enum ConfigError {
///
/// This struct is used to deserialize the UnusedFinderConfig struct
/// from a config file to with serde / over the debug bridge for napi
#[derive(Debug, Default, Clone, Deserialize)]
#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct UnusedFinderJSONConfig {
/// Path to the root directory of the repository.
Expand Down
57 changes: 57 additions & 0 deletions schemas/unused-config.schema.json
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"
}
}
}
}

0 comments on commit 840f346

Please sign in to comment.