From bc2fe4ea0e3573677d34fdb3d2a454532f8e974c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Thu, 4 Jul 2024 16:08:23 +0200 Subject: [PATCH] Fix the unit tests --- codegen-library.sh | 5 +- go.work.sum | 80 +++++++++-- library.sh | 128 +++++++++++++----- presubmit-tests.sh | 8 +- release.sh | 6 +- schema/go.mod | 4 +- schema/go.sum | 8 +- test/go.mod | 22 +++- test/go.sum | 65 +++++---- test/hack/verify-codegen.sh | 3 +- test/unit/codegen_test.go | 93 +++++++++---- test/unit/e2e_helpers_test.go | 7 +- test/unit/library_test.go | 24 ++-- test/unit/presubmit_test.go | 23 ++-- test/unit/release_test.go | 4 +- test/unit/scripts/fake-dumps.bash | 6 +- test/unit/sharedlib_test.go | 212 +++++++++++++++++++++++++----- test/unit/update_deps_test.go | 1 - 18 files changed, 517 insertions(+), 182 deletions(-) diff --git a/codegen-library.sh b/codegen-library.sh index 7eb4cec3..bc37859f 100755 --- a/codegen-library.sh +++ b/codegen-library.sh @@ -50,7 +50,7 @@ function go-resolve-pkg-dir() { return 1 fi else - go mod download -x + go mod download -x > /dev/stderr go list -m -f '{{.Dir}}' "${pkg}" 2>/dev/null return $? fi @@ -148,7 +148,8 @@ function restore-changes-if-its-copyright-year-only() { # Restore the GOPATH and clean up the temporary directory function restore-gopath() { - if (( IS_PROW )); then + # Skip this if the directory is already checked out onto the GOPATH. + if __is_checkout_onto_gopath; then return fi if [ -n "$CODEGEN_TMP_GOPATH" ] && [ -d "$CODEGEN_TMP_GOPATH" ]; then diff --git a/go.work.sum b/go.work.sum index e02e1f30..f8a39b23 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,37 +1,101 @@ github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46 h1:lsxEuwrXEAokXB9qhlbKWPpo3KMLZQ5WB5WLQRW1uq0= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= +github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46aU4V9E= +github.com/alecthomas/chroma/v2 v2.14.0/go.mod h1:QolEbTfmUHIMVpBqxeDnNBj2uoeI4EbYP4i6n68SG4I= +github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA= +github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os= +github.com/alecthomas/mango-kong v0.1.0 h1:iFVfP1k1K4qpml3JUQmD5I8MCQYfIvsD9mRdrw7jJC4= +github.com/alecthomas/mango-kong v0.1.0/go.mod h1:t+TYVdsONUolf/BwVcm+15eqcdAj15h4Qe9MMFAwwT4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA= github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= +github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= +github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA= +github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc= +github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0= +github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw= +github.com/charmbracelet/bubbletea v0.26.3 h1:iXyGvI+FfOWqkB2V07m1DF3xxQijxjY2j8PqiXYqasg= +github.com/charmbracelet/bubbletea v0.26.3/go.mod h1:bpZHfDHTYJC5g+FBK+ptJRCQotRC+Dhh3AoMxa/2+3Q= +github.com/charmbracelet/glamour v0.7.0 h1:2BtKGZ4iVJCDfMF229EzbeR1QRKLWztO9dMtjmqZSng= +github.com/charmbracelet/glamour v0.7.0/go.mod h1:jUMh5MeihljJPQbJ/wf4ldw2+yBP59+ctV36jASy7ps= +github.com/charmbracelet/huh v0.4.2 h1:5wLkwrA58XDAfEZsJzNQlfJ+K8N9+wYwvR5FOM7jXFM= +github.com/charmbracelet/huh v0.4.2/go.mod h1:g9OXBgtY3zRV4ahnVih9bZE+1yGYN+y2C9Q6L2P+WM0= +github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= +github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM= +github.com/charmbracelet/x/exp/strings v0.0.0-20240525152034-77596eb8760e h1:DhvN6ye3nHLhRtNHtlrQ0Zk+vmeN7YtEnyIRfcl7e0E= +github.com/charmbracelet/x/exp/strings v0.0.0-20240525152034-77596eb8760e/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ= +github.com/charmbracelet/x/input v0.1.1 h1:YDOJaTUKCqtGnq9PHzx3pkkl4pXDOANUHmhH3DqMtM4= +github.com/charmbracelet/x/input v0.1.1/go.mod h1:jvdTVUnNWj/RD6hjC4FsoB0SeZCJ2ZBkiuFP9zXvZI0= +github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI= +github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw= +github.com/charmbracelet/x/windows v0.1.2 h1:Iumiwq2G+BRmgoayww/qfcvof7W/3uLoelhxojXlRWg= +github.com/charmbracelet/x/windows v0.1.2/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w= +github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= +github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84= github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8= +github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0= github.com/kisielk/errcheck v1.5.0 h1:e8esj/e4R+SAOwFwN+n3zr0nYeCyeweozKfO23MvHzY= github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg= github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58= +github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs= github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/mango v0.2.0 h1:iNNc0c5VLQ6fsMgAqGQofByNUBH2Q2nEbD6TaI+5yyQ= +github.com/muesli/mango v0.2.0/go.mod h1:5XFpbC8jY5UUv89YQciiXNlbi+iJgt29VDC5xbzrLL4= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8= +github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d h1:7PxY7LVfSZm7PEeBTyK1rj1gABdCO2mbri6GKO1cMDs= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA= +github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= -github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= -golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U= +github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= +github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GAsO5s= +github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4= +golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= diff --git a/library.sh b/library.sh index 9cd2adc7..f4ef09c6 100755 --- a/library.sh +++ b/library.sh @@ -141,54 +141,41 @@ function calcRetcode() { # Print error message. # Parameters: $* - error message to be displayed function error() { - gum_style \ - --foreground '#D00' \ + local first="$1" + shift + local args=("ERROR: $first" "$@") + gum_banner \ + --color '#D00' \ --padding '1 3' \ --border double \ - --border-foreground '#D00' \ - "ERROR: $*" + "${args[@]}" > /dev/stderr } # 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() { - error "$*" + error "$@" readonly abort_retcode="${abort_retcode:-$(calcRetcode "$*")}" exit "$abort_retcode" } -# Display a box banner. -# Parameters: $1 - character to use for the box. -# $2 - banner message. -# Deprecated: Use `gum_style` instead. -function make_banner() { - local msg="$1$1$1$1 $2 $1$1$1$1" - local border="${msg//[^$1]/$1}" - echo -e "${border}\n${msg}\n${border}" - # TODO(adrcunha): Remove once logs have timestamps on Prow - # For details, see https://github.com/kubernetes/test-infra/issues/10100 - if (( IS_PROW )); then - echo -e "$1$1$1$1 $(TZ='UTC' date --rfc-3339=ns)\n${border}" - fi -} - # Simple header for logging purposes. function header() { local upper upper="$(echo "$*" | tr '[:lower:]' '[:upper:]')" - gum_style \ - --padding '1 3' \ + gum_banner \ --border double \ + --color 44 \ + --padding '1 3' \ "$upper" } # Simple subheader for logging purposes. function subheader() { - gum_style \ - --padding '1 3' \ - --border rounded \ - "$*" + gum_banner \ + --color 45 \ + "$@" } # Simple log step for logging purposes. @@ -203,22 +190,87 @@ function log() { # Simple warning banner for logging purposes. function warning() { - gum_style \ - --foreground '#DD0' \ + local first="$1" + shift + local args=("WARN: $first" "$@") + gum_banner \ + --color '#DD0' \ --padding '1 3' \ --border rounded \ - --border-foreground '#DD0' \ - "WARN: $*" + "${args[@]}" > /dev/stderr +} + +# Display a box banner. +# Parameters: $1 - character to use for the box. +# $2 - banner message. +# Deprecated: Use `gum_banner` instead. +function make_banner() { + local msg="$1$1$1$1 $2 $1$1$1$1" + local border="${msg//[^$1]/$1}" + echo -e "${border}\n${msg}\n${border}" + # TODO(adrcunha): Remove once logs have timestamps on Prow + # For details, see https://github.com/kubernetes/test-infra/issues/10100 + if (( IS_PROW )); then + echo -e "$1$1$1$1 $(TZ='UTC' date --rfc-3339=ns)\n${border}" + fi +} + +# Display a fancy box banner. +# Parameters: +# [--border ] - a gum border type for the box, defaults to 'rounded' +# [--color ] - a gum color for the box, defaults to '0'' +# [--padding ] - a gum padding for the box, defaults to '0 1' +# $* - banner message. +function gum_banner() { + local border='rounded' + local color='0' + local padding='0 1' + while [[ $# -gt 0 ]]; do + case "$1" in + --border) + border="$2" + shift 2 + ;; + --color) + color="$2" + shift 2 + ;; + --padding) + padding="$2" + shift 2 + ;; + *) + break + ;; + esac + done + local args=( + --align center + --border "$border" + --foreground "$color" + --border-foreground "$color" + --padding "$padding" + "$@" + ) + # TODO: Remove once logs have timestamps on Prow, details see: + # https://github.com/kubernetes/test-infra/issues/10100 + if (( IS_PROW )); then + local dt + # RFC3339Nano format with 3 digits of ns without timezone offset + dt="$(TZ='UTC' date --rfc-3339=ns | sed -E 's/\.([0-9]{3})[0-9]+.+$/.\1/')" + args+=('' "at $dt") + fi + gum_style "${args[@]}" } # Simple info banner for logging purposes. function gum_style() { - go_run github.com/charmbracelet/gum@v0.14.1 style "$@" > /dev/stderr + go_run github.com/charmbracelet/gum@v0.14.1 style "$@" } # Checks whether the given function exists. function function_exists() { - [[ "$(type -t $1)" == "function" ]] + [[ "$(type -t "$1")" == "function" ]] } # GitHub Actions aware output grouping. @@ -556,7 +608,7 @@ function report_go_test() { echo "Test log (ANSI) written to ${ansilog}" htmllog="${logfile/.jsonl/.html}" - go_run github.com/buildkite/terminal-to-html/v3/cmd/terminal-to-html@v3.11.0 \ + go_run github.com/buildkite/terminal-to-html/v3/cmd/terminal-to-html@v3.10.0 \ --preview < "$ansilog" > "$htmllog" echo "Test log (HTML) written to ${htmllog}" @@ -829,13 +881,17 @@ function go_mod_module_name() { go_run knative.dev/toolbox/modscope@latest current } +function __is_checkout_onto_gopath() { + ! [ "${REPO_ROOT_DIR##"$(go env GOPATH)"}" = "$REPO_ROOT_DIR" ] +} + # Return a GOPATH to a temp directory. Works around the out-of-GOPATH issues # for k8s client gen mixed with go mod. # Intended to be used like: # export GOPATH=$(go_mod_gopath_hack) function go_mod_gopath_hack() { # Skip this if the directory is already checked out onto the GOPATH. - if ! [ "${REPO_ROOT_DIR##"$(go env GOPATH)"}" = "$REPO_ROOT_DIR" ]; then + if __is_checkout_onto_gopath; then go env GOPATH return fi @@ -843,6 +899,10 @@ function go_mod_gopath_hack() { local TMP_GOPATH TMP_REPO_PATH TMP_GOPATH="$TMPDIR/go" TMP_REPO_PATH="${TMP_GOPATH}/src/$(go_mod_module_name)" + if [ -d "${TMP_REPO_PATH}" ]; then + echo "${TMP_GOPATH}" + return + fi mkdir -p "$(dirname "${TMP_REPO_PATH}")" ln -s "${REPO_ROOT_DIR}" "${TMP_REPO_PATH}" diff --git a/presubmit-tests.sh b/presubmit-tests.sh index d210dc70..b96ffadf 100755 --- a/presubmit-tests.sh +++ b/presubmit-tests.sh @@ -72,9 +72,11 @@ function initialize_environment() { # Parameters: $1 - test group name (e.g., build) # $2 - result (0=passed, 1=failed) function results_banner() { - local result - [[ $2 -eq 0 ]] && result="PASSED" || result="FAILED" - header "$1 tests ${result}" + if [[ $2 -eq 0 ]]; then + header "$1 tests PASSED" + else + error "$1 tests FAILED" + fi } # Run build tests. If there's no `build_tests` function, run the default diff --git a/release.sh b/release.sh index cbc20f69..7c5c2ba5 100755 --- a/release.sh +++ b/release.sh @@ -17,7 +17,7 @@ # This is a helper script for Knative release scripts. # See README.md for instructions on how to use it. -source $(dirname "${BASH_SOURCE[0]}")/library.sh +source "$(dirname "${BASH_SOURCE[0]}")/library.sh" # Organization name in GitHub; defaults to Knative. readonly ORG_NAME="${ORG_NAME:-knative}" @@ -37,9 +37,9 @@ readonly RELEASE_SIGNING_IDENTITY="signer@knative-releases.iam.gserviceaccount.c readonly GEO_REPLICATION=(us eu asia) # Simple banner for logging purposes. -# Parameters: $1 - message to display. +# Parameters: $* - message to display. function banner() { - make_banner "@" "$1" + subheader "$*" } # Tag images in the yaml files if $TAG is not empty. diff --git a/schema/go.mod b/schema/go.mod index e17437ce..470dbb4f 100644 --- a/schema/go.mod +++ b/schema/go.mod @@ -18,8 +18,8 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/net v0.19.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/text v0.15.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/schema/go.sum b/schema/go.sum index 4f7cf835..b8d45702 100644 --- a/schema/go.sum +++ b/schema/go.sum @@ -55,8 +55,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -65,8 +65,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= diff --git a/test/go.mod b/test/go.mod index 774e6e9f..3b08ad99 100644 --- a/test/go.mod +++ b/test/go.mod @@ -3,43 +3,51 @@ module knative.dev/hack/test go 1.21 require ( + github.com/abiosoft/lineprefix v0.1.4 + github.com/charmbracelet/gum v0.14.1 + github.com/fatih/color v1.12.0 github.com/stretchr/testify v1.8.4 github.com/thanhpk/randstr v1.0.4 golang.org/x/mod v0.14.0 - k8s.io/apimachinery v0.29.2 k8s.io/code-generator v0.29.2 + knative.dev/hack v0.0.0-20240704013904-b9799599afcf ) require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/lipgloss v0.11.0 // indirect + github.com/charmbracelet/x/ansi v0.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/go-logr/logr v1.3.0 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect - github.com/gogo/protobuf v1.3.2 // indirect + github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/gnostic-models v0.6.8 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.8 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/muesli/termenv v0.15.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/net v0.19.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/sys v0.20.0 // indirect golang.org/x/tools v0.16.1 // indirect google.golang.org/protobuf v1.31.0 // indirect - gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 // indirect k8s.io/klog/v2 v2.110.1 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect - k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect - sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) diff --git a/test/go.sum b/test/go.sum index 91338326..c2683ab2 100644 --- a/test/go.sum +++ b/test/go.sum @@ -1,9 +1,21 @@ +github.com/abiosoft/lineprefix v0.1.4 h1:fXu3jc+B2EaS98mTpEL5OH9EKv3scHRb7/gsvlqAD1A= +github.com/abiosoft/lineprefix v0.1.4/go.mod h1:Myq9hfXs8e2OmHFvajp3pHxxThZL645XK+BrEQNvNSs= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/gum v0.14.1 h1:GRONh4WFXM9l28fgj9HbWqQimLzzV/vUxJfyQZEhbdk= +github.com/charmbracelet/gum v0.14.1/go.mod h1:BMym0NN64JZvlEFe1uDY/Gj9UWgidwJOG4TB5gfyp+A= +github.com/charmbracelet/lipgloss v0.11.0 h1:UoAcbQ6Qml8hDwSWs0Y1cB5TEQuZkDPH/ZqwWWYTG4g= +github.com/charmbracelet/lipgloss v0.11.0/go.mod h1:1UdRTH9gYgpcdNN5oBtjbu/IzNKtzVtb7sqN1t9LNn8= +github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk= +github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g= github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc= +github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -15,8 +27,6 @@ github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/ github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= -github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= -github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= @@ -37,8 +47,6 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -47,19 +55,33 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4= github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o= github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg= github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -68,6 +90,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= @@ -76,43 +99,35 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl github.com/thanhpk/randstr v1.0.4 h1:IN78qu/bR+My+gHCvMEXhR/i5oriVHcTB/BJJIRTsNo= github.com/thanhpk/randstr v1.0.4/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= @@ -121,16 +136,12 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= -gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8= -k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU= k8s.io/code-generator v0.29.2 h1:c9/iw2KnNpw2IRV+wwuG/Wns2TjPSgjWzbbjTevyiHI= k8s.io/code-generator v0.29.2/go.mod h1:FwFi3C9jCrmbPjekhaCYcYG1n07CYiW1+PAPCockaos= k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 h1:pWEwq4Asjm4vjW7vcsmijwBhOr1/shsbSYiWXmNGlks= @@ -140,8 +151,8 @@ k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0= k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo= k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/AuzbMm96cd3YHRTU83I780= k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA= -k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI= -k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +knative.dev/hack v0.0.0-20240704013904-b9799599afcf h1:n92FmZRywgtHso7pFAku7CW0qvRAs1hXtMQqO0R6eiE= +knative.dev/hack v0.0.0-20240704013904-b9799599afcf/go.mod h1:yk2OjGDsbEnQjfxdm0/HJKS2WqTLEFg/N6nUs6Rqx3Q= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4= diff --git a/test/hack/verify-codegen.sh b/test/hack/verify-codegen.sh index 72b6ee76..c4fd75c4 100755 --- a/test/hack/verify-codegen.sh +++ b/test/hack/verify-codegen.sh @@ -25,7 +25,8 @@ 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." + abort "Codegen is out of date!" "" \ + "Please, run test/hack/update-codegen.sh, and commit (or stage) the changes." fi header "Codegen is up to date" diff --git a/test/unit/codegen_test.go b/test/unit/codegen_test.go index 46f112c6..541288c4 100644 --- a/test/unit/codegen_test.go +++ b/test/unit/codegen_test.go @@ -1,7 +1,10 @@ package unit_test import ( + "fmt" + "go/build" "os" + "path" "strings" "testing" @@ -10,49 +13,93 @@ import ( func TestCodegen(t *testing.T) { t.Parallel() - tmpdir := t.TempDir() - execPermission := os.FileMode(0o755) - require.NoError(t, os.MkdirAll(tmpdir+"/go/bin", execPermission)) - require.NoError(t, os.WriteFile( - tmpdir+"/go/bin/deepcopy-gen", - []byte(strings.Join([]string{"#!/bin/bash", - `git restore test/e2e/apis/hack/v1alpha1/zz_generated.deepcopy.go`, - `echo "Deepcopy generation complete"`, - "exit 248"}, "\n")), - execPermission)) + wantRetcode := 248 sc := newShellScript( - envs(map[string]string{"TMPDIR": tmpdir}), + mockDeepcopyGen(t, func(gen *deepcopyGen) { + gen.retcode = wantRetcode + }), + fakeProwJob(), loadFile("source-codegen-library.bash"), mockGo(), ) tcs := []testCase{{ name: "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 " + "hack:v1alpha1", - retcode: retcode(248), + retcode: retcode(wantRetcode), stdout: equal(`WARNING: generate-groups.sh is deprecated. WARNING: Please use k8s.io/code-generator/kube_codegen.sh instead. WARNING: generate-internal-groups.sh is deprecated. WARNING: Please use k8s.io/code-generator/kube_codegen.sh instead. -go install k8s.io/code-generator/cmd/applyconfiguration-gen k8s.io/code-generator/cmd/client-gen k8s.io/code-generator/cmd/conversion-gen k8s.io/code-generator/cmd/deepcopy-gen k8s.io/code-generator/cmd/defaulter-gen k8s.io/code-generator/cmd/informer-gen k8s.io/code-generator/cmd/lister-gen k8s.io/code-generator/cmd/openapi-gen +šŸ‘» go install k8s.io/code-generator/cmd/applyconfiguration-gen k8s.io/code-generator/cmd/client-gen k8s.io/code-generator/cmd/conversion-gen k8s.io/code-generator/cmd/deepcopy-gen k8s.io/code-generator/cmd/defaulter-gen k8s.io/code-generator/cmd/informer-gen k8s.io/code-generator/cmd/lister-gen k8s.io/code-generator/cmd/openapi-gen Generating deepcopy funcs Deepcopy generation complete +--- Cleaning up generated code `, ), - stderr: equal(strings.TrimLeft(` -ā•­ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•® -ā”‚ ā”‚ -ā”‚ WARN: Failed to determine the knative.dev/pkg package ā”‚ -ā”‚ ā”‚ -ā•°ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā•Æ ---- Cleaning up generated code -`, "\n")), + stderr: warned( + "Failed to determine the knative.dev/pkg package", + func(o *headerOpts) { + o.equals = true + }), }} for i := range tcs { tc := tcs[i] t.Run(tc.name, tc.test(sc)) } } + +type deepcopyGen struct { + retcode int +} + +type deepcopyGenOpt func(*deepcopyGen) + +func mockDeepcopyGen(t TestingT, opts ...deepcopyGenOpt) scriptlet { + d := deepcopyGen{} + for _, opt := range opts { + opt(&d) + } + execPermission := os.FileMode(0o755) + var slet scriptlet = instructions() + gobin := path.Join(currentGopath(), "bin") + deepcopyGenPath := path.Join(gobin, "deepcopy-gen") + if !isCheckoutOntoGopath() { + tmpdir := t.TempDir() + slet = envs(map[string]string{"TMPDIR": tmpdir}) + gobin = path.Join(tmpdir, "go", "bin") + deepcopyGenPath = path.Join(gobin, "deepcopy-gen") + require.NoError(t, os.MkdirAll(gobin, execPermission)) + } else { + t.Cleanup(func() { + require.NoError(t, os.Remove(deepcopyGenPath)) + }) + } + require.NoError(t, os.WriteFile( + deepcopyGenPath, + // restore zz_generated files, if they are deleted + []byte(strings.Join([]string{"#!/bin/bash", + `set -Eeuo pipefail`, + `git diff --numstat | grep -E '0\s+[0-9]{2,}.+zz_generated.+\.go' ` + + `| awk '{print $3}' | xargs -r git restore`, + `echo "Deepcopy generation complete"`, + fmt.Sprint("exit ", d.retcode)}, "\n")), + execPermission)) + return slet +} + +// isCheckoutOntoGopath checks if the current directory is under GOPATH +func isCheckoutOntoGopath() bool { + rootDir := path.Dir(path.Dir(currentDir())) + return strings.HasPrefix(rootDir, currentGopath()) +} + +func currentGopath() string { + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = build.Default.GOPATH + } + return gopath +} diff --git a/test/unit/e2e_helpers_test.go b/test/unit/e2e_helpers_test.go index e6fa1146..563b7d90 100644 --- a/test/unit/e2e_helpers_test.go +++ b/test/unit/e2e_helpers_test.go @@ -16,18 +16,19 @@ func TestE2eHelpers(t *testing.T) { ) tcs := []testCase{{ name: `initialize --smoke-test-custom-flag`, - stdout: []check{contains("Custom flag detected")}, + stdout: []check{subheader("Custom flag detected")}, }, { name: `fail_test`, commands: []string{ `initialize --run-tests`, `fail_test`, }, - stderr: aborted("test failed"), retcode: retcode(111), stdout: []check{ - contains(">> DUMPING THE CLUSTER STATE"), + contains("Cluster is gke_deadbeef_1.24"), + subheader("Dumping the cluster state"), }, + stderr: aborted("test failed"), }} for _, tc := range tcs { tc := tc diff --git a/test/unit/library_test.go b/test/unit/library_test.go index c207bfae..2c956247 100644 --- a/test/unit/library_test.go +++ b/test/unit/library_test.go @@ -25,14 +25,16 @@ func TestHelperFunctions(t *testing.T) { stdout: lines("Knative Hack"), }, { name: `get_canonical_path test/unit/library_test.go`, - stdout: []check{func(t assert.TestingT, output string) { - assert.Contains(t, output, "hack/test/unit/library_test.go") - pth := strings.Trim(output, "\n") - fi, err := os.Stat(pth) - assert.NoError(t, err) - assert.False(t, fi.IsDir()) - assert.True(t, path.IsAbs(pth)) - }}, + stdout: []check{ + contains("hack/test/unit/library_test.go"), + func(t TestingT, output string, otype outputType) bool { + pth := strings.Trim(output, "\n") + fi, err := os.Stat(pth) + assert.NoError(t, err) + assert.False(t, fi.IsDir()) + assert.True(t, path.IsAbs(pth)) + return !t.Failed() + }}, }, { name: `capitalize "foo bar"`, stdout: lines("Foo Bar"), @@ -41,11 +43,11 @@ func TestHelperFunctions(t *testing.T) { stdout: lines( ">>> Knative Hack controller logs:", ">>> Pod: acme", - "kubectl -n test-infra logs acme --all-containers", + "šŸ‘» kubectl -n test-infra logs acme --all-containers", ">>> Pod: example", - "kubectl -n test-infra logs example --all-containers", + "šŸ‘» kubectl -n test-infra logs example --all-containers", ">>> Pod: knative", - "kubectl -n test-infra logs knative --all-containers", + "šŸ‘» kubectl -n test-infra logs knative --all-containers", ), }, { name: `is_protected_gcr "gcr.io/knative-releases"`, diff --git a/test/unit/presubmit_test.go b/test/unit/presubmit_test.go index a87c9c46..af5904fc 100644 --- a/test/unit/presubmit_test.go +++ b/test/unit/presubmit_test.go @@ -8,18 +8,18 @@ import ( "github.com/thanhpk/randstr" ) -func TestMainFunc(t *testing.T) { - t.Parallel() +func TestPresubmitTestMainFunc(t *testing.T) { sc := newShellScript( + mockDeepcopyGen(t), fakeProwJob(), loadFile("source-presubmit-tests.bash"), mockGo(), mockKubectl(), ) tcs := []testCase{{ - name: `main --build-tests`, + name: "main --build-tests", stdout: []check{ - contains("RUNNING BUILD TESTS"), + header("RUNNING BUILD TESTS"), contains("Build tests for knative.dev/hack/test"), contains("Build tests for knative.dev/hack/schema"), contains("Build tests for knative.dev/hack"), @@ -28,24 +28,23 @@ func TestMainFunc(t *testing.T) { contains("go test -vet=off -tags -exec echo ./..."), contains("go run knative.dev/test-infra/tools/kntest/cmd/kntest@latest" + " junit --suite=_build_tests --name=Check_Licenses --err-msg= --dest="), - contains("BUILD TESTS PASSED"), + header("BUILD TESTS PASSED"), }, }, { - name: `main --unit-tests`, + name: "main --unit-tests", stdout: []check{ - contains("RUNNING UNIT TESTS"), + header("RUNNING UNIT TESTS"), contains("Unit tests for knative.dev/hack/test"), contains("Unit tests for knative.dev/hack/schema"), contains("Unit tests for knative.dev/hack"), contains("Running go test with args: -short -race -count 1 ./..."), contains("go run gotest.tools/gotestsum@v1.11.0 --format testname --junitfile"), contains("-- -short -race -count 1 ./..."), - contains("UNIT TESTS PASSED"), + header("UNIT TESTS PASSED"), }, - }, - } - for _, tc := range tcs { - tc := tc + }} + for i := range tcs { + tc := tcs[i] t.Run(tc.name, tc.test(sc)) } } diff --git a/test/unit/release_test.go b/test/unit/release_test.go index 82889d65..07c152ad 100644 --- a/test/unit/release_test.go +++ b/test/unit/release_test.go @@ -44,8 +44,8 @@ func TestBuildFromSource(t *testing.T) { }, stdout: append(outChecks, contains(checksumsContent)), }} - for _, tc := range tcs { - tc := tc + for i := range tcs { + tc := tcs[i] t.Run(tc.name, func(t *testing.T) { tmp := t.TempDir() sc := testReleaseShellScript( diff --git a/test/unit/scripts/fake-dumps.bash b/test/unit/scripts/fake-dumps.bash index 33a0d487..2df56cc4 100644 --- a/test/unit/scripts/fake-dumps.bash +++ b/test/unit/scripts/fake-dumps.bash @@ -15,10 +15,10 @@ # limitations under the License. function dump_metrics() { - header ">> Starting kube proxy" - header ">> Grabbing k8s metrics" + subheader "Starting kube proxy" + subheader "Grabbing k8s metrics" } function dump_cluster_state() { - header ">> Dumping the cluster state" + subheader "Dumping the cluster state" } diff --git a/test/unit/sharedlib_test.go b/test/unit/sharedlib_test.go index b314863a..0edd36bf 100644 --- a/test/unit/sharedlib_test.go +++ b/test/unit/sharedlib_test.go @@ -3,6 +3,7 @@ package unit_test import ( "bytes" "embed" + "errors" "fmt" "io" "os" @@ -12,7 +13,11 @@ import ( "runtime" "strings" "testing" + "time" + "github.com/abiosoft/lineprefix" + "github.com/charmbracelet/gum/style" + "github.com/fatih/color" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/thanhpk/randstr" @@ -25,27 +30,124 @@ var ( toMuchNlRx = regexp.MustCompile("(?m)\n{3,}") ) -func aborted(msg string) []check { +func TestMain(m *testing.M) { + ensureGoModDownloaded() + os.Exit(m.Run()) +} + +// ensureGoModDownloaded will download the go modules before running the tests +// to avoid the download and compilation messages, which may influence the +// output assertions. +func ensureGoModDownloaded() { + fmt.Println("Pre-fetching go modules, to avoid download messages during tests...") + cmd := exec.Command("go", "mod", "download", "-x") + cmd.Dir = path.Dir(currentDir()) + cmd.Stdout = lineprefix.New( + lineprefix.Writer(os.Stdout), + lineprefix.Color(color.New(color.FgCyan)), + lineprefix.Prefix("STDOUT |"), + ) + cmd.Stderr = lineprefix.New( + lineprefix.Writer(os.Stderr), + lineprefix.Color(color.New(color.FgRed)), + lineprefix.Prefix("STDERR |"), + ) + err := cmd.Run() + if err != nil { + panic(err) + } +} + +type headerOpts struct { + equals bool +} + +func (o headerOpts) check(s string) check { + if o.equals { + return equal(s)[0] + } + return contains(s) +} + +type headerOpt func(*headerOpts) + +func compileHeaderOpts(opts []headerOpt) headerOpts { + o := headerOpts{} + for _, opt := range opts { + opt(&o) + } + return o +} + +func aborted(msg string, opts ...headerOpt) []check { + o := compileHeaderOpts(opts) fmsg := fmt.Sprintf("ERROR: %s", msg) - return equal(makeBanner('*', fmsg)) + styles := libglossDefaults(style.StylesNotHidden{ + Border: "double", + Align: "center", + Padding: "1 3", + Foreground: "#D00", + BorderForeground: "#D00", + }) + return []check{o.check(makeBanner(styles, fmsg))} } -func warned(msg string) []check { +func warned(msg string, opts ...headerOpt) []check { + o := compileHeaderOpts(opts) fmsg := fmt.Sprintf("WARN: %s", msg) - return equal(makeBanner('!', fmsg)) + styles := libglossDefaults(style.StylesNotHidden{ + Border: "rounded", + Align: "center", + Padding: "1 3", + Foreground: "#DD0", + BorderForeground: "#DD0", + }) + + return []check{o.check(makeBanner(styles, fmsg))} +} + +func header(msg string, opts ...headerOpt) check { + o := compileHeaderOpts(opts) + styles := libglossDefaults(style.StylesNotHidden{ + Border: "double", + Align: "center", + Padding: "1 3", + Foreground: "45", + BorderForeground: "45", + }) + return o.check(makeBanner(styles, msg)) +} + +func subheader(msg string, opts ...headerOpt) check { + o := compileHeaderOpts(opts) + styles := libglossDefaults(style.StylesNotHidden{ + Border: "rounded", + Align: "center", + Padding: "0 1", + Foreground: "44", + BorderForeground: "44", + }) + return o.check(makeBanner(styles, msg)) +} + +func libglossDefaults(styles style.StylesNotHidden) style.StylesNotHidden { + if styles.Align == "" { + styles.Align = "left" + } + if styles.Border == "" { + styles.Border = "none" + } + if styles.Margin == "" { + styles.Margin = "0 0" + } + if styles.Padding == "" { + styles.Padding = "0 0" + } + return styles } -func makeBanner(ch rune, msg string) string { - const span = 4 - border := strings.Repeat(string(ch), len(msg)+span*2+2) - side := strings.Repeat(string(ch), span) - return strings.Join([]string{ - border, - side + " " + msg + " " + side, - border, - side + " 2018-07-18 23:00:00", - border, - }, "\n") + "\n" +func makeBanner(styles style.StylesNotHidden, msg string) string { + return styles.ToLipgloss().Render(msg+"\n\nat 2018-07-18 23:00:00") + "\n" } func empty() []check { @@ -57,18 +159,37 @@ func lines(strs ...string) []check { } func contains(str string) check { - return func(t assert.TestingT, output string) { - assert.Contains(t, output, str) + return func(t TestingT, output string, otype outputType) bool { + assert.Contains(t, output, str, + "The %s does not contain:\n%v", + otype, str) + return strings.Contains(output, str) } } func equal(str string) []check { - return []check{func(t assert.TestingT, output string) { - assert.Equal(t, str, output) + return []check{func(t TestingT, output string, otype outputType) bool { + assert.Equal(t, str, output, + "The %s wasn't equal to:\n%v", + otype, str) + return str == output }} } -type check func(t assert.TestingT, output string) +func dumpOutput(output string, otype outputType) string { + label := strings.ToUpper(string(otype)) + return fmt.Sprintf("\nā”€ā”€ā”€ā”€ā”€ BEGIN %s ā”€ā”€ā”€ā”€ā”€\n%vā”€ā”€ā”€ā”€ā”€ā”€ END %s ā”€ā”€ā”€ā”€ā”€ā”€\n", + label, output, label) +} + +type outputType string + +const ( + outputTypeStdout outputType = "stdout" + outputTypeStderr outputType = "stderr" +) + +type check func(t TestingT, output string, otype outputType) bool type testCase struct { name string @@ -90,17 +211,26 @@ func (tc testCase) test(sc shellScript) func(t *testing.T) { t.Parallel() code, out, err, src := sc.run(t, tc.testCommands()) tc.validRetcode(t, code) - for _, chck := range coalesce(tc.stdout, equal("")) { - chck(t, out) - } - for _, chck := range coalesce(tc.stderr, equal("")) { - chck(t, err) + checkStream := func(output string, otype outputType, checks []check) { + success := true + for _, chck := range checks { + success = success && chck(t, output, otype) + } + if !success { + t.Logf("Printing %s because of failed check:%s", otype, + dumpOutput(output, otype)) + } } + checkStream(out, outputTypeStdout, coalesce(tc.stdout, empty())) + checkStream(err, outputTypeStderr, coalesce(tc.stderr, empty())) + if t.Failed() { - t.Logf("Retcode: %v", code) - t.Logf("Stdout: \n---\n%v---\n", out) - t.Logf("Stderr: \n---\n%v---\n", err) - t.Logf("Shell script source: \n---\n%v---\n", src) + failedScriptPath := path.Join(os.TempDir(), + t.Name(), + time.Now().Format("20060102-150405")+".bash") + require.NoError(t, os.MkdirAll(path.Dir(failedScriptPath), 0o755)) + require.NoError(t, os.WriteFile(failedScriptPath, []byte(src), 0o755)) + t.Logf("The script that failed: %s", failedScriptPath) } } } @@ -122,14 +252,15 @@ func (tc testCase) testCommands() []string { } func (tc testCase) validRetcode(t TestingT, gotRetcode int) { + label := "Retcode mismatch" if tc.retcode != nil { - assert.Equal(t, int(*tc.retcode), gotRetcode) + assert.Equal(t, int(*tc.retcode), gotRetcode, label) return } if len(tc.stderr) > 0 { - assert.NotEqual(t, 0, gotRetcode) + assert.NotEqual(t, 0, gotRetcode, label) } else { - assert.Equal(t, 0, gotRetcode) + assert.Equal(t, 0, gotRetcode, label) } } @@ -145,9 +276,9 @@ func (f fnScriptlet) scriptlet(t TestingT) string { func newShellScript(scriptlets ...scriptlet) shellScript { return shellScript{ - append(scriptlets, mockBinary("date", response{ + append([]scriptlet{mockBinary("date", response{ anyArgs{}, simply("2018-07-18 23:00:00"), - })), + })}, scriptlets...), } } @@ -225,7 +356,9 @@ func mockBinary(name string, responses ...response) scriptlet { "fi") } code = append(code, - fmt.Sprintf(`echo "%s $*"`, name), + // The ghost icon is used to differentiate the mocked command output + // from the real one. + fmt.Sprintf(`echo "šŸ‘» %s $*"`, name), "EOF", fmt.Sprintf(`chmod +x "${TMPPATH}/%s"`, name), ) @@ -263,9 +396,11 @@ func (a anyArgs) String() string { func mockGo(responses ...response) scriptlet { lstags := "knative.dev/toolbox/go-ls-tags@latest" modscope := "knative.dev/toolbox/modscope@latest" + gum := "github.com/charmbracelet/gum@v0.14.1" callOriginals := []args{ startsWith{"run " + lstags}, startsWith{"run " + modscope}, + startsWith{"run " + gum}, startsWith{"run ./"}, startsWith{"list"}, startsWith{"env"}, @@ -280,6 +415,7 @@ func mockGo(responses ...response) scriptlet { prefetchers: []prefetcher{ goRunHelpPrefetcher(lstags), goRunHelpPrefetcher(modscope), + goRunHelpPrefetcher(gum), }, } } @@ -321,6 +457,9 @@ func union(scriptlets ...scriptlet) scriptlet { } type TestingT interface { + Cleanup(func()) + Parallel() + Failed() bool TempDir() string Logf(format string, args ...interface{}) require.TestingT @@ -341,7 +480,8 @@ func (s shellScript) run(t TestingT, commands []string) (int, string, string, st c.Dir = rootDir err := c.Run() if err != nil { - if _, ok := err.(*exec.ExitError); !ok { + var exitError *exec.ExitError + if !errors.As(err, &exitError) { require.NoError(t, err) } } diff --git a/test/unit/update_deps_test.go b/test/unit/update_deps_test.go index 45057376..26bdb290 100644 --- a/test/unit/update_deps_test.go +++ b/test/unit/update_deps_test.go @@ -25,7 +25,6 @@ func TestUpdateDeps(t *testing.T) { contains("Checking licenses"), contains("Removing unwanted vendor files"), contains("go mod tidy"), - contains("go mod vendor"), contains("go run github.com/google/go-licenses@v1.6.0 check"), }, }, {