Skip to content

Commit

Permalink
fix: no output when the source is erroneous
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadnucYard committed Feb 14, 2025
1 parent 2cf35b7 commit a79e855
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 36 deletions.
16 changes: 11 additions & 5 deletions crates/typstyle/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,17 @@ pub fn format_one(input: Option<&PathBuf>, args: &CliArguments) -> Result<Format
print!("{}", res);
}
}
FormatResult::Erroneous => {
FormatResult::Erroneous(content) => {
if !args.inplace && !args.check {
print!("{}", content); // still prints the original content to enable piping
}
if let Some(path) = input {
warn!("Failed to parse {}", path.display());
warn!(
"Failed to parse {}. The source is erroneous.",
path.display()
);
} else {
warn!("Failed to parse stdin");
warn!("Failed to parse stdin. The source is erroneous.");
}
}
}
Expand All @@ -207,7 +213,7 @@ pub fn format_one(input: Option<&PathBuf>, args: &CliArguments) -> Result<Format
enum FormatResult {
Changed(String),
Unchanged(String),
Erroneous,
Erroneous(String),
}

fn format_debug(content: String, args: &CliArguments) -> FormatResult {
Expand All @@ -226,7 +232,7 @@ fn format_debug(content: String, args: &CliArguments) -> FormatResult {
}
}) {
Ok(res) => res,
Err(_) => return FormatResult::Erroneous,
Err(_) => return FormatResult::Erroneous(content),
};

// Compare `res` with `content` to perform CI checks
Expand Down
60 changes: 47 additions & 13 deletions crates/typstyle/tests/test_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_one() {
let mut space = Workspace::new();
space.write_tracked("a.typ", "#let a = 0");

typstyle_cmd_snapshot!(space.cli().arg("a.typ"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -24,7 +24,7 @@ fn test_one_inplace() {
let mut space = Workspace::new();
space.write_tracked("a.typ", "#let a = 0");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("-i"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "-i"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -40,7 +40,7 @@ fn test_one_quiet() {
let mut space = Workspace::new();
space.write_tracked("a.typ", "#let a = 0");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("-q"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "-q"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -52,12 +52,46 @@ fn test_one_quiet() {
assert!(space.all_unmodified());
}

#[test]
fn test_one_erroneous() {
let mut space = Workspace::new();
space.write_tracked("a.typ", "#let");

typstyle_cmd_snapshot!(space.cli().args(["a.typ"]), @r"
success: true
exit_code: 0
----- stdout -----
#let
----- stderr -----
warn: Failed to parse a.typ. The source is erroneous.
");

assert!(space.all_unmodified());
}

#[test]
fn test_one_inplace_erroneous() {
let mut space = Workspace::new();
space.write_tracked("a.typ", "#let");

typstyle_cmd_snapshot!(space.cli().args(["a.typ", "-i"]), @r"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warn: Failed to parse a.typ. The source is erroneous.
");

assert!(space.all_unmodified());
}

#[test]
fn test_one_check_quiet() {
let mut space = Workspace::new();
space.write_tracked("a.typ", "#let a = 0");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("--check").arg("-q"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "--check", "-q"]), @r"
success: false
exit_code: 1
----- stdout -----
Expand All @@ -74,7 +108,7 @@ fn test_two_0() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -93,7 +127,7 @@ fn test_two_1() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -112,7 +146,7 @@ fn test_two_2() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -131,7 +165,7 @@ fn test_two_0_inplace() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ").arg("-i"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ", "-i"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -149,7 +183,7 @@ fn test_two_1_inplace() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ").arg("-i"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ", "-i"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -167,7 +201,7 @@ fn test_two_2_inplace() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ").arg("-i"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ", "-i"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -185,7 +219,7 @@ fn test_two_0_check() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ").arg("--check"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ", "--check"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -202,7 +236,7 @@ fn test_two_1_check() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ").arg("--check"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ", "--check"]), @r"
success: false
exit_code: 1
----- stdout -----
Expand All @@ -220,7 +254,7 @@ fn test_two_2_check() {
space.write_tracked("a.typ", "#let a = 0\n");
space.write_tracked("b.typ", "#let b = 1\n");

typstyle_cmd_snapshot!(space.cli().arg("a.typ").arg("b.typ").arg("--check"), @r"
typstyle_cmd_snapshot!(space.cli().args(["a.typ", "b.typ", "--check"]), @r"
success: false
exit_code: 1
----- stdout -----
Expand Down
22 changes: 11 additions & 11 deletions crates/typstyle/tests/test_format_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_all_0() {
space.write_tracked("x/y/.c.typ", "#let c = 2");
space.write_tracked("x/.z/d.typ", "#let d = 3");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "-v"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -33,7 +33,7 @@ fn test_all_1() {
space.write_tracked("x/y/.c.typ", "#let c = 2");
space.write_tracked("x/.z/d.typ", "#let d = 3");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "-v"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -56,7 +56,7 @@ fn test_all_2() {
space.write_tracked("x/y/.c.typ", "#let c = 2");
space.write_tracked("x/.z/d.typ", "#let d = 3");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "-v"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -80,7 +80,7 @@ fn test_all_0_check() {
space.write_tracked("x/y/.c.typ", "#let c = 2");
space.write_tracked("x/.z/d.typ", "#let d = 3");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("--check").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "--check", "-v"]), @r"
success: false
exit_code: 1
----- stdout -----
Expand All @@ -101,7 +101,7 @@ fn test_all_1_check() {
space.write_tracked("x/y/.c.typ", "#let c = 2");
space.write_tracked("x/.z/d.typ", "#let d = 3");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("--check").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "--check", "-v"]), @r"
success: false
exit_code: 1
----- stdout -----
Expand All @@ -122,7 +122,7 @@ fn test_all_2_check() {
space.write_tracked("x/y/.c.typ", "#let c = 2");
space.write_tracked("x/.z/d.typ", "#let d = 3");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("--check").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "--check", "-v"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -141,7 +141,7 @@ fn test_all_erroneous() {
space.write_tracked("x/b.typ", "#let b = 1");
space.write_tracked("x/y/c.typ", "#let c = 2; #");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "-v"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -163,7 +163,7 @@ fn test_all_erroneous_check() {
space.write_tracked("x/b.typ", "#let b = 1");
space.write_tracked("x/y/c.typ", "#let c = 2; #");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("--check").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "--check", "-v"]), @r"
success: false
exit_code: 1
----- stdout -----
Expand All @@ -182,7 +182,7 @@ fn test_all_column() {
let space = Workspace::new();
space.write("a.typ", "#let a = (1 + 2)");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("-c=0").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "-c=0", "-v"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand Down Expand Up @@ -213,7 +213,7 @@ for i in range(0, 5) {
}",
);

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("-t=4").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "-t=4", "-v"]), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -240,7 +240,7 @@ fn test_dir_all_check() {
space.write("x/b.typ", "#let b = 1");
space.write("x/y/.c.typ", "#let c = 2");

typstyle_cmd_snapshot!(space.cli().arg("format-all").arg("x").arg("--check").arg("-v"), @r"
typstyle_cmd_snapshot!(space.cli().args(["format-all", "x", "--check", "-v"]), @r"
success: false
exit_code: 1
----- stdout -----
Expand Down
14 changes: 7 additions & 7 deletions crates/typstyle/tests/test_format_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ fn test_stdin_erroneous() {
success: true
exit_code: 0
----- stdout -----
#
----- stderr -----
warn: Failed to parse stdin
warn: Failed to parse stdin. The source is erroneous.
");
}

#[test]
fn test_stdin_column() {
let space = Workspace::new();

typstyle_cmd_snapshot!(space.cli().arg("-c=0").pass_stdin(STDIN), @r"
typstyle_cmd_snapshot!(space.cli().args(["-c=0"]).pass_stdin(STDIN), @r"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -67,7 +67,7 @@ fn test_stdin_column() {
fn test_stdin_check() {
let space = Workspace::new();

typstyle_cmd_snapshot!(space.cli().arg("--check").pass_stdin(STDIN), @r"
typstyle_cmd_snapshot!(space.cli().args(["--check"]).pass_stdin(STDIN), @r"
success: false
exit_code: 1
----- stdout -----
Expand All @@ -80,7 +80,7 @@ fn test_stdin_check() {
fn test_stdin_inplace() {
let space = Workspace::new();

typstyle_cmd_snapshot!(space.cli().arg("-i").pass_stdin(STDIN), @r"
typstyle_cmd_snapshot!(space.cli().args(["-i"]).pass_stdin(STDIN), @r"
success: false
exit_code: 2
----- stdout -----
Expand All @@ -98,7 +98,7 @@ fn test_stdin_inplace() {
fn test_stdin_inplace_check() {
let space = Workspace::new();

typstyle_cmd_snapshot!(space.cli().arg("-i").arg("--check").pass_stdin(STDIN), @r"
typstyle_cmd_snapshot!(space.cli().args(["-i", "--check"]).pass_stdin(STDIN), @r"
success: false
exit_code: 2
----- stdout -----
Expand All @@ -116,7 +116,7 @@ fn test_stdin_inplace_check() {
fn test_stdin_debug_ast() {
let space = Workspace::new();

typstyle_cmd_snapshot!(space.cli().arg("-a").pass_stdin(STDIN), @r##"
typstyle_cmd_snapshot!(space.cli().args(["-a"]).pass_stdin(STDIN), @r##"
success: true
exit_code: 0
----- stdout -----
Expand Down

0 comments on commit a79e855

Please sign in to comment.