-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_project.sh
executable file
·147 lines (134 loc) · 4.25 KB
/
test_project.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
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
#!/bin/bash
#used to set terminal colours in the output
export RED="$(tput setaf 1)"
export GREEN="$(tput setaf 2)"
export ORANGE="$(tput setaf 3)"
export PURPLE="$(tput setaf 5)"
export BOLD="$(tput bold)"
export NC="$(tput sgr0)"
#get directory of this script so we can find the test subfolders
export THIS_DIR=`dirname $0`
#compiles a program for ex$1 with c files $2....$n
# $1 is the exercise
# $2 is files to compile
function compile {
void=`rm $PROJECT_PATH/test_output/$1.out 2>&1`
mkdir -p "$PROJECT_PATH/test_output"
cfiles=$2
cfiles=${cfiles[*]}
#cc -Wall -Wextra -Werror -o "$PROJECT_PATH/test_output/$1.out" -I "$PROJECT_PATH/" -F "$PROJECT_PATH/" "${@:2}"
cc -Wall -Wextra -Werror -o "$PROJECT_PATH/test_output/$1" -I"$THIS_DIR/$PROJECT_ID/$1" -I"$PROJECT_PATH/$1" $cfiles
}
export -f compile
#compares the output of program with given expected output and prints result
# $1 is path to program to test
# $2 is args to be passed to the program (can be "")
# $3 is expected output
# if $2 is "" or undefined then display a warning
function compare_output {
eval "$PROJECT_PATH/test_output/$1 $2"
output=`eval "$PROJECT_PATH/test_output/$1 $2"`
if [[ -z $3 || $3 == "" ]]
then
printf "\n${ORANGE}${BOLD}No automatic test defined.${NC}\n"
else
if [[ "$3" == "$output" ]]
then
printf "\n${GREEN}${BOLD}Got expected value for $1!${NC}\n"
else
printf "\n${RED}${BOLD}$1 fail: expected\n%s${NC}\n" "$3"
fi
fi
}
export -f compare_output
function anykey_continue {
tput sc
read -n 1 -srp "Press any key to continue..."
tput rc
tput el
printf "\n"
}
export -f anykey_continue
#tries to be as accurate as possible to an actual moulinette test on an excersize with a single function in it
#takes 2 or 3 arguments ->
# 1: label of the excersize ("ex05", etc)
# 2: array of filenames (not including PROJECT_PATH) of the source files to compile (eg "ft_putchar.c")
# 3: (optional) a string to compare the program's stdout with
#if argument 3 is not given then stdout will be directed to "$PROJECT_PATH/test_output/$1" for manual inspection
function test_function {
printf "Testing $1...\n"
#compile $1 "$PROJECT_PATH/$1/$2" "$THIS_DIR/$PROJECT_ID/test_$1.c"
cfile_path="$PROJECT_PATH/$1/"
if [[ -z $2 ]]
then
cfiles="$THIS_DIR/$PROJECT_ID/test_$1.c"
else
cfiles="$PROJECT_PATH/$1/${2// / $PROJECT_PATH/$1//} $THIS_DIR/$PROJECT_ID/test_$1.c"
fi
compile "$1" "$cfiles"
compare_output "$1" "" "$3"
anykey_continue
}
export -f test_function
#takes 1 or 2 arguments ->
# 1: label of the excersize ("ex05", etc)
# 2: (optional) a string to compare the program's stdout with
#if argument 3 is not given then stdout will be directed to "$PROJECT_PATH/test_output/$1" for manual inspection
function test_header {
printf "${ORANGE}${BOLD}Testing $1...${NC}\n"
compile "$1" "$THIS_DIR/$PROJECT_ID/test_$1.c"
compare_output "$1" "" "$2"
anykey_continue
}
export -f test_header
#similar to test_function above, but does not add a test c file to compilation and input is formatted differently
#takes 2 or more arguments ->
# 1: label of the excersize ("ex05", etc)
# 2: a space-separated list of files to compile with
# 3: (optional) args to call the program with (can be "")
# 4: (optional) expected output
function test_program {
printf "Testing $1...\n"
compile "$1" "$PROJECT_PATH/$1/$2"
compare_output "$1" "$3" "$4"
anykey_continue
}
export -f test_program
#similar to above once again, but relies on make to compile the program
# $1: exercise name (ex00 etc)
# $2: program name (ft_cat etc)
# $3: args to call program with
# $4: expected output
function test_program_with_make {
printf "Testing $1...\n"
(cd $PROJECT_PATH/$1 && make -s all)
cp "$PROJECT_PATH/$1/$2" "$PROJECT_PATH/test_output"
(cd $PROJECT_PATH/$1 && make -s fclean)
compare_output "$2" "$3" "$4"
anykey_continue
}
export -f test_program_with_make
#runs norminette on project dir
#$1 is flags to be passed to norminette
function norm {
printf "${ORANGE}${BOLD}Norminette:${NC}\n"
(cd $PROJECT_PATH && norminette $1)
printf "\n"
}
export -f norm
if [ -z $1 ]
then
echo "Invalid project id selected."
exit
fi
if [ -z $2 ]
then
echo "Invalid $PROJECT_ID path selected."
exit
fi
export PROJECT_ID=$1
export PROJECT_PATH=$2
#setup output directory
`mkdir -p "test_output"`
#go
sh "$THIS_DIR/$PROJECT_ID/run_tests.sh"