Files
lix/tests/functional/external-commands.sh
T
Raito Bezarius 5f50741ce9 feat: add support for external lix- prefixed commands in the CLI
This feature allows calling external binaries starting with `lix-` as
subcommands directly within the `lix` CLI, similar to how Git handles
external commands.

For example, a binary named `lix-example` can now be invoked using `lix
example`.

This change opens up the possibility for broader community-driven
extensions of the Lix CLI, offering flexibility that Flakes has somewhat
restricted by centralizing certain features around its own model of
dependency management.

By enabling users to introduce custom subcommands, we encourage
experimentation and diverse workflows, addressing the needs of those who
seek more modular, feature-rich approaches to package management and
system configuration beyond what Flakes currently offers [1] [2] [3].

This adds `lix` as a new binary to be able to use this feature.

[1]: https://github.com/nmattia/niv
[2]: https://github.com/andir/npins
[3]: https://github.com/nikstur/lon

Change-Id: Ic6344424a6a46fc9fb30432f00e21c3509659f8a
Signed-off-by: Raito Bezarius <raito@lix.systems>
2025-02-23 15:24:28 +01:00

69 lines
2.7 KiB
Bash

source common.sh
cd "$TEST_ROOT"
# To start off, we will produce some custom binaries that redirects to known functionality.
EXTRA_BINARIES_DIR=$(mktemp -d)
# trap "rm -rf $EXTRA_BINARIES_DIR" EXIT
# Create an extra command as an alias of an existing one.
create_extra_command() {
local existing_command="$1"
local script_path="$EXTRA_BINARIES_DIR/lix-$existing_command"
cat > "$script_path" <<EOF
#!/bin/sh
# Re-adjust the existing command ARGV0 as we are not making Lix variants of those.
exec -a nix-$existing_command nix-$existing_command "\$@"
EOF
chmod +x "$script_path"
}
# Example usage: create a test script
create_extra_command copy-closure
create_extra_command collect-garbage
export PATH="$PATH:/some/incorrect:/location/for/fun:$EXTRA_BINARIES_DIR"
# TODO: we do not support auto completion for external subcommands for now.
# Test the completion of an external subcommand
# [[ "$(NIX_GET_COMPLETIONS=1 nix copy-)" == $'normal\ncopy-closure\t' ]]
# [[ "$(NIX_GET_COMPLETIONS=1 nix collect)" == $'normal\ncollect-garbage\t' ]]
testSimpleExternalCommands() {
# Test that an external subcommand without the experimental flag will fail
# without mentioning the experimental feature.
expect 1 nix copy-closure --help 1>$TEST_ROOT/stdout 2>$TEST_ROOT/stderr
# Test that an external subcommand can be run successfully.
expect 0 lix --extra-experimental-features 'lix-custom-sub-commands' copy-closure --version 1>/dev/null
expect 0 lix --extra-experimental-features 'lix-custom-sub-commands' collect-garbage --version 1>/dev/null
# Test that the external subcommand can be run beyond `--help` processing successfully.
expect 0 lix --extra-experimental-features 'lix-custom-sub-commands' collect-garbage --dry-run 1>/dev/null
# Test that an external subcommand without the experimental flag will fail
# without mentioning the experimental feature.
expect 1 lix copy-closure --help 1>$TEST_ROOT/stdout 2>$TEST_ROOT/stderr
}
testSimpleExternalCommands
# Test that an external subcommand can be run successfully with a slightly modified PATH.
(
# Split the PATH into its first component and the rest
FIRST_PATH=${PATH%%:*} # The first directory
REST_PATH=${PATH#*:} # The rest of the PATH
echo "modified PATH: $PATH"
# Rebuild PATH with the first directory moved to the second position
export PATH=$(echo $REST_PATH | cut -d: -f1):$FIRST_PATH:$(echo $REST_PATH | cut -d: -s -f2-)
testSimpleExternalCommands
)
# TODO: Test flags handling.
# TODO: Short, long and multiple flags should be tested as well.
# TODO: `--` special flag?
# TODO: test positional arguments, but only `nix-copy-closure` implements some and it's pesky to test here.