forked from athena-oss/athena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.functions.fs.sh
116 lines (94 loc) · 3.74 KB
/
test.functions.fs.sh
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
function testcase_athena.fs.get_full_path()
{
bashunit.test.assert_exit_code.expects_fail "athena.fs.get_full_path" ""
bashunit.test.assert_exit_code.expects_fail "athena.fs.get_full_path" "/file/not/existing"
# full path
base=$(bashunit.test.create_tempdir)
bashunit.test.assert_output "athena.fs.get_full_path" "$base" "$base"
# relative path directory
mkdir $base/test
cd $base
bashunit.test.assert_output "athena.fs.get_full_path" "$base/test" "./test"
rm -r $base
# relative path file
tmpfile=$(bashunit.test.create_tempfile)
base="$(dirname $tmpfile)"
cd $base
bashunit.test.assert_output "athena.fs.get_full_path" "$base" "$(basename $tmpfile)"
rm $tmpfile
}
function testcase_athena.fs.absolutepath()
{
bashunit.test.assert_exit_code.expects_fail "athena.fs.absolutepath" "/non/existing/dir"
tmpfile=$(bashunit.test.create_tempfile)
bashunit.test.assert_output "athena.fs.absolutepath" "$tmpfile" "$tmpfile"
rm $tmpfile
}
function testcase_athena.fs.file_exists_or_fail()
{
bashunit.test.assert_exit_code.expects_fail "athena.fs.file_exists_or_fail" "/non/existing/file"
tmpfile=$(bashunit.test.create_tempfile)
bashunit.test.assert_exit_code "athena.fs.file_exists_or_fail" "$tmpfile"
rm $tmpfile
}
function testcase_athena.fs.dir_exists_or_fail()
{
bashunit.test.assert_exit_code.expects_fail "athena.fs.dir_exists_or_fail" "/non/existing/dir"
tmpfile=$(bashunit.test.create_tempdir)
bashunit.test.assert_exit_code "athena.fs.dir_exists_or_fail" "$tmpfile"
rm -r $tmpfile
bashunit.test.mock "athena.os.exit_with_msg" "_echo_arguments"
bashunit.test.assert_output "athena.fs.dir_exists_or_fail" "My custom message" "/non/existing/dir" "My custom message"
}
function testcase_athena.fs.dir_exists_or_create()
{
tmpdir="$HOME/xpto$(date +%s)"
bashunit.test.assert_exit_code "athena.fs.dir_exists_or_create" "$tmpdir"
bashunit.test.assert_exit_code "athena.fs.dir_exists_or_fail" "$tmpdir"
rm -r $tmpdir
}
function testcase_athena.fs.get_file_contents()
{
bashunit.test.assert_exit_code.expects_fail "athena.fs.get_file_contents" "/non/existing-file"
tmpfile=$(bashunit.test.create_tempfile)
echo "spinpans" > $tmpfile
bashunit.test.assert_output "athena.fs.get_file_contents" "spinpans" $tmpfile
rm $tmpfile
}
function testcase_athena.fs.file_contains_string()
{
tmpfile=$(bashunit.test.create_tempfile)
echo "spinpans" > $tmpfile
bashunit.test.assert_exit_code "athena.fs.file_contains_string" $tmpfile "spinpans"
bashunit.test.assert_exit_code.expects_fail "athena.fs.file_contains_string" $tmpfile "other"
rm $tmpfile
}
function testcase_athena.fs.dir_contains_files()
{
bashunit.test.assert_exit_code.expects_fail "athena.fs.dir_contains_files" "/path/to/non/existent/dir"
tmpdir=$(bashunit.test.create_tempdir)
bashunit.test.assert_exit_code.expects_fail "athena.fs.dir_contains_files" "$tmpdir"
bashunit.test.assert_return.expects_fail "athena.fs.dir_contains_files" "$tmpdir" "*.sh"
touch "$tmpdir/test.sh"
bashunit.test.assert_return "athena.fs.dir_contains_files" "$tmpdir" "*.sh"
bashunit.test.assert_return "athena.fs.dir_contains_files" "$tmpdir" "test?(_pre|_post).sh"
rm -r $tmpdir
}
function testcase_athena.fs.get_cache_dir()
{
local home=$HOME
local tmpdir=$(bashunit.test.create_tempdir)
HOME=$tmpdir
bashunit.test.mock.returns "athena.fs.dir_exists_or_create" 0
bashunit.test.assert_output "athena.fs.get_cache_dir" "$tmpdir/.athena"
HOME="$tmpdir/nonexistingpath"
bashunit.test.assert_exit_code.expects_fail "athena.fs.dir_exists_or_fail" "$HOME"
bashunit.test.unmock "athena.fs.dir_exists_or_create"
bashunit.test.assert_output "athena.fs.get_cache_dir" "$tmpdir/nonexistingpath/.athena"
bashunit.test.assert_exit_code "athena.fs.dir_exists_or_fail" "$HOME"
HOME=$home
}
function _echo_arguments()
{
echo -n "$@"
}