8
8
# Author Daniel Cadden, Github @Shikkic.
9
9
10
10
main () {
11
- # if [[ $(whoami) != "vagrant" ]]; then
12
- # exit 1;
13
- # fi
14
-
15
11
# Set globle variable defaults.
16
12
EXIT_STATUS=0;
17
13
MIN_COVERAGE_PERCENTAGE_NUM=0.0;
18
14
VERBOSE_FLAG=' false' ;
19
15
FILE_PATH=' .' ;
16
+ BUILD_FLAGS=' ' ;
20
17
21
18
# Parse flags and assign to global variables.
22
- while getopts ' :n:f:v' flag; do
19
+ while getopts ' :n:f:t: v' flag; do
23
20
case " ${flag} " in
24
21
n) MIN_COVERAGE_PERCENTAGE_NUM=" ${OPTARG} " ;;
25
- v) VERBOSE_FLAG=" true" ;;
26
22
f) FILE_PATH=" ${OPTARG} " ;;
23
+ t) BUILD_FLAGS=" ${OPTARG} " ;;
24
+ v) VERBOSE_FLAG=" true" ;;
27
25
* ) error " Unexpected option ${flag} " ;;
28
26
esac
29
27
done
@@ -39,30 +37,32 @@ main () {
39
37
# Make sure we initiate clean up before exiting.
40
38
trap clean_up EXIT;
41
39
42
- # Create a new coverage-all.out file, that will contain the contents of every individual
40
+ # Create a new coverage-all.txt file, that will contain the contents of every individual
43
41
# pkg's `coverage.out`.
44
- echo " mode: count" > coverage-all.out;
45
- TEMP_TEST_FILE_PATHS_ARR+=(" ${PWD} /coverage-all.out" );
42
+ echo " mode: count" > coverage-all.txt;
46
43
TEMP_TEST_FILE_PATHS_ARR+=(" ${PWD} /coverage.out" );
47
44
48
45
# Generate coverage numbers for all go files in the given FILE_PATH.
49
46
generate_test_coverage;
50
47
48
+ # Print the coverage report.
51
49
test_coverage_report=$( generate_test_coverage_report) ;
52
50
print_coverage_report " $test_coverage_report " ;
53
51
54
52
# Generate test report, and parse the total coverage number.
55
53
current_test_coverage_number=$( generate_test_coverage_number " $test_coverage_report " ) ;
56
- print_handler " \n\e [32mTest Coverage Percentage: $current_test_coverage_number %.\e [0m \n" ;
54
+ print_handler " \n\033 [32mTest Coverage Percentage: $current_test_coverage_number %.\033 [0m \n" ;
57
55
58
56
# Check if the current test coverage number is above the threshold.
59
57
check_for_minimum_test_coverage_percentage " $current_test_coverage_number " ;
60
58
61
- exit_status_color=" \e[32m" ;
59
+ # Assign a correlating color to the exit_status.
60
+ exit_status_color=" \033[32m" ;
62
61
if [[ $EXIT_STATUS -eq 1 ]]; then
63
- exit_status_color=" \e [31m" ;
62
+ exit_status_color=" \033 [31m" ;
64
63
fi
65
- echo -e $exit_status_color " Exiting with status code $EXIT_STATUS \e[0m\n" ;
64
+
65
+ echo -e $exit_status_color " Exiting with status code $EXIT_STATUS \033[0m\n" ;
66
66
exit $EXIT_STATUS ;
67
67
}
68
68
@@ -72,41 +72,41 @@ check_for_minimum_test_coverage_percentage () {
72
72
MIN_COVERAGE_PERCENTAGE_NUM=$( echo ${MIN_COVERAGE_PERCENTAGE_NUM%% .* } ) ;
73
73
74
74
if ! [[ " $current_test_coverage_number " -ge " $MIN_COVERAGE_PERCENTAGE_NUM " ]]; then
75
- print_handler " \e [31mERROR: The current test coverage of $current_test_coverage_number %
75
+ print_handler " \033 [31mERROR: The current test coverage of $current_test_coverage_number %
76
76
is not above the threshold of $MIN_COVERAGE_PERCENTAGE_NUM %.
77
- Please review your changes and add test coverage for those changes.\e [0m \n" ;
77
+ Please review your changes and add test coverage for those changes.\033 [0m \n" ;
78
78
EXIT_STATUS=1;
79
79
fi
80
80
}
81
81
82
- # TODO Refactor this func.
83
82
generate_test_coverage () {
84
- echo -e " \e [33m" ;
83
+ echo -e ' \033 [33m' ;
85
84
# For each go file found.
86
- for pkg in $( find . -name * .go -print0 | xargs -0 -n1 dirname | sort --unique ) ; do
85
+ for pkg in $( find . -name " *.go" -print0 | xargs -0 -n1 dirname | sort --unique ) ; do
87
86
# If the pkg filepath contains 'vendor' we do not want to test it.
88
- if ! [[ $pkg == * " vendor" * ]]; then
87
+ if [[ ${ pkg} != * " vendor" * && ${pkg} != * " mock " * ]]; then
89
88
# Run the go cover and output results to 'coverage.out'.
90
- OUTPUT=$( environator integration_test go test -v -cover -coverprofile=coverage.out -covermode=count $pkg ) ;
89
+ OUTPUT=$( go test -v --tags " $BUILD_FLAGS " -cover -coverprofile=coverage.out -covermode=count $pkg ) ;
91
90
if [[ $OUTPUT == * " [no test files]" * ]]; then
91
+ print_handler " no tests founds for " $pkg
92
92
BASE_FOLDER_NAME=$( basename $pkg ) ;
93
93
MOCK_FILE_PATH=" ${PWD} /${pkg} /${BASE_FOLDER_NAME} _test.go" ;
94
94
TEMP_TEST_FILE_PATHS_ARR+=($MOCK_FILE_PATH )
95
95
touch $MOCK_FILE_PATH ;
96
96
echo " package ${BASE_FOLDER_NAME} " > $MOCK_FILE_PATH ;
97
- k=$( environator integration_test go test -v -cover -coverprofile=coverage.out -covermode=count $pkg ) ;
97
+ k=$( go test -v --tags " $BUILD_FLAGS " -cover -coverprofile=coverage.out -covermode=count $pkg ) ;
98
98
fi
99
99
100
- # Take the results of coverage.out and append them to coverage-all.out .
101
- tail -n +2 coverage.out >> coverage-all.out ;
100
+ # Take the results of coverage.out and append them to coverage-all.txt .
101
+ tail -n +2 coverage.out >> coverage-all.txt ;
102
102
fi
103
103
done
104
- echo -e " \e [0m" ;
104
+ echo -e " \033 [0m" ;
105
105
}
106
106
107
107
generate_test_coverage_report () {
108
108
local golang_test_coverage_report;
109
- golang_test_coverage_report=$( environator integration_test go tool cover -func=" ${PWD} /coverage-all.out " ) ;
109
+ golang_test_coverage_report=$( go tool cover -func=" ${PWD} /coverage-all.txt " ) ;
110
110
echo " $golang_test_coverage_report " ;
111
111
}
112
112
@@ -123,8 +123,11 @@ parse_test_coverage_number() {
123
123
local report_string_array;
124
124
report_string_array=($tail_of_report_string );
125
125
126
+ local end_of_array;
127
+ end_of_array=${# report_string_array[@]}
128
+
126
129
local total_test_coverage_percentage;
127
- total_test_coverage_percentage=${report_string_array[- 1]} ;
130
+ total_test_coverage_percentage=${report_string_array[end_of_array - 1]} ;
128
131
129
132
local total_test_coverage_number;
130
133
total_test_coverage_number=$( echo $total_test_coverage_percentage | sed ' s/%//' ) ;
@@ -136,7 +139,7 @@ clean_up () {
136
139
print_handler " Deleteing Mock Test Files" ;
137
140
for i in " ${TEMP_TEST_FILE_PATHS_ARR[@]} "
138
141
do
139
- print_handler " \e [31mDeleteing $i \e [0m" ;
142
+ print_handler " \033 [31mDeleteing $i \033 [0m" ;
140
143
rm $i ;
141
144
done
142
145
print_handler " " ;
@@ -153,13 +156,13 @@ print_coverage_report () {
153
156
for i in " ${report_arr[@]} "
154
157
do
155
158
if [[ $(( count % 3 )) == 0 ]]; then
156
- print_handler " \e [33m$temp \e [0m" ;
159
+ print_handler " \033 [33m$temp \033 [0m" ;
157
160
temp=" " ;
158
161
fi
159
162
temp+=$i " " ;
160
163
count=$(( count + 1 )) ;
161
164
done
162
- print_handler " \e [33m$temp \e [0m" ;
165
+ print_handler " \033 [33m$temp \033 [0m" ;
163
166
}
164
167
165
168
print_handler () {
0 commit comments