diff --git a/scripts/lib/gitops-functions.sh b/scripts/lib/gitops-functions.sh index 5c4cf71..2b4f64b 100755 --- a/scripts/lib/gitops-functions.sh +++ b/scripts/lib/gitops-functions.sh @@ -41,7 +41,7 @@ update_file() { else local field_type field_type=$(yq "(.${field} | type)" "${file}" 2>/dev/null || echo "!!null") - if [[ "${field_type}" == "!!map" ]] && yq -e ".${field}.tag" "${file}" > /dev/null 2>&1; then + if [[ "${field_type}" == "!!map" ]] && yq -e ".${field} | (has(\"tag\") or has(\"repository\"))" "${file}" > /dev/null 2>&1; then yq -i ".${field}.tag=\"${INPUT_TAG}\"" "${file}" else yq -i ."${field}"=\""${image}"\" "${file}" diff --git a/tests/lib-gitops-functions.bats b/tests/lib-gitops-functions.bats index 81fa67e..635cbba 100644 --- a/tests/lib-gitops-functions.bats +++ b/tests/lib-gitops-functions.bats @@ -64,7 +64,7 @@ YQ_MOCK ! grep -q ".tag=\"${INPUT_TAG}\"" "${TEST_TEMP_DIR}/yq_calls.log" } -@test "update_file writes tag to .tag subfield when field is a map with tag property" { +@test "update_file writes tag to .tag subfield when field is a map" { cat > "${TEST_TEMP_DIR}/mocks/yq" << 'YQ_MOCK' #!/usr/bin/env bash echo "yq $*" >> "${MOCK_CALLS_DIR}/yq_calls.log" @@ -77,6 +77,38 @@ YQ_MOCK ! grep -q "${IMAGE}" "${TEST_TEMP_DIR}/yq_calls.log" } +# --- Integration tests using real yq --- + +@test "INTEGRATION: map with only repository gets tag added" { + rm -rf "${TEST_TEMP_DIR}/mocks" + skip_if_no_yq + local test_file="${TEST_TEMP_DIR}/helmrelease.yaml" + cat > "$test_file" << 'EOF' +apiVersion: helm.toolkit.fluxcd.io/v2 +kind: HelmRelease +metadata: + name: test-svc + annotations: {} +spec: + values: + workload: + container: + image: + repository: registry.example.com/my-image +EOF + + update_file "$test_file" "spec.values.workload.container.image" "$IMAGE" + + run yq '.spec.values.workload.container.image.tag' "$test_file" + assert_output "$INPUT_TAG" + + run yq '.spec.values.workload.container.image.repository' "$test_file" + assert_output "registry.example.com/my-image" + + run yq '.spec.values.workload.container.image | type' "$test_file" + assert_output "!!map" +} + @test "update_file writes tag only when field path ends with .tag" { update_file "helmrelease.yaml" "spec.values.workload.container.image.tag" "$IMAGE" grep -q ".spec.values.workload.container.image.tag=\"${INPUT_TAG}\"" "${TEST_TEMP_DIR}/yq_calls.log" diff --git a/tests/test_helper/setup.bash b/tests/test_helper/setup.bash index bc5d60c..ddd5822 100644 --- a/tests/test_helper/setup.bash +++ b/tests/test_helper/setup.bash @@ -15,6 +15,12 @@ teardown_common() { rm -rf "$TEST_TEMP_DIR" } +skip_if_no_yq() { + if ! command -v yq &> /dev/null; then + skip "yq not installed" + fi +} + # Assert that a specific output was written to GITHUB_OUTPUT assert_output_value() { local name="$1"