Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions crates/prek/src/hooks/builtin_hooks/check_json5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ use std::path::Path;
use crate::hook::Hook;
use crate::hooks::HookOutput;
use crate::hooks::pre_commit_hooks::check_json::JsonDuplicateKeyChecker;
use crate::hooks::run_concurrent_file_checks;
use crate::run::INTERNAL_CONCURRENCY;
use crate::hooks::pre_commit_hooks::run_blocking_file_checks;

pub(crate) async fn check_json5(hook: &Hook, filenames: &[&Path]) -> anyhow::Result<HookOutput> {
run_concurrent_file_checks(
filenames.iter().copied(),
*INTERNAL_CONCURRENCY,
|filename| check_file(hook.project().relative_path(), filename),
)
.await
run_blocking_file_checks(hook.project().relative_path(), &[], filenames, check_file).await
}

async fn check_file(file_base: &Path, filename: &Path) -> anyhow::Result<HookOutput> {
let file_path = file_base.join(filename);
let content = fs_err::tokio::read_to_string(file_path).await?;
fn check_file(file_path: &Path, display_path: &Path) -> anyhow::Result<HookOutput> {
let content = fs_err::read_to_string(file_path)?;
if content.is_empty() {
return Ok(HookOutput::unchanged(0, Vec::new()));
}

match json5::from_str::<JsonDuplicateKeyChecker>(&content) {
Ok(_) => Ok(HookOutput::unchanged(0, Vec::new())),
Err(e) => {
let error_message = format!("{}: Failed to json5 decode ({})\n", filename.display(), e);
let error_message = format!(
"{}: Failed to json5 decode ({})\n",
display_path.display(),
e
);
Ok(HookOutput::unchanged(1, error_message.into_bytes()))
}
}
Expand All @@ -37,18 +34,18 @@ mod tests {
use std::path::PathBuf;
use tempfile::tempdir;

async fn create_test_file(
fn create_test_file(
dir: &tempfile::TempDir,
name: &str,
content: &[u8],
) -> anyhow::Result<PathBuf> {
let file_path = dir.path().join(name);
fs_err::tokio::write(&file_path, content).await?;
fs_err::write(&file_path, content)?;
Ok(file_path)
}

#[tokio::test]
async fn test_valid_json5() -> anyhow::Result<()> {
#[test]
fn test_valid_json5() -> anyhow::Result<()> {
let dir = tempdir()?;
let content = indoc::indoc! {r#"
{
Expand All @@ -66,16 +63,16 @@ mod tests {
backwardsCompatible: "with JSON",
}
"#};
let file_path = create_test_file(&dir, "valid.json5", content.as_bytes()).await?;
let result = check_file(dir.path(), &file_path).await?;
let file_path = create_test_file(&dir, "valid.json5", content.as_bytes())?;
let result = check_file(&file_path, &file_path)?;
assert_eq!(result.exit_status, 0);
assert_eq!(result.output, b"");

Ok(())
}

#[tokio::test]
async fn test_duplicate_keys() -> anyhow::Result<()> {
#[test]
fn test_duplicate_keys() -> anyhow::Result<()> {
// JSON5 warns duplicate names are unpredictable; implementations may error or accept.
// Our custom deserializer rejects duplicates.
let dir = tempdir()?;
Expand All @@ -86,19 +83,19 @@ mod tests {
key: "value3",
}
"#};
let file_path = create_test_file(&dir, "duplicate.json5", content.as_bytes()).await?;
let result = check_file(dir.path(), &file_path).await?;
let file_path = create_test_file(&dir, "duplicate.json5", content.as_bytes())?;
let result = check_file(&file_path, &file_path)?;
assert_eq!(result.exit_status, 1);
assert!(String::from_utf8_lossy(&result.output).contains("duplicate key"));

Ok(())
}

#[tokio::test]
async fn test_invalid_json5() -> anyhow::Result<()> {
#[test]
fn test_invalid_json5() -> anyhow::Result<()> {
let dir = tempdir()?;
let file_path = create_test_file(&dir, "invalid.json5", b"{ key: 'value' ").await?;
let result = check_file(dir.path(), &file_path).await?;
let file_path = create_test_file(&dir, "invalid.json5", b"{ key: 'value' ")?;
let result = check_file(&file_path, &file_path)?;
assert_eq!(result.exit_status, 1);
assert_ne!(result.output, b"");

Expand Down
67 changes: 31 additions & 36 deletions crates/prek/src/hooks/builtin_hooks/check_jsonc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use clap::Parser;
use crate::hook::Hook;
use crate::hooks::HookOutput;
use crate::hooks::pre_commit_hooks::check_json::JsonDuplicateKeyChecker;
use crate::hooks::pre_commit_hooks::parse_hook_args;
use crate::hooks::run_concurrent_file_checks;
use crate::run::INTERNAL_CONCURRENCY;
use crate::hooks::pre_commit_hooks::{parse_hook_args, run_blocking_file_checks};

#[derive(Parser)]
#[command(disable_help_subcommand = true)]
Expand All @@ -21,27 +19,23 @@ pub(crate) struct Args {

pub(crate) async fn check_jsonc(hook: &Hook, filenames: &[&Path]) -> anyhow::Result<HookOutput> {
let args = parse_hook_args::<Args>(hook)?;
run_concurrent_file_checks(
filenames.iter().copied(),
*INTERNAL_CONCURRENCY,
|filename| {
check_file(
hook.project().relative_path(),
filename,
args.allow_trailing_commas,
)
run_blocking_file_checks(
hook.project().relative_path(),
&[],
filenames,
move |file_path, display_path| {
check_file(file_path, display_path, args.allow_trailing_commas)
},
)
.await
}

async fn check_file(
file_base: &Path,
filename: &Path,
fn check_file(
file_path: &Path,
display_path: &Path,
allow_trailing_commas: bool,
) -> anyhow::Result<HookOutput> {
let file_path = file_base.join(filename);
let content = fs_err::tokio::read_to_string(file_path).await?;
let content = fs_err::read_to_string(file_path)?;
if content.is_empty() {
return Ok(HookOutput::unchanged(0, Vec::new()));
}
Expand All @@ -61,7 +55,8 @@ async fn check_file(
match jsonc_parser::parse_to_serde_value::<JsonDuplicateKeyChecker>(&content, &options) {
Ok(_) => Ok(HookOutput::unchanged(0, Vec::new())),
Err(e) => {
let error_message = format!("{}: Failed to jsonc decode ({e})\n", filename.display());
let error_message =
format!("{}: Failed to jsonc decode ({e})\n", display_path.display());
Ok(HookOutput::unchanged(1, error_message.into_bytes()))
}
}
Expand All @@ -73,18 +68,18 @@ mod tests {
use std::path::PathBuf;
use tempfile::tempdir;

async fn create_test_file(
fn create_test_file(
dir: &tempfile::TempDir,
name: &str,
content: &[u8],
) -> anyhow::Result<PathBuf> {
let file_path = dir.path().join(name);
fs_err::tokio::write(&file_path, content).await?;
fs_err::write(&file_path, content)?;
Ok(file_path)
}

#[tokio::test]
async fn test_valid_jsonc() -> anyhow::Result<()> {
#[test]
fn test_valid_jsonc() -> anyhow::Result<()> {
let dir = tempdir()?;
let content = indoc::indoc! {r#"
{
Expand All @@ -101,50 +96,50 @@ mod tests {
"objects": [{"key": 1}, {"key": 2}]
}
"#};
let file_path = create_test_file(&dir, "valid.jsonc", content.as_bytes()).await?;
let result = check_file(dir.path(), &file_path, false).await?;
let file_path = create_test_file(&dir, "valid.jsonc", content.as_bytes())?;
let result = check_file(&file_path, &file_path, false)?;
assert_eq!(result.exit_status, 0);
assert_eq!(result.output, b"");

Ok(())
}

#[tokio::test]
async fn test_valid_jsonc_trailing_comma() -> anyhow::Result<()> {
#[test]
fn test_valid_jsonc_trailing_comma() -> anyhow::Result<()> {
let dir = tempdir()?;
let content = indoc::indoc! {r#"
{
"array": [1,2,3,],
"object": {"k": "v",}
}
"#};
let file_path = create_test_file(&dir, "valid.jsonc", content.as_bytes()).await?;
let result = check_file(dir.path(), &file_path, true).await?;
let file_path = create_test_file(&dir, "valid.jsonc", content.as_bytes())?;
let result = check_file(&file_path, &file_path, true)?;
assert_eq!(result.exit_status, 0);
assert_eq!(result.output, b"");

Ok(())
}

#[tokio::test]
async fn test_duplicate_keys() -> anyhow::Result<()> {
#[test]
fn test_duplicate_keys() -> anyhow::Result<()> {
let dir = tempdir()?;
for content in [
r#"{"key": 1, /* comment */ "key": 2}"#,
r#"[{"nested": {"key": 1, "key": 2}}]"#,
r#"{"key": 1, "\u006bey": 2}"#,
] {
let file_path = create_test_file(&dir, "duplicate.jsonc", content.as_bytes()).await?;
let result = check_file(dir.path(), &file_path, true).await?;
let file_path = create_test_file(&dir, "duplicate.jsonc", content.as_bytes())?;
let result = check_file(&file_path, &file_path, true)?;
assert_eq!(result.exit_status, 1, "input: {content:?}");
assert!(String::from_utf8_lossy(&result.output).contains("duplicate key"));
}

Ok(())
}

#[tokio::test]
async fn test_invalid_jsonc() -> anyhow::Result<()> {
#[test]
fn test_invalid_jsonc() -> anyhow::Result<()> {
let dir = tempdir()?;
for content in [
r#"{key: "value"}"#,
Expand All @@ -167,8 +162,8 @@ mod tests {
"{} /* unterminated",
"# comment\n{}",
] {
let file_path = create_test_file(&dir, "invalid.jsonc", content.as_bytes()).await?;
let result = check_file(dir.path(), &file_path, true).await?;
let file_path = create_test_file(&dir, "invalid.jsonc", content.as_bytes())?;
let result = check_file(&file_path, &file_path, true)?;
assert_eq!(result.exit_status, 1, "input: {content:?}");
}

Expand Down
Loading
Loading
⌂ 换网址