-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_spec
executable file
·133 lines (116 loc) · 3 KB
/
test_spec
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
#!/bin/bash
#==================================================================================
# Unit tests for bash_spec
#
# Version 1.1.0 13 May 2018
# Added check for unset variable
# Version 1.0.0 29 Jul 2014
# Release
# Author: Dave Nicolette
# License: MIT
#==================================================================================
. bash_spec
function displays_actual_and_expected_values {
it "displays correct actual and expected values"
fred="hello"
expect $fred to_be "junk" > tempout
`grep --quiet 'expected: junk | actual: hello' tempout` && {
(( _failed_-=1 ))
(( _passed_+=1 ))
pass
} || {
(( _failed_+=1 ))
fail
}
}
function equality_of_scalar_value {
it "reports two scalar values are equal"
one="1"
expect $one to_be 1
}
function nonequality_of_scalar_value {
it "reports two scalar values are unequal"
one="1"
expect $one not to_be 2
}
function matches_empty_or_unset_variable {
it "matches an empty or unset variable"
my_var=
expect my_var to_be_empty
}
function matches_variable_that_has_any_value {
it "matches a variable that is set (any value)"
my_var="something"
expect my_var not to_be_empty
}
function string_matches_regex_pattern {
it "reports regex match"
str="one fine day"
expect "$str" to_match day$
}
function string_does_not_match_regex_pattern {
it "reports regex mismatch"
str="one fine night"
expect "$str" not to_match day$
}
function array_contains_value {
it "reports an array contains a given value"
declare -a arr=(1 2 3 4)
expect "${arr[@]}" to_contain 3
}
function array_does_not_contain_value {
it "reports an array does not contain a given value"
declare -a arr=(1 2 3 4)
expect "${arr[@]}" not to_contain 5
}
function file_exists {
it "reports a file exists"
echo 'test' > tempfile
expect tempfile to_exist
rm -f tempfile
}
function file_does_not_exist {
it "reports a file does not exist"
rm -f tempfile
expect tempfile not to_exist
}
function file_has_given_mode {
it "reports a file has the given mode"
touch tempfile
chmod u=rw,g=r,o=x tempfile
expect tempfile to_have_mode -rw-r----x
rm -f tempfile
}
function file_does_not_have_given_mode {
it "reports a file does not have the given mode"
touch tempfile
chmod u=rw,g=r,o=x tempfile
expect tempfile not to_have_mode -rw-rw-rwx
rm -f tempfile
}
function package_is_installed {
it "reports a given package is installed"
expect bash to_be_installed
}
function package_is_not_installed {
it "reports a given package is not installed"
expect bashx not to_be_installed
}
print_header bash_spec
displays_actual_and_expected_values
equality_of_scalar_value
nonequality_of_scalar_value
matches_empty_or_unset_variable
matches_variable_that_has_any_value
string_matches_regex_pattern
string_does_not_match_regex_pattern
array_contains_value
array_does_not_contain_value
file_exists
file_does_not_exist
file_has_given_mode
file_does_not_have_given_mode
package_is_installed
package_is_not_installed
print_trailer
exit 0