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
2 changes: 1 addition & 1 deletion scripts/lib/gitops-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
34 changes: 33 additions & 1 deletion tests/lib-gitops-functions.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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'
Comment thread
m-seidel marked this conversation as resolved.
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"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_helper/setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading