test_common_utils.sh 3.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Test common_utils

[ "$(abspath foo)" == "$PWD/foo" ] || ingest "abspath foo"
[ "$(abspath foo/bar)" == "$PWD/foo/bar" ] || ingest "abspath foo/bar"
[ "$(abspath /foo)" == "/foo" ] || ingest "abspath /foo"
[ "$(relpath $PWD/foo)" == "foo" ] || ingest "relpath foo"
[ "$(relpath foo/bar foo)" == "bar" ] || ingest "relpath foo/bar"
[ "$(realpath /foo)" == "/foo" ] || ingest "realpath /foo"

[ "$(lex_ver 2)" == "002000000" ] || ingest "lex_ver 2"
[ "$(lex_ver 2.1)" == "002001000" ] || ingest "lex_ver 2.1"
[ "$(lex_ver 2.1.4)" == "002001004" ] || ingest "lex_ver 2.1.4"
[ "$(lex_ver 2.1.4rc1)" == "002001004" ] || ingest "lex_ver 2.1.4"

15
16
17
18
19
20
21
22
23
24
[ "$(unlex_ver 002000000)" == "2.0.0" ] || ingest "unlex_ver 002000000"
[ "$(unlex_ver 003002012)" == "3.2.12" ] || ingest "unlex_ver 003002012"
# Not octal
[ "$(unlex_ver 003044099)" == "3.44.99" ] || ingest "unlex_ver 003044099"
[ "$(unlex_ver 003543012)" == "3.543.12" ] || ingest "unlex_ver 003543012"
[ "$(unlex_ver 003543012abc)" == "3.543.12" ] || ingest "unlex_ver 003543012abc"

[ "$(strip_ver_suffix 3.4.0rc1)" == "3.4.0" ] || ingest "unlex_ver strip suff 1"
[ "$(strip_ver_suffix 3.24.12a4)" == "3.24.12" ] || ingest "unlex_ver strip suff 2"

25
26
27
28
29
[ "$(is_function abspath)" == "true" ] || ingest "is_function abspath"
[ "$(is_function foo)" == "" ] || ingest "is_function foo"
bar=baz
[ "$(is_function bar)" == "" ] || ingest "is_function bar"

Matthew Brett's avatar
Matthew Brett committed
30
31
32
33
34
35
36
37
38
39
# Check function is not run in is_function. Thanks to Andrew Murray.
function rmfile {
    rm testfile
}

touch testfile
[ "$(is_function rmfile)" == "true" ] || ingest "is_function rmfile"
[ -f testfile ] || ingest "testfile removed during isfunction check"
rm testfile

40
41
42
43
44
45
rm_mkdir tmp_dir
[ -d tmp_dir ] || ingest "tmp_dir does not exist"
touch tmp_dir/afile
rm_mkdir tmp_dir
[ -e tmp_dir/afile ] && ingest "tmp_dir/afile should have been deleted"
rmdir tmp_dir
46

47
48
49
50
51
52
53
# Test suppress command
function bad_cmd {
    echo bad
    return 1
}

function bad_mid_cmd {
54
    # Command returns 0, but errors in the middle
55
56
57
58
59
60
61
62
63
64
65
    echo ok for now
    false
    echo should be bad now
    return 0
}

function good_cmd {
    echo good
    return 0
}

66
# Store state of options including -e, -x
67
# https://stackoverflow.com/questions/14564746/in-bash-how-to-get-the-current-status-of-set-x?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
68
69
70
71
72
73
74
75
ORIG_OPTS=$-
set +ex
[ "$(suppress bad_cmd)" == "$(printf "Running bad_cmd\nbad")" ] \
    || ingest "suppress bad_cmd"
[ "$(suppress good_cmd)" == "Running good_cmd" ] \
    || ingest "suppress good_cmd"
[ "$(suppress bad_mid_cmd)" == "Running bad_mid_cmd" ] \
    || ingest "suppress bad_mid_cmd"
76
77
78
79
80
81
82
# Can't use pipes here, because of the effect on set -e behavior.
expected="$(printf "Running bad_cmd\nbad")"
actual="$(set -e; suppress bad_cmd)"
[ "$actual" == "$expected" ] || ingest "suppress bad_cmd set -e"
expected="$(printf "Running good_cmd")"
actual="$(set -e; suppress good_cmd)"
[ "$actual" == "$expected" ] || ingest "suppress good_cmd set -e"
83
expected="$(printf "Running bad_mid_cmd\nok for now")"
84
85
actual="$(set -e; suppress bad_mid_cmd)"
[ "$actual" == "$expected" ] || ingest "suppress bad_mid_cmd set -e"
86
87
# Reset options
set_opts $ORIG_OPTS
88

mattip's avatar
mattip committed
89
90
91
92
93
# On Linux docker containers in travis, can be x86_64, i686, or aarch64
[ "$(get_platform)" == x86_64 ] || \
    [ "$(get_platform)" == i686 ] || \
    [ "$(get_platform)" == aarch64 ] || \
    exit 1
94
95
96
97

# Crudest possible check for get_distutils_platform
expected=$(python -c "import distutils.util as du; print(du.get_platform())")
[ "$(get_distutils_platform)" == "$expected" ] || ingest "bad distutils platform"