test(jsonschema): add index.html for test suite outputs

Add functionality to generate an index.html file after running the
JSON Schema Test Suite integration tests, providing easy navigation
of generated documentation.

Features:
- Use Tera template (test_index.html) for consistent styling
- Display statistics: total schemas, test files, with/without content
- Annotate each schema with content status (has properties/$defs or not)
- Show JSON Schema features/keywords used in each test schema
- Include build information: git commit hash, dirty status, timestamps
- Group schemas by source test file for easy navigation
This commit is contained in:
LGUG2Z committed 2025-12-22 10:23:12 -08:00
1 parent 2e3913c325
commit 6bf0975898
4 files changed
+562

No files matched your search

Generated
+3
View File
@@ -203,7 +203,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2"
dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
@@ -1220,6 +1222,7 @@ dependencies = [
name = "schemars-docgen"
version = "0.1.0"
dependencies = [
"chrono",
"clap",
"lightningcss",
"pulldown-cmark",
+1
View File
@@ -31,3 +31,4 @@ serde = { version = "1", features = ["derive"] }
[dev-dependencies]
regex = "1"
rayon = "1"
chrono = "0.4"
+313
View File
@@ -0,0 +1,313 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JSON Schema Test Suite - Generated Documentation</title>
<style>
:root {
--bg-primary: #1a1a2e;
--bg-secondary: #16213e;
--bg-card: #1f2937;
--text-primary: #e2e8f0;
--text-secondary: #94a3b8;
--accent: #818cf8;
--accent-hover: #a5b4fc;
--border: #374151;
--green: #34d399;
--yellow: #fbbf24;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family:
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
line-height: 1.6;
padding: 2rem;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h1 {
font-size: 2rem;
margin-bottom: 0.5rem;
color: var(--accent);
}
.subtitle {
color: var(--text-secondary);
margin-bottom: 2rem;
}
.stats {
display: flex;
flex-wrap: wrap;
gap: 2rem;
margin-bottom: 2rem;
padding: 1rem;
background: var(--bg-secondary);
border-radius: 8px;
border: 1px solid var(--border);
}
.stat {
text-align: center;
}
.stat-value {
font-size: 2rem;
font-weight: bold;
color: var(--accent);
}
.stat-value.green {
color: var(--green);
}
.stat-value.yellow {
color: var(--yellow);
}
.stat-label {
color: var(--text-secondary);
font-size: 0.875rem;
}
.build-info {
margin-bottom: 2rem;
padding: 1rem;
background: var(--bg-secondary);
border-radius: 8px;
border: 1px solid var(--border);
font-size: 0.875rem;
}
.build-info h3 {
margin-bottom: 0.5rem;
font-size: 1rem;
}
.build-info-grid {
display: grid;
grid-template-columns: auto 1fr;
gap: 0.25rem 1rem;
}
.build-info-label {
color: var(--text-secondary);
}
.build-info-value {
font-family: "SF Mono", Consolas, monospace;
}
.dirty-badge {
display: inline-block;
padding: 0.125rem 0.5rem;
margin-left: 0.5rem;
background: rgba(239, 68, 68, 0.2);
color: #ef4444;
border-radius: 4px;
font-size: 0.75rem;
font-weight: 500;
}
.clean-badge {
display: inline-block;
padding: 0.125rem 0.5rem;
margin-left: 0.5rem;
background: rgba(52, 211, 153, 0.2);
color: var(--green);
border-radius: 4px;
font-size: 0.75rem;
font-weight: 500;
}
.legend {
margin-bottom: 2rem;
padding: 1rem;
background: var(--bg-secondary);
border-radius: 8px;
border: 1px solid var(--border);
}
.legend h3 {
margin-bottom: 0.5rem;
font-size: 1rem;
}
.legend-items {
display: flex;
gap: 2rem;
flex-wrap: wrap;
}
.legend-item {
display: flex;
align-items: center;
gap: 0.5rem;
color: var(--text-secondary);
font-size: 0.875rem;
}
.badge {
display: inline-block;
padding: 0.125rem 0.5rem;
border-radius: 4px;
font-size: 0.75rem;
font-weight: 500;
}
.badge-content {
background: rgba(52, 211, 153, 0.2);
color: var(--green);
}
.badge-empty {
background: rgba(251, 191, 36, 0.2);
color: var(--yellow);
}
.file-group {
margin-bottom: 1.5rem;
background: var(--bg-card);
border-radius: 8px;
border: 1px solid var(--border);
overflow: hidden;
}
.file-header {
padding: 0.75rem 1rem;
background: var(--bg-secondary);
border-bottom: 1px solid var(--border);
font-weight: 600;
font-family: "SF Mono", Consolas, monospace;
font-size: 0.9rem;
}
.schema-list {
list-style: none;
padding: 0.5rem;
}
.schema-item {
padding: 0.5rem;
border-radius: 4px;
}
.schema-item:hover {
background: var(--bg-secondary);
}
.schema-link {
color: var(--accent);
text-decoration: none;
display: flex;
align-items: baseline;
gap: 0.5rem;
flex-wrap: wrap;
}
.schema-link:hover {
color: var(--accent-hover);
}
.schema-index {
color: var(--text-secondary);
font-size: 0.75rem;
font-family: "SF Mono", Consolas, monospace;
min-width: 2rem;
}
.schema-desc {
flex: 1;
min-width: 200px;
}
.schema-features {
display: flex;
gap: 0.25rem;
flex-wrap: wrap;
}
.feature-tag {
display: inline-block;
padding: 0.0625rem 0.375rem;
background: var(--bg-secondary);
border-radius: 3px;
font-size: 0.625rem;
font-family: "SF Mono", Consolas, monospace;
color: var(--text-secondary);
}
</style>
</head>
<body>
<div class="container">
<h1>JSON Schema Test Suite</h1>
<p class="subtitle">
Generated HTML documentation for draft2020-12 test schemas
</p>
<div class="stats">
<div class="stat">
<div class="stat-value">{{ total_schemas }}</div>
<div class="stat-label">Total Schemas</div>
</div>
<div class="stat">
<div class="stat-value">{{ total_files }}</div>
<div class="stat-label">Test Files</div>
</div>
<div class="stat">
<div class="stat-value green">{{ with_content }}</div>
<div class="stat-label">With Content</div>
</div>
<div class="stat">
<div class="stat-value yellow">{{ without_content }}</div>
<div class="stat-label">Empty (Expected)</div>
</div>
</div>
<div class="build-info">
<h3>Build Information</h3>
<div class="build-info-grid">
<span class="build-info-label">Commit:</span>
<span class="build-info-value">
{{ commit_hash }} {%- if is_dirty %}
<span class="dirty-badge">dirty</span>
{%- else %}
<span class="clean-badge">clean</span>
{%- endif %}
</span>
<span class="build-info-label">Time (UTC):</span>
<span class="build-info-value">{{ time_utc }}</span>
<span class="build-info-label">Time (Local):</span>
<span class="build-info-value">{{ time_local }}</span>
</div>
</div>
<div class="legend">
<h3>Legend</h3>
<div class="legend-items">
<div class="legend-item">
<span class="badge badge-content">content</span>
<span
>Schema has properties or $defs -
Configuration/Types sections populated</span
>
</div>
<div class="legend-item">
<span class="badge badge-empty">empty</span>
<span
>Schema lacks properties/$defs - empty sections
expected</span
>
</div>
</div>
</div>
{%- for group in groups %}
<div class="file-group">
<div class="file-header">{{ group.file_path }}</div>
<ul class="schema-list">
{%- for schema in group.schemas %}
<li class="schema-item">
<a
class="schema-link"
href="{{ schema.output_path | safe }}"
>
<span class="schema-index"
>[{{ schema.index }}]</span
>
{%- if schema.has_content %}
<span class="badge badge-content">content</span>
{%- else %}
<span class="badge badge-empty">empty</span>
{%- endif %}
<span class="schema-desc"
>{{ schema.description }}</span
>
<span class="schema-features">
{%- for feature in schema.features %}
<span class="feature-tag">{{ feature }}</span>
{%- endfor %}
</span>
</a>
</li>
{%- endfor %}
</ul>
</div>
{%- endfor %}
</div>
</body>
</html>
+245
View File
@@ -4,8 +4,12 @@
//! for all schemas in the JSON-Schema-Test-Suite draft2020-12 examples
//! without crashing.
use chrono::Local;
use chrono::Utc;
use rayon::prelude::*;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
@@ -13,6 +17,8 @@ use std::process::Command;
use std::sync::Mutex;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use tera::Context;
use tera::Tera;
/// Represents a test suite from the JSON Schema Test Suite
#[derive(Debug, Deserialize)]
@@ -173,6 +179,242 @@ fn collect_all_test_cases() -> Vec<SchemaTestCase> {
.collect()
}
/// Data structure for a schema entry in the index template
#[derive(Debug, Serialize)]
struct IndexSchema {
index: usize,
description: String,
output_path: String,
has_content: bool,
features: Vec<String>,
}
/// Data structure for a file group in the index template
#[derive(Debug, Serialize)]
struct IndexGroup {
file_path: String,
schemas: Vec<IndexSchema>,
}
/// Analyze a schema and return (has_content, features)
/// has_content is true if the schema has properties or $defs (will show Configuration/Types)
/// features lists the JSON Schema keywords present in the schema
fn analyze_schema(schema: &serde_json::Value) -> (bool, Vec<String>) {
let mut features = Vec::new();
let obj = match schema.as_object() {
Some(o) => o,
None => {
// Handle boolean schemas
if schema.as_bool() == Some(true) {
return (false, vec!["boolean: true".to_string()]);
} else if schema.as_bool() == Some(false) {
return (false, vec!["boolean: false".to_string()]);
}
return (false, vec!["non-object".to_string()]);
}
};
if obj.is_empty() {
return (false, vec!["empty schema".to_string()]);
}
let has_properties = obj.contains_key("properties");
let has_defs = obj.contains_key("$defs") || obj.contains_key("definitions");
// Collect features
if has_properties {
features.push("properties".to_string());
}
if has_defs {
features.push("$defs".to_string());
}
if obj.contains_key("$ref") {
features.push("$ref".to_string());
}
if obj.contains_key("type") {
if let Some(t) = obj.get("type") {
if let Some(s) = t.as_str() {
features.push(format!("type: {}", s));
} else if let Some(arr) = t.as_array() {
let types: Vec<_> = arr.iter().filter_map(|v| v.as_str()).collect();
features.push(format!("type: [{}]", types.join(", ")));
}
}
}
if obj.contains_key("items") {
features.push("items".to_string());
}
if obj.contains_key("prefixItems") {
features.push("prefixItems".to_string());
}
if obj.contains_key("additionalProperties") {
features.push("additionalProperties".to_string());
}
if obj.contains_key("patternProperties") {
features.push("patternProperties".to_string());
}
if obj.contains_key("allOf") {
features.push("allOf".to_string());
}
if obj.contains_key("anyOf") {
features.push("anyOf".to_string());
}
if obj.contains_key("oneOf") {
features.push("oneOf".to_string());
}
if obj.contains_key("not") {
features.push("not".to_string());
}
if obj.contains_key("if") {
features.push("if/then/else".to_string());
}
if obj.contains_key("const") {
features.push("const".to_string());
}
if obj.contains_key("enum") {
features.push("enum".to_string());
}
if obj.contains_key("$dynamicRef") {
features.push("$dynamicRef".to_string());
}
if obj.contains_key("$dynamicAnchor") {
features.push("$dynamicAnchor".to_string());
}
if obj.contains_key("contains") {
features.push("contains".to_string());
}
if obj.contains_key("dependentSchemas") {
features.push("dependentSchemas".to_string());
}
if obj.contains_key("dependentRequired") {
features.push("dependentRequired".to_string());
}
if obj.contains_key("unevaluatedItems") {
features.push("unevaluatedItems".to_string());
}
if obj.contains_key("unevaluatedProperties") {
features.push("unevaluatedProperties".to_string());
}
// If no features found, list what keys are present
if features.is_empty() {
let keys: Vec<_> = obj.keys().take(3).cloned().collect();
features.push(format!("other: {}", keys.join(", ")));
}
(has_properties || has_defs, features)
}
/// Get git commit hash
fn get_git_commit_hash() -> String {
Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.ok()
.and_then(|o| {
if o.status.success() {
String::from_utf8(o.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
})
.unwrap_or_else(|| "unknown".to_string())
}
/// Check if git repo is dirty
fn is_git_dirty() -> bool {
Command::new("git")
.args(["status", "--porcelain"])
.output()
.ok()
.map(|o| !o.stdout.is_empty())
.unwrap_or(false)
}
/// Generate an index.html file with links to all test outputs using Tera
fn generate_index_html(test_cases: &[SchemaTestCase]) {
let output_base = project_root().join("tests/outputs");
let index_path = output_base.join("index.html");
// Group test cases by their source file (relative_path)
let mut grouped: BTreeMap<String, Vec<&SchemaTestCase>> = BTreeMap::new();
for test_case in test_cases {
grouped
.entry(test_case.relative_path.clone())
.or_default()
.push(test_case);
}
// Convert to template-friendly structure
let mut with_content = 0;
let mut without_content = 0;
let groups: Vec<IndexGroup> = grouped
.into_iter()
.map(|(file_path, cases)| {
let schemas = cases
.into_iter()
.map(|case| {
let relative_output = case
.output_path
.strip_prefix(&output_base)
.expect("Output path should be under output base");
let (has_content, features) = analyze_schema(&case.schema);
if has_content {
with_content += 1;
} else {
without_content += 1;
}
IndexSchema {
index: case.index,
description: case.description.clone(),
output_path: relative_output.display().to_string(),
has_content,
features,
}
})
.collect();
IndexGroup { file_path, schemas }
})
.collect();
// Load and render template
let template_path = project_root().join("src/templates/test_index.html");
let template_content =
fs::read_to_string(&template_path).expect("Failed to read test_index.html template");
let mut tera = Tera::default();
tera.add_raw_template("test_index.html", &template_content)
.expect("Failed to parse template");
// Get git and time info
let commit_hash = get_git_commit_hash();
let is_dirty = is_git_dirty();
let time_utc = Utc::now().format("%Y-%m-%d %H:%M:%S UTC").to_string();
let time_local = Local::now().format("%Y-%m-%d %H:%M:%S %Z").to_string();
let mut context = Context::new();
context.insert("total_schemas", &test_cases.len());
context.insert("total_files", &groups.len());
context.insert("with_content", &with_content);
context.insert("without_content", &without_content);
context.insert("commit_hash", &commit_hash);
context.insert("is_dirty", &is_dirty);
context.insert("time_utc", &time_utc);
context.insert("time_local", &time_local);
context.insert("groups", &groups);
let html = tera
.render("test_index.html", &context)
.expect("Failed to render template");
fs::write(&index_path, html).expect("Failed to write index.html");
println!("Generated index.html at {:?}", index_path);
}
#[test]
fn test_all_json_schema_test_suite_schemas() {
let fixtures = fixtures_dir();
@@ -235,6 +477,9 @@ fn test_all_json_schema_test_suite_schemas() {
failed_list.len()
);
}
// Generate index.html with links to all outputs
generate_index_html(&test_cases);
}
/// Test a single schema file - useful for debugging specific failures