Skip to content

Commit

Permalink
fix: zero-based indexing for git config variables
Browse files Browse the repository at this point in the history
NGL, missed the bit in the spec where it said "config pairs are zero-indexed".
  • Loading branch information
jamestelfer committed Apr 11, 2024
1 parent 732107b commit e7df40e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
13 changes: 6 additions & 7 deletions hooks/environment
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ git_config_add() {
local key="$1"
local value="$2"

count="${GIT_CONFIG_COUNT:--1}"
index=$(( "${GIT_CONFIG_COUNT:-0}" - 1 ))

# count is incremented each time a setting is added
(( count = count + 1 ))

export GIT_CONFIG_COUNT="${count}"
export "GIT_CONFIG_KEY_${count}=${key}"
export "GIT_CONFIG_VALUE_${count}=${value}"
# index is incremented each time a setting is added
index=$(( index + 1 ))
export GIT_CONFIG_COUNT=$(( index + 1 ))
export "GIT_CONFIG_KEY_${index}=${key}"
export "GIT_CONFIG_VALUE_${index}=${value}"
}

git_config_add "credential.https://github.com.usehttppath" "true"
Expand Down
27 changes: 15 additions & 12 deletions tests/environment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ run_environment() {
}

@test "Fails without configuration" {
# export BUILDKITE_COMMAND_EXIT_STATUS=0

run "$PWD/hooks/environment"

assert_failure
Expand All @@ -51,7 +49,7 @@ run_environment() {
run_environment "${PWD}/hooks/environment"

assert_success
assert_line "GIT_CONFIG_COUNT=1"
assert_line "GIT_CONFIG_COUNT=2"
assert_line "GIT_CONFIG_KEY_0=credential.https://github.com.usehttppath"
assert_line "GIT_CONFIG_VALUE_0=true"
assert_line "GIT_CONFIG_KEY_1=credential.https://github.com.helper"
Expand All @@ -65,7 +63,7 @@ run_environment() {
run_environment "${PWD}/hooks/environment"

assert_success
assert_line "GIT_CONFIG_COUNT=1"
assert_line "GIT_CONFIG_COUNT=2"
assert_line "GIT_CONFIG_KEY_0=credential.https://github.com.usehttppath"
assert_line "GIT_CONFIG_VALUE_0=true"
assert_line "GIT_CONFIG_KEY_1=credential.https://github.com.helper"
Expand All @@ -75,18 +73,23 @@ run_environment() {
@test "Adds to existing configuration if present" {
export BUILDKITE_PLUGIN_GITHUB_APP_AUTH_VENDOR_URL=http://test-location

# Setup existing config items. These must exist or Git will fail.
export GIT_CONFIG_COUNT="3"
export GIT_CONFIG_KEY_3="key-3"
export GIT_CONFIG_VALUE_3="value-3"
export GIT_CONFIG_KEY_0="key-0"
export GIT_CONFIG_VALUE_0="value-0"
export GIT_CONFIG_KEY_1="key-1"
export GIT_CONFIG_VALUE_1="value-1"
export GIT_CONFIG_KEY_2="key-2"
export GIT_CONFIG_VALUE_2="value-2"

run_environment "${PWD}/hooks/environment"

assert_success
assert_line "GIT_CONFIG_COUNT=5"
assert_line "GIT_CONFIG_KEY_3=key-3"
assert_line "GIT_CONFIG_VALUE_3=value-3"
assert_line "GIT_CONFIG_KEY_4=credential.https://github.com.usehttppath"
assert_line "GIT_CONFIG_VALUE_4=true"
assert_line "GIT_CONFIG_KEY_5=credential.https://github.com.helper"
assert_line --regexp "GIT_CONFIG_VALUE_5=/.*/credential-helper/buildkite-connector-credential-helper http://test-location github-app-auth:default"
assert_line "GIT_CONFIG_KEY_2=key-2"
assert_line "GIT_CONFIG_VALUE_2=value-2"
assert_line "GIT_CONFIG_KEY_3=credential.https://github.com.usehttppath"
assert_line "GIT_CONFIG_VALUE_3=true"
assert_line "GIT_CONFIG_KEY_4=credential.https://github.com.helper"
assert_line --regexp "GIT_CONFIG_VALUE_4=/.*/credential-helper/buildkite-connector-credential-helper http://test-location github-app-auth:default"
}

0 comments on commit e7df40e

Please sign in to comment.