Chore: Upgrade Jenkins-job-builder to 6.2.0
[releng/global-jjb.git] / jjb-compare-xml.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 # This script tests jjb templates by comparing the result with expected output
13 # from global-jjb's origin/master branch.
14
15 test_dir=$(mktemp -d)
16 script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/jjb
17 expected_xml_dir="$(mktemp -d -t gjjb-XXXXXXXX)"
18
19 echo "Script Directory: $script_dir"
20 echo "Test Directory: $test_dir"
21 echo "Expected XML Directory: $expected_xml_dir"
22
23 git fetch origin
24 gittmp="$(mktemp -d)"
25 git worktree add --detach "$gittmp" origin/master
26 pushd "$gittmp" || exit
27 echo "Generating expected XML."
28 jenkins-jobs test --recursive -o "$expected_xml_dir" \
29     "$gittmp":.jjb-test > /dev/null 2>&1
30 popd || exit
31 rm -r "$gittmp"
32
33 echo "Generating test XML."
34 jenkins-jobs test --recursive -o "$test_dir" "$script_dir":.jjb-test
35
36 fail=false
37 for xml in "$test_dir"/*; do
38     job=$(basename "$xml")
39     if ! cmp "$expected_xml_dir/$job" "$xml"; then
40         echo "Differences detected in $job"
41         diff "$expected_xml_dir/$job" "$xml"
42         fail=true
43     fi
44 done
45
46 echo "Checking for double curly braces..."
47 mapfile -t xml_files < <(find "$test_dir" -type f)
48 for xml in "${xml_files[@]}"; do
49     if grep '{{' "$xml"; then
50         echo "ERROR: Double curly braces discovered in output XML."
51         exit 1
52     fi
53 done
54 echo "No double curly braces found."
55
56 # Cleanup
57 rm -rf "$test_dir" "$expected_xml_dir"
58
59 if $fail; then
60     echo "WARN: Differences detected." \
61         "Check above for jobs that have been changed."
62     exit 0
63 fi