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
61 changes: 39 additions & 22 deletions step.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ if (value !== undefined && value !== null) {
' "$1"
}

# Parse env variables
env_list_parsed=""
# Parse env variables: each KEY=VALUE line of env_list becomes `-e KEY=VALUE`,
# skipping blank lines. An array keeps a value with spaces in one argument.
# read trims each line's leading and trailing whitespace.
env_args=()
env_display=""
if [ -n "$env_list" ]; then
# Convert newline-separated key-value pairs to -e KEY=VALUE format
while IFS='=' read -r key value; do
if [ -n "$key" ]; then
# Append each env var with -e prefix
env_list_parsed="$env_list_parsed -e $key=$value"
while read -r pair; do
if [ -n "${pair%%=*}" ]; then
env_args+=(-e "$pair")
env_display="$env_display -e \"$pair\""
fi
done <<< "$env_list"
fi
Expand All @@ -56,12 +58,14 @@ else
DCD_VERSION="@devicecloud.dev/dcd@>=4.4.0"
fi

# Parse metadata list (similar to env_list)
metadata_parsed=""
# Parse metadata list, as env_list: each key=value line becomes `-m key=value`.
meta_args=()
meta_display=""
if [ -n "$metadata" ]; then
while IFS='=' read -r key value; do
if [ -n "$key" ]; then
metadata_parsed="$metadata_parsed -m $key=$value"
while read -r pair; do
if [ -n "${pair%%=*}" ]; then
meta_args+=(-m "$pair")
meta_display="$meta_display -m \"$pair\""
fi
done <<< "$metadata"
fi
Expand Down Expand Up @@ -186,10 +190,23 @@ echo "use_beta: $use_beta"
echo "check_name: $check_name"
echo "include_github_context: $include_github_context"

# check_name is passed as its own quoted `-m` pair rather than folded into
# metadata_parsed, which expands unquoted: a check name containing a space would
# split into two argv entries and the stray word would land as a positional (app
# file / workspace).
# The CLI's positionals are [app file] [flows]. With app_binary_id set, it reads
# the first one as the flows and rejects a second ("You cannot provide both an
# appBinaryId and a binary file"), and an empty "$app_file" still counts as one.
# So the app file goes in only when it's set.
positionals=()
if [ -n "$app_file" ]; then
positionals+=("$app_file")
fi
positionals+=("$workspace")
# The same, quoted like the flags, for the "Running command" line.
positionals_display=""
for positional in "${positionals[@]}"; do
positionals_display="${positionals_display:+$positionals_display }\"$positional\""
done

# check_name goes in as its own `-m gh_check_name=...` pair, quoted so a name
# with a space stays one argument.
echo "Running command: npx --yes \"$DCD_VERSION\" cloud --quiet \
--apiKey \"${api_key:+[REDACTED]}\" \
${allure_path:+--allure-path \"$allure_path\"} \
Expand Down Expand Up @@ -232,10 +249,10 @@ ${is_show_crosshairs:+--show-crosshairs} \
${app_url:+--app-url \"$app_url\"} \
${is_disable_animations:+--disable-animations} \
${is_quiet:+--quiet} \
${env_list_parsed} \
${metadata_parsed} \
${env_display} \
${meta_display} \
${gh_context_args[*]} \
\"$app_file\" \"$workspace\""
${positionals_display}"

# Capture the command output and display it
echo "Waiting for full test results so we can parse outputs... this may take a while for non-async tests"
Expand Down Expand Up @@ -288,10 +305,10 @@ ${is_show_crosshairs:+--show-crosshairs} \
${app_url:+--app-url "$app_url"} \
${is_disable_animations:+--disable-animations} \
${is_quiet:+--quiet} \
${env_list_parsed} \
${metadata_parsed} \
"${env_args[@]}" \
"${meta_args[@]}" \
"${gh_context_args[@]}" \
"$app_file" "$workspace" 2>&1) || EXIT_CODE=$?
"${positionals[@]}" 2>&1) || EXIT_CODE=$?
echo "$OUTPUT"

# The CLI's exit code is the primary verdict: it is computed by the process that
Expand Down
53 changes: 52 additions & 1 deletion test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ STUB

# step.sh reads Bitrise inputs from lowercase env vars; clear any the host
# runner may have exported so each test specifies exactly what it wants.
unset api_key app_file workspace android_device android_api_level ios_device \
unset api_key app_file app_binary_id workspace android_device android_api_level ios_device \
name check_name async google_play debug disable_animations use_beta \
env_list metadata download_artifacts json_file cancel_previous priority \
include_github_context DCD_STEP_VERSION \
Expand Down Expand Up @@ -120,6 +120,35 @@ teardown() {
[[ "$output" == *".maestro"* ]]
}

@test "invokes npx cloud with --app-binary-id and the workspace as the only positional" {
# With --app-binary-id the CLI reads the first positional as the flows and
# rejects a second, so an empty app_file must not become an empty first
# positional ahead of the workspace. (`|| false`: bash 3.2 ignores a failed
# [[ ]] unless it's a test's last command.)
export api_key="test-key"
export app_binary_id="bin-123"
export app_file=""
export workspace=".maestro"
run bash "${TEST_DIR}/step.sh"
[ "$status" -eq 0 ]
[[ "$output" == *$'STUB_CLOUD_ARG: --app-binary-id\nSTUB_CLOUD_ARG: bin-123\n'* ]] || false
# The CLI's argv ends with the workspace and has no empty entry.
cloud_args=()
for line in "${lines[@]}"; do
case "$line" in
"STUB_CLOUD_ARG: "*) cloud_args+=("${line#STUB_CLOUD_ARG: }") ;;
esac
done
[ "${cloud_args[$(( ${#cloud_args[@]} - 1 ))]}" = ".maestro" ]
for arg in "${cloud_args[@]}"; do
[ -n "$arg" ]
done
# So does the logged command, with no "" where the app file would go.
running_line="$(printf '%s\n' "$output" | grep '^Running command:')"
[[ "$running_line" == *'--app-binary-id "bin-123"'*' ".maestro"' ]] || false
[[ "$running_line" != *'""'* ]] || false
}

@test "never prints the API key itself" {
export api_key="sk-live-do-not-print"
run bash "${TEST_DIR}/step.sh"
Expand Down Expand Up @@ -259,6 +288,28 @@ teardown() {
[[ "$output" == *"-m sha=abc"* ]]
}

@test "keeps env_list and metadata values with spaces in one argument" {
# Each line is one -e/-m argument: a space in a value must not split it, or
# the stray word lands as a positional. Blank lines are still skipped, and a
# line's leading and trailing whitespace is trimmed. (`|| false`: bash 3.2
# ignores a failed [[ ]] unless it's a test's last command.)
export api_key="k"
export env_list=$'USER_NAME=Jane Doe\n\n GREETING=hello world \n'
export metadata=$'team=Mobile QA\nbranch=main'
run bash "${TEST_DIR}/step.sh"
[ "$status" -eq 0 ]
[[ "$output" == *$'STUB_CLOUD_ARG: -e\nSTUB_CLOUD_ARG: USER_NAME=Jane Doe\n'* ]] || false
[[ "$output" == *$'STUB_CLOUD_ARG: -e\nSTUB_CLOUD_ARG: GREETING=hello world\n'* ]] || false
[[ "$output" == *$'STUB_CLOUD_ARG: -m\nSTUB_CLOUD_ARG: team=Mobile QA\n'* ]] || false
[[ "$output" == *$'STUB_CLOUD_ARG: -m\nSTUB_CLOUD_ARG: branch=main\n'* ]] || false
# Two of each: the blank line added nothing.
[ "$(printf '%s\n' "$output" | grep -c '^STUB_CLOUD_ARG: -e$')" -eq 2 ]
[ "$(printf '%s\n' "$output" | grep -c '^STUB_CLOUD_ARG: -m$')" -eq 2 ]
# The logged command shows each value quoted, in one piece.
[[ "$output" == *'-e "USER_NAME=Jane Doe" -e "GREETING=hello world"'* ]] || false
[[ "$output" == *'-m "team=Mobile QA" -m "branch=main"'* ]] || false
}

# --- GitHub context from Bitrise env vars ------------------------------------

@test "derives the gh_* context for a GitHub PR build" {
Expand Down
Loading