Skip to content

Commit 2d88312

Browse files
authored
fix: refine grit format exit code (#615)
1 parent 894d251 commit 2d88312

11 files changed

+330
-98
lines changed

Cargo.lock

Lines changed: 104 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ tracing-subscriber = { version = "0.3", default-features = false, optional = tru
8787
tracing-log = { version = "0.2.0", optional = true }
8888

8989
fs-err = { version = "2.11.0" }
90-
biome_grit_parser = { git = "https://github.com/biomejs/biome", rev = "71c825e65e58fc1937b55b4f26edafdd183a50f3" }
91-
biome_grit_formatter = { git = "https://github.com/biomejs/biome", rev = "71c825e65e58fc1937b55b4f26edafdd183a50f3" }
90+
biome_grit_parser = { git = "https://github.com/biomejs/biome", rev = "1835578712b69113be42ec61e6174227aa6a693b" }
91+
biome_grit_formatter = { git = "https://github.com/biomejs/biome", rev = "1835578712b69113be42ec61e6174227aa6a693b" }
9292
# biome_grit_parser = { path = "../../../../../../biome/crates/biome_grit_parser" }
9393
# biome_grit_formatter = { path = "../../../../../../biome/crates/biome_grit_formatter" }
9494

crates/cli/src/commands/format.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
commands::patterns_test::filter_patterns_by_regex,
3+
error::GoodError,
34
flags::GlobalFormatFlags,
45
messenger_variant::create_emitter,
56
resolver::{resolve_from_cwd, Source},
@@ -26,7 +27,7 @@ pub struct FormatGritArgs {
2627
// Level of detail to show for results
2728
#[clap(
2829
long = "output",
29-
default_value_t = OutputMode::Compact,
30+
default_value_t = OutputMode::Standard,
3031
)]
3132
output: OutputMode,
3233
/// Regex of a specific pattern to test
@@ -93,16 +94,21 @@ pub async fn run_format(arg: &FormatGritArgs, flags: &GlobalFormatFlags) -> Resu
9394
}
9495
}
9596

96-
println!(
97-
"Modified {} {}",
98-
format!("{}", details.rewritten).bold().yellow(),
99-
if details.rewritten == 1 {
100-
"file"
101-
} else {
102-
"files"
103-
}
104-
);
97+
if details.rewritten > 0 {
98+
println!(
99+
"{} formatting changes in {} {}",
100+
if arg.write { "Made" } else { "Found" },
101+
format!("{}", details.rewritten).bold().yellow(),
102+
if details.rewritten == 1 {
103+
"file"
104+
} else {
105+
"files"
106+
}
107+
);
108+
anyhow::bail!(GoodError::new());
109+
}
105110

111+
println!("No formatting changes were made");
106112
Ok(())
107113
}
108114

crates/cli_bin/tests/format.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::common::get_test_cmd;
22
use anyhow::Result;
33
use common::get_fixture;
4-
use insta::assert_yaml_snapshot;
4+
use insta::assert_snapshot;
55

66
mod common;
77

@@ -18,24 +18,25 @@ fn format_patterns_with_rewrite() -> Result<()> {
1818
println!("stderr: {}", String::from_utf8(output.stderr.clone())?);
1919
println!("stdout: {}", String::from_utf8(output.stdout.clone())?);
2020

21-
assert!(
22-
output.status.success(),
23-
"Command didn't finish successfully"
24-
);
21+
assert!(!output.status.success());
2522
assert!(output.stderr.is_empty());
2623

2724
let yaml_file_content = std::fs::read_to_string(grit_dir.join(".grit/grit.yaml"))?;
25+
assert_snapshot!(yaml_file_content);
2826
let test_move_import_file_content =
2927
std::fs::read_to_string(grit_dir.join(".grit/others/test_move_import.md"))?;
28+
assert_snapshot!(test_move_import_file_content);
3029
let aspect_ratio_md_file_content =
3130
std::fs::read_to_string(grit_dir.join(".grit/patterns/aspect_ratio.md"))?;
31+
assert_snapshot!(aspect_ratio_md_file_content);
3232
let dependency_grit_file_content =
3333
std::fs::read_to_string(grit_dir.join(".grit/patterns/dependency.grit"))?;
34-
assert_yaml_snapshot!(vec![
35-
yaml_file_content,
36-
test_move_import_file_content,
37-
aspect_ratio_md_file_content,
38-
dependency_grit_file_content
39-
]);
34+
assert_snapshot!(dependency_grit_file_content);
35+
let test_move_import_file_content =
36+
std::fs::read_to_string(grit_dir.join(".grit/others/test_move_import.md"))?;
37+
assert_snapshot!(test_move_import_file_content);
38+
let aspect_ratio_md_file_content =
39+
std::fs::read_to_string(grit_dir.join(".grit/patterns/aspect_ratio.md"))?;
40+
assert_snapshot!(aspect_ratio_md_file_content);
4041
Ok(())
4142
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/cli_bin/tests/format.rs
3+
expression: test_move_import_file_content
4+
---
5+
---
6+
private: true
7+
tags: [private]
8+
---
9+
```grit
10+
language js
11+
12+
`sanitizeFilePath` as $s where {
13+
move_import(`sanitizeFilePath`, `'@getgrit/universal'`)
14+
}
15+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
source: crates/cli_bin/tests/format.rs
3+
expression: aspect_ratio_md_file_content
4+
---
5+
---
6+
title: Aspect ratio
7+
---
8+
9+
```grit
10+
language css
11+
12+
`a { $props }` where { $props <: contains `aspect-ratio: $x` }
13+
```
14+
15+
## Matches the right selector and declaration block
16+
17+
```css
18+
a {
19+
width: calc(100% - 80px);
20+
aspect-ratio: 1/2;
21+
font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));
22+
}
23+
24+
#some-id {
25+
some-property: 5px;
26+
}
27+
28+
a.b ~ c.d {
29+
}
30+
.e.f + .g.h {
31+
}
32+
33+
@font-face {
34+
font-family: 'Open Sans';
35+
src: url('/a') format('woff2'), url('/b/c') format('woff');
36+
}
37+
```
38+
39+
```css
40+
a {
41+
width: calc(100% - 80px);
42+
aspect-ratio: 1/2;
43+
font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));
44+
}
45+
46+
#some-id {
47+
some-property: 5px;
48+
}
49+
50+
a.b ~ c.d {
51+
}
52+
.e.f + .g.h {
53+
}
54+
55+
@font-face {
56+
font-family: 'Open Sans';
57+
src: url('/a') format('woff2'), url('/b/c') format('woff');
58+
}
59+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
source: crates/cli_bin/tests/format.rs
3+
expression: dependency_grit_file_content
4+
---
5+
language json
6+
7+
pattern upgrade_dependency($target_dep, $target_version, $dependency_key) {
8+
or {
9+
`$key: $value` where {
10+
$key <: `"$target_dep"`,
11+
$value => `"$target_version"`
12+
},
13+
pair($key, $value) where {
14+
$key <: `"$dependency_key"`,
15+
$value <: object($properties) where {
16+
$properties <: not contains pair(key=$dep_key) where {
17+
$dep_key <: contains `$target_dep`
18+
},
19+
$properties => `"$target_dep": "$target_version",\n$properties`
20+
}
21+
}
22+
}
23+
}
24+
25+
pattern console_method_to_info($method) {
26+
`console.$method($message)` => `console.info($message)`
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/cli_bin/tests/format.rs
3+
expression: test_move_import_file_content
4+
---
5+
---
6+
private: true
7+
tags: [private]
8+
---
9+
```grit
10+
language js
11+
12+
`sanitizeFilePath` as $s where {
13+
move_import(`sanitizeFilePath`, `'@getgrit/universal'`)
14+
}
15+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
source: crates/cli_bin/tests/format.rs
3+
expression: aspect_ratio_md_file_content
4+
---
5+
---
6+
title: Aspect ratio
7+
---
8+
9+
```grit
10+
language css
11+
12+
`a { $props }` where { $props <: contains `aspect-ratio: $x` }
13+
```
14+
15+
## Matches the right selector and declaration block
16+
17+
```css
18+
a {
19+
width: calc(100% - 80px);
20+
aspect-ratio: 1/2;
21+
font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));
22+
}
23+
24+
#some-id {
25+
some-property: 5px;
26+
}
27+
28+
a.b ~ c.d {
29+
}
30+
.e.f + .g.h {
31+
}
32+
33+
@font-face {
34+
font-family: 'Open Sans';
35+
src: url('/a') format('woff2'), url('/b/c') format('woff');
36+
}
37+
```
38+
39+
```css
40+
a {
41+
width: calc(100% - 80px);
42+
aspect-ratio: 1/2;
43+
font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));
44+
}
45+
46+
#some-id {
47+
some-property: 5px;
48+
}
49+
50+
a.b ~ c.d {
51+
}
52+
.e.f + .g.h {
53+
}
54+
55+
@font-face {
56+
font-family: 'Open Sans';
57+
src: url('/a') format('woff2'), url('/b/c') format('woff');
58+
}
59+
```
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
---
22
source: crates/cli_bin/tests/format.rs
3-
expression: "vec![yaml_file_content, test_move_import_file_content,\naspect_ratio_md_file_content, dependency_grit_file_content]"
4-
snapshot_kind: text
3+
expression: yaml_file_content
54
---
6-
- "version: 0.0.1\npatterns:\n - name: aspect_ratio_yaml\n description: Yaml version of aspect_ratio.md\n body: |\n language css;\n `a { $props }` where { $props <: contains `aspect-ratio: $x` }\n\n - file: ./others/test_move_import.md\n\n - name: some_json_pattern\n body: |\n language json;\n `account: $val` where {\n \t$val <: contains `password: $password`,\n \t$password => raw`hidden`\n }"
7-
- "---\nprivate: true\ntags: [private]\n---\n```grit\nlanguage js;\n`sanitizeFilePath` as $s where {\n\tmove_import(`sanitizeFilePath`, `'@getgrit/universal'`)\n}\n```\n"
8-
- "---\ntitle: Aspect ratio\n---\n\n```grit\nlanguage css;\n`a { $props }` where { $props <: contains `aspect-ratio: $x` }\n```\n\n## Matches the right selector and declaration block\n\n```css\na {\n width: calc(100% - 80px);\n aspect-ratio: 1/2;\n font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));\n}\n\n#some-id {\n some-property: 5px;\n}\n\na.b ~ c.d {\n}\n.e.f + .g.h {\n}\n\n@font-face {\n font-family: 'Open Sans';\n src: url('/a') format('woff2'), url('/b/c') format('woff');\n}\n```\n\n```css\na {\n width: calc(100% - 80px);\n aspect-ratio: 1/2;\n font-size: calc(10px + (56 - 10) * ((100vw - 320px) / (1920 - 320)));\n}\n\n#some-id {\n some-property: 5px;\n}\n\na.b ~ c.d {\n}\n.e.f + .g.h {\n}\n\n@font-face {\n font-family: 'Open Sans';\n src: url('/a') format('woff2'), url('/b/c') format('woff');\n}\n```\n"
9-
- "language json;\npattern upgrade_dependency($target_dep, $target_version, $dependency_key) {\n\tor {\n\t\t`$key: $value` where {\n\t\t\t$key <: `\"$target_dep\"`,\n\t\t\t$value => `\"$target_version\"`\n\t\t},\n\t\tpair($key, $value) where {\n\t\t\t$key <: `\"$dependency_key\"`,\n\t\t\t$value <: object($properties) where {\n\t\t\t\t$properties <: notcontains pair(key = $dep_key) where {\n\t\t\t\t\t$dep_key <: contains `$target_dep`\n\t\t\t\t},\n\t\t\t\t$properties => `\"$target_dep\": \"$target_version\",\\n$properties`\n\t\t\t}\n\t\t}\n\t}}\n\npattern console_method_to_info($method) {\n\t`console.$method($message)` => `console.info($message)`}\n"
5+
version: 0.0.1
6+
patterns:
7+
- name: aspect_ratio_yaml
8+
description: Yaml version of aspect_ratio.md
9+
body: |
10+
language css
11+
12+
`a { $props }` where { $props <: contains `aspect-ratio: $x` }
13+
14+
- file: ./others/test_move_import.md
15+
16+
- name: some_json_pattern
17+
body: |
18+
language json
19+
20+
`account: $val` where {
21+
$val <: contains `password: $password`,
22+
$password => raw`hidden`
23+
}

0 commit comments

Comments
 (0)