]>
Lady’s Gitweb - CGirls/blob - sh/test.sh
2 # SPDX-FileCopyrightText: 2025 Lady <https://www.ladys.computer/about/#lady>
3 # SPDX-License-Identifier: MPL-2.0
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/>.
8 # The program name is used in rendering the output.
10 # The first argument specifies the test program(s) to use.
12 # The second argument specifies the test(s) to run.
16 TEST_PROGRAM_PREFIX
= cgirls-test-
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} "
21 TEST_PROGRAMS
= $
( find .
'(' -path '.' -o -prune ')' -a -type f
-a -name " ${TEST_PROGRAM_PREFIX} " '*.c' |
sed 's|^\./' " ${TEST_PROGRAM_PREFIX} " '||;s|\.c$||' )
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}
31 printf ' \033 [1;4m %-45s \033 [0m \n ' " ${PROGRAM_NAME} ${test_program} "
32 if test '!' -x " ${TEST_PROGRAM_PREFIX} ${test_program} "
34 printf '%b \n ' ' \003 3[1;93;41;m[ERROR] \003 3[0m Test program has not been compiled. \n\033 [7m \033 [0m (Try \003 3[3mmake ' " ${TEST_PROGRAM_PREFIX} ${test_program} " ' \003 3[23m.)'
35 TEST_EXIT_STATUS
= $
(( ${TEST_EXIT_STATUS} |
4 ))
37 if test '!' -d "test/ ${test_program} /logs"
39 mkdir
-p "test/ ${test_program} /logs"
41 if test '!' -d "expect/ ${test_program} "
43 mkdir
-p "expect/ ${test_program} "
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} "
49 SELECTED_TEST_FILES
= ${AVAILABLE_TEST_FILES}
51 SELECTED_TEST_FILES
= $
( LC_ALL
= POSIX
printf '%s \n ' ${TEST_FILES} |
sort - |
comm -1 -2 - 'test/' " ${test_program} " '/logs/!!INDEX!!' )
53 for test_file
in ${SELECTED_TEST_FILES}
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} "
60 printf '%27b \n ' ' \003 3[1;93;41;m[NG] \003 3[0m'
61 printf ' \033 [7m \033 [0m \033 [2m%b \033 [0m \n ' " ${TEST_LOG} "
62 TEST_EXIT_STATUS
= $
(( ${TEST_EXIT_STATUS} |
1 ))
64 if test -n " $SET_EXPECTATION "
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 ' ' \003 3[1;93;45;m[SAVED] \003 3[0m'
70 if test '!' -f "expect/ ${test_program} / ${test_file} "
72 printf '%27b \n ' ' \033 [1;93;44;m[UNEXPECTED] \003 3[0m'
73 printf ' \033 [7m \033 [0m \033 [2m%b \033 [0m \n ' 'Run with \003 3[3mSET_EXPECTATION=1 \003 3[23m to set an' 'expectation.'
74 TEST_EXIT_STATUS
= $
(( ${TEST_EXIT_STATUS} |
2 ))
76 TEST_EXPECTATION
= "$(sed '/# /d' <" expect
/ ${test_program} / ${test_file} ")"
77 if test " ${TEST_RESULT} " '=' " ${TEST_EXPECTATION} "
79 printf '%27b \n ' ' \003 3[1;93;42;m[OK] \003 3[0m'
81 printf '%27b \n ' ' \003 3[1;93;41;m[NG] \003 3[0m'
82 printf ' \033 [7m \033 [0m \033 [2m%b \033 [0m \n ' 'Result did not match expectation.' 'Got:'
83 printf '%s \n ' " ${TEST_RESULT} " |
sed 's/' "'" '/' "'" '"' "'" '"' "'" '/g;s/^/' "'" '/;s/$/' "'" '/' |
xargs -E '' printf ' \033 [30;107m%-49s \033 [0m \n '
84 printf ' \033 [7m \033 [0m \033 [2m%b \033 [0m \n ' 'Expected:'
85 printf '%s \n ' " ${TEST_EXPECTATION} " |
sed 's/' "'" '/' "'" '"' "'" '"' "'" '/g;s/^/' "'" '/;s/$/' "'" '/' |
xargs -E '' printf ' \033 [30;107m%-49s \033 [0m \n '
86 TEST_EXIT_STATUS
= $
(( ${TEST_EXIT_STATUS} |
1 ))
93 if test ${TEST_EXIT_STATUS} -eq 0
95 printf ' \033 [1m%s \033 [0m \n ' 'All tests succeeded.'
97 if test $
(( ${TEST_EXIT_STATUS} & 1 )) -gt 0
99 printf ' \033 [1m%s \033 [0m \n ' 'Not all tests completed successfully.'
101 if test $
(( ${TEST_EXIT_STATUS} & 2 )) -gt 0
103 printf ' \033 [1m%s \033 [0m \n ' 'At least one test was unexpected.'
105 if test $
(( ${TEST_EXIT_STATUS} & 4 )) -gt 0
107 printf ' \033 [1m%s \033 [0m \n ' 'At least one test program needs to be built.'
109 exit ${TEST_EXIT_STATUS}
This page took 0.351217 seconds and 5 git commands to generate.