Skip to content

Commit

Permalink
Use golangci-lint for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRichardson committed Feb 8, 2021
1 parent 7f51924 commit 7455a0b
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 193 deletions.
18 changes: 1 addition & 17 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
name: "Static Analysis"
on: [push, pull_request]
jobs:
golangci:
name: Go Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.35
args: -c .github/golangci-lint.config.yaml

lint:
name: Lint
runs-on: ubuntu-latest
Expand All @@ -29,12 +18,7 @@ jobs:
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
GO111MODULE=off go get -u github.com/client9/misspell/cmd/misspell
GO111MODULE=off go get -u github.com/tsenart/deadcode
GO111MODULE=off go get -u golang.org/x/lint/golint
GO111MODULE=off go get -u golang.org/x/tools/cmd/goimports
GO111MODULE=off go get -u github.com/mdempsky/unconvert
GO111MODULE=off go get -u github.com/gordonklaus/ineffassign
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.35.2
- name: Checkout
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion core/machinelock/machinelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (c *lock) writeLogEntry() {
MaxBackups: 5,
Compress: true,
}
defer writer.Close()
defer func() { _ = writer.Close() }()

if c.startMessage != "" {
_, err := fmt.Fprintln(writer, c.startMessage)
Expand Down
2 changes: 1 addition & 1 deletion core/watcher/multinotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewMultiNotifyWatcher(w ...NotifyWatcher) *MultiNotifyWatcher {
<-w.Changes()
go func(wCopy NotifyWatcher) {
defer wg.Done()
wCopy.Wait()
_ = wCopy.Wait()
}(w)
// Copy events from the watcher to the staging channel.
go copyEvents(staging, w.Changes(), &m.tomb)
Expand Down
2 changes: 1 addition & 1 deletion docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func doHttpGet(url string, timeout time.Duration) ([]byte, error) {
if err != nil {
return nil, errors.Trace(err)
}
defer response.Body.Close()
defer func() { _ = response.Body.Close() }()

if response.StatusCode != http.StatusOK {
return nil, errors.Errorf("invalid response code from registry: %v", response.StatusCode)
Expand Down
10 changes: 5 additions & 5 deletions downloader/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (dl *Download) run(req Request) {
logger.Infof("download complete (%q)", req.URL)
err = verifyDownload(filename, req)
if err != nil {
os.Remove(filename)
_ = os.Remove(filename)
filename = ""
}
}
Expand All @@ -119,17 +119,17 @@ func (dl *Download) download(req Request) (filename string, err error) {
return "", errors.Trace(err)
}
defer func() {
tempFile.Close()
_ = tempFile.Close()
if err != nil {
os.Remove(tempFile.Name())
_ = os.Remove(tempFile.Name())
}
}()

blobReader, err := dl.openBlob(req.URL)
if err != nil {
return "", errors.Trace(err)
}
defer blobReader.Close()
defer func() { _ = blobReader.Close() }()

reader := &abortableReader{blobReader, req.Abort}
_, err = io.Copy(tempFile, reader)
Expand Down Expand Up @@ -166,7 +166,7 @@ func verifyDownload(filename string, req Request) error {
if err != nil {
return errors.Annotate(err, "opening for verify")
}
defer file.Close()
defer func() { _ = file.Close() }()

if err := req.Verify(file); err != nil {
return errors.Trace(err)
Expand Down
2 changes: 1 addition & 1 deletion downloader/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewHTTPBlobOpener(hostnameVerification utils.SSLHostnameVerification) func(
}
if resp.StatusCode != http.StatusOK {
// resp.Body is always non-nil. (see https://golang.org/pkg/net/http/#Response)
resp.Body.Close()
_ = resp.Body.Close()
return nil, errors.Errorf("bad http response: %v", resp.Status)
}
return resp.Body, nil
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/bmizerany/pat v0.0.0-20160217103242-c068ca2f0aac
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/coreos/go-systemd/v22 v22.0.0-20200316104309-cb8b64719ae3
github.com/davecgh/go-spew v1.1.1
github.com/dnaeon/go-vcr v1.0.1 // indirect
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
Expand Down Expand Up @@ -121,7 +120,6 @@ require (
gopkg.in/httprequest.v1 v1.2.1
gopkg.in/ini.v1 v1.10.1
gopkg.in/juju/blobstore.v2 v2.0.0-20160125023703-51fa6e26128d
gopkg.in/juju/charm.v6 v6.0.0-20190729113111-40ffcf7d10e5
gopkg.in/juju/environschema.v1 v1.0.1-0.20201027142642-c89a4490670a
gopkg.in/juju/idmclient.v1 v1.0.0-20180320161856-203d20774ce8
gopkg.in/juju/names.v3 v3.0.0-20200331100531-2c9a102df211 // indirect
Expand Down
4 changes: 2 additions & 2 deletions proxy/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type FactoryMaker struct {
Register FactoryRegister
}

// Returns a raw config for a given proxy type that can be used to unmarshal
// against.
// Config returns a raw config for a given proxy type that can be used to
// unmarshal against.
func (f *FactoryMaker) Config() interface{} {
if f.RawConfig == nil {
f.RawConfig = f.Register.ConfigFn()
Expand Down
170 changes: 7 additions & 163 deletions tests/suites/static_analysis/lint_go.sh
Original file line number Diff line number Diff line change
@@ -1,103 +1,10 @@
run_go_vet() {
PACKAGES="${2}"
# shellcheck disable=SC2086
OUT=$(go vet -composites=false ${PACKAGES} 2>&1 || true)
if [ -n "${OUT}" ]; then
echo ""
echo "$(red 'Found some issues (go vet):')"
echo -e "\\n${OUT}" >&2
exit 1
fi
}

run_go_lint() {
PACKAGES="${2}"
# shellcheck disable=SC2086
OUT=$(golint -set_exit_status ${PACKAGES} 2>&1 || true)
if [ -n "${OUT}" ]; then
echo ""
echo "$(red 'Found some issues (go lint):')"
echo -e "\\n${OUT}" >&2
exit 1
fi
}

run_go_imports() {
FOLDERS="${2}"
OUT=$(echo "${FOLDERS}" | xargs -I % goimports -l % 2>&1 || true)
LIST=$(echo "${OUT}" | grep -v "mocks\/" | grep -v "mock_" | grep -v "_mock.go" | grep -v "_mock_test.go" | xargs grep -L "MACHINE GENERATED BY" || true)
GOFILES=$(echo "${LIST}" | xargs head -q -n1 | grep -v "Code generated by" | tr -d '\n')
if [ -n "${GOFILES}" ]; then
GOFILES=$(echo "${OUT}" | xargs -I% grep -L "Code generated by" % | sort -u || true)
echo ""
echo "$(red 'Found some issues (go imports):')"
for ITEM in ${GOFILES}; do
echo -e "\\ngoimports -w ${ITEM}" >&2
done
exit 1
fi
}

run_deadcode() {
FOLDERS="${2}"
# shellcheck disable=SC2086
OUT=$(deadcode ${FOLDERS} 2>&1 || true)
if [ -n "${OUT}" ]; then
echo ""
echo "$(red 'Found some issues (deadcode):')"
echo -e "\\n${OUT}" >&2
exit 1
fi
}

run_misspell() {
FILES="${2}"
# shellcheck disable=SC2086
OUT=$(misspell -i=gratuitious ${FILES} 2>/dev/null || true)
if [ -n "${OUT}" ]; then
echo ""
echo "$(red 'Found some issues (misspell):')"
echo -e "\\n${OUT}" >&2
exit 1
fi
}

run_unconvert() {
PACKAGES="${2}"
# shellcheck disable=SC2086
OUT=$(unconvert ${PACKAGES} 2>&1 || true)
if [ -n "${OUT}" ]; then
echo ""
echo "$(red 'Found some issues (unconvert):')"
echo -e "\\n${OUT}" >&2
exit 1
fi
}

run_ineffassign() {
FOLDERS="${2}"
# shellcheck disable=SC2086
OUT=$(ineffassign ${FOLDERS} | grep "github.com/juju/juju" | sed -E "s/^(.+src\\/github\\.com\\/juju\\/juju\\/)(.+)/\2/")
if [ -n "${OUT}" ]; then
echo ""
echo "$(red 'Found some issues (ineffassign):')"
echo -e "\\n${OUT}" >&2
exit 1
fi
}

run_go_fmt() {
FILES=${2}
OUT=$(echo "${FILES}" | xargs gofmt -l -s)
if [ -n "${OUT}" ]; then
OUT=$(echo "${OUT}" | sed "s/^/ /")
echo ""
echo "$(red 'Found some issues (gofmt):')"
for ITEM in ${OUT}; do
echo -e "\\ngofmt -s -w ${ITEM}" >&2
done
exit 1
run_go() {
VER=$(golangci-lint --version | tr -s ' ' | cut -d ' ' -f 4 | cut -d '.' -f 1,2)
if [ "${VER}" != "1.35" ]; then
(>&2 echo -e "\\nError: golangci-lint version does not match 1.35")
exit 1
fi
golangci-lint run -c .github/golangci-lint.config.yaml
}

run_go_tidy() {
Expand All @@ -121,70 +28,7 @@ test_static_analysis_go() {

cd .. || exit

FILES=$(find ./* -name '*.go' -not -name '.#*' -not -name '*_mock.go' | grep -v vendor/ | grep -v acceptancetests/)
FOLDERS=$(echo "${FILES}" | sed s/^\.//g | xargs dirname | awk -F "/" '{print $2}' | uniq | sort)
PACKAGES=$(go list ./... | grep -v github.com/juju/juju\$ | grep -v github.com/juju/juju/vendor/ | grep -v github.com/juju/juju/acceptancetests/)

## Functions starting by empty line
# turned off until we get approval of test suite
# run_linter "func vet"

## go vet, if it exists
if go help vet >/dev/null 2>&1; then
run_linter "run_go_vet" "${PACKAGES}"
else
echo "vet not found, vet static analysis disabled"
fi

# TODO(hpidcock): re-enable golint when all the errors are fixed.

## golint
#if which golint >/dev/null 2>&1; then
# run_linter "run_go_lint" "${PACKAGES}"
#else
# echo "golint not found, golint static analysis disabled"
#fi

## goimports
if which goimports >/dev/null 2>&1; then
run_linter "run_go_imports" "${FOLDERS}"
else
echo "goimports not found, goimports static analysis disabled"
fi

# TODO(hpidcock): re-enable deadcode when it supports tests/
# fix deadcode errors.

## deadcode
#if which deadcode >/dev/null 2>&1; then
# run_linter "run_deadcode" "${FOLDERS}"
#else
# echo "deadcode not found, deadcode static analysis disabled"
#fi

## misspell
if which misspell >/dev/null 2>&1; then
run_linter "run_misspell" "${FILES}"
else
echo "misspell not found, misspell static analysis disabled"
fi

## unconvert
if which unconvert >/dev/null 2>&1; then
run_linter "run_unconvert" "${PACKAGES}"
else
echo "unconvert not found, unconvert static analysis disabled"
fi

## ineffassign
if which ineffassign >/dev/null 2>&1; then
run_linter "run_ineffassign" "${FOLDERS}"
else
echo "ineffassign not found, ineffassign static analysis disabled"
fi

## go fmt
run_linter "run_go_fmt" "${FILES}"
run_linter "run_go"
run_linter "run_go_tidy"
)
}

0 comments on commit 7455a0b

Please sign in to comment.