Vendoring bats libraries.

Signed-off-by: Chad Metcalf <chad@docker.com>
This commit is contained in:
Chad Metcalf
2020-08-08 12:46:22 -07:00
committed by Chad Metcalf
parent 5185cedf4e
commit 6565a1f745
48 changed files with 4646 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_err() <message...>: displays <message...>' {
run batslib_err 'm1' 'm2'
[ "$status" -eq 0 ]
[ "$output" == 'm1 m2' ]
}
@test 'batslib_err(): reads <message...> from STDIN' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
echo 'm1' 'm2' | batslib_err"
[ "$status" -eq 0 ]
[ "$output" == 'm1 m2' ]
}

View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_count_lines() <string>: displays the number of lines in <string>' {
run batslib_count_lines $'a\nb\nc\n'
[ "$status" -eq 0 ]
[ "$output" == '3' ]
}
@test 'batslib_count_lines() <string>: counts the last line when it is not terminated by a newline' {
run batslib_count_lines $'a\nb\nc'
[ "$status" -eq 0 ]
[ "$output" == '3' ]
}
@test 'batslib_count_lines() <string>: counts empty lines' {
run batslib_count_lines $'\n\n\n'
[ "$status" -eq 0 ]
[ "$output" == '3' ]
}

View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_is_single_line() <string...>: returns 0 if all <string...> are single-line' {
run batslib_is_single_line 'a' $'b\n' 'c'
[ "$status" -eq 0 ]
}
@test 'batslib_is_single_line() <string...>: returns 1 if at least one of <string...> is longer than one line' {
run batslib_is_single_line 'a' $'b\nb' 'c'
[ "$status" -eq 1 ]
}

View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_get_max_single_line_key_width() <pair...>: displays the length of the longest key' {
local -ar pairs=( 'k _1' 'v 1'
'k 2' 'v 2'
'k __3' 'v 3' )
run batslib_get_max_single_line_key_width "${pairs[@]}"
[ "$status" -eq 0 ]
[ "$output" == '5' ]
}
@test 'batslib_get_max_single_line_key_width() <pair...>: only considers keys with single-line values' {
local -ar pairs=( 'k _1' 'v 1'
'k 2' 'v 2'
'k __3' $'v\n3' )
run batslib_get_max_single_line_key_width "${pairs[@]}"
[ "$status" -eq 0 ]
[ "$output" == '4' ]
}

View File

@@ -0,0 +1,27 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_print_kv_single() <width> <pair...>: displays <pair...> in two-column format with <width> wide key column' {
local -ar pairs=( 'k _1' 'v 1'
'k 2 ' 'v 2'
'k __3' 'v 3' )
run batslib_print_kv_single 5 "${pairs[@]}"
[ "$status" -eq 0 ]
[ "${#lines[@]}" == '3' ]
[ "${lines[0]}" == 'k _1 : v 1' ]
[ "${lines[1]}" == 'k 2 : v 2' ]
[ "${lines[2]}" == 'k __3 : v 3' ]
}
@test 'batslib_print_kv_single() <width> <pair...>: does not truncate keys when the column is too narrow' {
local -ar pairs=( 'k _1' 'v 1'
'k 2' 'v 2'
'k __3' 'v 3' )
run batslib_print_kv_single 0 "${pairs[@]}"
[ "$status" -eq 0 ]
[ "${#lines[@]}" == '3' ]
[ "${lines[0]}" == 'k _1 : v 1' ]
[ "${lines[1]}" == 'k 2 : v 2' ]
[ "${lines[2]}" == 'k __3 : v 3' ]
}

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_print_kv_multi() <pair...>: displays <pair...> in multi-line format' {
local -ar pairs=( 'k _1' 'v 1'
'k 2' $'v 2-1\nv 2-2'
'k __3' 'v 3' )
run batslib_print_kv_multi "${pairs[@]}"
[ "$status" -eq 0 ]
[ "${#lines[@]}" == '7' ]
[ "${lines[0]}" == 'k _1 (1 lines):' ]
[ "${lines[1]}" == 'v 1' ]
[ "${lines[2]}" == 'k 2 (2 lines):' ]
[ "${lines[3]}" == 'v 2-1' ]
[ "${lines[4]}" == 'v 2-2' ]
[ "${lines[5]}" == 'k __3 (1 lines):' ]
[ "${lines[6]}" == 'v 3' ]
}

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_print_kv_single_or_multi() <width> <pair...>: displays <pair...> in two-column format if all values are one line long' {
local -ar pairs=( 'k _1' 'v 1'
'k 2 ' 'v 2'
'k __3' 'v 3' )
run batslib_print_kv_single_or_multi 5 "${pairs[@]}"
[ "$status" -eq 0 ]
[ "${#lines[@]}" == '3' ]
[ "${lines[0]}" == 'k _1 : v 1' ]
[ "${lines[1]}" == 'k 2 : v 2' ]
[ "${lines[2]}" == 'k __3 : v 3' ]
}
@test 'batslib_print_kv_single_or_multi() <width> <pair...>: displays <pair...> in multi-line format if at least one value is longer than one line' {
local -ar pairs=( 'k _1' 'v 1'
'k 2' $'v 2-1\nv 2-2'
'k __3' 'v 3' )
run batslib_print_kv_single_or_multi 5 "${pairs[@]}"
[ "$status" -eq 0 ]
[ "${#lines[@]}" == '7' ]
[ "${lines[0]}" == 'k _1 (1 lines):' ]
[ "${lines[1]}" == ' v 1' ]
[ "${lines[2]}" == 'k 2 (2 lines):' ]
[ "${lines[3]}" == ' v 2-1' ]
[ "${lines[4]}" == ' v 2-2' ]
[ "${lines[5]}" == 'k __3 (1 lines):' ]
[ "${lines[6]}" == ' v 3' ]
}

View File

@@ -0,0 +1,43 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_prefix() <prefix>: prefixes each line of the input with <prefix>' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf 'a\nb\nc\n' | batslib_prefix '_'"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '_a' ]
[ "${lines[1]}" == '_b' ]
[ "${lines[2]}" == '_c' ]
}
@test 'batslib_prefix() <prefix>: prefixes the last line when it is not terminated by a newline' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf 'a\nb\nc' | batslib_prefix '_'"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '_a' ]
[ "${lines[1]}" == '_b' ]
[ "${lines[2]}" == '_c' ]
}
@test 'batslib_prefix() <prefix>: prefixes empty lines' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf '\n\n\n' | batslib_prefix '_'"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '_' ]
[ "${lines[1]}" == '_' ]
[ "${lines[2]}" == '_' ]
}
@test 'batslib_prefix(): <prefix> default to two spaces' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf 'a\nb\nc\n' | batslib_prefix"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == ' a' ]
[ "${lines[1]}" == ' b' ]
[ "${lines[2]}" == ' c' ]
}

View File

@@ -0,0 +1,72 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_mark() <mark> <index>: marks the <index>-th line of the input with <mark>' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf ' a\n b\n c\n' | batslib_mark '>' 0"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '>a' ]
[ "${lines[1]}" == ' b' ]
[ "${lines[2]}" == ' c' ]
}
@test 'batslib_mark() <mark> <index...>: marks multiple lines when <index...> is in ascending order' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf ' a\n b\n c\n' | batslib_mark '>' 1 2"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == ' a' ]
[ "${lines[1]}" == '>b' ]
[ "${lines[2]}" == '>c' ]
}
@test 'batslib_mark() <mark> <index...>: marks multiple lines when <index...> is in random order' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf ' a\n b\n c\n d\n' | batslib_mark '>' 2 1 3"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == ' a' ]
[ "${lines[1]}" == '>b' ]
[ "${lines[2]}" == '>c' ]
[ "${lines[3]}" == '>d' ]
}
@test 'batslib_mark() <mark> <index...>: ignores duplicate indices' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf ' a\n b\n c\n' | batslib_mark '>' 1 2 1"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == ' a' ]
[ "${lines[1]}" == '>b' ]
[ "${lines[2]}" == '>c' ]
}
@test 'batslib_mark() <mark> <index...>: outputs the input untouched if <mark> is the empty string' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf ' a\n b\n c\n' | batslib_mark '' 1"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == ' a' ]
[ "${lines[1]}" == ' b' ]
[ "${lines[2]}" == ' c' ]
}
@test 'batslib_mark() <mark> <index>: marks the last line when it is not terminated by a newline' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf ' a\n b\n c' | batslib_mark '>' 2"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == ' a' ]
[ "${lines[1]}" == ' b' ]
[ "${lines[2]}" == '>c' ]
}
@test 'batslib_mark() <mark> <index>: does not truncate <mark> if it is longer than the marked line' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
printf '\n' | batslib_mark '>' 0"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 1 ]
[ "${lines[0]}" == '>' ]
}

View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bats
load test_helper
@test 'batslib_decorate() <title>: encloses the input in a footer line and a header line containing <title>' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
echo 'body' | batslib_decorate 'title'"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- title --' ]
[ "${lines[1]}" == 'body' ]
[ "${lines[2]}" == '--' ]
}

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bats
load test_helper
@test 'fail() <message>: returns 1 and displays <message>' {
run fail 'message'
[ "$status" -eq 1 ]
[ "$output" == 'message' ]
}
@test 'fail(): reads <message> from STDIN' {
run bash -c "source '${TEST_MAIN_DIR}/load.bash'
echo 'message' | fail"
[ "$status" -eq 1 ]
[ "$output" == 'message' ]
}

View File

@@ -0,0 +1,88 @@
#!/usr/bin/env bats
load 'test_helper'
# Test functions
test_func_lvl_2() {
test_func_lvl_1 "$@"
}
test_func_lvl_1() {
test_func_lvl_0 "$@"
}
test_func_lvl_0() {
batslib_is_caller "$@"
}
#
# Direct invocation
#
# Interface
@test 'batslib_is_caller() <function>: returns 0 if the current function was called directly from <function>' {
run test_func_lvl_1 test_func_lvl_1
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'batslib_is_caller() <function>: returns 1 if the current function was not called directly from <function>' {
run test_func_lvl_0 test_func_lvl_1
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 0 ]
}
# Correctness
@test 'batslib_is_caller() <function>: the current function does not appear on the call stack' {
run test_func_lvl_0 test_func_lvl_0
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 0 ]
}
#
# Indirect invocation
#
# Options
test_i_indirect() {
run test_func_lvl_2 "$@"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'batslib_is_caller() -i <function>: enables indirect checking' {
test_i_indirect -i test_func_lvl_2
}
@test 'batslib_is_caller() --indirect <function>: enables indirect checking' {
test_i_indirect --indirect test_func_lvl_2
}
# Interface
@test 'batslib_is_caller() --indirect <function>: returns 0 if the current function was called indirectly from <function>' {
run test_func_lvl_2 --indirect test_func_lvl_2
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'batslib_is_caller() --indirect <function>: returns 1 if the current function was not called indirectly from <function>' {
run test_func_lvl_1 --indirect test_func_lvl_2
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 0 ]
}
# Correctness
@test 'batslib_is_caller() --indirect <function>: direct invocation is a special case of indirect invocation with zero intermediate calls' {
run test_func_lvl_1 --indirect test_func_lvl_1
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'batslib_is_caller() --indirect <function>: the current function does not appear on the call stack' {
run test_func_lvl_0 --indirect test_func_lvl_0
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 0 ]
}

View File

@@ -0,0 +1,6 @@
setup() {
export TEST_MAIN_DIR="${BATS_TEST_DIRNAME}/.."
# Load library.
load '../load'
}