Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cli/bash/commands/basectl/subcommands/export_context.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ readonly _base_export_context_subcommand_sourced

import_base_lib arg/lib_arg.sh

_base_project_command_helpers_path="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)/project_command_helpers.sh"
# shellcheck source=/dev/null
source "$_base_project_command_helpers_path"

base_export_context_subcommand_usage() {
cat <<'EOF'
Usage:
Expand Down Expand Up @@ -126,11 +130,11 @@ base_export_context_subcommand_main() {
resolved_name="${BASE_COMMAND_PROTOCOL_FIELDS[project_name]}"
project_root="${BASE_COMMAND_PROTOCOL_FIELDS[project_root]}"
manifest_path="${BASE_COMMAND_PROTOCOL_FIELDS[manifest_path]}"
base_project_set_history_context "$resolved_name" "$project_root" "$manifest_path"

[[ -n "$resolved_name" && -n "$project_root" && -n "$manifest_path" ]] || {
base_std_fatal_error "Unable to resolve project for export-context."
}
base_project_set_history_context "$resolved_name" "$project_root" "$manifest_path"

exporter_args+=(
--project-name "$resolved_name"
Expand Down
53 changes: 52 additions & 1 deletion cli/bash/commands/basectl/tests/export-context.bats
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ load ./basectl_helpers.bash
local python_bin="$TEST_HOME/.base.d/base/.venv/bin/python"
local workspace="$TEST_TMPDIR/workspace"
local state_file="$TEST_TMPDIR/export-state"
local history_state="$TEST_TMPDIR/history-state"
local history_call

mkdir -p "$(dirname "$python_bin")" "$workspace/demo"
cat > "$python_bin" <<'EOF'
Expand All @@ -54,6 +56,10 @@ if [[ "${1:-}" == "-m" && "${2:-}" == "base_export_context" ]]; then
printf 'exported current\n'
exit 0
fi
if [[ "${1:-}" == "-m" && "${2:-}" == "base_history.record" ]]; then
printf '%s\n' "$*" > "${BASE_TEST_HISTORY_STATE:?}"
exit 0
fi
printf 'unexpected export-context python args: %s\n' "$*" >&2
exit 1
EOF
Expand All @@ -66,15 +72,60 @@ EOF
PATH="/usr/bin:/bin:/usr/sbin:/sbin" \
BASE_TEST_PROJECT_ROOT="$workspace/demo" \
BASE_TEST_EXPORT_STATE="$state_file" \
BASE_TEST_HISTORY_STATE="$history_state" \
bash -c '
cd "$1"
shift
"$@"
' bash "$workspace/demo" "$BASE_REPO_ROOT/bin/basectl" export-context --print

[ "$status" -eq 0 ]
[[ "$output" == *"exported current"* ]]
[ "$output" = "exported current" ]
[ "$(cat "$state_file")" = "--project-name demo --project-root $workspace/demo --format markdown --print" ]
history_call="$(cat "$history_state")"
[[ "$history_call" == *"-m base_history.record --command export-context "* ]]
[[ "$history_call" == *" --project demo --project-root $workspace/demo "* ]]
[[ "$history_call" == *" --manifest $workspace/demo/base_manifest.yaml "* ]]
}

@test "basectl export-context rejects incomplete project context before setting history attribution" {
local python_bin="$TEST_HOME/.base.d/base/.venv/bin/python"
local workspace="$TEST_TMPDIR/workspace"
local history_state="$TEST_TMPDIR/history-state"

mkdir -p "$(dirname "$python_bin")" "$workspace/demo"
cat > "$python_bin" <<'EOF'
#!/usr/bin/env bash
if [[ "${1:-}" == "-m" && "${2:-}" == "base_projects" && "${3:-}" == "current" ]]; then
base_test_protocol_project_reference demo "${BASE_TEST_PROJECT_ROOT:?}" ""
exit 0
fi
printf 'unexpected export-context python args: %s\n' "$*" >&2
exit 1
EOF
chmod +x "$python_bin"
workspace="$(cd "$workspace" && pwd -P)"

run env \
HOME="$TEST_HOME" \
PATH="/usr/bin:/bin:/usr/sbin:/sbin" \
BASE_HOME="$BASE_REPO_ROOT" \
BASE_BASH_LIBS_DIR="${BASE_BASH_LIBS_DIR:-}" \
BASE_TEST_PROJECT_ROOT="$workspace/demo" \
BASE_TEST_HISTORY_STATE="$history_state" \
"$TEST_BASH_BIN_DIR/bash" -c '
source "$BASE_HOME/base_init.sh"
source "$BASE_HOME/cli/bash/commands/basectl/subcommands/export_context.sh"
base_project_set_history_context() {
printf "%s\n" "$*" > "${BASE_TEST_HISTORY_STATE:?}"
}
cd "$1"
base_export_context_subcommand_main --print
' bash "$workspace/demo"

[ "$status" -eq 1 ]
[[ "$output" == *"Unable to resolve project for export-context."* ]]
[ ! -e "$history_state" ]
}

@test "basectl export-context resolves named project and forwards output options" {
Expand Down
Loading