From 924aeba69c61109f5f1db68fb2b66cdd72a6432c Mon Sep 17 00:00:00 2001 From: psteinroe Date: Mon, 27 Oct 2025 12:03:46 +0100 Subject: [PATCH 1/6] chore(cli): add snapshot tests for reporters --- .gitignore | 2 +- Cargo.lock | 21 +---- crates/pgt_cli/Cargo.toml | 2 +- crates/pgt_cli/tests/assert_cmd.rs | 56 ++++++++++-- crates/pgt_cli/tests/commands/check.rs | 88 +++++++++++++++---- ...heck__check_default_reporter_snapshot.snap | 9 ++ ...check__check_github_reporter_snapshot.snap | 8 ++ ...check__check_gitlab_reporter_snapshot.snap | 21 +++++ ..._check__check_junit_reporter_snapshot.snap | 15 ++++ crates/pgt_cli/tests/main.rs | 52 ----------- .../assert_cmd__cli_check_command.snap | 9 ++ 11 files changed, 186 insertions(+), 97 deletions(-) create mode 100644 crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap create mode 100644 crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap create mode 100644 crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap create mode 100644 crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap create mode 100644 crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap diff --git a/.gitignore b/.gitignore index 94835bc44..37103117b 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,4 @@ squawk/ # Auto generated treesitter files crates/pgt_treesitter_grammar/src/grammar.json crates/pgt_treesitter_grammar/src/node-types.json -crates/pgt_treesitter_grammar/src/parser.c \ No newline at end of file +crates/pgt_treesitter_grammar/src/parser.c diff --git a/Cargo.lock b/Cargo.lock index 8d488c4af..4f7434f16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1408,15 +1408,6 @@ dependencies = [ "miniz_oxide", ] -[[package]] -name = "float-cmp" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8" -dependencies = [ - "num-traits", -] - [[package]] name = "flume" version = "0.11.1" @@ -2018,6 +2009,7 @@ dependencies = [ "linked-hash-map", "once_cell", "pin-project", + "serde", "similar", ] @@ -2393,12 +2385,6 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "normalize-line-endings" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" - [[package]] name = "ntest" version = "0.9.3" @@ -2697,6 +2683,7 @@ dependencies = [ "crossbeam", "dashmap 5.5.3", "hdrhistogram", + "insta", "libc", "mimalloc", "path-absolutize", @@ -2709,7 +2696,6 @@ dependencies = [ "pgt_lsp", "pgt_text_edit", "pgt_workspace", - "predicates", "quick-junit", "rayon", "rustc-hash 2.1.0", @@ -3337,10 +3323,7 @@ checksum = "a5d19ee57562043d37e82899fade9a22ebab7be9cef5026b07fda9cdd4293573" dependencies = [ "anstyle", "difflib", - "float-cmp", - "normalize-line-endings", "predicates-core", - "regex", ] [[package]] diff --git a/crates/pgt_cli/Cargo.toml b/crates/pgt_cli/Cargo.toml index fb20036fe..7d2cd2efd 100644 --- a/crates/pgt_cli/Cargo.toml +++ b/crates/pgt_cli/Cargo.toml @@ -53,7 +53,7 @@ tikv-jemallocator = "0.6.0" [dev-dependencies] assert_cmd = "2.0.16" -predicates = "3.1.3" +insta = { version = "1.40.0", features = ["yaml"] } [lib] doctest = false diff --git a/crates/pgt_cli/tests/assert_cmd.rs b/crates/pgt_cli/tests/assert_cmd.rs index a7ddc17fb..3e6110ecf 100644 --- a/crates/pgt_cli/tests/assert_cmd.rs +++ b/crates/pgt_cli/tests/assert_cmd.rs @@ -1,16 +1,58 @@ use std::path::PathBuf; use assert_cmd::Command; -use predicates::prelude::*; +use insta::assert_snapshot; + +const BIN: &str = "postgres-language-server"; #[test] fn test_cli_check_command() { - let mut cmd = Command::cargo_bin("postgrestools").unwrap(); + let output = Command::cargo_bin(BIN) + .unwrap() + .args([ + "check", + PathBuf::from("tests/fixtures/test.sql").to_str().unwrap(), + ]) + .output() + .unwrap(); + + assert!(!output.status.success(), "command unexpectedly succeeded"); - let test_sql_path = PathBuf::from("tests/fixtures/test.sql"); + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); + assert_snapshot!(normalize_durations(&stdout)); +} - cmd.args(["check", test_sql_path.to_str().unwrap()]) - .assert() - .failure() - .stdout(predicate::str::contains("Found 1 error")); +fn normalize_durations(input: &str) -> String { + let mut content = input.to_owned(); + let mut search_start = 0; + while let Some(relative) = content[search_start..].find(" in ") { + let start = search_start + relative + 4; + if let Some(end_rel) = content[start..].find('.') { + let end = start + end_rel; + let slice = &content[start..end]; + if slice.chars().any(|c| c.is_ascii_digit()) { + content.replace_range(start..end, ""); + search_start = start + "".len() + 1; + continue; + } + search_start = end + 1; + } else { + break; + } + } + let mut time_search = 0; + while let Some(relative) = content[time_search..].find("time=\"") { + let start = time_search + relative + 6; + if let Some(end_rel) = content[start..].find('"') { + let end = start + end_rel; + let slice = &content[start..end]; + if slice.chars().any(|c| c.is_ascii_digit()) { + content.replace_range(start..end, ""); + } + time_search = end + 1; + } else { + break; + } + } + content } diff --git a/crates/pgt_cli/tests/commands/check.rs b/crates/pgt_cli/tests/commands/check.rs index ce0de03da..c5f5eebd1 100644 --- a/crates/pgt_cli/tests/commands/check.rs +++ b/crates/pgt_cli/tests/commands/check.rs @@ -1,24 +1,78 @@ -use bpaf::Args; -use std::path::Path; +use assert_cmd::Command; +use insta::assert_snapshot; +use std::path::PathBuf; -use crate::run_cli; -use pgt_console::BufferConsole; -use pgt_fs::MemoryFileSystem; -use pgt_workspace::DynRef; +const BIN: &str = "postgres-language-server"; + +fn run_check(args: &[&str]) -> String { + let mut cmd = Command::cargo_bin(BIN).expect("binary not built"); + let mut full_args = vec!["check".to_string()]; + full_args.extend(args.iter().map(|s| s.to_string())); + let test_sql = PathBuf::from("tests/fixtures/test.sql"); + full_args.push(test_sql.to_str().unwrap().to_string()); + + let output = cmd.args(&full_args).output().expect("failed to run CLI"); + assert!( + !output.status.success(), + "command unexpectedly succeeded: {:?}", + output.status + ); + + normalize_durations(&String::from_utf8_lossy(&output.stdout)) +} + +fn normalize_durations(input: &str) -> String { + let mut content = input.to_owned(); + + let mut search_start = 0; + while let Some(relative) = content[search_start..].find(" in ") { + let start = search_start + relative + 4; + if let Some(end_rel) = content[start..].find('.') { + let end = start + end_rel; + if content[start..end].chars().any(|c| c.is_ascii_digit()) { + content.replace_range(start..end, ""); + search_start = start + "".len() + 1; + continue; + } + search_start = end + 1; + } else { + break; + } + } + + let mut time_search = 0; + while let Some(relative) = content[time_search..].find("time=\"") { + let start = time_search + relative + 6; + if let Some(end_rel) = content[start..].find('"') { + let end = start + end_rel; + if content[start..end].chars().any(|c| c.is_ascii_digit()) { + content.replace_range(start..end, ""); + } + time_search = end + 1; + } else { + break; + } + } + + content +} #[test] -fn syntax_error() { - let mut fs = MemoryFileSystem::default(); - let mut console = BufferConsole::default(); +fn check_default_reporter_snapshot() { + assert_snapshot!(run_check(&[])); +} - let file_path = Path::new("test.sql"); - fs.insert(file_path.into(), "select 1".as_bytes()); +#[test] +fn check_github_reporter_snapshot() { + assert_snapshot!(run_check(&["--reporter", "github"])); +} - let result = run_cli( - DynRef::Borrowed(&mut fs), - &mut console, - Args::from([("check"), file_path.as_os_str().to_str().unwrap()].as_slice()), - ); +#[test] +fn check_gitlab_reporter_snapshot() { + assert_snapshot!(run_check(&["--reporter", "gitlab"])); +} - assert!(result.is_ok(), "run_cli returned {result:?}"); +#[test] +fn check_junit_reporter_snapshot() { + assert_snapshot!(run_check(&["--reporter", "junit"])); } diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap new file mode 100644 index 000000000..d840ec807 --- /dev/null +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap @@ -0,0 +1,9 @@ +--- +source: crates/pgt_cli/tests/commands/check.rs +expression: "run_check(&[])" +snapshot_kind: text +--- +Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. + +Checked 1 file in . No fixes applied. +Found 1 error. diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap new file mode 100644 index 000000000..1de08e199 --- /dev/null +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap @@ -0,0 +1,8 @@ +--- +source: crates/pgt_cli/tests/commands/check.rs +expression: "run_check(&[\"--reporter\", \"github\"])" +snapshot_kind: text +--- +Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. + +::error title=syntax,file=tests/fixtures/test.sql,line=1,endLine=1,col=1,endColumn=35::Invalid statement: syntax error at or near "tqjable" diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap new file mode 100644 index 000000000..40b08a54c --- /dev/null +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap @@ -0,0 +1,21 @@ +--- +source: crates/pgt_cli/tests/commands/check.rs +expression: "run_check(&[\"--reporter\", \"gitlab\"])" +snapshot_kind: text +--- +Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. + +[ + { + "description": "Invalid statement: syntax error at or near \"tqjable\"", + "check_name": "syntax", + "fingerprint": "15806099827984337215", + "severity": "critical", + "location": { + "path": "tests/fixtures/test.sql", + "lines": { + "begin": 1 + } + } + } +] diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap new file mode 100644 index 000000000..55dcf5caa --- /dev/null +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap @@ -0,0 +1,15 @@ +--- +source: crates/pgt_cli/tests/commands/check.rs +expression: "run_check(&[\"--reporter\", \"junit\"])" +snapshot_kind: text +--- +Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. + + + + + + line 0, col 0, Invalid statement: syntax error at or near "tqjable" + + + diff --git a/crates/pgt_cli/tests/main.rs b/crates/pgt_cli/tests/main.rs index 4ab061727..f3d44688d 100644 --- a/crates/pgt_cli/tests/main.rs +++ b/crates/pgt_cli/tests/main.rs @@ -1,53 +1 @@ mod commands; - -use bpaf::ParseFailure; -use pgt_cli::{CliDiagnostic, CliSession, pgt_command}; -use pgt_console::{Console, ConsoleExt, markup}; -use pgt_fs::FileSystem; -use pgt_workspace::{App, DynRef}; - -/// Create an [App] instance using the provided [FileSystem] and [Console] -/// instance, and using an in-process "remote" instance of the workspace -pub(crate) fn run_cli<'app>( - fs: DynRef<'app, dyn FileSystem>, - console: &'app mut dyn Console, - args: bpaf::Args, -) -> Result<(), CliDiagnostic> { - use pgt_cli::SocketTransport; - use pgt_lsp::ServerFactory; - use pgt_workspace::{WorkspaceRef, workspace}; - use tokio::{ - io::{duplex, split}, - runtime::Runtime, - }; - - let factory = ServerFactory::default(); - let connection = factory.create(None); - - let runtime = Runtime::new().expect("failed to create runtime"); - - let (client, server) = duplex(4096); - let (stdin, stdout) = split(server); - runtime.spawn(connection.accept(stdin, stdout)); - - let (client_read, client_write) = split(client); - let transport = SocketTransport::open(runtime, client_read, client_write); - - let workspace = workspace::client(transport).unwrap(); - let app = App::new(fs, console, WorkspaceRef::Owned(workspace)); - - let mut session = CliSession { app }; - let command = pgt_command().run_inner(args); - match command { - Ok(command) => session.run(command), - Err(failure) => { - if let ParseFailure::Stdout(help, _) = &failure { - let console = &mut session.app.console; - console.log(markup! {{help.to_string()}}); - Ok(()) - } else { - Err(CliDiagnostic::parse_error_bpaf(failure)) - } - } - } -} diff --git a/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap b/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap new file mode 100644 index 000000000..a0ff570e4 --- /dev/null +++ b/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap @@ -0,0 +1,9 @@ +--- +source: crates/pgt_cli/tests/assert_cmd.rs +expression: normalize_durations(&stdout) +snapshot_kind: text +--- +Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. + +Checked 1 file in . No fixes applied. +Found 1 error. From ed4db5239d3a82e8c8c2aea81a7a5a2c4fee188a Mon Sep 17 00:00:00 2001 From: psteinroe Date: Mon, 27 Oct 2025 12:53:06 +0100 Subject: [PATCH 2/6] progress --- .gitignore | 2 + crates/pgt_cli/tests/assert_cmd.rs | 28 +++- crates/pgt_cli/tests/commands/check.rs | 121 +++++++++++++++--- ...heck__check_default_reporter_snapshot.snap | 19 ++- ...k__check_directory_traversal_snapshot.snap | 31 +++++ ...check__check_github_reporter_snapshot.snap | 10 +- ...check__check_gitlab_reporter_snapshot.snap | 10 +- ..._check__check_junit_reporter_snapshot.snap | 10 +- ...commands__check__check_stdin_snapshot.snap | 9 ++ .../fixtures/postgres-language-server.jsonc | 17 +++ .../tests/fixtures/traversal/another_bad.sql | 1 + .../pgt_cli/tests/fixtures/traversal/bad.sql | 1 + .../traversal/postgres-language-server.jsonc | 9 ++ .../assert_cmd__cli_check_command.snap | 19 ++- 14 files changed, 252 insertions(+), 35 deletions(-) create mode 100644 crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap create mode 100644 crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap create mode 100644 crates/pgt_cli/tests/fixtures/postgres-language-server.jsonc create mode 100644 crates/pgt_cli/tests/fixtures/traversal/another_bad.sql create mode 100644 crates/pgt_cli/tests/fixtures/traversal/bad.sql create mode 100644 crates/pgt_cli/tests/fixtures/traversal/postgres-language-server.jsonc diff --git a/.gitignore b/.gitignore index 37103117b..f32da18cb 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ squawk/ crates/pgt_treesitter_grammar/src/grammar.json crates/pgt_treesitter_grammar/src/node-types.json crates/pgt_treesitter_grammar/src/parser.c +crates/pgt_treesitter_grammar/src/parser.c.codex-session-id +.codex-session-id diff --git a/crates/pgt_cli/tests/assert_cmd.rs b/crates/pgt_cli/tests/assert_cmd.rs index 3e6110ecf..87061a5c9 100644 --- a/crates/pgt_cli/tests/assert_cmd.rs +++ b/crates/pgt_cli/tests/assert_cmd.rs @@ -2,15 +2,23 @@ use std::path::PathBuf; use assert_cmd::Command; use insta::assert_snapshot; +use std::process::ExitStatus; const BIN: &str = "postgres-language-server"; +const CONFIG_PATH: &str = "tests/fixtures/postgres-language-server.jsonc"; #[test] +#[cfg_attr( + not(target_os = "linux"), + ignore = "snapshot expectations only validated on Linux" +)] fn test_cli_check_command() { let output = Command::cargo_bin(BIN) .unwrap() .args([ "check", + "--config-path", + CONFIG_PATH, PathBuf::from("tests/fixtures/test.sql").to_str().unwrap(), ]) .output() @@ -18,8 +26,24 @@ fn test_cli_check_command() { assert!(!output.status.success(), "command unexpectedly succeeded"); - let stdout = String::from_utf8_lossy(&output.stdout).to_string(); - assert_snapshot!(normalize_durations(&stdout)); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert_snapshot!(normalize_output(output.status, &stdout, &stderr)); +} + +fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { + let normalized_stdout = normalize_durations(stdout); + let normalized_stderr = normalize_durations(stderr); + let status_label = if status.success() { + "success" + } else { + "failure" + }; + format!( + "status: {status_label}\nstdout:\n{}\nstderr:\n{}\n", + normalized_stdout.trim_end(), + normalized_stderr.trim_end() + ) } fn normalize_durations(input: &str) -> String { diff --git a/crates/pgt_cli/tests/commands/check.rs b/crates/pgt_cli/tests/commands/check.rs index c5f5eebd1..b63548297 100644 --- a/crates/pgt_cli/tests/commands/check.rs +++ b/crates/pgt_cli/tests/commands/check.rs @@ -1,24 +1,33 @@ use assert_cmd::Command; use insta::assert_snapshot; -use std::path::PathBuf; +use std::path::Path; +use std::process::ExitStatus; const BIN: &str = "postgres-language-server"; +const CONFIG_PATH: &str = "tests/fixtures/postgres-language-server.jsonc"; fn run_check(args: &[&str]) -> String { + let mut full_args = vec!["check", "--config-path", CONFIG_PATH]; + full_args.extend_from_slice(args); + run_check_with(&full_args, None, None) +} + +fn run_check_with(args: &[&str], stdin: Option<&str>, cwd: Option<&Path>) -> String { let mut cmd = Command::cargo_bin(BIN).expect("binary not built"); - let mut full_args = vec!["check".to_string()]; - full_args.extend(args.iter().map(|s| s.to_string())); - let test_sql = PathBuf::from("tests/fixtures/test.sql"); - full_args.push(test_sql.to_str().unwrap().to_string()); - - let output = cmd.args(&full_args).output().expect("failed to run CLI"); - assert!( - !output.status.success(), - "command unexpectedly succeeded: {:?}", - output.status - ); - - normalize_durations(&String::from_utf8_lossy(&output.stdout)) + if let Some(dir) = cwd { + cmd.current_dir(dir); + } + if let Some(input) = stdin { + cmd.write_stdin(input); + } + + let output = cmd.args(args).output().expect("failed to run CLI"); + + normalize_output( + output.status, + &String::from_utf8_lossy(&output.stdout), + &String::from_utf8_lossy(&output.stderr), + ) } fn normalize_durations(input: &str) -> String { @@ -57,22 +66,98 @@ fn normalize_durations(input: &str) -> String { content } +fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { + let normalized_stdout = normalize_durations(stdout); + let normalized_stderr = normalize_durations(stderr); + let status_label = if status.success() { + "success" + } else { + "failure" + }; + format!( + "status: {status_label}\nstdout:\n{}\nstderr:\n{}\n", + normalized_stdout.trim_end(), + normalized_stderr.trim_end() + ) +} + #[test] +#[cfg_attr( + not(target_os = "linux"), + ignore = "snapshot expectations only validated on Linux" +)] fn check_default_reporter_snapshot() { - assert_snapshot!(run_check(&[])); + assert_snapshot!(run_check(&["tests/fixtures/test.sql"])); } #[test] +#[cfg_attr( + not(target_os = "linux"), + ignore = "snapshot expectations only validated on Linux" +)] fn check_github_reporter_snapshot() { - assert_snapshot!(run_check(&["--reporter", "github"])); + assert_snapshot!(run_check(&[ + "--reporter", + "github", + "tests/fixtures/test.sql" + ])); } #[test] +#[cfg_attr( + not(target_os = "linux"), + ignore = "snapshot expectations only validated on Linux" +)] fn check_gitlab_reporter_snapshot() { - assert_snapshot!(run_check(&["--reporter", "gitlab"])); + assert_snapshot!(run_check(&[ + "--reporter", + "gitlab", + "tests/fixtures/test.sql" + ])); } #[test] +#[cfg_attr( + not(target_os = "linux"), + ignore = "snapshot expectations only validated on Linux" +)] fn check_junit_reporter_snapshot() { - assert_snapshot!(run_check(&["--reporter", "junit"])); + assert_snapshot!(run_check(&[ + "--reporter", + "junit", + "tests/fixtures/test.sql" + ])); +} + +#[test] +#[cfg_attr( + not(target_os = "linux"), + ignore = "snapshot expectations only validated on Linux" +)] +fn check_stdin_snapshot() { + assert_snapshot!(run_check_with( + &[ + "check", + "--config-path", + CONFIG_PATH, + "--stdin-file-path", + "virtual.sql" + ], + Some("alter tqjable stdin drop column id;\n"), + None + )); +} + +#[test] +#[cfg_attr( + not(target_os = "linux"), + ignore = "snapshot expectations only validated on Linux" +)] +fn check_directory_traversal_snapshot() { + let project_dir = Path::new("tests/fixtures/traversal"); + assert_snapshot!(run_check_with( + &["check", "--diagnostic-level", "info", "."], + None, + Some(project_dir) + )); } diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap index d840ec807..e3f582a19 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap @@ -1,9 +1,22 @@ --- source: crates/pgt_cli/tests/commands/check.rs -expression: "run_check(&[])" +expression: "run_check(&[\"tests/fixtures/test.sql\"])" snapshot_kind: text --- -Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. - +status: failure +stdout: Checked 1 file in . No fixes applied. Found 1 error. +stderr: +tests/fixtures/test.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Invalid statement: syntax error at or near "tqjable" + + > 1 │ alter tqjable test drop column id; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 2 │ + + +check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks. diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap new file mode 100644 index 000000000..877be4dc4 --- /dev/null +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap @@ -0,0 +1,31 @@ +--- +source: crates/pgt_cli/tests/commands/check.rs +expression: "run_check_with(&[\"check\", \"--diagnostic-level\", \"info\", \".\"], None,\nSome(project_dir))" +snapshot_kind: text +--- +status: failure +stdout: +Checked 2 files in . No fixes applied. +Found 2 errors. +stderr: +./bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Invalid statement: syntax error at or near "tqjable" + + > 1 │ alter tqjable bad drop column id; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 2 │ + + +./another_bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Invalid statement: syntax error at or near "tqjable" + + > 1 │ alter tqjable another drop column id; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 2 │ + + +check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks. diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap index 1de08e199..7a2b7e721 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap @@ -1,8 +1,12 @@ --- source: crates/pgt_cli/tests/commands/check.rs -expression: "run_check(&[\"--reporter\", \"github\"])" +expression: "run_check(&[\"--reporter\", \"github\", \"tests/fixtures/test.sql\"])" snapshot_kind: text --- -Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. - +status: failure +stdout: ::error title=syntax,file=tests/fixtures/test.sql,line=1,endLine=1,col=1,endColumn=35::Invalid statement: syntax error at or near "tqjable" +stderr: +check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks. diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap index 40b08a54c..80ccf17b9 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap @@ -1,10 +1,10 @@ --- source: crates/pgt_cli/tests/commands/check.rs -expression: "run_check(&[\"--reporter\", \"gitlab\"])" +expression: "run_check(&[\"--reporter\", \"gitlab\", \"tests/fixtures/test.sql\"])" snapshot_kind: text --- -Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. - +status: failure +stdout: [ { "description": "Invalid statement: syntax error at or near \"tqjable\"", @@ -19,3 +19,7 @@ Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Ple } } ] +stderr: +check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks. diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap index 55dcf5caa..c11791d65 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap @@ -1,10 +1,10 @@ --- source: crates/pgt_cli/tests/commands/check.rs -expression: "run_check(&[\"--reporter\", \"junit\"])" +expression: "run_check(&[\"--reporter\", \"junit\", \"tests/fixtures/test.sql\"])" snapshot_kind: text --- -Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. - +status: failure +stdout: @@ -13,3 +13,7 @@ Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Ple +stderr: +check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks. diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap new file mode 100644 index 000000000..b62bbf516 --- /dev/null +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap @@ -0,0 +1,9 @@ +--- +source: crates/pgt_cli/tests/commands/check.rs +expression: "run_check_with(&[\"check\", \"--config-path\", CONFIG_PATH, \"--stdin-file-path\",\n\"virtual.sql\"], Some(\"alter tqjable stdin drop column id;\\n\"), None)" +snapshot_kind: text +--- +status: success +stdout: +alter tqjable stdin drop column id; +stderr: diff --git a/crates/pgt_cli/tests/fixtures/postgres-language-server.jsonc b/crates/pgt_cli/tests/fixtures/postgres-language-server.jsonc new file mode 100644 index 000000000..95c6dfa04 --- /dev/null +++ b/crates/pgt_cli/tests/fixtures/postgres-language-server.jsonc @@ -0,0 +1,17 @@ +{ + "$schema": "https://pg-language-server.com/schema/postgres-language-server.schema.json", + "vcs": { + "enabled": false, + "clientKind": "git", + "useIgnoreFile": false + }, + "files": { + "ignore": [] + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true + } + } +} diff --git a/crates/pgt_cli/tests/fixtures/traversal/another_bad.sql b/crates/pgt_cli/tests/fixtures/traversal/another_bad.sql new file mode 100644 index 000000000..9dae41f29 --- /dev/null +++ b/crates/pgt_cli/tests/fixtures/traversal/another_bad.sql @@ -0,0 +1 @@ +alter tqjable another drop column id; diff --git a/crates/pgt_cli/tests/fixtures/traversal/bad.sql b/crates/pgt_cli/tests/fixtures/traversal/bad.sql new file mode 100644 index 000000000..6cca86c02 --- /dev/null +++ b/crates/pgt_cli/tests/fixtures/traversal/bad.sql @@ -0,0 +1 @@ +alter tqjable bad drop column id; diff --git a/crates/pgt_cli/tests/fixtures/traversal/postgres-language-server.jsonc b/crates/pgt_cli/tests/fixtures/traversal/postgres-language-server.jsonc new file mode 100644 index 000000000..771c82670 --- /dev/null +++ b/crates/pgt_cli/tests/fixtures/traversal/postgres-language-server.jsonc @@ -0,0 +1,9 @@ +{ + "$schema": "https://pg-language-server.com/schema/postgres-language-server.schema.json", + "linter": { + "enabled": true, + "rules": { + "recommended": true + } + } +} diff --git a/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap b/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap index a0ff570e4..3bce5e37e 100644 --- a/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap +++ b/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap @@ -1,9 +1,22 @@ --- source: crates/pgt_cli/tests/assert_cmd.rs -expression: normalize_durations(&stdout) +expression: "normalize_output(output.status, &stdout, &stderr)" snapshot_kind: text --- -Warning: You are using the deprecated config filename 'postgrestools.jsonc'. Please rename it to 'postgres-language-server.jsonc'. Support for the old filename will be removed in a future version. - +status: failure +stdout: Checked 1 file in . No fixes applied. Found 1 error. +stderr: +tests/fixtures/test.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Invalid statement: syntax error at or near "tqjable" + + > 1 │ alter tqjable test drop column id; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 2 │ + + +check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks. From 1f3a3a08ff424039e41423418a258340ca4f6abd Mon Sep 17 00:00:00 2001 From: psteinroe Date: Mon, 27 Oct 2025 13:12:03 +0100 Subject: [PATCH 3/6] progress --- crates/pgt_cli/tests/assert_cmd.rs | 4 ++-- crates/pgt_cli/tests/commands/check.rs | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/pgt_cli/tests/assert_cmd.rs b/crates/pgt_cli/tests/assert_cmd.rs index 87061a5c9..cbbe496df 100644 --- a/crates/pgt_cli/tests/assert_cmd.rs +++ b/crates/pgt_cli/tests/assert_cmd.rs @@ -9,8 +9,8 @@ const CONFIG_PATH: &str = "tests/fixtures/postgres-language-server.jsonc"; #[test] #[cfg_attr( - not(target_os = "linux"), - ignore = "snapshot expectations only validated on Linux" + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" )] fn test_cli_check_command() { let output = Command::cargo_bin(BIN) diff --git a/crates/pgt_cli/tests/commands/check.rs b/crates/pgt_cli/tests/commands/check.rs index b63548297..64fe51064 100644 --- a/crates/pgt_cli/tests/commands/check.rs +++ b/crates/pgt_cli/tests/commands/check.rs @@ -83,8 +83,8 @@ fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { #[test] #[cfg_attr( - not(target_os = "linux"), - ignore = "snapshot expectations only validated on Linux" + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" )] fn check_default_reporter_snapshot() { assert_snapshot!(run_check(&["tests/fixtures/test.sql"])); @@ -92,8 +92,8 @@ fn check_default_reporter_snapshot() { #[test] #[cfg_attr( - not(target_os = "linux"), - ignore = "snapshot expectations only validated on Linux" + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" )] fn check_github_reporter_snapshot() { assert_snapshot!(run_check(&[ @@ -105,8 +105,8 @@ fn check_github_reporter_snapshot() { #[test] #[cfg_attr( - not(target_os = "linux"), - ignore = "snapshot expectations only validated on Linux" + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" )] fn check_gitlab_reporter_snapshot() { assert_snapshot!(run_check(&[ @@ -118,8 +118,8 @@ fn check_gitlab_reporter_snapshot() { #[test] #[cfg_attr( - not(target_os = "linux"), - ignore = "snapshot expectations only validated on Linux" + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" )] fn check_junit_reporter_snapshot() { assert_snapshot!(run_check(&[ @@ -131,8 +131,8 @@ fn check_junit_reporter_snapshot() { #[test] #[cfg_attr( - not(target_os = "linux"), - ignore = "snapshot expectations only validated on Linux" + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" )] fn check_stdin_snapshot() { assert_snapshot!(run_check_with( @@ -150,8 +150,8 @@ fn check_stdin_snapshot() { #[test] #[cfg_attr( - not(target_os = "linux"), - ignore = "snapshot expectations only validated on Linux" + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" )] fn check_directory_traversal_snapshot() { let project_dir = Path::new("tests/fixtures/traversal"); From 19969515c34b2042a464f11e2355a01a19d24f0b Mon Sep 17 00:00:00 2001 From: psteinroe Date: Mon, 27 Oct 2025 13:52:38 +0100 Subject: [PATCH 4/6] progress --- crates/pgt_cli/tests/assert_cmd.rs | 57 ++++++++++++++++++- crates/pgt_cli/tests/commands/check.rs | 57 ++++++++++++++++++- ...k__check_directory_traversal_snapshot.snap | 12 ++-- 3 files changed, 118 insertions(+), 8 deletions(-) diff --git a/crates/pgt_cli/tests/assert_cmd.rs b/crates/pgt_cli/tests/assert_cmd.rs index cbbe496df..7828b9b21 100644 --- a/crates/pgt_cli/tests/assert_cmd.rs +++ b/crates/pgt_cli/tests/assert_cmd.rs @@ -33,7 +33,7 @@ fn test_cli_check_command() { fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { let normalized_stdout = normalize_durations(stdout); - let normalized_stderr = normalize_durations(stderr); + let normalized_stderr = normalize_diagnostics(stderr); let status_label = if status.success() { "success" } else { @@ -80,3 +80,58 @@ fn normalize_durations(input: &str) -> String { } content } + +fn normalize_diagnostics(input: &str) -> String { + let normalized = normalize_durations(input); + let mut lines = normalized.lines().peekable(); + let mut diagnostic_sections: Vec = Vec::new(); + let mut other_lines: Vec = Vec::new(); + + while let Some(line) = lines.next() { + if is_path_line(line) { + let mut block = String::from(line); + while let Some(&next) = lines.peek() { + if is_path_line(next) || next.starts_with("check ") { + break; + } + block.push('\n'); + block.push_str(next); + lines.next(); + } + diagnostic_sections.push(trim_trailing_newlines(block)); + } else { + other_lines.push(line.to_string()); + } + } + + diagnostic_sections.sort(); + + let mut parts = Vec::new(); + if !diagnostic_sections.is_empty() { + parts.push(diagnostic_sections.join("\n\n")); + } + + let rest = trim_trailing_newlines(other_lines.join("\n")); + if rest.trim().is_empty() { + parts.join("\n\n") + } else if parts.is_empty() { + rest + } else { + parts.push(rest); + parts.join("\n\n") + } +} + +fn is_path_line(line: &str) -> bool { + let trimmed = line.trim_start(); + (trimmed.starts_with("./") || trimmed.starts_with("tests/")) + && trimmed.contains(':') + && trimmed.contains(" syntax") +} + +fn trim_trailing_newlines(mut value: String) -> String { + while value.ends_with('\n') { + value.pop(); + } + value +} diff --git a/crates/pgt_cli/tests/commands/check.rs b/crates/pgt_cli/tests/commands/check.rs index 64fe51064..54d4d59a9 100644 --- a/crates/pgt_cli/tests/commands/check.rs +++ b/crates/pgt_cli/tests/commands/check.rs @@ -68,7 +68,7 @@ fn normalize_durations(input: &str) -> String { fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { let normalized_stdout = normalize_durations(stdout); - let normalized_stderr = normalize_durations(stderr); + let normalized_stderr = normalize_diagnostics(stderr); let status_label = if status.success() { "success" } else { @@ -81,6 +81,61 @@ fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { ) } +fn normalize_diagnostics(input: &str) -> String { + let normalized = normalize_durations(input); + let mut lines = normalized.lines().peekable(); + let mut diagnostic_sections: Vec = Vec::new(); + let mut other_lines: Vec = Vec::new(); + + while let Some(line) = lines.next() { + if is_path_line(line) { + let mut block = String::from(line); + while let Some(&next) = lines.peek() { + if is_path_line(next) || next.starts_with("check ") { + break; + } + block.push('\n'); + block.push_str(next); + lines.next(); + } + diagnostic_sections.push(trim_trailing_newlines(block)); + } else { + other_lines.push(line.to_string()); + } + } + + diagnostic_sections.sort(); + + let mut parts = Vec::new(); + if !diagnostic_sections.is_empty() { + parts.push(diagnostic_sections.join("\n\n")); + } + + let rest = trim_trailing_newlines(other_lines.join("\n")); + if rest.trim().is_empty() { + parts.join("\n\n") + } else if parts.is_empty() { + rest + } else { + parts.push(rest); + parts.join("\n\n") + } +} + +fn is_path_line(line: &str) -> bool { + let trimmed = line.trim_start(); + (trimmed.starts_with("./") || trimmed.starts_with("tests/")) + && trimmed.contains(':') + && trimmed.contains(" syntax") +} + +fn trim_trailing_newlines(mut value: String) -> String { + while value.ends_with('\n') { + value.pop(); + } + value +} + #[test] #[cfg_attr( target_os = "windows", diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap index 877be4dc4..7726d1e1d 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap +++ b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap @@ -8,21 +8,21 @@ stdout: Checked 2 files in . No fixes applied. Found 2 errors. stderr: -./bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +./another_bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Invalid statement: syntax error at or near "tqjable" - > 1 │ alter tqjable bad drop column id; - │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + > 1 │ alter tqjable another drop column id; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2 │ -./another_bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +./bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ × Invalid statement: syntax error at or near "tqjable" - > 1 │ alter tqjable another drop column id; - │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + > 1 │ alter tqjable bad drop column id; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2 │ From a825e6c206bbd3d7ddb982d0a91f94f50d266b6c Mon Sep 17 00:00:00 2001 From: psteinroe Date: Mon, 27 Oct 2025 20:52:50 +0100 Subject: [PATCH 5/6] progress --- .../{commands/check.rs => assert_check.rs} | 167 +++++++++--------- crates/pgt_cli/tests/assert_cmd.rs | 137 -------------- crates/pgt_cli/tests/commands/mod.rs | 1 - ...commands__check__check_stdin_snapshot.snap | 9 - crates/pgt_cli/tests/main.rs | 1 - ...eck__check_default_reporter_snapshot.snap} | 2 +- ...__check_directory_traversal_snapshot.snap} | 4 +- ...heck__check_github_reporter_snapshot.snap} | 2 +- ...heck__check_gitlab_reporter_snapshot.snap} | 2 +- ...check__check_junit_reporter_snapshot.snap} | 2 +- .../assert_check__check_stdin_snapshot.snap | 9 + .../assert_cmd__cli_check_command.snap | 22 --- 12 files changed, 99 insertions(+), 259 deletions(-) rename crates/pgt_cli/tests/{commands/check.rs => assert_check.rs} (95%) delete mode 100644 crates/pgt_cli/tests/assert_cmd.rs delete mode 100644 crates/pgt_cli/tests/commands/mod.rs delete mode 100644 crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap delete mode 100644 crates/pgt_cli/tests/main.rs rename crates/pgt_cli/tests/{commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap => snapshots/assert_check__check_default_reporter_snapshot.snap} (95%) rename crates/pgt_cli/tests/{commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap => snapshots/assert_check__check_directory_traversal_snapshot.snap} (89%) rename crates/pgt_cli/tests/{commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap => snapshots/assert_check__check_github_reporter_snapshot.snap} (93%) rename crates/pgt_cli/tests/{commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap => snapshots/assert_check__check_gitlab_reporter_snapshot.snap} (94%) rename crates/pgt_cli/tests/{commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap => snapshots/assert_check__check_junit_reporter_snapshot.snap} (95%) create mode 100644 crates/pgt_cli/tests/snapshots/assert_check__check_stdin_snapshot.snap delete mode 100644 crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap diff --git a/crates/pgt_cli/tests/commands/check.rs b/crates/pgt_cli/tests/assert_check.rs similarity index 95% rename from crates/pgt_cli/tests/commands/check.rs rename to crates/pgt_cli/tests/assert_check.rs index 54d4d59a9..eaf77be33 100644 --- a/crates/pgt_cli/tests/commands/check.rs +++ b/crates/pgt_cli/tests/assert_check.rs @@ -6,8 +6,88 @@ use std::process::ExitStatus; const BIN: &str = "postgres-language-server"; const CONFIG_PATH: &str = "tests/fixtures/postgres-language-server.jsonc"; +#[test] +#[cfg_attr( + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" +)] +fn check_default_reporter_snapshot() { + assert_snapshot!(run_check(&["tests/fixtures/test.sql"])); +} + +#[test] +#[cfg_attr( + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" +)] +fn check_github_reporter_snapshot() { + assert_snapshot!(run_check(&[ + "--reporter", + "github", + "tests/fixtures/test.sql" + ])); +} + +#[test] +#[cfg_attr( + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" +)] +fn check_gitlab_reporter_snapshot() { + assert_snapshot!(run_check(&[ + "--reporter", + "gitlab", + "tests/fixtures/test.sql" + ])); +} + +#[test] +#[cfg_attr( + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" +)] +fn check_junit_reporter_snapshot() { + assert_snapshot!(run_check(&[ + "--reporter", + "junit", + "tests/fixtures/test.sql" + ])); +} + +#[test] +#[cfg_attr( + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" +)] +fn check_stdin_snapshot() { + assert_snapshot!(run_check_with( + &[ + "--config-path", + CONFIG_PATH, + "--stdin-file-path", + "virtual.sql" + ], + Some("alter tqjable stdin drop column id;\n"), + None + )); +} + +#[test] +#[cfg_attr( + target_os = "windows", + ignore = "snapshot expectations only validated on unix-like platforms" +)] +fn check_directory_traversal_snapshot() { + let project_dir = Path::new("tests/fixtures/traversal"); + assert_snapshot!(run_check_with( + &["--diagnostic-level", "info", "."], + None, + Some(project_dir) + )); +} + fn run_check(args: &[&str]) -> String { - let mut full_args = vec!["check", "--config-path", CONFIG_PATH]; + let mut full_args = vec!["--config-path", CONFIG_PATH]; full_args.extend_from_slice(args); run_check_with(&full_args, None, None) } @@ -21,7 +101,9 @@ fn run_check_with(args: &[&str], stdin: Option<&str>, cwd: Option<&Path>) -> Str cmd.write_stdin(input); } - let output = cmd.args(args).output().expect("failed to run CLI"); + let mut full_args = vec!["check"]; + full_args.extend_from_slice(args); + let output = cmd.args(full_args).output().expect("failed to run CLI"); normalize_output( output.status, @@ -135,84 +217,3 @@ fn trim_trailing_newlines(mut value: String) -> String { } value } - -#[test] -#[cfg_attr( - target_os = "windows", - ignore = "snapshot expectations only validated on unix-like platforms" -)] -fn check_default_reporter_snapshot() { - assert_snapshot!(run_check(&["tests/fixtures/test.sql"])); -} - -#[test] -#[cfg_attr( - target_os = "windows", - ignore = "snapshot expectations only validated on unix-like platforms" -)] -fn check_github_reporter_snapshot() { - assert_snapshot!(run_check(&[ - "--reporter", - "github", - "tests/fixtures/test.sql" - ])); -} - -#[test] -#[cfg_attr( - target_os = "windows", - ignore = "snapshot expectations only validated on unix-like platforms" -)] -fn check_gitlab_reporter_snapshot() { - assert_snapshot!(run_check(&[ - "--reporter", - "gitlab", - "tests/fixtures/test.sql" - ])); -} - -#[test] -#[cfg_attr( - target_os = "windows", - ignore = "snapshot expectations only validated on unix-like platforms" -)] -fn check_junit_reporter_snapshot() { - assert_snapshot!(run_check(&[ - "--reporter", - "junit", - "tests/fixtures/test.sql" - ])); -} - -#[test] -#[cfg_attr( - target_os = "windows", - ignore = "snapshot expectations only validated on unix-like platforms" -)] -fn check_stdin_snapshot() { - assert_snapshot!(run_check_with( - &[ - "check", - "--config-path", - CONFIG_PATH, - "--stdin-file-path", - "virtual.sql" - ], - Some("alter tqjable stdin drop column id;\n"), - None - )); -} - -#[test] -#[cfg_attr( - target_os = "windows", - ignore = "snapshot expectations only validated on unix-like platforms" -)] -fn check_directory_traversal_snapshot() { - let project_dir = Path::new("tests/fixtures/traversal"); - assert_snapshot!(run_check_with( - &["check", "--diagnostic-level", "info", "."], - None, - Some(project_dir) - )); -} diff --git a/crates/pgt_cli/tests/assert_cmd.rs b/crates/pgt_cli/tests/assert_cmd.rs deleted file mode 100644 index 7828b9b21..000000000 --- a/crates/pgt_cli/tests/assert_cmd.rs +++ /dev/null @@ -1,137 +0,0 @@ -use std::path::PathBuf; - -use assert_cmd::Command; -use insta::assert_snapshot; -use std::process::ExitStatus; - -const BIN: &str = "postgres-language-server"; -const CONFIG_PATH: &str = "tests/fixtures/postgres-language-server.jsonc"; - -#[test] -#[cfg_attr( - target_os = "windows", - ignore = "snapshot expectations only validated on unix-like platforms" -)] -fn test_cli_check_command() { - let output = Command::cargo_bin(BIN) - .unwrap() - .args([ - "check", - "--config-path", - CONFIG_PATH, - PathBuf::from("tests/fixtures/test.sql").to_str().unwrap(), - ]) - .output() - .unwrap(); - - assert!(!output.status.success(), "command unexpectedly succeeded"); - - let stdout = String::from_utf8_lossy(&output.stdout); - let stderr = String::from_utf8_lossy(&output.stderr); - assert_snapshot!(normalize_output(output.status, &stdout, &stderr)); -} - -fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String { - let normalized_stdout = normalize_durations(stdout); - let normalized_stderr = normalize_diagnostics(stderr); - let status_label = if status.success() { - "success" - } else { - "failure" - }; - format!( - "status: {status_label}\nstdout:\n{}\nstderr:\n{}\n", - normalized_stdout.trim_end(), - normalized_stderr.trim_end() - ) -} - -fn normalize_durations(input: &str) -> String { - let mut content = input.to_owned(); - let mut search_start = 0; - while let Some(relative) = content[search_start..].find(" in ") { - let start = search_start + relative + 4; - if let Some(end_rel) = content[start..].find('.') { - let end = start + end_rel; - let slice = &content[start..end]; - if slice.chars().any(|c| c.is_ascii_digit()) { - content.replace_range(start..end, ""); - search_start = start + "".len() + 1; - continue; - } - search_start = end + 1; - } else { - break; - } - } - let mut time_search = 0; - while let Some(relative) = content[time_search..].find("time=\"") { - let start = time_search + relative + 6; - if let Some(end_rel) = content[start..].find('"') { - let end = start + end_rel; - let slice = &content[start..end]; - if slice.chars().any(|c| c.is_ascii_digit()) { - content.replace_range(start..end, ""); - } - time_search = end + 1; - } else { - break; - } - } - content -} - -fn normalize_diagnostics(input: &str) -> String { - let normalized = normalize_durations(input); - let mut lines = normalized.lines().peekable(); - let mut diagnostic_sections: Vec = Vec::new(); - let mut other_lines: Vec = Vec::new(); - - while let Some(line) = lines.next() { - if is_path_line(line) { - let mut block = String::from(line); - while let Some(&next) = lines.peek() { - if is_path_line(next) || next.starts_with("check ") { - break; - } - block.push('\n'); - block.push_str(next); - lines.next(); - } - diagnostic_sections.push(trim_trailing_newlines(block)); - } else { - other_lines.push(line.to_string()); - } - } - - diagnostic_sections.sort(); - - let mut parts = Vec::new(); - if !diagnostic_sections.is_empty() { - parts.push(diagnostic_sections.join("\n\n")); - } - - let rest = trim_trailing_newlines(other_lines.join("\n")); - if rest.trim().is_empty() { - parts.join("\n\n") - } else if parts.is_empty() { - rest - } else { - parts.push(rest); - parts.join("\n\n") - } -} - -fn is_path_line(line: &str) -> bool { - let trimmed = line.trim_start(); - (trimmed.starts_with("./") || trimmed.starts_with("tests/")) - && trimmed.contains(':') - && trimmed.contains(" syntax") -} - -fn trim_trailing_newlines(mut value: String) -> String { - while value.ends_with('\n') { - value.pop(); - } - value -} diff --git a/crates/pgt_cli/tests/commands/mod.rs b/crates/pgt_cli/tests/commands/mod.rs deleted file mode 100644 index be0c6a3ea..000000000 --- a/crates/pgt_cli/tests/commands/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod check; diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap b/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap deleted file mode 100644 index b62bbf516..000000000 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_stdin_snapshot.snap +++ /dev/null @@ -1,9 +0,0 @@ ---- -source: crates/pgt_cli/tests/commands/check.rs -expression: "run_check_with(&[\"check\", \"--config-path\", CONFIG_PATH, \"--stdin-file-path\",\n\"virtual.sql\"], Some(\"alter tqjable stdin drop column id;\\n\"), None)" -snapshot_kind: text ---- -status: success -stdout: -alter tqjable stdin drop column id; -stderr: diff --git a/crates/pgt_cli/tests/main.rs b/crates/pgt_cli/tests/main.rs deleted file mode 100644 index f3d44688d..000000000 --- a/crates/pgt_cli/tests/main.rs +++ /dev/null @@ -1 +0,0 @@ -mod commands; diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap b/crates/pgt_cli/tests/snapshots/assert_check__check_default_reporter_snapshot.snap similarity index 95% rename from crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap rename to crates/pgt_cli/tests/snapshots/assert_check__check_default_reporter_snapshot.snap index e3f582a19..6293dc01f 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_default_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/snapshots/assert_check__check_default_reporter_snapshot.snap @@ -1,5 +1,5 @@ --- -source: crates/pgt_cli/tests/commands/check.rs +source: crates/pgt_cli/tests/assert_check.rs expression: "run_check(&[\"tests/fixtures/test.sql\"])" snapshot_kind: text --- diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap b/crates/pgt_cli/tests/snapshots/assert_check__check_directory_traversal_snapshot.snap similarity index 89% rename from crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap rename to crates/pgt_cli/tests/snapshots/assert_check__check_directory_traversal_snapshot.snap index 7726d1e1d..cec4b985a 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap +++ b/crates/pgt_cli/tests/snapshots/assert_check__check_directory_traversal_snapshot.snap @@ -1,6 +1,6 @@ --- -source: crates/pgt_cli/tests/commands/check.rs -expression: "run_check_with(&[\"check\", \"--diagnostic-level\", \"info\", \".\"], None,\nSome(project_dir))" +source: crates/pgt_cli/tests/assert_check.rs +expression: "run_check_with(&[\"--diagnostic-level\", \"info\", \".\"], None, Some(project_dir))" snapshot_kind: text --- status: failure diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap b/crates/pgt_cli/tests/snapshots/assert_check__check_github_reporter_snapshot.snap similarity index 93% rename from crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap rename to crates/pgt_cli/tests/snapshots/assert_check__check_github_reporter_snapshot.snap index 7a2b7e721..8410450f9 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_github_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/snapshots/assert_check__check_github_reporter_snapshot.snap @@ -1,5 +1,5 @@ --- -source: crates/pgt_cli/tests/commands/check.rs +source: crates/pgt_cli/tests/assert_check.rs expression: "run_check(&[\"--reporter\", \"github\", \"tests/fixtures/test.sql\"])" snapshot_kind: text --- diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap b/crates/pgt_cli/tests/snapshots/assert_check__check_gitlab_reporter_snapshot.snap similarity index 94% rename from crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap rename to crates/pgt_cli/tests/snapshots/assert_check__check_gitlab_reporter_snapshot.snap index 80ccf17b9..66516538f 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_gitlab_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/snapshots/assert_check__check_gitlab_reporter_snapshot.snap @@ -1,5 +1,5 @@ --- -source: crates/pgt_cli/tests/commands/check.rs +source: crates/pgt_cli/tests/assert_check.rs expression: "run_check(&[\"--reporter\", \"gitlab\", \"tests/fixtures/test.sql\"])" snapshot_kind: text --- diff --git a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap b/crates/pgt_cli/tests/snapshots/assert_check__check_junit_reporter_snapshot.snap similarity index 95% rename from crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap rename to crates/pgt_cli/tests/snapshots/assert_check__check_junit_reporter_snapshot.snap index c11791d65..243b265cc 100644 --- a/crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_junit_reporter_snapshot.snap +++ b/crates/pgt_cli/tests/snapshots/assert_check__check_junit_reporter_snapshot.snap @@ -1,5 +1,5 @@ --- -source: crates/pgt_cli/tests/commands/check.rs +source: crates/pgt_cli/tests/assert_check.rs expression: "run_check(&[\"--reporter\", \"junit\", \"tests/fixtures/test.sql\"])" snapshot_kind: text --- diff --git a/crates/pgt_cli/tests/snapshots/assert_check__check_stdin_snapshot.snap b/crates/pgt_cli/tests/snapshots/assert_check__check_stdin_snapshot.snap new file mode 100644 index 000000000..33f48f349 --- /dev/null +++ b/crates/pgt_cli/tests/snapshots/assert_check__check_stdin_snapshot.snap @@ -0,0 +1,9 @@ +--- +source: crates/pgt_cli/tests/assert_check.rs +expression: "run_check_with(&[\"--config-path\", CONFIG_PATH, \"--stdin-file-path\",\n\"virtual.sql\"], Some(\"alter tqjable stdin drop column id;\\n\"), None)" +snapshot_kind: text +--- +status: success +stdout: +alter tqjable stdin drop column id; +stderr: diff --git a/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap b/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap deleted file mode 100644 index 3bce5e37e..000000000 --- a/crates/pgt_cli/tests/snapshots/assert_cmd__cli_check_command.snap +++ /dev/null @@ -1,22 +0,0 @@ ---- -source: crates/pgt_cli/tests/assert_cmd.rs -expression: "normalize_output(output.status, &stdout, &stderr)" -snapshot_kind: text ---- -status: failure -stdout: -Checked 1 file in . No fixes applied. -Found 1 error. -stderr: -tests/fixtures/test.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × Invalid statement: syntax error at or near "tqjable" - - > 1 │ alter tqjable test drop column id; - │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - 2 │ - - -check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × Some errors were emitted while running checks. From a9283488c0f936904b98bb064f81f25634435af5 Mon Sep 17 00:00:00 2001 From: psteinroe Date: Mon, 27 Oct 2025 20:54:47 +0100 Subject: [PATCH 6/6] progress --- crates/pgt_cli/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/pgt_cli/Cargo.toml b/crates/pgt_cli/Cargo.toml index 7d2cd2efd..0e6093cbb 100644 --- a/crates/pgt_cli/Cargo.toml +++ b/crates/pgt_cli/Cargo.toml @@ -53,7 +53,7 @@ tikv-jemallocator = "0.6.0" [dev-dependencies] assert_cmd = "2.0.16" -insta = { version = "1.40.0", features = ["yaml"] } +insta = { workspace = true, features = ["yaml"] } [lib] doctest = false