Skip to content

Commit 1f64ba4

Browse files
authored
Add a 'preflight check' stage to CI (grpc#923)
Motivation: Often contributors miss out license checks, have formatting issues or haven't generated linux tests. Let's bring these checks up front and make sure they're done before we run any tests. Modifications: - Add a "Preflight Checks" stage to CI - Add a sanity script which currently just checks license headers; we can add other checks later. Note that in CI this is called via .travis-script.sh as it sets up the environment (setting up PATH etc.t) - Add building and caching SwiftFormat to the install step, it's not used yet but will be later. - Fix a bunch of shellcheck issues - Replaces some travis environment variables with flags Result: - We have a way to check non-code related things before checking functional changes
1 parent da54baa commit 1f64ba4

File tree

5 files changed

+183
-68
lines changed

5 files changed

+183
-68
lines changed

.travis-install.sh

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ PROTOBUF_VERSION=3.9.1
3838
BAZEL_VERSION=0.28.1
3939
GRPC_VERSION=1.23.0
4040

41-
GREEN='\033[0;32m'
42-
BLUE='\033[0;34m'
43-
NO_COLOR='\033[0m'
44-
4541
info() {
46-
printf "${BLUE}$1${NO_COLOR}\n"
42+
printf '\033[0;34m%s\033[0m\n' "$1"
4743
}
4844

4945
success() {
50-
printf "${GREEN}$1${NO_COLOR}\n"
46+
printf '\033[0;32m%s\033[0m\n' "$1"
5147
}
5248

5349
# Install the protoc compiler.
@@ -182,24 +178,74 @@ build_grpc_cpp_server() {
182178
echo -en 'travis_fold:end:install.grpc_cpp_server\\r'
183179
}
184180

185-
main() {
186-
cd
187-
mkdir -p local "$BIN_CACHE"
188-
mkdir -p local "$ZIP_CACHE"
181+
function install_swiftformat() {
182+
echo -en 'travis_fold:start:install.swiftformat\\r'
183+
info "Installing swiftformat"
189184

190-
install_protoc
191-
install_swift
192185

193-
if [ "$RUN_INTEROP_TESTS" = "true" ]; then
194-
install_bazel
195-
build_grpc_cpp_server
186+
if [ ! -f "$BIN_CACHE/swiftformat" ]; then
187+
git clone --depth 1 "https://github.com/nicklockwood/SwiftFormat"
188+
189+
info "Building swiftformat"
190+
cd SwiftFormat
191+
swift build --product swiftformat
192+
193+
cp "$(swift build --show-bin-path)/swiftformat" "$BIN_CACHE/swiftformat"
194+
else
195+
info "Skipping download and build of SwiftFormat, using cached binaries"
196196
fi
197197

198-
# Verify installation
199-
info "Contents of $HOME/local:"
200-
find local
201-
success "Install script completed"
198+
# We should have cached swiftformat now, copy it to $HOME/local/bin
199+
cp "$BIN_CACHE/swiftformat" "$HOME"/local/bin/swiftformat
200+
201+
success "Installed swiftformat"
202+
echo -en 'travis_fold:end:install.swiftformat\\r'
202203
}
203204

204-
# Run the installation.
205-
main
205+
cd
206+
mkdir -p local/bin "$BIN_CACHE" "$ZIP_CACHE"
207+
208+
should_install_protoc=false
209+
should_install_interop_server=false
210+
should_install_swiftformat=false
211+
212+
while getopts "pif" optname; do
213+
case $optname in
214+
p)
215+
should_install_protoc=true
216+
;;
217+
i)
218+
should_install_interop_server=true
219+
;;
220+
f)
221+
should_install_swiftformat=true
222+
;;
223+
\?)
224+
echo "Uknown option $optname"
225+
exit 2
226+
;;
227+
esac
228+
done
229+
230+
# Always install Swift.
231+
install_swift
232+
233+
if $should_install_protoc; then
234+
install_protoc
235+
fi
236+
237+
if $should_install_interop_server; then
238+
install_bazel
239+
build_grpc_cpp_server
240+
fi
241+
242+
if $should_install_swiftformat; then
243+
# If we're building SwiftFormat we need to know where Swift lives.
244+
export PATH=$HOME/local/bin:$PATH
245+
install_swiftformat
246+
fi
247+
248+
# Verify installation
249+
info "Contents of $HOME/local:"
250+
find "$HOME"/local
251+
success "Install script completed"

.travis-script.sh

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717
# See: Makefile
1818
BUILD_OUTPUT=./.build/debug
1919

20-
GREEN='\033[0;32m'
21-
BLUE='\033[0;34m'
22-
NO_COLOR='\033[0m'
23-
2420
info() {
25-
printf "${BLUE}$1${NO_COLOR}\n"
21+
printf '\033[0;34m%s\033[0m\n' "$1"
2622
}
2723

2824
success() {
29-
printf "${GREEN}$1${NO_COLOR}\n"
25+
printf '\033[0;32m%s\033[0m\n' "$1"
3026
}
3127

3228
setup_environment() {
@@ -45,14 +41,15 @@ make_all() {
4541
}
4642

4743
make_test() {
44+
local tsan=$1
4845
echo -en 'travis_fold:start:make.test\\r'
4946

50-
if [[ ${NO_TSAN} == "true" ]]; then
51-
info "Running Swift tests"
52-
make test
53-
else
47+
if $tsan; then
5448
info "Running Swift tests with TSAN"
5549
make test-tsan
50+
else
51+
info "Running Swift tests"
52+
make test
5653
fi
5754

5855
success "Swift tests passed"
@@ -168,21 +165,39 @@ run_interop_reconnect_test() {
168165
echo -en 'travis_fold:end:test.interop_reconnect\\r'
169166
}
170167

171-
main() {
172-
setup_environment
173-
174-
# If we're running interop tests don't bother with the other stuff.
175-
if [ "$RUN_INTEROP_TESTS" = "true" ]; then
176-
run_interop_tests
177-
run_interop_reconnect_test
178-
else
179-
make_all
180-
make_check_linuxmain
181-
make_test
182-
make_test_plugin
183-
make_project
184-
fi
185-
}
186-
187-
# Run the thing!
188-
main
168+
just_sanity=false
169+
just_interop_tests=false
170+
tsan=false
171+
172+
while getopts "sit" optname; do
173+
case $optname in
174+
s)
175+
just_sanity=true
176+
;;
177+
i)
178+
just_interop_tests=true
179+
;;
180+
t)
181+
tsan=true
182+
;;
183+
\?)
184+
echo "Uknown option $optname"
185+
exit 2
186+
;;
187+
esac
188+
done
189+
190+
setup_environment
191+
192+
if $just_sanity; then
193+
./scripts/sanity.sh
194+
elif $just_interop_tests; then
195+
run_interop_tests
196+
run_interop_reconnect_test
197+
else
198+
make_all
199+
make_check_linuxmain
200+
make_test $tsan
201+
make_test_plugin
202+
make_project
203+
fi

.travis.yml

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,32 @@
1818
language: minimal
1919
jobs:
2020
include:
21+
# Sanity checks for PRs.
22+
- &sanity
23+
stage: "Preflight Checks"
24+
name: "Sanity (license headers and formatting)"
25+
os: linux
26+
dist: bionic
27+
install: ./.travis-install.sh -f # install swiftformat
28+
script: ./.travis-script.sh -s # just sanity
29+
env: SWIFT_VERSION=5.2
2130
# Tests for each PR.
2231
- &tests
2332
stage: "Test"
2433
name: "Unit Tests: Ubuntu 18.04 (Swift 5.2)"
2534
os: linux
2635
dist: bionic
27-
script: ./.travis-script.sh
28-
env: RUN_INTEROP_TESTS=false SWIFT_VERSION=5.2
36+
install: ./.travis-install.sh -p # install protoc
37+
script: ./.travis-script.sh -t # with tsan
38+
env: SWIFT_VERSION=5.2
2939
- <<: *tests
3040
name: "Unit Tests: Ubuntu 18.04 (Swift 5.1)"
31-
env: RUN_INTEROP_TESTS=false SWIFT_VERSION=5.1.5 NO_TSAN=true
41+
script: ./.travis-script.sh
42+
env: SWIFT_VERSION=5.1.5
3243
- <<: *tests
3344
name: "Unit Tests: Ubuntu 18.04 (Swift 5.0)"
34-
env: RUN_INTEROP_TESTS=false SWIFT_VERSION=5.0.3 NO_TSAN=true
45+
script: ./.travis-script.sh
46+
env: SWIFT_VERSION=5.0.3
3547
- <<: *tests
3648
name: "Unit Tests: Xcode 11.5"
3749
os: osx
@@ -47,14 +59,15 @@ jobs:
4759
name: "Interoperability Tests: Ubuntu 18.04 (Swift 5.2)"
4860
os: linux
4961
dist: bionic
50-
script: ./.travis-script.sh
51-
env: RUN_INTEROP_TESTS=true SWIFT_VERSION=5.2
62+
install: ./.travis-install.sh -p -i # install protoc and interop server
63+
script: ./.travis-script.sh -i # interop tests
64+
env: SWIFT_VERSION=5.2
5265
- <<: *interop_tests
5366
name: "Interoperability Tests: Ubuntu 18.04 (Swift 5.1)"
54-
env: RUN_INTEROP_TESTS=true SWIFT_VERSION=5.1.5
67+
env: SWIFT_VERSION=5.1.5
5568
- <<: *interop_tests
5669
name: "Interoperability Tests: Ubuntu 18.04 (Swift 5.0)"
57-
env: RUN_INTEROP_TESTS=true SWIFT_VERSION=5.0.3
70+
env: SWIFT_VERSION=5.0.3
5871
- <<: *interop_tests
5972
name: "Interoperability Tests: Xcode 11.5"
6073
os: osx
@@ -68,6 +81,7 @@ jobs:
6881
- <<: *development
6982

7083
stages:
84+
- name: "Preflight Checks"
7185
# Always run this stage.
7286
- name: "Test"
7387
# Only run when pushing (or merging) to main
@@ -91,7 +105,3 @@ addons:
91105
- g++
92106
- zlib1g-dev
93107
- python3
94-
95-
before_install: ./scripts/license-check.sh
96-
install: ./.travis-install.sh
97-
script: ./.travis-script.sh

scripts/license-check.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,4 @@ check_copyright_headers() {
140140

141141
errors=0
142142
check_copyright_headers
143-
144-
if [[ "$errors" == 0 ]]; then
145-
echo "License headers: OK"
146-
else
147-
echo "License headers: found $errors issue(s)."
148-
fi
149-
150143
exit $errors

scripts/sanity.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# Copyright 2020, gRPC Authors All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -eu
18+
19+
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
20+
21+
function run_logged() {
22+
local message=$1
23+
local command=$2
24+
25+
log=$(mktemp)
26+
27+
printf '==> %s ... ' "$message"
28+
29+
if $command > "$log" 2>&1; then
30+
printf "\033[0;32mOK\033[0m\n"
31+
else
32+
printf "\033[0;31mFAILED\033[0m\n"
33+
echo "=== Captured output:"
34+
cat "$log"
35+
echo "==="
36+
fi
37+
}
38+
39+
function check_license_headers() {
40+
run_logged "Checking license headers" "$HERE/license-check.sh"
41+
}
42+
43+
function check_formatting() {
44+
hash swiftformat 2> /dev/null || { printf "\033[0;31mERROR\033[0m swiftformat must be installed (see: https://github.com/nicklockwood/SwiftFormat)\n"; exit 1; }
45+
run_logged "Checking formatting" "swiftformat --lint $HERE/.."
46+
}
47+
48+
check_license_headers
49+
50+
# We won't run this just yet.
51+
# check_formatting

0 commit comments

Comments
 (0)