File: test_bash_comment_config_line.bats.jinja

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (65 lines) | stat: -rw-r--r-- 1,828 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash

set -pu

function call_comment_config_line {
    {{{ bash_comment_config_line("$1", "$2") | indent(4) }}}
}

is_old_bats=0

setup() {
    if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
        BATS_TEST_TMPDIR="$(mktemp -d)"  # 1.4.0
        # shellcheck disable=SC2034
        BATS_TEARDOWN_STARTED=  # 1.3.0
        is_old_bats=1
    else
        is_old_bats=0
    fi
    pushd "${BATS_TEST_TMPDIR}" || exit 1
    mkdir -p comment_cfg_test
}

teardown() {
    if (( is_old_bats )); then
        if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        local tmppath xpwd
        tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")"
        if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        xpwd="$(readlink -f -- .)"
        if [[ "${tmppath}" != "${xpwd}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        popd || exit 1
        rm -rf -- "${tmppath}"
        BATS_TEST_TMPDIR=""
    fi
}

@test "bash_comment_config_line - Basic line comment" {
    printf "\nSomeSetting = true\n" > comment_cfg_test/test.conf
    expected_output="\n#SomeSetting = true\n"

    call_comment_config_line "comment_cfg_test/test.conf" "^SomeSetting"

    run diff "comment_cfg_test/test.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_comment_config_line - Comment line with wildcards" {
    printf "\n*.* 1.2.3.4\n" > comment_cfg_test/test.conf
    expected_output="\n#*.* 1.2.3.4\n"

    call_comment_config_line "comment_cfg_test/test.conf" "^\*\.\*"

    run diff "comment_cfg_test/test.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}