forked from exercism/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-exercise
executable file
·87 lines (77 loc) · 2.37 KB
/
test-exercise
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
#!/bin/bash
# Test an exercise
# which exercise are we testing right now?
# if we were passed an argument, that should be the
# exercise directory. Otherwise, assume we're in
# it currently.
if [ $# -ge 1 ]; then
exercise=$1
# if this script is called with arguments, it will pass through
# any beyond the first to cargo. Note that we can only get a
# free default argument if no arguments at all were passed,
# so if you are in the exercise directory and want to pass any
# arguments to cargo, you need to include the local path first.
# I.e. to test in release mode:
# $ test-exercise . --release
shift 1
else
exercise='.'
fi
# what cargo command will we use?
if [ -n "$BENCHMARK" ]; then
command="bench"
else
command="test"
fi
declare -a preserve_files=("src/lib.rs" "Cargo.toml" "Cargo.lock")
declare -a preserve_dirs=("tests")
# reset instructions
reset () {
for file in ${preserve_files[@]}; do
if [ -f "$exercise/$file.orig" ]; then
mv -f "$exercise/$file.orig" "$exercise/$file"
fi
done
for dir in ${preserve_dirs[@]}; do
if [ -d "$exercise/$dir.orig" ]; then
rm -rf "$exercise/$dir"
mv "$exercise/$dir.orig" "$exercise/$dir"
fi
done
}
# cause the reset to execute when the script exits normally or is killed
trap reset EXIT INT TERM
# preserve the files and directories we care about
for file in ${preserve_files[@]}; do
if [ -f "$exercise/$file" ]; then
cp "$exercise/$file" "$exercise/$file.orig"
fi
done
for dir in ${preserve_dirs[@]}; do
if [ -d "$exercise/$dir" ]; then
cp -r "$exercise/$dir" "$exercise/$dir.orig"
fi
done
# Move example files to where Cargo expects them
cp -f "$exercise/example.rs" "$exercise/src/lib.rs"
if [ -f "$exercise/Cargo-example.toml" ]; then
cp -f "$exercise/Cargo-example.toml" "$exercise/Cargo.toml"
fi
# If deny warnings, insert a deny warnings compiler directive in the header
if [ -n "$DENYWARNINGS" ]; then
sed -i -e '1i #![deny(warnings)]' "$exercise/src/lib.rs"
fi
# eliminate #[ignore] lines from tests
for test in "$exercise/tests/*.rs"; do
sed -i -e '/#\[ignore\]/{
s/#\[ignore\]\s*//
/^\s*$/d
}' $test
done
# run tests from within exercise directory
# (use subshell so we auto-reset to current pwd after)
(
cd $exercise
# this is the last command; its exit code is what's passed on
cargo $command "$@"
)