Skip to content

Commit 398c8c2

Browse files
authored
Merge pull request #3 from Shikkic/feature/osx-release
Feature/osx release
2 parents edd8735 + 5d4e3dc commit 398c8c2

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# go-test-runner
2+
3+
Evaluate total Golag test coverage for a given filepath. Optionally returns an error if test coverage is below a passed in threshold.
4+
5+
(Written using Google's shell styleguide https://google.github.io/styleguide/shell.xml, for modifications to this script,
6+
please consult the styling guide).
7+
8+
Author Daniel Cadden, Github @Shikkic.

go-test-runner

+32-29
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@
88
# Author Daniel Cadden, Github @Shikkic.
99

1010
main () {
11-
#if [[ $(whoami) != "vagrant" ]]; then
12-
# exit 1;
13-
#fi
14-
1511
# Set globle variable defaults.
1612
EXIT_STATUS=0;
1713
MIN_COVERAGE_PERCENTAGE_NUM=0.0;
1814
VERBOSE_FLAG='false';
1915
FILE_PATH='.';
16+
BUILD_FLAGS='';
2017

2118
# Parse flags and assign to global variables.
22-
while getopts ':n:f:v' flag; do
19+
while getopts ':n:f:t:v' flag; do
2320
case "${flag}" in
2421
n) MIN_COVERAGE_PERCENTAGE_NUM="${OPTARG}" ;;
25-
v) VERBOSE_FLAG="true" ;;
2622
f) FILE_PATH="${OPTARG}" ;;
23+
t) BUILD_FLAGS="${OPTARG}" ;;
24+
v) VERBOSE_FLAG="true" ;;
2725
*) error "Unexpected option ${flag}" ;;
2826
esac
2927
done
@@ -39,30 +37,32 @@ main () {
3937
# Make sure we initiate clean up before exiting.
4038
trap clean_up EXIT;
4139

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
4341
# 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;
4643
TEMP_TEST_FILE_PATHS_ARR+=("${PWD}/coverage.out");
4744

4845
# Generate coverage numbers for all go files in the given FILE_PATH.
4946
generate_test_coverage;
5047

48+
# Print the coverage report.
5149
test_coverage_report=$(generate_test_coverage_report);
5250
print_coverage_report "$test_coverage_report";
5351

5452
# Generate test report, and parse the total coverage number.
5553
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";
5755

5856
# Check if the current test coverage number is above the threshold.
5957
check_for_minimum_test_coverage_percentage "$current_test_coverage_number";
6058

61-
exit_status_color="\e[32m";
59+
# Assign a correlating color to the exit_status.
60+
exit_status_color="\033[32m";
6261
if [[ $EXIT_STATUS -eq 1 ]]; then
63-
exit_status_color="\e[31m";
62+
exit_status_color="\033[31m";
6463
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";
6666
exit $EXIT_STATUS;
6767
}
6868

@@ -72,41 +72,41 @@ check_for_minimum_test_coverage_percentage () {
7272
MIN_COVERAGE_PERCENTAGE_NUM=$(echo ${MIN_COVERAGE_PERCENTAGE_NUM%%.*});
7373

7474
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%
7676
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";
7878
EXIT_STATUS=1;
7979
fi
8080
}
8181

82-
# TODO Refactor this func.
8382
generate_test_coverage () {
84-
echo -e "\e[33m";
83+
echo -e '\033[33m';
8584
# 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
8786
# 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
8988
# 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);
9190
if [[ $OUTPUT == *"[no test files]"* ]]; then
91+
print_handler "no tests founds for "$pkg
9292
BASE_FOLDER_NAME=$(basename $pkg);
9393
MOCK_FILE_PATH="${PWD}/${pkg}/${BASE_FOLDER_NAME}_test.go";
9494
TEMP_TEST_FILE_PATHS_ARR+=($MOCK_FILE_PATH)
9595
touch $MOCK_FILE_PATH;
9696
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);
9898
fi
9999

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;
102102
fi
103103
done
104-
echo -e "\e[0m";
104+
echo -e "\033[0m";
105105
}
106106

107107
generate_test_coverage_report () {
108108
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");
110110
echo "$golang_test_coverage_report";
111111
}
112112

@@ -123,8 +123,11 @@ parse_test_coverage_number() {
123123
local report_string_array;
124124
report_string_array=($tail_of_report_string);
125125

126+
local end_of_array;
127+
end_of_array=${#report_string_array[@]}
128+
126129
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]};
128131

129132
local total_test_coverage_number;
130133
total_test_coverage_number=$(echo $total_test_coverage_percentage | sed 's/%//');
@@ -136,7 +139,7 @@ clean_up () {
136139
print_handler "Deleteing Mock Test Files";
137140
for i in "${TEMP_TEST_FILE_PATHS_ARR[@]}"
138141
do
139-
print_handler "\e[31mDeleteing $i\e[0m";
142+
print_handler "\033[31mDeleteing $i\033[0m";
140143
rm $i;
141144
done
142145
print_handler "";
@@ -153,13 +156,13 @@ print_coverage_report () {
153156
for i in "${report_arr[@]}"
154157
do
155158
if [[ $((count % 3)) == 0 ]]; then
156-
print_handler "\e[33m$temp\e[0m";
159+
print_handler "\033[33m$temp\033[0m";
157160
temp="";
158161
fi
159162
temp+=$i" ";
160163
count=$(( count + 1 ));
161164
done
162-
print_handler "\e[33m$temp\e[0m";
165+
print_handler "\033[33m$temp\033[0m";
163166
}
164167

165168
print_handler () {

0 commit comments

Comments
 (0)