Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use golangci-lint & fix formatting #158

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
go-version: "1.20"
- name: test
run: |
make install-goimports
make lint
make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
/tools
108 changes: 108 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Using [email protected]
run:
timeout: 5m
tests: true
skip-dirs:
- vendor$
output:
print-linter-name: true
linters:
enable-all: true
fast: false
disable:
# Deprecated (see https://golangci-lint.run/usage/linters/)
- deadcode
- golint
- ifshort
- interfacer
- maligned
- nosnakecase
- scopelint
- structcheck
- varcheck
- exhaustivestruct

# Annoying style guides that are very subjective
- funlen
- nlreturn
- wsl
- cyclop
- varnamelen
- maintidx
- gocognit
- godot
- gocyclo
- nestif
- nilnil
- exhaustruct

# Requires too many changes
- testpackage

- thelper
- wrapcheck
- testifylint

# TODO
- usestdlibvars
- paralleltest
- perfsprint
- staticcheck
- gomnd
- nonamedreturns
- goerr113
- gochecknoglobals
- dupword
- depguard
- unparam
- revive
- stylecheck
- unused
- protogetter
- lll
- prealloc
- forcetypeassert
- gocritic
- forbidigo
- dupl
- errcheck
- gosec
- ineffassign
- wastedassign

linters-settings:
tagliatelle:
case:
rules:
json: snake
yaml: kebab
depguard:
rules:
main:
deny:
- pkg: "github.com/pkg/errors"
desc: "Please use go built-in error wrapping and handling via `fmt` and `errors` packages"
gci:
sections:
- standard
- default
- prefix(github.com/form3tech-oss/f1)
issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
# exclude long lines with URLs
- path: \.go
source: https://
linters:
- lll
# Don't wrap check RoundTrip
- path: \.go
source: RoundTrip
linters:
- wrapcheck
- path: _test\.go
linters:
# unwrapped errors are ok in tests
- wrapcheck

30 changes: 16 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
GO_FILES?=$$(find ./ -name '*.go' | grep -v /vendor | grep -v /template/ | grep -v /build/ | grep -v swagger-client)
GOLANGCI_VERSION := 1.56.2

test: goimportscheck
.PHONY: test
test:
go test ./... -v -race -failfast -p 1 -mod=readonly

require-travis-env:
ifndef TRAVIS
$(error TRAVIS is undefined)
endif
.PHONY: tools/golangci-lint
tools/golangci-lint:
@echo "==> Installing golangci-lint..."
@./scripts/install-golangci-lint.sh $(GOLANGCI_VERSION)

install-goimports:
@if [ -z "$$(command -v goimports)" ]; then \
go get golang.org/x/tools/cmd/goimports; \
fi
.PHONY: lint
lint: tools/golangci-lint
@echo "==> Running golangci-lint..."
@tools/golangci-lint run --timeout 600s

.PHONY: lint-fix
lint-fix: tools/golangci-lint
@echo "==> Running golangci-lint..."
@tools/golangci-lint run --timeout 600s --fix

goimports:
@goimports -w $(GO_FILES)

goimportscheck:
@sh -c "'$(CURDIR)/scripts/goimportscheck.sh'"
25 changes: 10 additions & 15 deletions internal/chart/chart_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import (
"os"
"time"

"github.com/pkg/errors"
"github.com/guptarohit/asciigraph"
"github.com/spf13/cobra"
"github.com/wcharczuk/go-chart/v2"

"github.com/form3tech-oss/f1/v2/internal/support/errorh"

"github.com/form3tech-oss/f1/v2/internal/trigger/api"

"github.com/wcharczuk/go-chart/v2"

"github.com/guptarohit/asciigraph"

"github.com/spf13/cobra"
)

func Cmd(builders []api.Builder) *cobra.Command {
Expand Down Expand Up @@ -46,24 +41,24 @@ func chartCmdExecute(t api.Builder) func(cmd *cobra.Command, args []string) erro

startString, err := cmd.Flags().GetString("chart-start")
if err != nil {
return errors.Wrap(err, "Invalid chart-start value")
return fmt.Errorf("getting flag: %w", err)
}
start, err := time.Parse(time.RFC3339, startString)
if err != nil {
return err
return fmt.Errorf("parsing start time: %w", err)
}
duration, err := cmd.Flags().GetDuration("chart-duration")
if err != nil {
return err
return fmt.Errorf("getting flag: %w", err)
}
filename, err := cmd.Flags().GetString("filename")
if err != nil {
return err
return fmt.Errorf("getting flag: %w", err)
}

trig, err := t.New(cmd.Flags())
if err != nil {
return err
return fmt.Errorf("creating builder: %w", err)
}

if trig.DryRun == nil {
Expand Down Expand Up @@ -119,13 +114,13 @@ func chartCmdExecute(t api.Builder) func(cmd *cobra.Command, args []string) erro

f, err := os.Create(filename)
if err != nil {
return err
return fmt.Errorf("creting file: %w", err)
}
defer errorh.SafeClose(f)

err = graph.Render(chart.PNG, f)
if err != nil {
return err
return fmt.Errorf("rendering graph: %w", err)
}
fmt.Printf("Detailed chart written to %s\n", filename)
return nil
Expand Down
5 changes: 3 additions & 2 deletions internal/chart/chart_cmd_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"testing"
"time"

"github.com/form3tech-oss/f1/v2/internal/trigger"

"github.com/stretchr/testify/assert"

"github.com/form3tech-oss/f1/v2/internal/trigger"
)

type ChartTestStage struct {
Expand All @@ -28,6 +28,7 @@ func NewChartTestStage(t *testing.T) (*ChartTestStage, *ChartTestStage, *ChartTe
func (s *ChartTestStage) and() *ChartTestStage {
return s
}

func (s *ChartTestStage) i_execute_the_chart_command() *ChartTestStage {
cmd := Cmd(trigger.GetBuilders())
cmd.SetArgs(s.args)
Expand Down
7 changes: 0 additions & 7 deletions internal/chart/chart_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func TestChartConstant(t *testing.T) {

then.
the_command_is_successful()

}

func TestChartConstantNoJitter(t *testing.T) {
Expand All @@ -30,7 +29,6 @@ func TestChartConstantNoJitter(t *testing.T) {

then.
the_command_is_successful()

}

func TestChartStaged(t *testing.T) {
Expand All @@ -44,7 +42,6 @@ func TestChartStaged(t *testing.T) {

then.
the_command_is_successful()

}

func TestChartGaussian(t *testing.T) {
Expand All @@ -59,7 +56,6 @@ func TestChartGaussian(t *testing.T) {

then.
the_command_is_successful()

}

func TestChartGaussianWithJitter(t *testing.T) {
Expand All @@ -75,7 +71,6 @@ func TestChartGaussianWithJitter(t *testing.T) {

then.
the_command_is_successful()

}

func TestChartRamp(t *testing.T) {
Expand All @@ -89,7 +84,6 @@ func TestChartRamp(t *testing.T) {

then.
the_command_is_successful()

}

func TestChartFileConfig(t *testing.T) {
Expand All @@ -103,5 +97,4 @@ func TestChartFileConfig(t *testing.T) {

then.
the_command_is_successful()

}
7 changes: 4 additions & 3 deletions internal/raterun/runner_test_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"testing"
"time"

"go.uber.org/goleak"

"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)

type RatedRunnerStage struct {
Expand All @@ -19,6 +18,8 @@ type RatedRunnerStage struct {
}

func NewRatedRunnerStage(t *testing.T) (*RatedRunnerStage, *RatedRunnerStage, *RatedRunnerStage) {
t.Helper()

stage := RatedRunnerStage{
t: t,
funcRuns: make(map[time.Duration]int),
Expand Down Expand Up @@ -69,7 +70,7 @@ func (s *RatedRunnerStage) function_ran_times(expectedRuns int) *RatedRunnerStag
totalRuns += timesRunForRate
}

assert.Equal(s.t, totalRuns, expectedRuns)
assert.Equal(s.t, expectedRuns, totalRuns)
return s
}

Expand Down
7 changes: 3 additions & 4 deletions internal/run/active_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package run
import (
"time"

"github.com/form3tech-oss/f1/v2/pkg/f1/scenarios"

"github.com/form3tech-oss/f1/v2/pkg/f1/testing"
"github.com/google/uuid"

"github.com/form3tech-oss/f1/v2/internal/metrics"
"github.com/google/uuid"
"github.com/form3tech-oss/f1/v2/pkg/f1/scenarios"
"github.com/form3tech-oss/f1/v2/pkg/f1/testing"
)

type ActiveScenario struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/run/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func getLogFilePath(scenario string) string {
func redirectLoggingToFile(scenario string) string {
logFilePath := getLogFilePath(scenario)

file, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
file, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err == nil {
log.StandardLogger().SetOutput(file)
} else {
Expand Down
9 changes: 3 additions & 6 deletions internal/run/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
)

func TestProvidingCustomLogFilePathWithDirectoryThatDoesExist(t *testing.T) {
defer os.Setenv("LOG_FILE_PATH", os.Getenv("LOG_FILE_PATH"))
os.Setenv("LOG_FILE_PATH", "/does-not-exist/my-scenario.log")
t.Setenv("LOG_FILE_PATH", "/does-not-exist/my-scenario.log")
expected := getGeneratedLogFilePath("my-scenario")

actual := getLogFilePath("my-scenario")
Expand All @@ -20,9 +19,8 @@ func TestProvidingCustomLogFilePathWithDirectoryThatDoesExist(t *testing.T) {
}

func TestProvidingCustomLogFilePathWithDirectoryThatDoesNotExistResultsInGeneratedLogFile(t *testing.T) {
defer os.Setenv("LOG_FILE_PATH", os.Getenv("LOG_FILE_PATH"))
expected := filepath.Join(os.TempDir(), "my-scenario.log")
os.Setenv("LOG_FILE_PATH", expected)
t.Setenv("LOG_FILE_PATH", expected)

actual := getLogFilePath("my-scenario")

Expand All @@ -31,8 +29,7 @@ func TestProvidingCustomLogFilePathWithDirectoryThatDoesNotExistResultsInGenerat
}

func TestProvidingCustomLogFilePathWhichIsEmptyResultsInGeneratedLogFile(t *testing.T) {
defer os.Setenv("LOG_FILE_PATH", os.Getenv("LOG_FILE_PATH"))
os.Setenv("LOG_FILE_PATH", "")
t.Setenv("LOG_FILE_PATH", "")
expected := getGeneratedLogFilePath("my-scenario")

actual := getLogFilePath("my-scenario")
Expand Down
7 changes: 4 additions & 3 deletions internal/run/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (

var fakePrometheus FakePrometheus

const fakePrometheusNamespace = "test-namespace"
const fakePrometheusID = "test-run-name"
const (
fakePrometheusNamespace = "test-namespace"
fakePrometheusID = "test-run-name"
)

func TestMain(m *testing.M) {

var err error
fakePrometheus.Port, err = freeport.GetFreePort()
if err != nil {
Expand Down
Loading
Loading