2023-01-12 20:59:13 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use test_util as util;
|
|
|
|
use test_util::TempDir;
|
2023-05-22 21:39:59 -04:00
|
|
|
use util::assert_contains;
|
2023-06-10 11:09:45 -04:00
|
|
|
use util::PathRef;
|
2023-03-13 09:40:46 -04:00
|
|
|
use util::TestContext;
|
2023-05-22 21:39:59 -04:00
|
|
|
use util::TestContextBuilder;
|
2023-01-12 20:59:13 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fmt_test() {
|
2023-03-13 09:40:46 -04:00
|
|
|
let context = TestContext::default();
|
|
|
|
let t = context.deno_dir();
|
2023-01-12 20:59:13 -05:00
|
|
|
let testdata_fmt_dir = util::testdata_path().join("fmt");
|
|
|
|
let fixed_js = testdata_fmt_dir.join("badly_formatted_fixed.js");
|
|
|
|
let badly_formatted_original_js =
|
|
|
|
testdata_fmt_dir.join("badly_formatted.mjs");
|
|
|
|
let badly_formatted_js = t.path().join("badly_formatted.js");
|
2023-06-10 11:09:45 -04:00
|
|
|
badly_formatted_original_js.copy(&badly_formatted_js);
|
2023-01-12 20:59:13 -05:00
|
|
|
|
|
|
|
let fixed_md = testdata_fmt_dir.join("badly_formatted_fixed.md");
|
|
|
|
let badly_formatted_original_md = testdata_fmt_dir.join("badly_formatted.md");
|
|
|
|
let badly_formatted_md = t.path().join("badly_formatted.md");
|
2023-06-10 11:09:45 -04:00
|
|
|
badly_formatted_original_md.copy(&badly_formatted_md);
|
2023-01-12 20:59:13 -05:00
|
|
|
|
|
|
|
let fixed_json = testdata_fmt_dir.join("badly_formatted_fixed.json");
|
|
|
|
let badly_formatted_original_json =
|
|
|
|
testdata_fmt_dir.join("badly_formatted.json");
|
|
|
|
let badly_formatted_json = t.path().join("badly_formatted.json");
|
2023-06-10 11:09:45 -04:00
|
|
|
badly_formatted_original_json.copy(&badly_formatted_json);
|
2023-01-12 20:59:13 -05:00
|
|
|
// First, check formatting by ignoring the badly formatted file.
|
2023-03-13 09:40:46 -04:00
|
|
|
|
|
|
|
let output = context
|
|
|
|
.new_command()
|
2023-06-10 11:09:45 -04:00
|
|
|
.cwd(&testdata_fmt_dir)
|
2023-03-13 09:40:46 -04:00
|
|
|
.args_vec(vec![
|
|
|
|
"fmt".to_string(),
|
|
|
|
format!(
|
2023-06-10 11:09:45 -04:00
|
|
|
"--ignore={badly_formatted_js},{badly_formatted_md},{badly_formatted_json}",
|
2023-03-13 09:40:46 -04:00
|
|
|
),
|
|
|
|
format!(
|
2023-06-10 11:09:45 -04:00
|
|
|
"--check {badly_formatted_js} {badly_formatted_md} {badly_formatted_json}",
|
2023-03-13 09:40:46 -04:00
|
|
|
),
|
|
|
|
])
|
|
|
|
.run();
|
|
|
|
|
2023-01-12 20:59:13 -05:00
|
|
|
// No target files found
|
2023-03-13 09:40:46 -04:00
|
|
|
output.assert_exit_code(1);
|
|
|
|
output.skip_output_check();
|
2023-01-12 20:59:13 -05:00
|
|
|
|
|
|
|
// Check without ignore.
|
2023-03-13 09:40:46 -04:00
|
|
|
let output = context
|
|
|
|
.new_command()
|
2023-06-10 11:09:45 -04:00
|
|
|
.cwd(&testdata_fmt_dir)
|
2023-03-13 09:40:46 -04:00
|
|
|
.args_vec(vec![
|
|
|
|
"fmt".to_string(),
|
|
|
|
"--check".to_string(),
|
2023-06-10 11:09:45 -04:00
|
|
|
badly_formatted_js.to_string(),
|
|
|
|
badly_formatted_md.to_string(),
|
|
|
|
badly_formatted_json.to_string(),
|
2023-03-13 09:40:46 -04:00
|
|
|
])
|
|
|
|
.run();
|
|
|
|
|
|
|
|
output.assert_exit_code(1);
|
|
|
|
output.skip_output_check();
|
2023-01-12 20:59:13 -05:00
|
|
|
|
|
|
|
// Format the source file.
|
2023-03-13 09:40:46 -04:00
|
|
|
let output = context
|
|
|
|
.new_command()
|
2023-06-10 11:09:45 -04:00
|
|
|
.cwd(&testdata_fmt_dir)
|
2023-03-13 09:40:46 -04:00
|
|
|
.args_vec(vec![
|
|
|
|
"fmt".to_string(),
|
2023-06-10 11:09:45 -04:00
|
|
|
badly_formatted_js.to_string(),
|
|
|
|
badly_formatted_md.to_string(),
|
|
|
|
badly_formatted_json.to_string(),
|
2023-03-13 09:40:46 -04:00
|
|
|
])
|
|
|
|
.run();
|
|
|
|
|
|
|
|
output.assert_exit_code(0);
|
|
|
|
output.skip_output_check();
|
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
let expected_js = fixed_js.read_to_string();
|
|
|
|
let expected_md = fixed_md.read_to_string();
|
|
|
|
let expected_json = fixed_json.read_to_string();
|
|
|
|
let actual_js = badly_formatted_js.read_to_string();
|
|
|
|
let actual_md = badly_formatted_md.read_to_string();
|
|
|
|
let actual_json = badly_formatted_json.read_to_string();
|
2023-01-12 20:59:13 -05:00
|
|
|
assert_eq!(expected_js, actual_js);
|
|
|
|
assert_eq!(expected_md, actual_md);
|
|
|
|
assert_eq!(expected_json, actual_json);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fmt_stdin_error() {
|
|
|
|
use std::io::Write;
|
|
|
|
let mut deno = util::deno_cmd()
|
|
|
|
.current_dir(util::testdata_path())
|
|
|
|
.arg("fmt")
|
|
|
|
.arg("-")
|
|
|
|
.stdin(std::process::Stdio::piped())
|
|
|
|
.stdout(std::process::Stdio::piped())
|
|
|
|
.stderr(std::process::Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.unwrap();
|
|
|
|
let stdin = deno.stdin.as_mut().unwrap();
|
|
|
|
let invalid_js = b"import { example }";
|
|
|
|
stdin.write_all(invalid_js).unwrap();
|
|
|
|
let output = deno.wait_with_output().unwrap();
|
|
|
|
// Error message might change. Just check stdout empty, stderr not.
|
|
|
|
assert!(output.stdout.is_empty());
|
|
|
|
assert!(!output.stderr.is_empty());
|
|
|
|
assert!(!output.status.success());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fmt_ignore_unexplicit_files() {
|
2023-03-13 09:40:46 -04:00
|
|
|
let context = TestContext::default();
|
|
|
|
let output = context
|
|
|
|
.new_command()
|
2023-01-12 20:59:13 -05:00
|
|
|
.env("NO_COLOR", "1")
|
2023-03-13 09:40:46 -04:00
|
|
|
.args("fmt --check --ignore=./")
|
|
|
|
.run();
|
|
|
|
|
|
|
|
output.assert_exit_code(1);
|
|
|
|
assert_eq!(output.combined_output(), "error: No target files found.\n");
|
2023-01-12 20:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fmt_auto_ignore_git_and_node_modules() {
|
2023-06-10 11:09:45 -04:00
|
|
|
fn create_bad_json(t: PathRef) {
|
2023-01-12 20:59:13 -05:00
|
|
|
let bad_json_path = t.join("bad.json");
|
2023-06-10 11:09:45 -04:00
|
|
|
bad_json_path.write("bad json\n");
|
2023-01-12 20:59:13 -05:00
|
|
|
}
|
2023-06-10 11:09:45 -04:00
|
|
|
|
2023-01-12 20:59:13 -05:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let t = temp_dir.path().join("target");
|
|
|
|
let nest_git = t.join("nest").join(".git");
|
|
|
|
let git_dir = t.join(".git");
|
|
|
|
let nest_node_modules = t.join("nest").join("node_modules");
|
|
|
|
let node_modules_dir = t.join("node_modules");
|
2023-06-10 11:09:45 -04:00
|
|
|
nest_git.create_dir_all();
|
|
|
|
git_dir.create_dir_all();
|
|
|
|
nest_node_modules.create_dir_all();
|
|
|
|
node_modules_dir.create_dir_all();
|
2023-01-12 20:59:13 -05:00
|
|
|
create_bad_json(nest_git);
|
|
|
|
create_bad_json(git_dir);
|
|
|
|
create_bad_json(nest_node_modules);
|
|
|
|
create_bad_json(node_modules_dir);
|
2023-03-13 09:40:46 -04:00
|
|
|
|
|
|
|
let context = TestContext::default();
|
|
|
|
let output = context
|
|
|
|
.new_command()
|
2023-06-10 11:09:45 -04:00
|
|
|
.cwd(t)
|
2023-01-12 20:59:13 -05:00
|
|
|
.env("NO_COLOR", "1")
|
2023-03-13 09:40:46 -04:00
|
|
|
.args("fmt")
|
|
|
|
.run();
|
|
|
|
|
|
|
|
output.assert_exit_code(1);
|
|
|
|
assert_eq!(output.combined_output(), "error: No target files found.\n");
|
2023-01-12 20:59:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
itest!(fmt_quiet_check_fmt_dir {
|
|
|
|
args: "fmt --check --quiet fmt/regular/",
|
|
|
|
output_str: Some(""),
|
|
|
|
exit_code: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_check_formatted_files {
|
|
|
|
args: "fmt --check fmt/regular/formatted1.js fmt/regular/formatted2.ts fmt/regular/formatted3.markdown fmt/regular/formatted4.jsonc",
|
|
|
|
output: "fmt/expected_fmt_check_formatted_files.out",
|
|
|
|
exit_code: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_check_ignore {
|
|
|
|
args: "fmt --check --ignore=fmt/regular/formatted1.js fmt/regular/",
|
|
|
|
output: "fmt/expected_fmt_check_ignore.out",
|
|
|
|
exit_code: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_check_parse_error {
|
|
|
|
args: "fmt --check fmt/parse_error/parse_error.ts",
|
|
|
|
output: "fmt/fmt_check_parse_error.out",
|
|
|
|
exit_code: 1,
|
|
|
|
});
|
|
|
|
|
2023-01-15 19:30:52 -05:00
|
|
|
itest!(fmt_check_invalid_data {
|
|
|
|
args: "fmt --check fmt/invalid_data.json",
|
|
|
|
output: "fmt/invalid_data.out",
|
|
|
|
exit_code: 1,
|
|
|
|
});
|
|
|
|
|
2023-01-12 20:59:13 -05:00
|
|
|
itest!(fmt_stdin {
|
|
|
|
args: "fmt -",
|
|
|
|
input: Some("const a = 1\n"),
|
|
|
|
output_str: Some("const a = 1;\n"),
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_stdin_markdown {
|
|
|
|
args: "fmt --ext=md -",
|
|
|
|
input: Some("# Hello Markdown\n```ts\nconsole.log( \"text\")\n```\n\n```cts\nconsole.log( 5 )\n```"),
|
|
|
|
output_str: Some("# Hello Markdown\n\n```ts\nconsole.log(\"text\");\n```\n\n```cts\nconsole.log(5);\n```\n"),
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_stdin_json {
|
|
|
|
args: "fmt --ext=json -",
|
|
|
|
input: Some("{ \"key\": \"value\"}"),
|
|
|
|
output_str: Some("{ \"key\": \"value\" }\n"),
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_stdin_check_formatted {
|
|
|
|
args: "fmt --check -",
|
|
|
|
input: Some("const a = 1;\n"),
|
|
|
|
output_str: Some(""),
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_stdin_check_not_formatted {
|
|
|
|
args: "fmt --check -",
|
|
|
|
input: Some("const a = 1\n"),
|
|
|
|
output_str: Some("Not formatted stdin\n"),
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_with_config {
|
|
|
|
args: "fmt --config fmt/with_config/deno.jsonc fmt/with_config/subdir",
|
|
|
|
output: "fmt/fmt_with_config.out",
|
|
|
|
});
|
|
|
|
|
2023-04-26 23:02:36 -04:00
|
|
|
itest!(fmt_with_deprecated_config {
|
|
|
|
args:
|
|
|
|
"fmt --config fmt/with_config/deno.deprecated.jsonc fmt/with_config/subdir",
|
|
|
|
output: "fmt/fmt_with_deprecated_config.out",
|
|
|
|
});
|
|
|
|
|
2023-01-12 20:59:13 -05:00
|
|
|
itest!(fmt_with_config_default {
|
|
|
|
args: "fmt fmt/with_config/subdir",
|
|
|
|
output: "fmt/fmt_with_config.out",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check if CLI flags take precedence
|
|
|
|
itest!(fmt_with_config_and_flags {
|
|
|
|
args: "fmt --config fmt/with_config/deno.jsonc --ignore=fmt/with_config/subdir/a.ts,fmt/with_config/subdir/b.ts",
|
|
|
|
output: "fmt/fmt_with_config_and_flags.out",
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_with_malformed_config {
|
|
|
|
args: "fmt --config fmt/deno.malformed.jsonc",
|
|
|
|
output: "fmt/fmt_with_malformed_config.out",
|
|
|
|
exit_code: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
itest!(fmt_with_malformed_config2 {
|
|
|
|
args: "fmt --config fmt/deno.malformed2.jsonc",
|
|
|
|
output: "fmt/fmt_with_malformed_config2.out",
|
|
|
|
exit_code: 1,
|
|
|
|
});
|
2023-05-22 21:39:59 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fmt_with_glob_config() {
|
|
|
|
let context = TestContextBuilder::new().cwd("fmt").build();
|
|
|
|
|
|
|
|
let cmd_output = context
|
|
|
|
.new_command()
|
|
|
|
.args("fmt --check --config deno.glob.json")
|
|
|
|
.run();
|
|
|
|
|
|
|
|
cmd_output.assert_exit_code(1);
|
|
|
|
|
|
|
|
let output = cmd_output.combined_output();
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_contains!(output, r#"glob\nested\fizz\fizz.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\pages\[id].ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\fizz\bar.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\foo\foo.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\data\test1.js"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\foo\bar.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\foo\fizz.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\fizz\foo.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\data\test1.ts"#);
|
|
|
|
} else {
|
|
|
|
assert_contains!(output, "glob/nested/fizz/fizz.ts");
|
|
|
|
assert_contains!(output, "glob/pages/[id].ts");
|
|
|
|
assert_contains!(output, "glob/nested/fizz/bar.ts");
|
|
|
|
assert_contains!(output, "glob/nested/foo/foo.ts");
|
|
|
|
assert_contains!(output, "glob/data/test1.js");
|
|
|
|
assert_contains!(output, "glob/nested/foo/bar.ts");
|
|
|
|
assert_contains!(output, "glob/nested/foo/fizz.ts");
|
|
|
|
assert_contains!(output, "glob/nested/fizz/foo.ts");
|
|
|
|
assert_contains!(output, "glob/data/test1.ts");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_contains!(output, "Found 9 not formatted files in 9 files");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fmt_with_glob_config_and_flags() {
|
|
|
|
let context = TestContextBuilder::new().cwd("fmt").build();
|
|
|
|
|
|
|
|
let cmd_output = context
|
|
|
|
.new_command()
|
|
|
|
.args("fmt --check --config deno.glob.json --ignore=glob/nested/**/bar.ts")
|
|
|
|
.run();
|
|
|
|
|
|
|
|
cmd_output.assert_exit_code(1);
|
|
|
|
|
|
|
|
let output = cmd_output.combined_output();
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_contains!(output, r#"glob\nested\fizz\fizz.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\pages\[id].ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\fizz\bazz.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\foo\foo.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\data\test1.js"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\foo\bazz.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\foo\fizz.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\nested\fizz\foo.ts"#);
|
|
|
|
assert_contains!(output, r#"glob\data\test1.ts"#);
|
|
|
|
} else {
|
|
|
|
assert_contains!(output, "glob/nested/fizz/fizz.ts");
|
|
|
|
assert_contains!(output, "glob/pages/[id].ts");
|
|
|
|
assert_contains!(output, "glob/nested/fizz/bazz.ts");
|
|
|
|
assert_contains!(output, "glob/nested/foo/foo.ts");
|
|
|
|
assert_contains!(output, "glob/data/test1.js");
|
|
|
|
assert_contains!(output, "glob/nested/foo/bazz.ts");
|
|
|
|
assert_contains!(output, "glob/nested/foo/fizz.ts");
|
|
|
|
assert_contains!(output, "glob/nested/fizz/foo.ts");
|
|
|
|
assert_contains!(output, "glob/data/test1.ts");
|
|
|
|
}
|
|
|
|
assert_contains!(output, "Found 9 not formatted files in 9 files");
|
|
|
|
let cmd_output = context
|
|
|
|
.new_command()
|
|
|
|
.args("fmt --check --config deno.glob.json glob/data/test1.?s")
|
|
|
|
.run();
|
|
|
|
|
|
|
|
cmd_output.assert_exit_code(1);
|
|
|
|
|
|
|
|
let output = cmd_output.combined_output();
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_contains!(output, r#"glob\data\test1.js"#);
|
|
|
|
assert_contains!(output, r#"glob\data\test1.ts"#);
|
|
|
|
} else {
|
|
|
|
assert_contains!(output, "glob/data/test1.js");
|
|
|
|
assert_contains!(output, "glob/data/test1.ts");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_contains!(output, "Found 2 not formatted files in 2 files");
|
|
|
|
}
|