aboutsummaryrefslogtreecommitdiff
path: root/spec/01_lib_spec.sh
blob: 29bd05a4215a88b97baaa7a58312f8df8e8a0514 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# shellcheck disable=2091,2034
CPT_VERBOSE=1

Describe 'CPT Library'
    export CPT_COLOR=0
    Include ./src/cpt-lib
    Describe '--help'
        It 'prints usage information when called as a standalone script'
            When run script src/cpt-lib --help
            The word 1 of output should eq "usage:"
        End
    End
    Describe 'version()'
        VERSION=$(sed -n '/VERSION/s/.* //gp' config.mk)
        It 'prints version information'
            When run script src/cpt-lib version
            The line 1 of stdout should eq "Carbs Packaging Tools, version $VERSION"
        End
    End
    Describe 'text functions'
        Describe 'out()'
            It 'outputs a message with every argument triggering a newline'
                When call out Line1 Line2
                The output should eq "$(printf 'Line1\nLine2\n')"
            End
        End

        Describe 'log()'
            Parameters
                "#1" "-> hello " hello
                "#2" "-> hello world" hello world
                "#3" "hello test world" test world hello
            End
            It "prints a message prettily ($1)"
                When call log "$3" "$4" "$5"
                The stderr should eq "$2"
            End
        End

        Describe 'die()'
            It "exits the script by printing the given message"
                When run script src/cpt-lib die "Exiting"
                The stderr should eq "!> Exiting "
                The status should be failure
            End
        End

        Describe 'warn()'
            Parameters
                "#1" "WARNING notice " "notice"
                "#2" "WARNING package not found" "package" "not found"
                "#3" "!!! package not found" "package" "not found" "!!!"
            End
            It "displays a warning message ($1)"
                When call warn "$3" "$4" "$5"
                The stderr should eq "$2"
            End
        End

        Describe 'contains()'
            Parameters
                "#1" "foo bar" baz failure
                "#2" "foo bar baz" baz success
            End
            It "checks whether the given string list contains a word ($1)"
                When call contains "$2" "$3"
                The status should be "$4"
            End
        End

        Describe 'pop()'
            It "removes the first item from the following items"
                When call pop baz from foo bar baz
                The output should eq " foo  bar "
            End
        End

        Describe 'regesc()'
            Parameters
                "#1" '^[\\test$' '\^\[\\\\test\$'
                "#2" '\.*$' '\\\.\*\$'
            End
            It "escapes POSIX BRE sequences ($1)"
                When call regesc "$2"
                The output should eq "$3"
            End
        End

        Describe 'sepchar()'
            It 'seperates the output of given string'
                When call sepchar test
                The output should eq "$(printf 't\ne\ns\nt\n')"
            End
        End
    End


    Describe 'helper functions'
        Describe '_seq()'
            It 'counts to the given number'
                When call _seq 3
                The output should eq " 1  2  3 "
            End
        End

        Describe '_stat()'
            It 'outputs the owner of the given file'
               When call _stat LICENSE
               The output should eq "$(id -un)"
            End
        End

        Describe '_readlinkf()'
            mklink() { :> tests/testfile; ln -s testfile tests/testfile2 ;}
            rmlink() { rm -f tests/testfile tests/testfile2 ;}
            RPWD=$(cd -P .||:; printf %s "$PWD")
            Before mklink
            After  rmlink
            Parameters
                "#1" . "$RPWD"
                "#2" "$PWD/tests/testfile2" "$RPWD/tests/testfile"
            End
            It "outputs the real location of the given file ($1)"
               When call _readlinkf "$2"
               The output should eq "$3"
            End
        End

        Describe 'sh256()'
            It 'outputs an sha256 digest of the given file using any valid system tool'
                # This should cover our bases for a long time.
                When call sh256 .editorconfig
                The output should eq "da42265df733ca05a08d77405c35aa3dd5b8b7fefcc2da915f508067a49351da  .editorconfig"
            End
        End
    End

    Describe 'system functions'
        Describe 'as_root()'
            as_root_env() { user=$1 as_root env ;}
            Parameters
                root
                "$(id -un)"
            End
            It "runs the given command as user: '$1'"
                When call as_root_env "$1"
                The output should not eq ""
                The stderr should not eq ""
                The status should be success
            End
        End
    End

    Describe 'package functions'
        Describe 'run_hook()'
            CPT_HOOK=$PWD/tests/hook-file
            CPT_ROOT=$PWD/tests
            It "runs the given hook file"
                When call run_hook 1 test-package destination
                The stderr should eq "-> test-package Running 1 hook"
                The output should eq "$CPT_HOOK 1 test-package destination"
            End
            It "doesn't log 'running hook' if no package is given"
                When call run_hook 2 '' destination
                The stderr should eq "-> Running 2 hook"
                The output should eq "$CPT_HOOK 2 null destination"
            End
            It "uses the /etc/cpt-hook file of the root when called with a fourth arg"
                When call run_hook 3 cpt destdir root
                The stderr should eq "-> cpt Running 3 hook"
                The output should eq "$CPT_ROOT/etc/cpt-hook 3 cpt destdir"
                The variable CPT_HOOK should eq "$PWD/tests/hook-file"
            End
            It "returns with success even when the file doesn't exist"
                CPT_HOOK=$PWD/some-non-existent-file
                When call run_hook 4 thiswillnotrun
                The variable CPT_HOOK should not be exist
                The stderr should eq ""
                The status should be success
            End
            It "restores the \$CPT_HOOK variable when called with root"
                CPT_ROOT=$PWD/nonexistentdir
                When call run_hook 5 cpt dest root
                The variable CPT_ROOT should not be exist
                The stderr should eq ""
                The status should be success
                The variable CPT_HOOK should eq "$PWD/tests/hook-file"
            End
        End
        Describe 'create_tmp()'
            After pkg_clean
            It 'creates cache directories'
                When call create_tmp
                The variable mak_dir should be a directory
            End
        End
        Describe 'pkg_get_base()'
        CPT_ROOT=$PWD/tests
        CPT_PATH=$PWD/tests/repository
            It 'returns packages defined in base'
                When call pkg_get_base nonl
                The output should eq "dummy-pkg contrib-dummy-pkg "
            End
        End
    End
End