-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.sh
executable file
·181 lines (167 loc) · 5.1 KB
/
test.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
# test.sh
# Author(s): Mayank Kumar (@mayankk2308, github.com)
# License: Specified in LICENSE.md
# Version: 1.0.0
# Options: -v | --version, -d | --delete, -ps | --project-size, -h | --help
# Console text preferences
underline="$(tput smul)"
bold="$(tput bold)"
normal="$(tput sgr0)"
# Script information
script_version="1.0.0"
test_dir="tests/"
# Test information
project_size=""
output="/dev/stdout"
pyosphere_config="pyosphere.config"
# Print the script version to console
print_version() {
echo "${script_version}"
}
# Print test.sh usage
print_usage() {
echo "Usage: ${bold}./test.sh${normal} [-v|--version] [-d|--delete] [-ps|--project-size] [-h|--help]
where:
${underline}-v${normal} Prints script version.
${underline}-d${normal} Deletes generated test files and folders.
${underline}-ps${normal} Specifies project size [small/medium (default)/large].
${underline}-h${normal} Prints script usage."
}
# Delete any test files/folders
delete_tests() {
if [[ -d "${test_dir}" ]]
then
rm -r "${test_dir}"
if [[ "$1" != "-s" ]]
then
echo "Test files have been removed sucessfully."
fi
else
if [[ "$1" != "-s" ]]
then
echo "Test directory not found. No action required."
fi
fi
}
# Auto-generate test libraries with provided args
generate_test_files() {
local folder_name="${1}"
local file_prefix="${2}"
local file_count="${3}"
local file_var_count="${4}"
local file_func_count="${5}"
local nest_level="${6}"
local base_path="${test_dir}${folder_name}/"
mkdir -p "${base_path}"
for (( file_num=1; file_num<="${file_count}"; file_num++ ))
do
local nests=0
local folder_value=""
local nested_path="${base_path}"
if [[ "${nest_level}" != 0 ]]
then
nests="$(( $RANDOM % $nest_level + 1 ))"
fi
if [[ "$(( $RANDOM % 2 ))" == 1 ]]
then
folder_value="${file_num}"
fi
for (( depth=1; depth<=nests; depth++ ))
do
nested_path="${nested_path}${file_prefix}${folder_value}_internal_${depth}/"
done
mkdir -p "${nested_path}"
local program_file="${nested_path}/${file_prefix}${file_num}.py"
touch "${program_file}"
for (( value_num=1; value_num<="${file_var_count}"; value_num++ ))
do
echo "${file_prefix}${file_num}_value${value_num} = ${value_num}" >> "${program_file}"
done
for (( func_num=1; func_num<="${file_func_count}"; func_num++ ))
do
echo -e "\n" >> "${program_file}"
echo -e "def ${file_prefix}${file_num}_func${func_num}():\n\tprint(\"Inside func${func_num}\")" >> "${program_file}"
done
echo -e "\n" >> "${program_file}"
done
}
# Prepare small, medium (default), or large test
prepare_test() {
case "${1}" in
"small")
echo "${bold}Preparing small test..${normal}"
generate_test_files "Libraries" "lib" 3 3 3 1
generate_test_files "Sources" "main" 1 0 0 0
local main_program="from lib1 import lib1_value1\nimport lib2\nprint(lib1_value1)\nlib2.lib2_func1()"
echo -e "${main_program}" > "${test_dir}Sources/main1.py"
echo "Small test generated."
;;
""|"medium")
echo "${bold}Preparing medium test..${normal}"
generate_test_files "Libraries" "lib" 20 5 5 5
generate_test_files "Sources" "main" 1 0 0 0
local main_program="from lib7 import lib7_value4\nimport lib19\nprint(lib7_value4)\nlib19.lib19_func5()"
echo -e "${main_program}" > "${test_dir}Sources/main1.py"
echo "Medium test generated."
;;
"large")
echo "${bold}Preparing large test..${normal}"
generate_test_files "Libraries" "lib" 100 20 20 10
generate_test_files "Sources" "main" 1 0 0 0
local main_program="from lib97 import lib97_value20\nimport lib58\nprint(lib97_value20)\nlib58.lib58_func7()"
echo -e "${main_program}" > "${test_dir}Sources/main1.py"
echo "Large test generated."
;;
*)
echo "Invalid project size provided."
print_usage
exit
;;
esac
}
# ----- CONFIG TESTING
# Generates specific config file according to test case
generate_test_config() {
local python_bin="${1}"
local given_run_source="${2}"
local given_project_path="${3}"
local always_prune_pref="${4}"
echo "${bold}Generating pyosphere configuration...${normal}" > "${output}"
touch "${pyosphere_config}"
> "${pyosphere_config}"
echo -e "#!/bin/bash\n" >> "${pyosphere_config}"
echo -e "python=\"${python_bin}\"" >> "${pyosphere_config}"
echo -e "run_source=\"${given_run_source}\"" >> "${pyosphere_config}"
echo -e "project_path=\"${given_project_path}\"" >> "${pyosphere_config}"
echo -e "always_prune=${always_prune_pref}" >> "${pyosphere_config}"
echo "Configuration generated." > "${output}"
}
# Destroys Config file for specific test case
destroy_test_config() {
echo "Destroying ${1}..."
rm "${pyosphere_config}"
echo "Destruction complete."
}
# Parse provided user arguments
parse_args() {
case "${@}" in
-v|--version)
print_version
;;
-d|--delete)
delete_tests
;;
-h|--help)
print_usage
;;
-ps=*|--project-size=*|"")
delete_tests -s
prepare_test "${@#*=}"
;;
*)
echo "Invalid argument."
print_usage
esac
}
parse_args "${@}"