1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use test_util as util;
use test_util::TempDir;
use util::TestContext;
#[test]
fn fmt_test() {
let context = TestContext::default();
let t = context.deno_dir();
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");
let badly_formatted_js_str = badly_formatted_js.to_str().unwrap();
std::fs::copy(badly_formatted_original_js, &badly_formatted_js).unwrap();
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");
let badly_formatted_md_str = badly_formatted_md.to_str().unwrap();
std::fs::copy(badly_formatted_original_md, &badly_formatted_md).unwrap();
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");
let badly_formatted_json_str = badly_formatted_json.to_str().unwrap();
std::fs::copy(badly_formatted_original_json, &badly_formatted_json).unwrap();
// First, check formatting by ignoring the badly formatted file.
let s = testdata_fmt_dir.as_os_str().to_str().unwrap();
let output = context
.new_command()
.cwd(s)
.args_vec(vec![
"fmt".to_string(),
format!(
"--ignore={badly_formatted_js_str},{badly_formatted_md_str},{badly_formatted_json_str}",
),
format!(
"--check {badly_formatted_js_str} {badly_formatted_md_str} {badly_formatted_json_str}",
),
])
.run();
// No target files found
output.assert_exit_code(1);
output.skip_output_check();
// Check without ignore.
let output = context
.new_command()
.cwd(s)
.args_vec(vec![
"fmt".to_string(),
"--check".to_string(),
badly_formatted_js_str.to_string(),
badly_formatted_md_str.to_string(),
badly_formatted_json_str.to_string(),
])
.run();
output.assert_exit_code(1);
output.skip_output_check();
// Format the source file.
let output = context
.new_command()
.cwd(s)
.args_vec(vec![
"fmt".to_string(),
badly_formatted_js_str.to_string(),
badly_formatted_md_str.to_string(),
badly_formatted_json_str.to_string(),
])
.run();
output.assert_exit_code(0);
output.skip_output_check();
let expected_js = std::fs::read_to_string(fixed_js).unwrap();
let expected_md = std::fs::read_to_string(fixed_md).unwrap();
let expected_json = std::fs::read_to_string(fixed_json).unwrap();
let actual_js = std::fs::read_to_string(badly_formatted_js).unwrap();
let actual_md = std::fs::read_to_string(badly_formatted_md).unwrap();
let actual_json = std::fs::read_to_string(badly_formatted_json).unwrap();
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() {
let context = TestContext::default();
let output = context
.new_command()
.env("NO_COLOR", "1")
.args("fmt --check --ignore=./")
.run();
output.assert_exit_code(1);
assert_eq!(output.combined_output(), "error: No target files found.\n");
}
#[test]
fn fmt_auto_ignore_git_and_node_modules() {
use std::fs::create_dir_all;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn create_bad_json(t: PathBuf) {
let bad_json_path = t.join("bad.json");
let mut bad_json_file = File::create(bad_json_path).unwrap();
writeln!(bad_json_file, "bad json").unwrap();
}
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");
create_dir_all(&nest_git).unwrap();
create_dir_all(&git_dir).unwrap();
create_dir_all(&nest_node_modules).unwrap();
create_dir_all(&node_modules_dir).unwrap();
create_bad_json(nest_git);
create_bad_json(git_dir);
create_bad_json(nest_node_modules);
create_bad_json(node_modules_dir);
let context = TestContext::default();
let output = context
.new_command()
.cwd(t.as_os_str().to_str().unwrap())
.env("NO_COLOR", "1")
.args("fmt")
.run();
output.assert_exit_code(1);
assert_eq!(output.combined_output(), "error: No target files found.\n");
}
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,
});
itest!(fmt_check_invalid_data {
args: "fmt --check fmt/invalid_data.json",
output: "fmt/invalid_data.out",
exit_code: 1,
});
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",
});
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",
});
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,
});
|