mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-09-21 08:33:20 +00:00
4b9572a0a3
Eight unit tests measure shell code: the Genesis dracut cmdline hooks, doxcat, getcert, the two Genesis deb builders, go-xcat and the genesis test case. The Perl in each one is scaffolding. It reads the script, lifts a block out with a regular expression, writes a wrapper, shells out and reads the files back. A reader follows two languages to reach one assertion, and the scaffolding is longer than the assertion. xCAT-test/bats already states this kind of assertion in the language of the thing under test, and the xcat_test workflow runs it. The eight files move there. Each one keeps what it proved: the rpm architecture becomes the Debian architecture and names the deb it supersedes, the dracut hook picks the console mode the multiplexer can provide, the hook gives root the home directory /, getcert stops when the image ships no openssl, the genesis case defines its node with the architecture of the management node and fails when nodeset fails, doxcat picks dhcpcd where the release drops the ISC client, and go-xcat names the Genesis packages the packaging builds. helpers/shell_source.bash gains refute_grep. bash ignores errexit for a command inverted with "!", so "! grep" anywhere but the last line of a test can never fail it. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
143 lines
2.7 KiB
Bash
143 lines
2.7 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
repo_root()
|
|
{
|
|
printf '%s\n' "${BATS_TEST_DIRNAME}/../.."
|
|
}
|
|
|
|
repo_path()
|
|
{
|
|
printf '%s/%s\n' "$(repo_root)" "$1"
|
|
}
|
|
|
|
require_repo_file()
|
|
{
|
|
local path
|
|
path="$(repo_path "$1")"
|
|
[ -r "$path" ] || skip "$path is required"
|
|
printf '%s\n' "$path"
|
|
}
|
|
|
|
read_file_or_empty()
|
|
{
|
|
local path="$1"
|
|
[ -f "$path" ] || return 0
|
|
cat "$path"
|
|
}
|
|
|
|
extract_shell_function()
|
|
{
|
|
local file="$1"
|
|
local name="$2"
|
|
|
|
awk -v name="$name" '
|
|
BEGIN {
|
|
signature = "^[[:space:]]*(function[[:space:]]+)?" name "([[:space:]]*\\(\\))?[[:space:]]*$"
|
|
inline_signature = "^[[:space:]]*(function[[:space:]]+)?" name "([[:space:]]*\\(\\))?[[:space:]]*\\{"
|
|
}
|
|
$0 ~ signature || $0 ~ inline_signature {
|
|
copy = 1
|
|
}
|
|
copy {
|
|
print
|
|
opened += gsub(/\{/, "{")
|
|
closed += gsub(/\}/, "}")
|
|
if (opened > 0 && opened == closed) {
|
|
found = 1
|
|
exit
|
|
}
|
|
}
|
|
END {
|
|
if (!found) {
|
|
exit 1
|
|
}
|
|
}
|
|
' "$file"
|
|
}
|
|
|
|
extract_shell_if_block()
|
|
{
|
|
local file="$1"
|
|
local start="$2"
|
|
|
|
awk -v start="$start" '
|
|
index($0, start) {
|
|
copy = 1
|
|
}
|
|
copy {
|
|
print
|
|
if ($0 ~ /^[[:space:]]*if[[:space:]\[]/) {
|
|
depth++
|
|
}
|
|
line = $0
|
|
while (line ~ /(^|[;[:space:]])fi([;[:space:]]|$)/) {
|
|
depth--
|
|
sub(/(^|[;[:space:]])fi([;[:space:]]|$)/, " ", line)
|
|
}
|
|
if (depth == 0) {
|
|
found = 1
|
|
exit
|
|
}
|
|
}
|
|
END {
|
|
if (!found) {
|
|
exit 1
|
|
}
|
|
}
|
|
' "$file"
|
|
}
|
|
|
|
extract_line_range()
|
|
{
|
|
local file="$1"
|
|
local start="$2"
|
|
local end="$3"
|
|
|
|
awk -v start="$start" -v end="$end" '
|
|
$0 ~ start {
|
|
copy = 1
|
|
}
|
|
copy {
|
|
print
|
|
if ($0 ~ end) {
|
|
found = 1
|
|
exit
|
|
}
|
|
}
|
|
END {
|
|
if (!found) {
|
|
exit 1
|
|
}
|
|
}
|
|
' "$file"
|
|
}
|
|
|
|
extract_first_matching_line()
|
|
{
|
|
local file="$1"
|
|
local pattern="$2"
|
|
|
|
awk -v pattern="$pattern" '
|
|
$0 ~ pattern {
|
|
print
|
|
found = 1
|
|
exit
|
|
}
|
|
END {
|
|
if (!found) {
|
|
exit 1
|
|
}
|
|
}
|
|
' "$file"
|
|
}
|
|
|
|
# grep that fails when the pattern IS present.
|
|
#
|
|
# Do not write "! grep ..." for this. bash ignores errexit for a command inverted with "!",
|
|
# so such a line never fails a test unless it is the last line of one.
|
|
refute_grep()
|
|
{
|
|
! grep "$@"
|
|
return $?
|
|
}
|