From 21bc8fc6185bc77ec0cb07db864fbf79f3cb7532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Thu, 4 Jul 2024 13:43:48 +0200 Subject: [PATCH] Adding verify-codegen script --- codegen-library.sh | 22 +++++++------ library.sh | 21 ++++++++----- pkg/inflator/extract/extract_test.go | 4 ++- .../testdata}/apis/hack/register.go | 0 .../testdata}/apis/hack/v1alpha1/doc.go | 0 .../apis/hack/v1alpha1/example_types.go | 0 .../testdata}/apis/hack/v1alpha1/register.go | 3 +- .../hack/v1alpha1/zz_generated.deepcopy.go | 0 test/hack/update-codegen.sh | 4 +-- test/hack/verify-codegen.sh | 31 +++++++++++++++++++ test/presubmit-tests.sh | 2 +- 11 files changed, 65 insertions(+), 22 deletions(-) rename test/{e2e => codegen/testdata}/apis/hack/register.go (100%) rename test/{e2e => codegen/testdata}/apis/hack/v1alpha1/doc.go (100%) rename test/{e2e => codegen/testdata}/apis/hack/v1alpha1/example_types.go (100%) rename test/{e2e => codegen/testdata}/apis/hack/v1alpha1/register.go (97%) rename test/{e2e => codegen/testdata}/apis/hack/v1alpha1/zz_generated.deepcopy.go (100%) create mode 100755 test/hack/verify-codegen.sh diff --git a/codegen-library.sh b/codegen-library.sh index 623c40b2..7eb4cec3 100755 --- a/codegen-library.sh +++ b/codegen-library.sh @@ -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}")")" @@ -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 @@ -146,13 +148,15 @@ 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 (( IS_PROW )); then + return + fi + 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 diff --git a/library.sh b/library.sh index 798abc27..9cd2adc7 100755 --- a/library.sh +++ b/library.sh @@ -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" } @@ -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 \ diff --git a/pkg/inflator/extract/extract_test.go b/pkg/inflator/extract/extract_test.go index f60b99a6..bc94ccd7 100644 --- a/pkg/inflator/extract/extract_test.go +++ b/pkg/inflator/extract/extract_test.go @@ -26,6 +26,7 @@ func TestExtract(t *testing.T) { assert.Equal(t, prtr.out.String(), tmpdir+"/library.sh\n") assert.Equal(t, `[hack] Extracting hack scripts to directory: /tmp/x +[hack] boilerplate.go.txt [hack] codegen-library.sh [hack] e2e-tests.sh [hack] infra-library.sh @@ -45,6 +46,7 @@ func TestExtract(t *testing.T) { assert.Equal(t, prtr.out.String(), tmpdir+"/library.sh\n") assert.Equal(t, `[hack] Extracting hack scripts to directory: /tmp/x +[hack] boilerplate.go.txt up-to-date [hack] codegen-library.sh up-to-date [hack] e2e-tests.sh up-to-date [hack] infra-library.sh up-to-date @@ -59,7 +61,7 @@ func TestExtract(t *testing.T) { func standarizeErrOut(errOut string, tmpdir string) string { errOut = strings.ReplaceAll(errOut, tmpdir, "/tmp/x") - re := regexp.MustCompile(`\s+\d+ KiB \++`) + re := regexp.MustCompile(`\s+\d+ (?:Ki)?B \+*`) errOut = re.ReplaceAllString(errOut, "") return errOut } diff --git a/test/e2e/apis/hack/register.go b/test/codegen/testdata/apis/hack/register.go similarity index 100% rename from test/e2e/apis/hack/register.go rename to test/codegen/testdata/apis/hack/register.go diff --git a/test/e2e/apis/hack/v1alpha1/doc.go b/test/codegen/testdata/apis/hack/v1alpha1/doc.go similarity index 100% rename from test/e2e/apis/hack/v1alpha1/doc.go rename to test/codegen/testdata/apis/hack/v1alpha1/doc.go diff --git a/test/e2e/apis/hack/v1alpha1/example_types.go b/test/codegen/testdata/apis/hack/v1alpha1/example_types.go similarity index 100% rename from test/e2e/apis/hack/v1alpha1/example_types.go rename to test/codegen/testdata/apis/hack/v1alpha1/example_types.go diff --git a/test/e2e/apis/hack/v1alpha1/register.go b/test/codegen/testdata/apis/hack/v1alpha1/register.go similarity index 97% rename from test/e2e/apis/hack/v1alpha1/register.go rename to test/codegen/testdata/apis/hack/v1alpha1/register.go index c66a84cd..04ec86a7 100644 --- a/test/e2e/apis/hack/v1alpha1/register.go +++ b/test/codegen/testdata/apis/hack/v1alpha1/register.go @@ -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 diff --git a/test/e2e/apis/hack/v1alpha1/zz_generated.deepcopy.go b/test/codegen/testdata/apis/hack/v1alpha1/zz_generated.deepcopy.go similarity index 100% rename from test/e2e/apis/hack/v1alpha1/zz_generated.deepcopy.go rename to test/codegen/testdata/apis/hack/v1alpha1/zz_generated.deepcopy.go diff --git a/test/hack/update-codegen.sh b/test/hack/update-codegen.sh index ffe61cf2..b25c7365 100755 --- a/test/hack/update-codegen.sh +++ b/test/hack/update-codegen.sh @@ -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 \ "$@" diff --git a/test/hack/verify-codegen.sh b/test/hack/verify-codegen.sh new file mode 100755 index 00000000..72b6ee76 --- /dev/null +++ b/test/hack/verify-codegen.sh @@ -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" diff --git a/test/presubmit-tests.sh b/test/presubmit-tests.sh index cf0b34a3..e539a148 100755 --- a/test/presubmit-tests.sh +++ b/test/presubmit-tests.sh @@ -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} }