Debian Codegraph @

cli/tests/test_help_command.rs

raw
1 // Copyright 2024 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use crate::common::TestEnvironment;
16
17 #[test]
18 fn test_help() {
19 let test_env = TestEnvironment::default();
20
21 let help_cmd = test_env.run_jj_in(".", ["help"]).success();
22 // The help command output should be equal to the long --help flag
23 let help_flag = test_env.run_jj_in(".", ["--help"]);
24 // TODO: fix --help to not show COMMAND as required
25 assert_eq!(
26 help_cmd.stdout.raw(),
27 help_flag.stdout.raw().replace("<COMMAND>", "[COMMAND]")
28 );
29 assert_eq!(help_cmd.stderr.raw(), help_flag.stderr.raw());
30
31 // Help command should work with commands
32 let help_cmd = test_env.run_jj_in(".", ["help", "log"]).success();
33 let help_flag = test_env.run_jj_in(".", ["log", "--help"]);
34 assert_eq!(help_cmd, help_flag);
35
36 // Help command should work with subcommands
37 let help_cmd = test_env
38 .run_jj_in(".", ["help", "workspace", "root"])
39 .success();
40 let help_flag = test_env.run_jj_in(".", ["workspace", "root", "--help"]);
41 assert_eq!(help_cmd, help_flag);
42
43 // Help command should not work recursively
44 let output = test_env.run_jj_in(".", ["workspace", "help", "root"]);
45 insta::assert_snapshot!(output, @"
46 ------- stderr -------
47 error: unrecognized subcommand 'help'
48
49 Usage: jj workspace [OPTIONS] <COMMAND>
50
51 For more information, try '--help'.
52 [EOF]
53 [exit status: 2]
54 ");
55
56 let output = test_env.run_jj_in(".", ["workspace", "add", "help"]);
57 insta::assert_snapshot!(output, @r#"
58 ------- stderr -------
59 Error: There is no jj repo in "."
60 [EOF]
61 [exit status: 1]
62 "#);
63
64 let output = test_env.run_jj_in(".", ["new", "help", "main"]);
65 insta::assert_snapshot!(output, @r#"
66 ------- stderr -------
67 Error: There is no jj repo in "."
68 [EOF]
69 [exit status: 1]
70 "#);
71
72 // Help command should output the same as --help for nonexistent commands
73 let help_cmd = test_env.run_jj_in(".", ["help", "nonexistent"]);
74 let help_flag = test_env.run_jj_in(".", ["nonexistent", "--help"]);
75 assert_eq!(help_cmd.status.code(), Some(2), "{help_cmd}");
76 // TODO: fix --help to not show COMMAND as required
77 assert_eq!(help_cmd.stdout.raw(), help_flag.stdout.raw());
78 assert_eq!(
79 help_cmd.stderr.raw(),
80 help_flag.stderr.raw().replace("<COMMAND>", "[COMMAND]")
81 );
82
83 // Some edge cases
84 let help_cmd = test_env.run_jj_in(".", ["help", "help"]).success();
85 let help_flag = test_env.run_jj_in(".", ["help", "--help"]);
86 assert_eq!(help_cmd, help_flag);
87
88 let output = test_env.run_jj_in(".", ["help", "unknown"]);
89 insta::assert_snapshot!(output, @"
90 ------- stderr -------
91 error: unrecognized subcommand 'unknown'
92
93 tip: a similar subcommand exists: 'undo'
94
95 Usage: jj [OPTIONS] [COMMAND]
96
97 For more information, try '--help'.
98 [EOF]
99 [exit status: 2]
100 ");
101
102 let output = test_env.run_jj_in(".", ["help", "log", "--", "-r"]);
103 insta::assert_snapshot!(output, @"
104 ------- stderr -------
105 Error: Unknown command: log -r
106 [EOF]
107 [exit status: 2]
108 ");
109 }
110
111 #[test]
112 fn test_help_keyword() {
113 let test_env = TestEnvironment::default();
114
115 // It should show help for a certain keyword if the `--keyword` flag is present
116 let help_cmd = test_env
117 .run_jj_in(".", ["help", "--keyword", "revsets"])
118 .success();
119 // It should be equal to the docs
120 assert_eq!(help_cmd.stdout.raw(), include_str!("../../docs/revsets.md"));
121
122 // It should show help for a certain keyword if the `-k` flag is present
123 let help_cmd = test_env.run_jj_in(".", ["help", "-k", "revsets"]).success();
124 // It should be equal to the docs
125 assert_eq!(help_cmd.stdout.raw(), include_str!("../../docs/revsets.md"));
126
127 // It should give hints if a similar keyword is present
128 let output = test_env.run_jj_in(".", ["help", "-k", "rev"]);
129 insta::assert_snapshot!(output, @"
130 ------- stderr -------
131 error: invalid value 'rev' for '--keyword <KEYWORD>'
132 [possible values: bookmarks, config, filesets, glossary, revsets, templates, tutorial]
133
134 tip: a similar value exists: 'revsets'
135
136 For more information, try '--help'.
137 [EOF]
138 [exit status: 2]
139 ");
140
141 // It should give error with a hint if no similar keyword is found
142 let output = test_env.run_jj_in(".", ["help", "-k", "<no-similar-keyword>"]);
143 insta::assert_snapshot!(output, @"
144 ------- stderr -------
145 error: invalid value '<no-similar-keyword>' for '--keyword <KEYWORD>'
146 [possible values: bookmarks, config, filesets, glossary, revsets, templates, tutorial]
147
148 For more information, try '--help'.
149 [EOF]
150 [exit status: 2]
151 ");
152
153 // The keyword flag with no argument should error with a hint
154 let output = test_env.run_jj_in(".", ["help", "-k"]);
155 insta::assert_snapshot!(output, @"
156 ------- stderr -------
157 error: a value is required for '--keyword <KEYWORD>' but none was supplied
158 [possible values: bookmarks, config, filesets, glossary, revsets, templates, tutorial]
159
160 For more information, try '--help'.
161 [EOF]
162 [exit status: 2]
163 ");
164
165 // It shouldn't show help for a certain keyword if the `--keyword` is not
166 // present
167 let output = test_env.run_jj_in(".", ["help", "revsets"]);
168 insta::assert_snapshot!(output, @"
169 ------- stderr -------
170 error: unrecognized subcommand 'revsets'
171
172 tip: some similar subcommands exist: 'resolve', 'prev', 'restore', 'rebase', 'revert'
173
174 Usage: jj [OPTIONS] [COMMAND]
175
176 For more information, try '--help'.
177 [EOF]
178 [exit status: 2]
179 ");
180 }