Skip to content

Commit

Permalink
Merge pull request #175 from WillAbides/deadcode
Browse files Browse the repository at this point in the history
check for dead code
  • Loading branch information
WillAbides authored Aug 7, 2023
2 parents b5d7d09 + 6a73552 commit e26c090
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 0 additions & 8 deletions cmd/bindown/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion cmd/bindown/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 0 additions & 10 deletions internal/bindown/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bindown
import (
"context"
_ "embed"
"encoding/json"
"fmt"

"github.com/santhosh-tekuri/jsonschema/v5"
Expand All @@ -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)
}
11 changes: 11 additions & 0 deletions internal/bindown/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
27 changes: 27 additions & 0 deletions script/deadcode
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion script/fmt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

CDPATH="" cd -- "$(dirname -- "$(dirname -- "$0")")"
CDPATH="" cd -- "$(dirname -- "$0")/.."

script/bindown -q install gofumpt shfmt

Expand Down

0 comments on commit e26c090

Please sign in to comment.