Skip to content

Commit

Permalink
Ensure the knative.dev/hack package doesn't have any deps
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Sep 25, 2023
1 parent b823ec5 commit c7d4a1a
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 77 deletions.
8 changes: 4 additions & 4 deletions cmd/script/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func TestMainFn(t *testing.T) {
var buf bytes.Buffer
var retcode *int
var retcode = -1_234_567_890 // nolint:gomnd // gate value
withOptions(
func() {
main.RunMain()
Expand All @@ -37,12 +37,12 @@ func TestMainFn(t *testing.T) {
ex.Stderr = &buf
ex.Args = []string{"--help"}
ex.Exit = func(c int) {
retcode = &c
retcode = c
}
},
)
assert.Nil(t, retcode)
assert.ContainsSubstring(t, buf.String(), "Script will extract Hack scripts")
assert.Equal(t, 0, retcode)
assert.ContainsSubstring(t, buf.String(), "Hacks as Go self-extracting binary")
}

func withOptions(fn func(), options ...cli.Option) {
Expand Down
20 changes: 0 additions & 20 deletions pkg/inflator/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ func ExecuteOrDie(opts ...Option) {
}
}

type usageErr struct{}

func (u usageErr) Error() string {
return `Hacks as Go self-extracting binary
Will extract Hack scripts to a temporary directory, and provide a source
file path to requested shell script.
# In Bash script
source "$(go run knative.dev/hack/cmd/script@latest library.sh)"
Usage:
script [flags] library.sh
Flags:
-h, --help help
-v, --verbose verbose output
`
}

func createOperation(fl *flags, argv []string) extract.Operation {
return extract.Operation{
ScriptName: argv[0],
Expand Down
25 changes: 25 additions & 0 deletions pkg/inflator/cli/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cli

type usageErr struct{}

func (u usageErr) Retcode() int {
return 0
}

func (u usageErr) Error() string {
return `Hacks as Go self-extracting binary
Will extract Hack scripts to a temporary directory, and provide a source
file path to requested shell script.
# In Bash script
source "$(go run knative.dev/hack/cmd/script@latest library.sh)"
Usage:
script [flags] library.sh
Flags:
-h, --help help
-v, --verbose verbose output
`
}
2 changes: 1 addition & 1 deletion pkg/inflator/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path"
"strings"

hack "knative.dev/hack"
"knative.dev/hack"
)

const (
Expand Down
7 changes: 7 additions & 0 deletions pkg/retcode/retcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ func Calc(err error) int {
if err == nil {
return 0
}
if r, ok := err.(retcodeErr); ok {
return r.Retcode()
}
upper := UpperBound - LowerBound
return int(crc32.ChecksumIEEE([]byte(err.Error())))%upper + LowerBound
}

type retcodeErr interface {
Retcode() int
}
52 changes: 0 additions & 52 deletions pkg/utest/assert/empty.go

This file was deleted.

1 change: 1 addition & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/stretchr/testify v1.8.1
github.com/thanhpk/randstr v1.0.4
golang.org/x/mod v0.12.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
32 changes: 32 additions & 0 deletions test/no_deps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sample_test

import (
"os"
"testing"

"golang.org/x/mod/modfile"
)

func TestNoDependenciesPresent(t *testing.T) {
var (
bytes []byte
err error
)
if bytes, err = os.ReadFile("../go.mod"); err != nil {
t.Fatal(err)
}

var mf *modfile.File
if mf, err = modfile.ParseLax("go.mod", bytes, nil); err != nil {
t.Fatal(err)
}

if len(mf.Require) > 0 {
deps := make([]string, 0, len(mf.Require))
for _, r := range mf.Require {
deps = append(deps, r.Mod.String())
}
t.Errorf("go.mod file should not have dependencies, but has %d: %+q",
len(mf.Require), deps)
}
}

0 comments on commit c7d4a1a

Please sign in to comment.