-
Notifications
You must be signed in to change notification settings - Fork 236
/
test_global_opts.rs
424 lines (367 loc) · 15.8 KB
/
test_global_opts.rs
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2022 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::ffi::OsString;
use itertools::Itertools;
use crate::common::{get_stderr_string, TestEnvironment};
pub mod common;
#[test]
fn test_non_utf8_arg() {
let test_env = TestEnvironment::default();
#[cfg(unix)]
let invalid_utf = {
use std::os::unix::ffi::OsStringExt;
OsString::from_vec(vec![0x66, 0x6f, 0x80, 0x6f])
};
#[cfg(windows)]
let invalid_utf = {
use std::os::windows::prelude::*;
OsString::from_wide(&[0x0066, 0x006f, 0xD800, 0x006f])
};
let assert = test_env
.jj_cmd(test_env.env_root(), &[])
.args(&[invalid_utf])
.assert()
.code(2);
let stderr = get_stderr_string(&assert);
insta::assert_snapshot!(stderr, @r###"
Error: Non-utf8 argument
"###);
}
#[test]
fn test_no_subcommand() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
// Outside of a repo.
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &[]);
insta::assert_snapshot!(stderr, @r###"
Error: There is no jj repo in "."
"###);
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--help"]);
insta::assert_snapshot!(stdout.lines().next().unwrap(), @"Jujutsu (An experimental VCS)");
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--version"]);
let sanitized = stdout.replace(|c: char| c.is_ascii_hexdigit(), "?");
assert!(
sanitized == "jj ?.?.?\n"
|| sanitized == "jj ?.?.?-????????????????????????????????????????\n",
"{sanitized}"
);
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["-R", "repo"]);
assert_eq!(stdout, test_env.jj_cmd_success(&repo_path, &["log"]));
// Inside of a repo.
let stdout = test_env.jj_cmd_success(&repo_path, &[]);
assert_eq!(stdout, test_env.jj_cmd_success(&repo_path, &["log"]));
}
#[test]
fn test_ignore_working_copy() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
std::fs::write(repo_path.join("file"), "initial").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ 438471f3fbf1004298d8fb01eeb13663a051a643
◉ 0000000000000000000000000000000000000000
"###);
// Modify the file. With --ignore-working-copy, we still get the same commit
// ID.
std::fs::write(repo_path.join("file"), "modified").unwrap();
let stdout_again = test_env.jj_cmd_success(
&repo_path,
&["log", "-T", "commit_id", "--ignore-working-copy"],
);
assert_eq!(stdout_again, stdout);
// But without --ignore-working-copy, we get a new commit ID.
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ fab22d1acf5bb9c5aa48cb2c3dd2132072a359ca
◉ 0000000000000000000000000000000000000000
"###);
}
#[test]
fn test_repo_arg_with_init() {
let test_env = TestEnvironment::default();
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["init", "-R=.", "repo"]);
insta::assert_snapshot!(stderr, @r###"
Error: '--repository' cannot be used with 'init'
"###);
}
#[test]
fn test_repo_arg_with_git_clone() {
let test_env = TestEnvironment::default();
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["git", "clone", "-R=.", "remote"]);
insta::assert_snapshot!(stderr, @r###"
Error: '--repository' cannot be used with 'git clone'
"###);
}
#[test]
fn test_resolve_workspace_directory() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let subdir = repo_path.join("dir").join("subdir");
std::fs::create_dir_all(&subdir).unwrap();
// Ancestor of cwd
let stdout = test_env.jj_cmd_success(&subdir, &["status"]);
insta::assert_snapshot!(stdout, @r###"
Parent commit: 000000000000 (no description set)
Working copy : 230dd059e1b0 (no description set)
The working copy is clean
"###);
// Explicit subdirectory path
let stderr = test_env.jj_cmd_failure(&subdir, &["status", "-R", "."]);
insta::assert_snapshot!(stderr, @r###"
Error: There is no jj repo in "."
"###);
// Valid explicit path
let stdout = test_env.jj_cmd_success(&subdir, &["status", "-R", "../.."]);
insta::assert_snapshot!(stdout, @r###"
Parent commit: 000000000000 (no description set)
Working copy : 230dd059e1b0 (no description set)
The working copy is clean
"###);
// "../../..".ancestors() contains "../..", but it should never be looked up.
let stderr = test_env.jj_cmd_failure(&subdir, &["status", "-R", "../../.."]);
insta::assert_snapshot!(stderr, @r###"
Error: There is no jj repo in "../../.."
"###);
}
#[test]
fn test_no_workspace_directory() {
let test_env = TestEnvironment::default();
let repo_path = test_env.env_root().join("repo");
std::fs::create_dir(&repo_path).unwrap();
let stderr = test_env.jj_cmd_failure(&repo_path, &["status"]);
insta::assert_snapshot!(stderr, @r###"
Error: There is no jj repo in "."
"###);
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["status", "-R", "repo"]);
insta::assert_snapshot!(stderr, @r###"
Error: There is no jj repo in "repo"
"###);
std::fs::create_dir(repo_path.join(".git")).unwrap();
let stderr = test_env.jj_cmd_failure(&repo_path, &["status"]);
insta::assert_snapshot!(stderr, @r###"
Error: There is no jj repo in "."
Hint: It looks like this is a git repo. You can create a jj repo backed by it by running this:
jj init --git-repo=.
"###);
}
#[test]
fn test_broken_repo_structure() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let store_type_path = repo_path
.join(".jj")
.join("repo")
.join("store")
.join("type");
// Test the error message when the commit backend is of unknown type.
std::fs::write(&store_type_path, "unknown").unwrap();
let stderr = test_env.jj_cmd_internal_error(&repo_path, &["log"]);
insta::assert_snapshot!(stderr, @r###"
Internal error: This version of the jj binary doesn't support this type of repo: Unsupported commit backend type 'unknown'
"###);
// Test the error message when the file indicating the commit backend type
// cannot be read.
std::fs::remove_file(&store_type_path).unwrap();
std::fs::create_dir(&store_type_path).unwrap();
let stderr = test_env.jj_cmd_internal_error(&repo_path, &["log"]);
// Trim off the OS-specific error message.
let stderr = stderr.split(':').take(3).join(":");
insta::assert_snapshot!(stderr, @"Internal error: The repository appears broken or inaccessible: Failed to read commit backend type");
}
#[test]
fn test_color_config() {
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
// Test that --color=always is respected.
let stdout = test_env.jj_cmd_success(&repo_path, &["--color=always", "log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ [38;5;4m230dd059e1b059aefc0da06a2e5a7dbf22362f22[39m
◉ [38;5;4m0000000000000000000000000000000000000000[39m
"###);
// Test that color is used if it's requested in the config file
test_env.add_config(r#"ui.color="always""#);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ [38;5;4m230dd059e1b059aefc0da06a2e5a7dbf22362f22[39m
◉ [38;5;4m0000000000000000000000000000000000000000[39m
"###);
// Test that --color=never overrides the config.
let stdout = test_env.jj_cmd_success(&repo_path, &["--color=never", "log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◉ 0000000000000000000000000000000000000000
"###);
// Test that --color=auto overrides the config.
let stdout = test_env.jj_cmd_success(&repo_path, &["--color=auto", "log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◉ 0000000000000000000000000000000000000000
"###);
// Test that --config-toml 'ui.color="never"' overrides the config.
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"--config-toml",
"ui.color=\"never\"",
"log",
"-T",
"commit_id",
],
);
insta::assert_snapshot!(stdout, @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◉ 0000000000000000000000000000000000000000
"###);
// --color overrides --config-toml 'ui.color=...'.
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"--color",
"never",
"--config-toml",
"ui.color=\"always\"",
"log",
"-T",
"commit_id",
],
);
insta::assert_snapshot!(stdout, @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◉ 0000000000000000000000000000000000000000
"###);
// Test that NO_COLOR does NOT override the request for color in the config file
test_env.add_env_var("NO_COLOR", "");
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ [38;5;4m230dd059e1b059aefc0da06a2e5a7dbf22362f22[39m
◉ [38;5;4m0000000000000000000000000000000000000000[39m
"###);
// Test that per-repo config overrides the user config.
std::fs::write(
repo_path.join(".jj/repo/config.toml"),
r#"ui.color = "never""#,
)
.unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◉ 0000000000000000000000000000000000000000
"###);
}
#[test]
fn test_early_args() {
// Test that help output parses early args
let test_env = TestEnvironment::default();
// The default is no color.
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["help"]);
insta::assert_snapshot!(stdout.lines().next().unwrap(), @"Jujutsu (An experimental VCS)");
// Check that output is colorized.
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--color=always", "help"]);
insta::assert_snapshot!(stdout.lines().next().unwrap(), @"[0mJujutsu (An experimental VCS)");
// Early args are parsed with clap's ignore_errors(), but there is a known
// bug that causes defaults to be unpopulated. Test that the early args are
// tolerant of this bug and don't cause a crash.
test_env.jj_cmd_success(test_env.env_root(), &["--no-pager", "help"]);
test_env.jj_cmd_success(
test_env.env_root(),
&["--config-toml", "ui.color = 'always'", "help"],
);
}
#[test]
fn test_invalid_config() {
// Test that we get a reasonable error if the config is invalid (#55)
let test_env = TestEnvironment::default();
test_env.add_config("[section]key = value-missing-quotes");
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["init", "repo"]);
insta::assert_snapshot!(stderr.replace('\\', "/"), @r###"
Config error: expected newline, found an identifier at line 1 column 10 in config/config0002.toml
For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md.
"###);
}
#[test]
fn test_no_user_configured() {
// Test that the user is reminded if they haven't configured their name or email
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let assert = test_env
.jj_cmd(&repo_path, &["describe", "-m", "without name"])
.env_remove("JJ_USER")
.assert()
.success();
insta::assert_snapshot!(get_stderr_string(&assert), @r###"
Name and email not configured. Until configured, your commits will be created with the empty identity, and can't be pushed to remotes. To configure, add something like the following to $HOME/.jjconfig.toml:
user.name = "Some One"
user.email = "someone@example.com"
"###);
let assert = test_env
.jj_cmd(&repo_path, &["describe", "-m", "without email"])
.env_remove("JJ_EMAIL")
.assert()
.success();
insta::assert_snapshot!(get_stderr_string(&assert), @r###"
Name and email not configured. Until configured, your commits will be created with the empty identity, and can't be pushed to remotes. To configure, add something like the following to $HOME/.jjconfig.toml:
user.name = "Some One"
user.email = "someone@example.com"
"###);
}
#[test]
fn test_help() {
// Test that global options are separated out in the help output
let test_env = TestEnvironment::default();
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["diffedit", "-h"]);
insta::assert_snapshot!(stdout, @r###"
Touch up the content changes in a revision with a diff editor
Usage: jj diffedit [OPTIONS]
Options:
-r, --revision <REVISION> The revision to touch up. Defaults to @ if --to/--from are not specified
--from <FROM> Show changes from this revision. Defaults to @ if --to is specified
--to <TO> Edit changes in this revision. Defaults to @ if --from is specified
-h, --help Print help (see more with '--help')
Global Options:
-R, --repository <REPOSITORY> Path to repository to operate on
--ignore-working-copy Don't snapshot the working copy, and don't update it
--at-operation <AT_OPERATION> Operation to load the repo at [default: @] [aliases: at-op]
-v, --verbose Enable verbose logging
--color <WHEN> When to colorize output (always, never, auto)
--no-pager Disable the pager
--config-toml <TOML> Additional configuration options
"###);
}
#[test]
fn test_verbose_logging_enabled() {
// Test that the verbose flag enabled verbose logging
let test_env = TestEnvironment::default();
let assert = test_env
.jj_cmd(test_env.env_root(), &["version", "-v"])
.assert()
.success();
let stderr = get_stderr_string(&assert);
// Split the first log line into a timestamp and the rest.
// The timestamp is constant sized so this is a robust operation.
// Example timestamp: 2022-11-20T06:24:05.477703Z
let (_timestamp, log_line) = stderr
.lines()
.next()
.expect("verbose logging on first line")
.split_at(36);
// The log format is currently Pretty so we include the terminal markup.
// Luckily, insta will print this in colour when reviewing.
insta::assert_snapshot!(log_line, @"[34mDEBUG[0m [2mjujutsu::cli_util[0m[2m:[0m verbose logging enabled");
}