Skip to content

Commit 1996951

Browse files
committed
progress
1 parent 1f3a3a0 commit 1996951

File tree

3 files changed

+118
-8
lines changed

3 files changed

+118
-8
lines changed

crates/pgt_cli/tests/assert_cmd.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn test_cli_check_command() {
3333

3434
fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String {
3535
let normalized_stdout = normalize_durations(stdout);
36-
let normalized_stderr = normalize_durations(stderr);
36+
let normalized_stderr = normalize_diagnostics(stderr);
3737
let status_label = if status.success() {
3838
"success"
3939
} else {
@@ -80,3 +80,58 @@ fn normalize_durations(input: &str) -> String {
8080
}
8181
content
8282
}
83+
84+
fn normalize_diagnostics(input: &str) -> String {
85+
let normalized = normalize_durations(input);
86+
let mut lines = normalized.lines().peekable();
87+
let mut diagnostic_sections: Vec<String> = Vec::new();
88+
let mut other_lines: Vec<String> = Vec::new();
89+
90+
while let Some(line) = lines.next() {
91+
if is_path_line(line) {
92+
let mut block = String::from(line);
93+
while let Some(&next) = lines.peek() {
94+
if is_path_line(next) || next.starts_with("check ") {
95+
break;
96+
}
97+
block.push('\n');
98+
block.push_str(next);
99+
lines.next();
100+
}
101+
diagnostic_sections.push(trim_trailing_newlines(block));
102+
} else {
103+
other_lines.push(line.to_string());
104+
}
105+
}
106+
107+
diagnostic_sections.sort();
108+
109+
let mut parts = Vec::new();
110+
if !diagnostic_sections.is_empty() {
111+
parts.push(diagnostic_sections.join("\n\n"));
112+
}
113+
114+
let rest = trim_trailing_newlines(other_lines.join("\n"));
115+
if rest.trim().is_empty() {
116+
parts.join("\n\n")
117+
} else if parts.is_empty() {
118+
rest
119+
} else {
120+
parts.push(rest);
121+
parts.join("\n\n")
122+
}
123+
}
124+
125+
fn is_path_line(line: &str) -> bool {
126+
let trimmed = line.trim_start();
127+
(trimmed.starts_with("./") || trimmed.starts_with("tests/"))
128+
&& trimmed.contains(':')
129+
&& trimmed.contains(" syntax")
130+
}
131+
132+
fn trim_trailing_newlines(mut value: String) -> String {
133+
while value.ends_with('\n') {
134+
value.pop();
135+
}
136+
value
137+
}

crates/pgt_cli/tests/commands/check.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn normalize_durations(input: &str) -> String {
6868

6969
fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String {
7070
let normalized_stdout = normalize_durations(stdout);
71-
let normalized_stderr = normalize_durations(stderr);
71+
let normalized_stderr = normalize_diagnostics(stderr);
7272
let status_label = if status.success() {
7373
"success"
7474
} else {
@@ -81,6 +81,61 @@ fn normalize_output(status: ExitStatus, stdout: &str, stderr: &str) -> String {
8181
)
8282
}
8383

84+
fn normalize_diagnostics(input: &str) -> String {
85+
let normalized = normalize_durations(input);
86+
let mut lines = normalized.lines().peekable();
87+
let mut diagnostic_sections: Vec<String> = Vec::new();
88+
let mut other_lines: Vec<String> = Vec::new();
89+
90+
while let Some(line) = lines.next() {
91+
if is_path_line(line) {
92+
let mut block = String::from(line);
93+
while let Some(&next) = lines.peek() {
94+
if is_path_line(next) || next.starts_with("check ") {
95+
break;
96+
}
97+
block.push('\n');
98+
block.push_str(next);
99+
lines.next();
100+
}
101+
diagnostic_sections.push(trim_trailing_newlines(block));
102+
} else {
103+
other_lines.push(line.to_string());
104+
}
105+
}
106+
107+
diagnostic_sections.sort();
108+
109+
let mut parts = Vec::new();
110+
if !diagnostic_sections.is_empty() {
111+
parts.push(diagnostic_sections.join("\n\n"));
112+
}
113+
114+
let rest = trim_trailing_newlines(other_lines.join("\n"));
115+
if rest.trim().is_empty() {
116+
parts.join("\n\n")
117+
} else if parts.is_empty() {
118+
rest
119+
} else {
120+
parts.push(rest);
121+
parts.join("\n\n")
122+
}
123+
}
124+
125+
fn is_path_line(line: &str) -> bool {
126+
let trimmed = line.trim_start();
127+
(trimmed.starts_with("./") || trimmed.starts_with("tests/"))
128+
&& trimmed.contains(':')
129+
&& trimmed.contains(" syntax")
130+
}
131+
132+
fn trim_trailing_newlines(mut value: String) -> String {
133+
while value.ends_with('\n') {
134+
value.pop();
135+
}
136+
value
137+
}
138+
84139
#[test]
85140
#[cfg_attr(
86141
target_os = "windows",

crates/pgt_cli/tests/commands/snapshots/main__commands__check__check_directory_traversal_snapshot.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ stdout:
88
Checked 2 files in <duration>. No fixes applied.
99
Found 2 errors.
1010
stderr:
11-
./bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
11+
./another_bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1212

1313
× Invalid statement: syntax error at or near "tqjable"
1414

15-
> 1 │ alter tqjable bad drop column id;
16-
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
> 1 │ alter tqjable another drop column id;
16+
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717
2 │
1818

1919

20-
./another_bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
20+
./bad.sql:1:1 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2121

2222
× Invalid statement: syntax error at or near "tqjable"
2323

24-
> 1 │ alter tqjable another drop column id;
25-
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
> 1 │ alter tqjable bad drop column id;
25+
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626
2 │
2727

2828

0 commit comments

Comments
 (0)