Skip to content

Commit bcfebb1

Browse files
authored
Add tests to ensure action works
Added simple tests to ensure the action works - checking exit code on - checking exit code on - checking output on - checking output on failure
1 parent c07901f commit bcfebb1

File tree

11 files changed

+242
-1
lines changed

11 files changed

+242
-1
lines changed

.ecrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Verbose": false,
3+
"Debug": false,
4+
"IgnoreDefaults": false,
5+
"SpacesAftertabs": false,
6+
"NoColor": false,
7+
"Exclude": ["tests/test-cases", "tests/tools/assert.sh"],
8+
"AllowedContentTypes": [],
9+
"PassedFiles": [],
10+
"Disable": {
11+
"EndOfLine": false,
12+
"Indentation": false,
13+
"InsertFinalNewline": false,
14+
"TrimTrailingWhitespace": false,
15+
"IndentSize": false,
16+
"MaxLineLength": false
17+
}
18+
}

.github/workflows/ci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ jobs:
88
steps:
99
- uses: actions/checkout@v2
1010
- uses: editorconfig-checker/action-editorconfig-checker@main
11+
12+
tests:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Run tests
17+
run: |
18+
cd tests
19+
DEBUG=1 ./run-tests

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ outputs:
77
description: 'validation results'
88
runs:
99
using: 'docker'
10-
image: 'Dockerfile'
10+
image: 'src/Dockerfile'

Dockerfile src/Dockerfile

File renamed without changes.

entrypoint.sh src/entrypoint.sh

File renamed without changes.

tests/run-tests

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
source ./tools/assert.sh
3+
4+
docker build --tag system-under-test ../src/ &> /dev/null
5+
6+
# checking invalid run
7+
out=$(docker run --rm -v "$PWD/test-cases/invalid-testfiles":/check system-under-test)
8+
exit=$?
9+
10+
assert "$exit" "1"
11+
assert_contains "$out" "wrong-indentation-type.md:"
12+
13+
# check valid run
14+
out=$(docker run --rm -v "$PWD/test-cases/valid-testfiles":/check system-under-test)
15+
exit=$?
16+
17+
assert "$exit" "0"
18+
assert "$out" "\n::set-output name=output::"
19+
20+
assert_end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
trim_trailing_whitespace = true
6+
indent_style = tab
7+
indent_size = 3
8+
insert_final_newline = true
9+
block_comment_start = /*
10+
block_comment = *
11+
block_comment_end = */
12+
13+
[Makefile]
14+
indent_style = tab
15+
16+
[*.yml]
17+
indent_style = space
18+
indent_size = unset
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Unit Test with wrong indentation type
2+
3+
indent with spaces
4+
indent with tabs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
trim_trailing_whitespace = true
6+
indent_style = tab
7+
indent_size = 3
8+
insert_final_newline = true
9+
block_comment_start = /*
10+
block_comment = *
11+
block_comment_end = */
12+
13+
[Makefile]
14+
indent_style = tab
15+
16+
[*.yml]
17+
indent_style = space
18+
indent_size = unset
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Unit Test with wrong indentation type
2+
3+
indent with tabs

tests/tools/assert.sh

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/bin/bash
2+
# assert.sh 1.1 - bash unit testing framework
3+
# Copyright (C) 2009-2015 Robert Lehmann
4+
#
5+
# http://github.com/lehmannro/assert.sh
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published
9+
# by the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
20+
export DISCOVERONLY=${DISCOVERONLY:-}
21+
export DEBUG=${DEBUG:-}
22+
export STOP=${STOP:-}
23+
export INVARIANT=${INVARIANT:-}
24+
export CONTINUE=${CONTINUE:-}
25+
26+
args="$(getopt -n "$0" -l \
27+
verbose,help,stop,discover,invariant,continue vhxdic $*)" \
28+
|| exit -1
29+
for arg in $args; do
30+
case "$arg" in
31+
-h)
32+
echo "$0 [-vxidc]" \
33+
"[--verbose] [--stop] [--invariant] [--discover] [--continue]"
34+
echo "`sed 's/./ /g' <<< "$0"` [-h] [--help]"
35+
exit 0;;
36+
--help)
37+
cat <<EOF
38+
Usage: $0 [options]
39+
Language-agnostic unit tests for subprocesses.
40+
41+
Options:
42+
-v, --verbose generate output for every individual test case
43+
-x, --stop stop running tests after the first failure
44+
-i, --invariant do not measure timings to remain invariant between runs
45+
-d, --discover collect test suites only, do not run any tests
46+
-c, --continue do not modify exit code to test suite status
47+
-h show brief usage information and exit
48+
--help show this help message and exit
49+
EOF
50+
exit 0;;
51+
-v|--verbose)
52+
DEBUG=1;;
53+
-x|--stop)
54+
STOP=1;;
55+
-i|--invariant)
56+
INVARIANT=1;;
57+
-d|--discover)
58+
DISCOVERONLY=1;;
59+
-c|--continue)
60+
CONTINUE=1;;
61+
esac
62+
done
63+
64+
_indent=$'\n\t' # local format helper
65+
66+
_assert_reset() {
67+
tests_ran=0
68+
tests_failed=0
69+
tests_errors=()
70+
tests_starttime="$(date +%s%N)" # nanoseconds_since_epoch
71+
}
72+
73+
assert_end() {
74+
# assert_end [suite ..]
75+
tests_endtime="$(date +%s%N)"
76+
# required visible decimal place for seconds (leading zeros if needed)
77+
local tests_time="$( \
78+
printf "%010d" "$(( ${tests_endtime/%N/000000000}
79+
- ${tests_starttime/%N/000000000} ))")" # in ns
80+
tests="$tests_ran ${*:+$* }tests"
81+
[[ -n "$DISCOVERONLY" ]] && echo "collected $tests." && _assert_reset && return
82+
[[ -n "$DEBUG" ]] && echo
83+
# to get report_time split tests_time on 2 substrings:
84+
# ${tests_time:0:${#tests_time}-9} - seconds
85+
# ${tests_time:${#tests_time}-9:3} - milliseconds
86+
[[ -z "$INVARIANT" ]] \
87+
&& report_time=" in ${tests_time:0:${#tests_time}-9}.${tests_time:${#tests_time}-9:3}s" \
88+
|| report_time=
89+
90+
if [[ "$tests_failed" -eq 0 ]]; then
91+
echo "all $tests passed$report_time."
92+
else
93+
for error in "${tests_errors[@]}"; do echo "$error"; done
94+
echo "$tests_failed of $tests failed$report_time."
95+
fi
96+
tests_failed_previous=$tests_failed
97+
[[ $tests_failed -gt 0 ]] && tests_suite_status=1
98+
_assert_reset
99+
}
100+
101+
assert_contains() {
102+
# assert <command> <expected stdout> [stdin]
103+
(( tests_ran++ )) || :
104+
[[ -z "$DISCOVERONLY" ]] || return
105+
expected=$(echo -ne "${2:-}")
106+
result="$1" || true
107+
if [[ "$result" == *"$expected"* ]]; then
108+
[[ -z "$DEBUG" ]] || echo -n .
109+
return
110+
fi
111+
result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")"
112+
[[ -z "$result" ]] && result="nothing" || result="\"$result\""
113+
[[ -z "$2" ]] && expected="nothing" || expected="\"$2\""
114+
_assert_fail "expected $expected${_indent}got $result" "$1" "$3"
115+
}
116+
117+
assert() {
118+
# assert <command> <expected stdout> [stdin]
119+
(( tests_ran++ )) || :
120+
[[ -z "$DISCOVERONLY" ]] || return
121+
122+
expected=$(echo -ne "${2:-}")
123+
result="$1" || true
124+
if [[ "$result" == "$expected" ]]; then
125+
[[ -z "$DEBUG" ]] || echo -n .
126+
return
127+
fi
128+
_assert_fail "expected $expected${_indent}got $result" "$1" "$3"
129+
}
130+
131+
_assert_fail() {
132+
# _assert_fail <failure> <command> <stdin>
133+
[[ -n "$DEBUG" ]] && echo -n X
134+
report="test #$tests_ran \"$2${3:+ <<< $3}\" failed:${_indent}$1"
135+
if [[ -n "$STOP" ]]; then
136+
[[ -n "$DEBUG" ]] && echo
137+
echo "$report"
138+
exit 1
139+
fi
140+
tests_errors[$tests_failed]="$report"
141+
(( tests_failed++ )) || :
142+
}
143+
144+
_assert_reset
145+
: ${tests_suite_status:=0} # remember if any of the tests failed so far
146+
_assert_cleanup() {
147+
local status=$?
148+
# modify exit code if it's not already non-zero
149+
[[ $status -eq 0 && -z $CONTINUE ]] && exit $tests_suite_status
150+
}
151+
trap _assert_cleanup EXIT

0 commit comments

Comments
 (0)