Skip to content

Commit

Permalink
Add Code Quality Analysis via golangci-lint tool, fix detected issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Nov 1, 2019
1 parent 71c2c61 commit 170b7c8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
issues:
exclude:
# Check this issue for more info: https://github.com/kyoh86/scopelint/issues/4
- Using the variable on range scope `tc` in function literal
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ jobs:
# Make the binary executable
- chmod +x $GOPATH/bin/dep
script: ./hack/ci/run-tests.sh
- stage: "Code Quality Analysis"
name: tests
script: ./hack/ci/run-tests.sh
46 changes: 46 additions & 0 deletions hack/ci/run-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# standard bash error handling
set -o nounset # treat unset variables as an error and exit immediately.
set -o errexit # exit immediately when a command fails.
set -E # needs to be set if we want the ERR trap

readonly CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
readonly ROOT_PATH=${CURRENT_DIR}/../..
readonly GOLANGCI_LINT_VERSION="v1.21.0"

source "${CURRENT_DIR}/utilities.sh" || { echo 'Cannot load CI utilities.'; exit 1; }

golangci::install() {
shout "Install the golangci-lint in version ${GOLANGCI_LINT_VERSION}"
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b "$GOPATH"/bin ${GOLANGCI_LINT_VERSION}

echo -e "${GREEN}√ install golangci-lint${NC}"
}

golangci::run_checks() {
shout "Run golangci-lint checks"
LINTS=(
# default golangci-lint lints
deadcode errcheck gosimple govet ineffassign staticcheck \
structcheck typecheck unused varcheck \
# additional lints
golint gofmt misspell gochecknoinits unparam scopelint gosec
)

ENABLE=$(sed 's/ /,/g' <<< "${LINTS[@]}")

golangci-lint --disable-all --enable="${ENABLE}" run ./pkg/... ./cmd/... .

echo -e "${GREEN}√ run golangci-lint${NC}"
}

main() {
if [[ "${RUN_ON_CI:-x}" == "true" ]]; then
golangci::install
fi

golangci::run_checks
}

main
3 changes: 0 additions & 3 deletions pkg/codeowners/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ func SetFS(newFs afero.Fs) func() {

return revert
}



2 changes: 1 addition & 1 deletion pkg/codeowners/owners_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/mszostok/codeowners-validator/pkg/codeowners"
)

func ExampleCodeownersEntries() {
func ExampleNewFromPath() {
pathToCodeownersFile := "./testdata/"

entries, err := codeowners.NewFromPath(pathToCodeownersFile)
Expand Down
10 changes: 7 additions & 3 deletions pkg/codeowners/owners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestParseCodeownersSuccess(t *testing.T) {
defer revert()

f, _ := tFS.Create(path.Join(givenCodeownerPath, "CODEOWNERS"))
f.WriteString(sampleCodeownerFile)
_, err := f.WriteString(sampleCodeownerFile)
require.NoError(t, err)

// when
entries, err := codeowners.NewFromPath(givenCodeownerPath)
Expand Down Expand Up @@ -82,12 +83,15 @@ func TestFindCodeownersFileSuccess(t *testing.T) {
revert := codeowners.SetFS(tFS)
defer revert()

tFS.Create(path.Join(tc.basePath, "CODEOWNERS"))
_, err := tFS.Create(path.Join(tc.basePath, "CODEOWNERS"))
require.NoError(t, err)

// when
_, err := codeowners.NewFromPath(tc.basePath)
entry, err := codeowners.NewFromPath(tc.basePath)

// then
require.NoError(t, err)
require.Empty(t, entry)
})
}
}
Expand Down

0 comments on commit 170b7c8

Please sign in to comment.