Skip to content

Commit

Permalink
Adding verify-codegen script
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jul 4, 2024
1 parent efe1245 commit 6845b07
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 21 deletions.
19 changes: 10 additions & 9 deletions codegen-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ oldstate="$(set +o)"
set -Eeuo pipefail

export repodir kn_hack_dir kn_hack_library \
MODULE_NAME TMP_GOPATH GOPATH GOBIN \
MODULE_NAME CODEGEN_TMP_GOPATH CODEGEN_ORIGINAL_GOPATH GOPATH GOBIN \
CODEGEN_PKG KNATIVE_CODEGEN_PKG

kn_hack_dir="$(realpath "$(dirname "${BASH_SOURCE[0]:-$0}")")"
Expand Down Expand Up @@ -67,8 +67,10 @@ if ! KNATIVE_CODEGEN_PKG="${KNATIVE_CODEGEN_PKG:-"$(go-resolve-pkg-dir knative.d
fi

popd > /dev/null
TMP_GOPATH=$(go_mod_gopath_hack)
GOPATH="${TMP_GOPATH}"

CODEGEN_ORIGINAL_GOPATH="$(go env GOPATH)"
CODEGEN_TMP_GOPATH=$(go_mod_gopath_hack)
GOPATH="${CODEGEN_TMP_GOPATH}"
GOBIN="${GOPATH}/bin" # Set GOBIN explicitly as k8s-gen' are installed by go install.

if [[ -n "${CODEGEN_PKG}" ]] && ! [ -x "${CODEGEN_PKG}/generate-groups.sh" ]; then
Expand Down Expand Up @@ -146,13 +148,12 @@ function restore-changes-if-its-copyright-year-only() {

# Restore the GOPATH and clean up the temporary directory
function restore-gopath() {
if [ -n "$TMP_GOPATH" ] && [ -d "$TMP_GOPATH" ]; then
chmod -R u+w "${TMP_GOPATH}"
rm -rf "${TMP_GOPATH}"
unset TMP_GOPATH
if [ -n "$CODEGEN_TMP_GOPATH" ] && [ -d "$CODEGEN_TMP_GOPATH" ]; then
chmod -R u+w "${CODEGEN_TMP_GOPATH}"
rm -rf "${CODEGEN_TMP_GOPATH}"
unset CODEGEN_TMP_GOPATH
fi
unset GOBIN
unset GOPATH
unset CODEGEN_ORIGINAL_GOPATH GOPATH GOBIN
}

add_trap cleanup-codegen EXIT
Expand Down
21 changes: 14 additions & 7 deletions library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,22 @@ function calcRetcode() {
echo "$rc"
}

# Print error message.
# Parameters: $* - error message to be displayed
function error() {
gum_style \
--foreground '#D00' \
--padding '1 3' \
--border double \
--border-foreground '#D00' \
"ERROR: $*"
}

# Print error message and call exit(n) where n calculated from the error message.
# Parameters: $1..$n - error message to be displayed
# Globals: abort_retcode will change the default retcode to be returned
function abort() {
gum_style \
--foreground '#D00' \
--padding '1 3' \
--border double \
--border-foreground '#D00' \
"ERROR: $*"
error "$*"
readonly abort_retcode="${abort_retcode:-$(calcRetcode "$*")}"
exit "$abort_retcode"
}
Expand All @@ -169,7 +175,8 @@ function make_banner() {

# Simple header for logging purposes.
function header() {
local upper="$(echo "$*" | tr a-z A-Z)"
local upper
upper="$(echo "$*" | tr '[:lower:]' '[:upper:]')"
gum_style \
--padding '1 3' \
--border double \
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

"knative.dev/hack/test/e2e/apis/hack"
"knative.dev/hack/test/codegen/testdata/apis/hack"
)

// SchemeGroupVersion is group version used to register these objects
Expand Down
4 changes: 2 additions & 2 deletions test/hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ relative_rootdir="$(realpath -s --relative-to="$PWD" "$rootdir")"
source "$(go run "${relative_rootdir}/cmd/script" codegen-library.sh)"

generate-groups deepcopy \
knative.dev/hack/test/e2e/apis/hack/v1alpha1/generated \
knative.dev/hack/test/e2e/apis \
knative.dev/hack/test/codegen/testdata/apis/hack/v1alpha1 \
knative.dev/hack/test/codegen/testdata/apis \
hack:v1alpha1 \
"$@"
31 changes: 31 additions & 0 deletions test/hack/verify-codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# Copyright 2024 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -Eeuo pipefail

rootdir="$(realpath "$(dirname "${BASH_SOURCE[0]:-$0}")/../..")"
cd "${rootdir}"

# shellcheck disable=SC1090
source "$(go run ./cmd/script library.sh)"

./test/hack/update-codegen.sh

if ! git diff --exit-code; then
abort "codegen is out of date, please run test/hack/update-codegen.sh, and commit the changes."
fi

header "Codegen is up to date"
2 changes: 1 addition & 1 deletion test/presubmit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function post_build_tests() {
local failed=0
for script in *.sh; do
subheader "Checking integrity of ${script}"
bash -c "source ${script}" || { failed=1; echo "--- FAIL: ${script}"; }
bash -c "source ${script}" || { failed=1; error "${script}"; }
done
return ${failed}
}
Expand Down

0 comments on commit 6845b07

Please sign in to comment.