Skip to content

Commit 97e8bbb

Browse files
authored
api breakage detection (#163)
motivation: test for api breakage on PR changes: * add script to detect api breakage * amend docker setup to include jq
1 parent 8418932 commit 97e8bbb

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ENV LANGUAGE en_US.UTF-8
1313

1414
# dependencies
1515
RUN apt-get update && apt-get install -y wget
16-
RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools # used by integration tests
16+
RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools curl jq # used by integration tests
1717

1818
# ruby and jazzy for docs generation
1919
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev

scripts/check_no_api_breakages.sh

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftNIO open source project
5+
##
6+
## Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
15+
16+
set -eu
17+
18+
# repodir
19+
function all_modules() {
20+
local repodir="$1"
21+
(
22+
set -eu
23+
cd "$repodir"
24+
swift package dump-package | jq '.products |
25+
map(select(.type | has("library") )) |
26+
map(.name) | .[]' | tr -d '"'
27+
)
28+
}
29+
30+
# repodir tag output
31+
function build_and_do() {
32+
local repodir=$1
33+
local tag=$2
34+
local output=$3
35+
36+
(
37+
cd "$repodir"
38+
git checkout -q "$tag"
39+
swift build
40+
while read -r module; do
41+
swift api-digester -sdk "$sdk" -dump-sdk -module "$module" \
42+
-o "$output/$module.json" -I "$repodir/.build/debug"
43+
done < <(all_modules "$repodir")
44+
)
45+
}
46+
47+
function usage() {
48+
echo >&2 "Usage: $0 REPO-GITHUB-URL NEW-VERSION OLD-VERSIONS..."
49+
echo >&2
50+
echo >&2 "This script requires a Swift 5.1+ toolchain."
51+
echo >&2
52+
echo >&2 "Examples:"
53+
echo >&2
54+
echo >&2 "Check between master and tag 2.1.1 of swift-nio:"
55+
echo >&2 " $0 https://github.com/apple/swift-nio master 2.1.1"
56+
echo >&2
57+
echo >&2 "Check between HEAD and commit 64cf63d7 using the provided toolchain:"
58+
echo >&2 " xcrun --toolchain org.swift.5120190702a $0 ../some-local-repo HEAD 64cf63d7"
59+
}
60+
61+
if [[ $# -lt 3 ]]; then
62+
usage
63+
exit 1
64+
fi
65+
66+
sdk=/
67+
if [[ "$(uname -s)" == Darwin ]]; then
68+
sdk=$(xcrun --show-sdk-path)
69+
fi
70+
71+
hash jq 2> /dev/null || { echo >&2 "ERROR: jq must be installed"; exit 1; }
72+
tmpdir=$(mktemp -d /tmp/.check-api_XXXXXX)
73+
repo_url=$1
74+
new_tag=$2
75+
shift 2
76+
77+
repodir="$tmpdir/repo"
78+
git clone "$repo_url" "$repodir"
79+
git -C "$repodir" fetch -q origin '+refs/pull/*:refs/remotes/origin/pr/*'
80+
errors=0
81+
82+
for old_tag in "$@"; do
83+
mkdir "$tmpdir/api-old"
84+
mkdir "$tmpdir/api-new"
85+
86+
echo "Checking public API breakages from $old_tag to $new_tag"
87+
88+
build_and_do "$repodir" "$new_tag" "$tmpdir/api-new/"
89+
build_and_do "$repodir" "$old_tag" "$tmpdir/api-old/"
90+
91+
for f in "$tmpdir/api-new"/*; do
92+
f=$(basename "$f")
93+
report="$tmpdir/$f.report"
94+
if [[ ! -f "$tmpdir/api-old/$f" ]]; then
95+
echo "NOTICE: NEW MODULE $f"
96+
continue
97+
fi
98+
99+
echo -n "Checking $f... "
100+
swift api-digester -sdk "$sdk" -diagnose-sdk \
101+
--input-paths "$tmpdir/api-old/$f" -input-paths "$tmpdir/api-new/$f" 2>&1 \
102+
> "$report" 2>&1
103+
104+
if ! shasum "$report" | grep -q cefc4ee5bb7bcdb7cb5a7747efa178dab3c794d5; then
105+
echo ERROR
106+
echo >&2 "=============================="
107+
echo >&2 "ERROR: public API change in $f"
108+
echo >&2 "=============================="
109+
cat >&2 "$report"
110+
errors=$(( errors + 1 ))
111+
else
112+
echo OK
113+
fi
114+
done
115+
rm -rf "$tmpdir/api-new" "$tmpdir/api-old"
116+
done
117+
118+
if [[ "$errors" == 0 ]]; then
119+
echo "OK, all seems good"
120+
fi
121+
echo done
122+
exit "$errors"

0 commit comments

Comments
 (0)