]> Lady’s Gitweb - CGirls/blob - sh/test.sh
Build testing infrastructure
[CGirls] / sh / test.sh
1 #!/bin/sh
2 # SPDX-FileCopyrightText: 2025 Lady <https://www.ladys.computer/about/#lady>
3 # SPDX-License-Identifier: MPL-2.0
4
5 # This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
6 # If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
7
8 # The program name is used in rendering the output.
9 PROGRAM_NAME=$0
10 # The first argument specifies the test program(s) to use.
11 TEST_PROGRAMS=$1
12 # The second argument specifies the test(s) to run.
13 TEST_FILES=$2
14
15 TEST_EXIT_STATUS=0
16 TEST_PROGRAM_PREFIX=cgirls-test-
17
18 # If no test programs are specified, find all of them by looking for files which start with `cgirls-test-´ and end in `.c´.
19 if test -z "${TEST_PROGRAMS}"
20 then :
21 TEST_PROGRAMS=$(find . '(' -path '.' -o -prune ')' -a -type f -a -name "${TEST_PROGRAM_PREFIX}"'*.c' | sed 's|^\./'"${TEST_PROGRAM_PREFIX}"'||;s|\.c$||')
22 fi
23
24 # Iterate over the test programs and run the tests.
25 # This code first gets the list of available tests (via another `find´) and saves it as the `.index´ in the test directory.
26 # Tests must have names which start with two digits and a hymin.
27 # If a specific test was not requested, all tests are run.
28 # Otherwise, only those tests which match one of the specified tests will run.
29 for test_program in ${TEST_PROGRAMS}
30 do :
31 printf '\033[1;4m %-45s \033[0m\n' "${PROGRAM_NAME} ${test_program}"
32 if test '!' -x "${TEST_PROGRAM_PREFIX}${test_program}"
33 then :
34 printf '%b\n' '\0033[1;93;41;m[ERROR]\0033[0m Test program has not been compiled.\n\033[7m \033[0m (Try \0033[3mmake '"${TEST_PROGRAM_PREFIX}${test_program}"'\0033[23m.)'
35 TEST_EXIT_STATUS=$((${TEST_EXIT_STATUS} | 4))
36 else :
37 if test '!' -d "test/${test_program}/logs"
38 then :
39 mkdir -p "test/${test_program}/logs"
40 fi
41 if test '!' -d "expect/${test_program}"
42 then :
43 mkdir -p "expect/${test_program}"
44 fi
45 AVAILABLE_TEST_FILES=$(LC_ALL=POSIX find "test/${test_program}" '(' -path "test/${test_program}" -o -prune ')' -a -type f -a -name '[0-9][0-9]-*' | sed 's|^[^/]*/[^/]*/||' | sort -)
46 printf '%s\n' $AVAILABLE_TEST_FILES >|'test/'"${test_program}"'/logs/!!INDEX!!'
47 if test -z "${TEST_FILES}"
48 then :
49 SELECTED_TEST_FILES=${AVAILABLE_TEST_FILES}
50 else :
51 SELECTED_TEST_FILES=$(LC_ALL=POSIX printf '%s\n' ${TEST_FILES} | sort - | comm -1 -2 - 'test/'"${test_program}"'/logs/!!INDEX!!')
52 fi
53 for test_file in ${SELECTED_TEST_FILES}
54 do :
55 printf '\033[7m \033[0m \033[3m%-34s\033[0m ' "${PROGRAM_NAME} ${test_program} ${test_file}"
56 TEST_RESULT="$(sed '/# /d' <"test/${test_program}/${test_file}" | "./${TEST_PROGRAM_PREFIX}${test_program}" 2>|"test/${test_program}/logs/${test_file}")"
57 TEST_LOG="$(cat "test/${test_program}/logs/${test_file}")"
58 if test -n "${TEST_LOG}"
59 then :
60 printf '%27b \n' '\0033[1;93;41;m[NG]\0033[0m'
61 printf '\033[7m \033[0m \033[2m%b\033[0m \n' "${TEST_LOG}"
62 TEST_EXIT_STATUS=$((${TEST_EXIT_STATUS} | 1))
63 else :
64 if test -n "$SET_EXPECTATION"
65 then :
66 sed '/# SPDX-/!d' <"test/${test_program}/${test_file}" >|"expect/${test_program}/${test_file}"
67 printf '%s\n' "${TEST_RESULT}" >>"expect/${test_program}/${test_file}"
68 printf '%27b\n' '\0033[1;93;45;m[SAVED]\0033[0m'
69 else :
70 if test '!' -f "expect/${test_program}/${test_file}"
71 then :
72 printf '%27b\n' '\033[1;93;44;m[UNEXPECTED]\0033[0m'
73 printf '\033[7m \033[0m \033[2m%b\033[0m \n' 'Run with \0033[3mSET_EXPECTATION=1\0033[23m to set an' 'expectation.'
74 TEST_EXIT_STATUS=$((${TEST_EXIT_STATUS} | 2))
75 else :
76 TEST_EXPECTATION="$(sed '/# /d' <"expect/${test_program}/${test_file}")"
77 if test "${TEST_RESULT}" '=' "${TEST_EXPECTATION}"
78 then :
79 printf '%27b \n' '\0033[1;93;42;m[OK]\0033[0m'
80 else :
81 printf '%27b \n' '\0033[1;93;41;m[NG]\0033[0m'
82 printf '\033[7m \033[0m \033[2m%b\033[0m \n' 'Result did not match expectation.' 'Got:'
83 printf '\033[30;107m%-49s\033[0m\n' "${TEST_RESULT}"
84 printf '\033[7m \033[0m \033[2m%b\033[0m \n' 'Expected:'
85 printf '\033[30;107m%-49s\033[0m\n' "${TEST_EXPECTATION}"
86 TEST_EXIT_STATUS=$((${TEST_EXIT_STATUS} | 1))
87 fi
88 fi
89 fi
90 fi
91 done
92 fi
93 if test ${TEST_EXIT_STATUS} -eq 0
94 then :
95 printf '\033[1m%s\033[0m\n' 'All tests succeeded.'
96 fi
97 if test $((${TEST_EXIT_STATUS} & 1)) -gt 0
98 then :
99 printf '\033[1m%s\033[0m\n' 'Not all tests completed successfully.'
100 fi
101 if test $((${TEST_EXIT_STATUS} & 2)) -gt 0
102 then :
103 printf '\033[1m%s\033[0m\n' 'At least one test was unexpected.'
104 fi
105 if test $((${TEST_EXIT_STATUS} & 4)) -gt 0
106 then :
107 printf '\033[1m%s\033[0m\n' 'At least one test program needs to be built.'
108 fi
109 exit ${TEST_EXIT_STATUS}
110 done
This page took 0.426541 seconds and 5 git commands to generate.