Skip to content

Commit

Permalink
Merge pull request #5754 from epage/shell
Browse files Browse the repository at this point in the history
test: Make shell integration tests non-blocking
  • Loading branch information
epage committed Sep 27, 2024
2 parents 4621a49 + 9e4fc9b commit fd31a1b
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 10 deletions.
32 changes: 29 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
- uses: Swatinem/rust-cache@v2
- name: Install shells
if: runner.os == 'Linux'
run: sudo apt-get install -y elvish fish zsh
- name: Build
run: make build-${{matrix.features}}
- name: Test
Expand All @@ -84,6 +81,35 @@ jobs:
run: make test-minimal ARGS='--manifest-path Cargo.toml'
- name: Test dynamic completions
run: cargo test -p clap_complete -F unstable-dynamic
shell-integration:
name: Shell Integration
runs-on: buildjet-8vcpu-ubuntu-2204
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Install shells
if: runner.os == 'Linux'
run: sudo apt-get install -y elvish fish zsh
- name: clap_complete
run: cargo test -p clap_complete -F unstable-dynamic -F unstable-shell-tests
shell-integration-nu:
name: Nushell Integration
runs-on: buildjet-8vcpu-ubuntu-2204
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: clap_complete_nu
run: cargo test -p clap_complete_nushell -F unstable-shell-tests
check:
name: Check
runs-on: ubuntu-latest
Expand Down
5 changes: 3 additions & 2 deletions clap_complete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ clap = { path = "../", version = "4.5.18", default-features = false, features =
clap_lex = { path = "../clap_lex", version = "0.7.0", optional = true }
is_executable = { version = "1.0.1", optional = true }
shlex = { version = "1.3.0", optional = true }
completest = { version = "0.4.2", optional = true }
completest-pty = { version = "0.5.5", optional = true }

[dev-dependencies]
snapbox = { version = "0.6.0", features = ["diff", "dir", "examples"] }
# Cutting out `filesystem` feature
trycmd = { version = "0.15.1", default-features = false, features = ["color-auto", "diff", "examples"] }
completest = "0.4.2"
completest-pty = "0.5.5"
clap = { path = "../", version = "4.5.18", default-features = false, features = ["std", "derive", "help"] }
automod = "1.0.14"

Expand All @@ -56,6 +56,7 @@ required-features = ["unstable-dynamic"]
default = []
unstable-doc = ["unstable-dynamic"] # for docs.rs
unstable-dynamic = ["dep:clap_lex", "dep:shlex", "dep:is_executable", "clap/unstable-ext"]
unstable-shell-tests = ["dep:completest", "dep:completest-pty"]
debug = ["clap/debug"]

[lints]
Expand Down
10 changes: 10 additions & 0 deletions clap_complete/tests/testsuite/bash.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#[allow(unused_imports)]
use snapbox::assert_data_eq;

use crate::common;

#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
const CMD: &str = "bash";
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
type RuntimeBuilder = completest_pty::BashRuntimeBuilder;

#[test]
Expand Down Expand Up @@ -142,12 +145,14 @@ fn subcommand_last() {

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn register_completion() {
common::register_example::<RuntimeBuilder>("static", "exhaustive");
}

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn complete() {
if !common::has_command(CMD) {
return;
Expand Down Expand Up @@ -236,12 +241,14 @@ fn complete() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn register_dynamic_env() {
common::register_example::<RuntimeBuilder>("dynamic-env", "exhaustive");
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_toplevel() {
if !common::has_command(CMD) {
return;
Expand All @@ -262,6 +269,7 @@ quote pacman alias help --generate --version

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_help() {
if !common::has_command(CMD) {
return;
Expand All @@ -283,6 +291,7 @@ cmd-backticks cmd-expansions --single-quotes --backticks --ex

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_option_value() {
if !common::has_command(CMD) {
return;
Expand All @@ -304,6 +313,7 @@ fn complete_dynamic_env_option_value() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_value() {
if !common::has_command(CMD) {
return;
Expand Down
10 changes: 7 additions & 3 deletions clap_complete/tests/testsuite/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ pub(crate) fn assert_matches(
.eq(buf, expected);
}

#[cfg(feature = "unstable-shell-tests")]
pub(crate) fn register_example<R: completest::RuntimeBuilder>(context: &str, name: &str) {
use completest::Runtime as _;

Expand Down Expand Up @@ -352,6 +353,7 @@ pub(crate) fn register_example<R: completest::RuntimeBuilder>(context: &str, nam
scratch.close().unwrap();
}

#[cfg(feature = "unstable-shell-tests")]
pub(crate) fn load_runtime<R: completest::RuntimeBuilder>(
context: &str,
name: &str,
Expand Down Expand Up @@ -394,12 +396,14 @@ where
})
}

#[cfg(feature = "unstable-shell-tests")]
#[derive(Debug)]
struct ScratchRuntime {
_scratch: snapbox::dir::DirRoot,
runtime: Box<dyn completest::Runtime>,
}

#[cfg(feature = "unstable-shell-tests")]
impl completest::Runtime for ScratchRuntime {
fn home(&self) -> &std::path::Path {
self.runtime.home()
Expand All @@ -419,6 +423,7 @@ impl completest::Runtime for ScratchRuntime {
}
}

#[cfg(feature = "unstable-shell-tests")]
pub(crate) fn has_command(command: &str) -> bool {
let output = match std::process::Command::new(command)
.arg("--version")
Expand All @@ -428,9 +433,7 @@ pub(crate) fn has_command(command: &str) -> bool {
Err(e) => {
// CI is expected to support all of the commands
if is_ci() && cfg!(target_os = "linux") {
panic!(
"expected command `{command}` to be somewhere in PATH: {e}"
);
panic!("expected command `{command}` to be somewhere in PATH: {e}");
}
return false;
}
Expand Down Expand Up @@ -463,6 +466,7 @@ pub(crate) fn has_command(command: &str) -> bool {
}

/// Whether or not this running in a Continuous Integration environment.
#[cfg(feature = "unstable-shell-tests")]
fn is_ci() -> bool {
// Consider using `tracked_env` instead of option_env! when it is stabilized.
// `tracked_env` will handle changes, but not require rebuilding the macro
Expand Down
10 changes: 10 additions & 0 deletions clap_complete/tests/testsuite/elvish.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::common;
#[allow(unused_imports)]
use snapbox::assert_data_eq;

#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
const CMD: &str = "elvish";
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
type RuntimeBuilder = completest_pty::ElvishRuntimeBuilder;

#[test]
Expand Down Expand Up @@ -141,12 +144,14 @@ fn subcommand_last() {

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn register_completion() {
common::register_example::<RuntimeBuilder>("static", "exhaustive");
}

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn complete_static_toplevel() {
if !common::has_command(CMD) {
return;
Expand Down Expand Up @@ -180,12 +185,14 @@ value value

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn register_dynamic_env() {
common::register_example::<RuntimeBuilder>("dynamic-env", "exhaustive");
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_toplevel() {
if !common::has_command(CMD) {
return;
Expand All @@ -207,6 +214,7 @@ fn complete_dynamic_env_toplevel() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_help() {
if !common::has_command(CMD) {
return;
Expand All @@ -229,6 +237,7 @@ fn complete_dynamic_env_quoted_help() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_option_value() {
if !common::has_command(CMD) {
return;
Expand Down Expand Up @@ -258,6 +267,7 @@ fn complete_dynamic_env_option_value() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_value() {
if !common::has_command(CMD) {
return;
Expand Down
10 changes: 10 additions & 0 deletions clap_complete/tests/testsuite/fish.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::common;
#[allow(unused_imports)]
use snapbox::assert_data_eq;

#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
const CMD: &str = "fish";
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
type RuntimeBuilder = completest_pty::FishRuntimeBuilder;

#[test]
Expand Down Expand Up @@ -141,12 +144,14 @@ fn subcommand_last() {

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn register_completion() {
common::register_example::<RuntimeBuilder>("static", "exhaustive");
}

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn complete() {
if !common::has_command(CMD) {
return;
Expand Down Expand Up @@ -175,12 +180,14 @@ another shell (something with a space) bash (bash (shell)) fish (fish shell

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn register_dynamic_env() {
common::register_example::<RuntimeBuilder>("dynamic-env", "exhaustive");
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_toplevel() {
if !common::has_command(CMD) {
return;
Expand All @@ -202,6 +209,7 @@ value alias --global (everywhere

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_help() {
if !common::has_command(CMD) {
return;
Expand Down Expand Up @@ -238,6 +246,7 @@ help (Print this message or the help of the given subcommand(s))

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_option_value() {
if !common::has_command(CMD) {
return;
Expand All @@ -262,6 +271,7 @@ fn complete_dynamic_env_option_value() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_value() {
if !common::has_command(CMD) {
return;
Expand Down
10 changes: 10 additions & 0 deletions clap_complete/tests/testsuite/zsh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#[allow(unused_imports)]
use snapbox::assert_data_eq;

use crate::common;

#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
const CMD: &str = "zsh";
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
type RuntimeBuilder = completest_pty::ZshRuntimeBuilder;

#[test]
Expand Down Expand Up @@ -142,12 +145,14 @@ fn subcommand_last() {

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn register_completion() {
common::register_example::<RuntimeBuilder>("static", "exhaustive");
}

#[test]
#[cfg(unix)]
#[cfg(feature = "unstable-shell-tests")]
fn complete() {
if !common::has_command(CMD) {
return;
Expand All @@ -168,12 +173,14 @@ pacman action alias value quote hint last --

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn register_dynamic_env() {
common::register_example::<RuntimeBuilder>("dynamic-env", "exhaustive");
}

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_toplevel() {
if !common::has_command(CMD) {
return;
Expand All @@ -194,6 +201,7 @@ fn complete_dynamic_env_toplevel() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_help() {
if !common::has_command(CMD) {
return;
Expand All @@ -215,6 +223,7 @@ fn complete_dynamic_env_quoted_help() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_option_value() {
if !common::has_command(CMD) {
return;
Expand All @@ -239,6 +248,7 @@ fn complete_dynamic_env_option_value() {

#[test]
#[cfg(all(unix, feature = "unstable-dynamic"))]
#[cfg(feature = "unstable-shell-tests")]
fn complete_dynamic_env_quoted_value() {
if !common::has_command(CMD) {
return;
Expand Down
Loading

0 comments on commit fd31a1b

Please sign in to comment.