Skip to content

Commit

Permalink
Fix the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jul 4, 2024
1 parent 21bc8fc commit 6adc3c7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 34 deletions.
102 changes: 74 additions & 28 deletions library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ function calcRetcode() {
# Print error message.
# Parameters: $* - error message to be displayed
function error() {
gum_style \
--foreground '#D00' \
gum_banner \
--color '#D00' \
--padding '1 3' \
--border double \
--border-foreground '#D00' \
"ERROR: $*"
}

Expand All @@ -158,37 +157,22 @@ function abort() {
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.
Expand All @@ -203,22 +187,84 @@ function log() {

# Simple warning banner for logging purposes.
function warning() {
gum_style \
--foreground '#DD0' \
gum_banner \
--color '#DD0' \
--padding '1 3' \
--border rounded \
--border-foreground '#DD0' \
"WARN: $*"
}

# 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 <type>] - a gum border type for the box, defaults to 'rounded'
# [--color <color>] - a gum color for the box, defaults to '0''
# [--padding <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
}

# Checks whether the given function exists.
function function_exists() {
[[ "$(type -t $1)" == "function" ]]
[[ "$(type -t "$1")" == "function" ]]
}

# GitHub Actions aware output grouping.
Expand Down
8 changes: 5 additions & 3 deletions presubmit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/unit/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestCodegen(t *testing.T) {
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`,
`git restore test/e2e/apis/codegen/testdata/v1alpha1/zz_generated.deepcopy.go`,
`echo "Deepcopy generation complete"`,
"exit 248"}, "\n")),
execPermission))
Expand All @@ -27,8 +27,8 @@ func TestCodegen(t *testing.T) {
)
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/generated " +
"knative.dev/hack/test/codegen/testdata/apis " +
"hack:v1alpha1",
retcode: retcode(248),
stdout: equal(`WARNING: generate-groups.sh is deprecated.
Expand Down

0 comments on commit 6adc3c7

Please sign in to comment.