Skip to content
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
76 changes: 76 additions & 0 deletions go/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
linters-settings:
errcheck:
check-type-assertions: true
govet:
enable-all: true

linters:
disable-all: true
enable:
# enabled by default
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
# disabled by default
- asasalint
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- copyloopvar
- durationcheck
- errname
- errorlint
- exhaustive
- exptostd
- fatcontext
- gocheckcompilerdirectives
- gochecknoinits
- gochecksumtype
- goconst
- gocyclo
- godot
- goimports
- gomoddirectives
- goprintffuncname
- gosec
- iface
- intrange
- loggercheck
- makezero
- mirror
- musttag
- nakedret
- nestif
- nilerr
- nilnesserr
- nilnil
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- perfsprint
- predeclared
- promlinter
- protogetter
- reassign
- recvcheck
- revive
- sloglint
- spancheck
- sqlclosecheck
- stylecheck
- tenv
- testableexamples
- testifylint
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- usetesting
- wastedassign
- whitespace
8 changes: 4 additions & 4 deletions go/cmd/snekcheck/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package main

// Common errors.
var (
invalidFileNamesErr = Error{Code: 1, Message: "invalid filenames found"}
noPathsProvidedErr = Error{Code: 2, Message: "no paths provided"}
failedToBuildFileTreeErr = Error{Code: 3, Message: "failed to build file tree"}
errInvalidFileNames = Error{Code: 1, Message: "invalid filenames found"}
errNoPathsProvided = Error{Code: 2, Message: "no paths provided"}
errFailedToBuildFileTree = Error{Code: 3, Message: "failed to build file tree"}
)

// A command-line error.
type Error struct {
Code uint8
Message string
Code uint8
}

// Returns the error's status code.
Expand Down
6 changes: 6 additions & 0 deletions go/cmd/snekcheck/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
func TestError(t *testing.T) {
t.Parallel()
t.Run("Error()", func(t *testing.T) {
t.Parallel()
t.Run("returns the correct error message", func(t *testing.T) {
t.Parallel()
testCases := []string{
"an error occurred",
"yikes!",
Expand All @@ -23,6 +25,7 @@ func TestError(t *testing.T) {
}
})
t.Run("formats correctly as an error", func(t *testing.T) {
t.Parallel()
testCases := []string{
"an error occurred",
"yikes!",
Expand All @@ -33,6 +36,7 @@ func TestError(t *testing.T) {
}
})
t.Run("can be wrapped and unwrapped", func(t *testing.T) {
t.Parallel()
testCases := []string{
"an error occurred",
"yikes!",
Expand All @@ -45,7 +49,9 @@ func TestError(t *testing.T) {
})
})
t.Run("ExitCode()", func(t *testing.T) {
t.Parallel()
t.Run("produces the correct exit code", func(t *testing.T) {
t.Parallel()
testCases := []uint8{
0,
1,
Expand Down
8 changes: 4 additions & 4 deletions go/cmd/snekcheck/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Run(config Config) cli.Error {
panic("invalid filesystem")
}
if len(config.Paths) == 0 {
return noPathsProvidedErr
return errNoPathsProvided
}

// Build file tree.
Expand All @@ -38,7 +38,7 @@ func Run(config Config) cli.Error {
})
for _, path := range config.Paths {
if addPathWithChildren(fileTree, config.Fs, path, config.Depth) != nil {
return failedToBuildFileTreeErr
return errFailedToBuildFileTree
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func Run(config Config) cli.Error {

// Terminate.
if len(invalidPaths) != 0 {
return invalidFileNamesErr
return errInvalidFileNames
}
return nil
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func addPathWithChildren(fileTree tree.UniqueNode[string], fs billy.Filesystem,
return recurse(path, 0)
}

// Parses gitignore patterns in a single directory
// Parses gitignore patterns in a single directory.
func parseGitIgnorePatterns(fs billy.Filesystem, path files.Path) files.GitIgnore {
patterns, ignoreErr := files.ParseGitIgnore(fs, path)
if ignoreErr != nil {
Expand Down
34 changes: 16 additions & 18 deletions go/cmd/snekcheck/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -33,57 +31,57 @@ func (suite *RunTestSuite) SetupSubTest() {
func (suite *RunTestSuite) TestRun() {
suite.Run("Run()", func() {
suite.Run("panics given an invalid file system", func() {
assert.Panics(suite.T(), func() { _ = main.Run(main.Config{Fs: nil}) })
suite.Panics(func() { _ = main.Run(main.Config{Fs: nil}) })
})
suite.Run("returns an error when given no paths", func() {
assert.NotNil(suite.T(), main.Run(main.Config{Fs: memfs.New(), Paths: nil}))
suite.NotNil(main.Run(main.Config{Fs: memfs.New(), Paths: nil}))
})
suite.Run("check", func() {
suite.Run("succeeds with an empty directory", func() {
config := main.Config{Depth: math.MaxUint, Fs: suite.Fs, Fix: false, Paths: []files.Path{suite.Root}, Verbose: false}
assert.Nil(suite.T(), main.Run(config))
suite.Nil(main.Run(config))
})
suite.Run("succeeds with a single valid file", func() {
validFilename := append(suite.Root, "valid")
require.True(suite.T(), main.IsValid(validFilename.Base()))
suite.Require().True(main.IsValid(validFilename.Base()))
_, createErr := suite.Fs.Create(validFilename.String())
require.Nil(suite.T(), createErr)
suite.Require().NoError(createErr)

config := main.Config{Depth: math.MaxUint, Fs: suite.Fs, Fix: false, Paths: []files.Path{suite.Root}, Verbose: false}
assert.Nil(suite.T(), main.Run(config))
suite.Nil(main.Run(config))
})
suite.Run("fails with a single invalid file", func() {
invalidFilename := append(suite.Root, "InVaLiD")
require.False(suite.T(), main.IsValid(invalidFilename.Base()))
suite.Require().False(main.IsValid(invalidFilename.Base()))
_, createErr := suite.Fs.Create(invalidFilename.String())
require.Nil(suite.T(), createErr)
suite.Require().NoError(createErr)

config := main.Config{Depth: math.MaxUint, Fs: suite.Fs, Fix: false, Paths: []files.Path{suite.Root}, Verbose: false}
assert.NotNil(suite.T(), main.Run(config))
suite.NotNil(main.Run(config))
})
})
suite.Run("fix", func() {
suite.Run("succeeds with an empty directory", func() {
config := main.Config{Depth: math.MaxUint, Fs: suite.Fs, Fix: true, Paths: []files.Path{suite.Root}, Verbose: false}
assert.Nil(suite.T(), main.Run(config))
suite.Nil(main.Run(config))
})
suite.Run("succeeds with a single valid file", func() {
validFilename := append(suite.Root, "valid")
require.True(suite.T(), main.IsValid(validFilename.Base()))
suite.Require().True(main.IsValid(validFilename.Base()))
_, createErr := suite.Fs.Create(validFilename.String())
require.Nil(suite.T(), createErr)
suite.Require().NoError(createErr)

config := main.Config{Depth: math.MaxUint, Fs: suite.Fs, Fix: true, Paths: []files.Path{suite.Root}, Verbose: false}
assert.Nil(suite.T(), main.Run(config))
suite.Nil(main.Run(config))
})
suite.Run("succeeds with a single invalid file", func() {
invalidFilename := append(suite.Root, "InVaLiD")
require.False(suite.T(), main.IsValid(invalidFilename.Base()))
suite.Require().False(main.IsValid(invalidFilename.Base()))
_, createErr := suite.Fs.Create(invalidFilename.String())
require.Nil(suite.T(), createErr)
suite.Require().NoError(createErr)

config := main.Config{Depth: math.MaxUint, Fs: suite.Fs, Fix: true, Paths: []files.Path{suite.Root}, Verbose: false}
assert.Nil(suite.T(), main.Run(config))
suite.Nil(main.Run(config))
})
})
})
Expand Down
2 changes: 2 additions & 0 deletions go/cmd/snekcheck/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func TestIsValid(t *testing.T) {
t.Parallel()
t.Run("identifies valid file names", func(t *testing.T) {
t.Parallel()
testCases := []string{
"main.go",
"flake.nix",
Expand All @@ -23,6 +24,7 @@ func TestIsValid(t *testing.T) {
}
})
t.Run("identifies invalid file names", func(t *testing.T) {
t.Parallel()
testCases := []string{
"Snake",
"snake case 123",
Expand Down
11 changes: 11 additions & 0 deletions go/internal/files/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
func TestPath(t *testing.T) {
t.Parallel()
t.Run("NewPath()", func(t *testing.T) {
t.Parallel()
t.Run("creates a path", func(t *testing.T) {
t.Parallel()
testCases := []string{
"",
"longstring",
Expand All @@ -27,7 +29,9 @@ func TestPath(t *testing.T) {
})
})
t.Run("Base()", func(t *testing.T) {
t.Parallel()
t.Run("returns the last element", func(t *testing.T) {
t.Parallel()
testCases := []files.Path{
{"dir", "dir", "file.txt"},
{"longstring"},
Expand All @@ -38,11 +42,14 @@ func TestPath(t *testing.T) {
}
})
t.Run("panics if the path is empty", func(t *testing.T) {
t.Parallel()
assert.Panics(t, func() { files.Path{}.Base() })
})
})
t.Run("Parent()", func(t *testing.T) {
t.Parallel()
t.Run("returns every element except the last element", func(t *testing.T) {
t.Parallel()
testCases := []files.Path{
{"dir", "dir", "file.txt"},
{"longstring"},
Expand All @@ -53,11 +60,14 @@ func TestPath(t *testing.T) {
}
})
t.Run("panics if the path is empty", func(t *testing.T) {
t.Parallel()
assert.Panics(t, func() { files.Path{}.Parent() })
})
})
t.Run("String()", func(t *testing.T) {
t.Parallel()
t.Run("returns the full path", func(t *testing.T) {
t.Parallel()
testCases := []files.Path{
{"dir", "dir", "file.txt"},
{"longstring"},
Expand All @@ -68,6 +78,7 @@ func TestPath(t *testing.T) {
}
})
t.Run("formats correctly as a string", func(t *testing.T) {
t.Parallel()
testCases := []files.Path{
{"dir", "dir", "file.txt"},
{"longstring"},
Expand Down
10 changes: 8 additions & 2 deletions go/internal/patterns/posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

func BenchmarkPosix(b *testing.B) {
b.Run("IsPosixFileName()", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for range b.N {
patterns.IsPosixFileName("Bench mark")
}
})
b.Run("ToPosixFileName()", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for range b.N {
patterns.ToPosixFileName("Bench mark")
}
})
Expand All @@ -34,7 +34,9 @@ func FuzzPosix(f *testing.F) {
func TestPosix(t *testing.T) {
t.Parallel()
t.Run("IsPosixFileName()", func(t *testing.T) {
t.Parallel()
t.Run("identifies valid POSIX filenames", func(t *testing.T) {
t.Parallel()
testCases := []string{
"posix",
"POSIX_FILE",
Expand All @@ -47,6 +49,7 @@ func TestPosix(t *testing.T) {
}
})
t.Run("identifies invalid POSIX filenames", func(t *testing.T) {
t.Parallel()
testCases := []string{
"-TEST",
"lol@email",
Expand All @@ -59,7 +62,9 @@ func TestPosix(t *testing.T) {
})
})
t.Run("ToPosixFileName()", func(t *testing.T) {
t.Parallel()
t.Run("does not change valid POSIX filenames", func(t *testing.T) {
t.Parallel()
testCases := []string{
"POSIX",
".POSIX_123_.md",
Expand All @@ -71,6 +76,7 @@ func TestPosix(t *testing.T) {
}
})
t.Run("converts invalid POSIX filenames to valid POSIX filenames", func(t *testing.T) {
t.Parallel()
testCases := []struct {
input string
output string
Expand Down
Loading