jj/cli/tests/test_help_command.rs
Arthur Grillo f2acb98ef2
Some checks are pending
binaries / Build binary artifacts (linux-aarch64-gnu, ubuntu-24.04, aarch64-unknown-linux-gnu) (push) Waiting to run
binaries / Build binary artifacts (linux-aarch64-musl, ubuntu-24.04, aarch64-unknown-linux-musl) (push) Waiting to run
binaries / Build binary artifacts (linux-x86_64-gnu, ubuntu-24.04, x86_64-unknown-linux-gnu) (push) Waiting to run
binaries / Build binary artifacts (linux-x86_64-musl, ubuntu-24.04, x86_64-unknown-linux-musl) (push) Waiting to run
binaries / Build binary artifacts (macos-aarch64, macos-14, aarch64-apple-darwin) (push) Waiting to run
binaries / Build binary artifacts (macos-x86_64, macos-13, x86_64-apple-darwin) (push) Waiting to run
binaries / Build binary artifacts (win-x86_64, windows-2022, x86_64-pc-windows-msvc) (push) Waiting to run
nix / flake check (macos-14) (push) Waiting to run
nix / flake check (ubuntu-latest) (push) Waiting to run
build / build (, macos-13) (push) Waiting to run
build / build (, macos-14) (push) Waiting to run
build / build (, ubuntu-latest) (push) Waiting to run
build / build (, windows-latest) (push) Waiting to run
build / build (--all-features, ubuntu-latest) (push) Waiting to run
build / Build jj-lib without Git support (push) Waiting to run
build / Check protos (push) Waiting to run
build / Check formatting (push) Waiting to run
build / Check that MkDocs can build the docs (push) Waiting to run
build / Check that MkDocs can build the docs with Poetry 1.8 (push) Waiting to run
build / cargo-deny (advisories) (push) Waiting to run
build / cargo-deny (bans licenses sources) (push) Waiting to run
build / Clippy check (push) Waiting to run
Codespell / Codespell (push) Waiting to run
website / prerelease-docs-build-deploy (ubuntu-latest) (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
help: Add a keyword feature
It would be nice to not need to go the documentation website. This aims
to solve that by introducing the concept of keyword to the help
command.

Basically, keywords are things that we want to add help messages to,
but they don't necessarily have an associated subcommand.

For now we only have two keywords:
	- `revsets`: Shows the docs for revsets
	- `tutorial`: Shows the tutorial that is on the documentation

You get the keyword content by tipping `jj help --keyword revsets` or
`jj help -k revsets`.

You can also list the available keywords with `jj help --help`.

It would be nice to have all the documentation on the keywords, maybe
a next commit could do it.
2024-10-31 16:55:18 -03:00

145 lines
5.7 KiB
Rust

// Copyright 2024 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 crate::common::TestEnvironment;
#[test]
fn test_help() {
let test_env = TestEnvironment::default();
let help_cmd_stdout = test_env.jj_cmd_success(test_env.env_root(), &["help"]);
// The help command output should be equal to the long --help flag
let help_flag_stdout = test_env.jj_cmd_success(test_env.env_root(), &["--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);
// Help command should work with commands
let help_cmd_stdout = test_env.jj_cmd_success(test_env.env_root(), &["help", "log"]);
let help_flag_stdout = test_env.jj_cmd_success(test_env.env_root(), &["log", "--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);
// Help command should work with subcommands
let help_cmd_stdout =
test_env.jj_cmd_success(test_env.env_root(), &["help", "workspace", "root"]);
let help_flag_stdout =
test_env.jj_cmd_success(test_env.env_root(), &["workspace", "root", "--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);
// Help command should not work recursively
let stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["workspace", "help", "root"]);
insta::assert_snapshot!(stderr, @r#"
error: unrecognized subcommand 'help'
Usage: jj workspace [OPTIONS] <COMMAND>
For more information, try '--help'.
"#);
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["workspace", "add", "help"]);
insta::assert_snapshot!(stderr, @r#"
Error: There is no jj repo in "."
"#);
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["new", "help", "main"]);
insta::assert_snapshot!(stderr, @r#"
Error: There is no jj repo in "."
"#);
// Help command should output the same as --help for nonexistent commands
let help_cmd_stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["help", "nonexistent"]);
let help_flag_stderr =
test_env.jj_cmd_cli_error(test_env.env_root(), &["nonexistent", "--help"]);
assert_eq!(help_cmd_stderr, help_flag_stderr);
// Some edge cases
let help_cmd_stdout = test_env.jj_cmd_success(test_env.env_root(), &["help", "help"]);
let help_flag_stdout = test_env.jj_cmd_success(test_env.env_root(), &["help", "--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);
let stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["help", "unknown"]);
insta::assert_snapshot!(stderr, @r#"
error: unrecognized subcommand 'unknown'
tip: a similar subcommand exists: 'undo'
Usage: jj [OPTIONS] <COMMAND>
For more information, try '--help'.
"#);
let stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["help", "log", "--", "-r"]);
insta::assert_snapshot!(stderr, @r#"
error: a value is required for '--revisions <REVISIONS>' but none was supplied
For more information, try '--help'.
"#);
}
#[test]
fn test_help_keyword() {
let test_env = TestEnvironment::default();
// It should show help for a certain keyword if the `--keyword` flag is present
let help_cmd_stdout =
test_env.jj_cmd_success(test_env.env_root(), &["help", "--keyword", "revsets"]);
// It should be equal to the docs
assert_eq!(help_cmd_stdout, include_str!("../../docs/revsets.md"));
// It should show help for a certain keyword if the `-k` flag is present
let help_cmd_stdout = test_env.jj_cmd_success(test_env.env_root(), &["help", "-k", "revsets"]);
// It should be equal to the docs
assert_eq!(help_cmd_stdout, include_str!("../../docs/revsets.md"));
// It should give hints if a similar keyword is present
let help_cmd_stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["help", "-k", "rev"]);
insta::assert_snapshot!(help_cmd_stderr, @r#"
error: invalid value 'rev' for '--keyword <KEYWORD>'
[possible values: revsets, tutorial]
tip: a similar value exists: 'revsets'
For more information, try '--help'.
"#);
// It should give error with a hint if no similar keyword is found
let help_cmd_stderr =
test_env.jj_cmd_cli_error(test_env.env_root(), &["help", "-k", "<no-similar-keyword>"]);
insta::assert_snapshot!(help_cmd_stderr, @r#"
error: invalid value '<no-similar-keyword>' for '--keyword <KEYWORD>'
[possible values: revsets, tutorial]
For more information, try '--help'.
"#);
// The keyword flag with no argument should error with a hint
let help_cmd_stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["help", "-k"]);
insta::assert_snapshot!(help_cmd_stderr, @r#"
error: a value is required for '--keyword <KEYWORD>' but none was supplied
[possible values: revsets, tutorial]
For more information, try '--help'.
"#);
// It shouldn't show help for a certain keyword if the `--keyword` is not
// present
let help_cmd_stderr = test_env.jj_cmd_cli_error(test_env.env_root(), &["help", "revsets"]);
insta::assert_snapshot!(help_cmd_stderr, @r#"
error: unrecognized subcommand 'revsets'
tip: some similar subcommands exist: 'resolve', 'prev', 'restore', 'rebase', 'revert'
Usage: jj [OPTIONS] <COMMAND>
For more information, try '--help'.
"#);
}