From 6472bce6ba8f0f5f18658e5e9e65c40948b01d79 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 7 Aug 2023 10:45:24 -0500 Subject: [PATCH 1/2] check for dead code --- .github/workflows/ci.yml | 1 + script/deadcode | 27 +++++++++++++++++++++++++++ script/fmt | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100755 script/deadcode diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a559d4b..f1a4473 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,6 +53,7 @@ jobs: key: ${{ runner.os }}-go-lint-${{ hashFiles('**/go.sum', 'bindown.yml', 'script/*') }} restore-keys: ${{ runner.os }}-go-lint - run: script/lint + - run: script/deadcode generate: runs-on: ubuntu-22.04 steps: diff --git a/script/deadcode b/script/deadcode new file mode 100755 index 0000000..862586f --- /dev/null +++ b/script/deadcode @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e + +REPO_DIR="$(CDPATH="" cd -- "$(dirname -- "$0")/.." && pwd -P)" + +DEADCODE_REVISION="aac7fb67aecb" + +GOBIN="$REPO_DIR"/bin go install golang.org/x/tools/internal/cmd/deadcode@"$DEADCODE_REVISION" + +RESULT="$( + "$REPO_DIR"/bin/deadcode -line ./... | + grep -v "internal/expecttest." | + grep -v "internal/testutil." || true +)" + +TEST_RESULT="$( + "$REPO_DIR"/bin/deadcode -line -test ./... | + grep -v "internal/builddep.adhocRelease" || true +)" + +if [ -n "$RESULT" ] || [ -n "$TEST_RESULT" ]; then + echo "deadcode found unused code:" + echo "$RESULT" + echo "$TEST_RESULT" + exit 1 +fi diff --git a/script/fmt b/script/fmt index eaa0c9e..d452e12 100755 --- a/script/fmt +++ b/script/fmt @@ -2,7 +2,7 @@ set -e -CDPATH="" cd -- "$(dirname -- "$(dirname -- "$0")")" +CDPATH="" cd -- "$(dirname -- "$0")/.." script/bindown -q install gofumpt shfmt From 6a73552b54655b2aa7c049b75370fa8d2dba0cc8 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 7 Aug 2023 10:48:52 -0500 Subject: [PATCH 2/2] move test-only code to tests --- cmd/bindown/cli.go | 8 -------- cmd/bindown/testutil_test.go | 10 +++++++++- internal/bindown/jsonschema.go | 10 ---------- internal/bindown/jsonschema_test.go | 11 +++++++++++ 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cmd/bindown/cli.go b/cmd/bindown/cli.go index cf4ca72..660a3bb 100644 --- a/cmd/bindown/cli.go +++ b/cmd/bindown/cli.go @@ -113,14 +113,6 @@ type fileReader interface { Fd() uintptr } -type SimpleFileReader struct { - io.Reader -} - -func (s SimpleFileReader) Fd() uintptr { - return 0 -} - type runContext struct { parent context.Context stdin fileReader diff --git a/cmd/bindown/testutil_test.go b/cmd/bindown/testutil_test.go index e0119e8..d6052d6 100644 --- a/cmd/bindown/testutil_test.go +++ b/cmd/bindown/testutil_test.go @@ -20,6 +20,14 @@ import ( "gopkg.in/yaml.v3" ) +type simpleFileReader struct { + io.Reader +} + +func (s simpleFileReader) Fd() uintptr { + return 0 +} + type cmdRunner struct { t testing.TB configFile string @@ -60,7 +68,7 @@ func (c *cmdRunner) run(commandLine ...string) *runCmdResult { ctx, commandLine, &runOpts{ - stdin: SimpleFileReader{c.stdin}, + stdin: simpleFileReader{c.stdin}, stdout: SimpleFileWriter{&result.stdOut}, stderr: &result.stdErr, cmdName: "cmd", diff --git a/internal/bindown/jsonschema.go b/internal/bindown/jsonschema.go index 3e89de6..e8794c2 100644 --- a/internal/bindown/jsonschema.go +++ b/internal/bindown/jsonschema.go @@ -3,7 +3,6 @@ package bindown import ( "context" _ "embed" - "encoding/json" "fmt" "github.com/santhosh-tekuri/jsonschema/v5" @@ -30,12 +29,3 @@ func validateConfig(ctx context.Context, cfg []byte) error { } return nil } - -func yaml2json(y []byte) ([]byte, error) { - var data any - err := yaml.Unmarshal(y, &data) - if err != nil { - return nil, err - } - return json.Marshal(data) -} diff --git a/internal/bindown/jsonschema_test.go b/internal/bindown/jsonschema_test.go index 4103a82..13a8588 100644 --- a/internal/bindown/jsonschema_test.go +++ b/internal/bindown/jsonschema_test.go @@ -2,11 +2,13 @@ package bindown import ( "context" + "encoding/json" "os" "path/filepath" "testing" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) func TestValidateConfig(t *testing.T) { @@ -72,3 +74,12 @@ url_checksums: require.Error(t, err) }) } + +func yaml2json(y []byte) ([]byte, error) { + var data any + err := yaml.Unmarshal(y, &data) + if err != nil { + return nil, err + } + return json.Marshal(data) +}