Skip to content

Commit

Permalink
Merge pull request #150 from wednesday-solutions/feature/linters
Browse files Browse the repository at this point in the history
Added Linters
  • Loading branch information
alichherawalla authored Apr 6, 2024
2 parents 49eeb93 + 6f3eb37 commit f9dd96c
Show file tree
Hide file tree
Showing 42 changed files with 2,670 additions and 2,484 deletions.
17 changes: 10 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ linters-settings:
line-length: 128
tab-width: 2
autofix: true
funlen:
ignore-comments: true
linters:
enable:
- lll
Expand All @@ -11,12 +13,13 @@ linters:
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
# - cyclop
# - funlen
# - gocritic
# - gocyclo
# - gofmt
- funlen
- cyclop
- goconst
- gofmt
- godox
- misspell
- reassign
- whitespace
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"editor.insertSpaces": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "golang.go"
}
},
"go.testFlags": ["-gcflags=all=-l"]
}
3 changes: 0 additions & 3 deletions cmd/seeder/exec/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func main() {

base, _ := os.Getwd()
log.Println(base)

Expand All @@ -26,7 +25,6 @@ func main() {
log.Println(len(files))

for _, file := range files {

log.Println(file.Name())
if slices.Contains([]string{"seed.go"}, file.Name()) && strings.Contains(file.Name(), "env") {
continue
Expand All @@ -41,6 +39,5 @@ func main() {
log.Fatal(err)
}
fmt.Println("out:", outb.String(), "err:", errb.String())

}
}
1 change: 0 additions & 1 deletion cmd/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,5 @@ func main() {
}
fmt.Println(err)
}

}
}
74 changes: 44 additions & 30 deletions cmd/server/main_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package main_test

import (
"log"
"os"
"testing"

main "go-template/cmd/server"
"go-template/internal/config"
"go-template/pkg/api"
"go-template/pkg/utl/convert"

. "github.com/agiledragon/gomonkey/v2"
"github.com/joho/godotenv"
Expand All @@ -17,59 +16,74 @@ import (

const SuccessCase = "Success"

func TestSetup(t *testing.T) {
type TestArgs struct {
setBaseEnv bool
patchDotEnv bool
mockStart bool
apiStarted bool
}

initEnv := func() {
func initEnv(args *TestArgs) *Patches {
if args != nil {
if args.setBaseEnv {
os.Setenv("ENVIRONMENT_NAME", "")
}
if args.patchDotEnv {
loadPatches := ApplyFunc(godotenv.Load, func(...string) error {
return nil
})
return loadPatches
}
if args.mockStart {
apiPatches := ApplyFunc(api.Start, func(cfg *config.Configuration) (*echo.Echo, error) {
args.apiStarted = true
return nil, nil
})

err := config.LoadEnvWithFilePrefix(convert.StringToPointerString("./../../"))
if err != nil {
log.Fatal(err)
return apiPatches
}
}

return nil
}
func TestSetup(t *testing.T) {
cases := map[string]struct {
error string
isPanic bool
init func()
init func(*TestArgs) *Patches
args *TestArgs
}{
"Failure__envFileNotFound": {
error: "open .env.base: no such file or directory",
error: "error loading port from .env",
isPanic: true,
init: initEnv,
},
"Failure_NoEnvName": {
error: "open .env.base: no such file or directory",
isPanic: true,
args: &TestArgs{
patchDotEnv: true,
},
},
SuccessCase: {
isPanic: false,
init: initEnv,
args: &TestArgs{
apiStarted: false,
mockStart: true,
},
},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
if tt.init != nil {
tt.init()
patches := tt.init(tt.args)
if patches != nil {
defer patches.Reset()
}
}
if tt.isPanic {
assert.PanicsWithValue(t, tt.error, main.Setup, "os.Exit was not called")
assert.PanicsWithValue(t, tt.error, main.Setup, tt.error)
} else {
apiStarted := false
loadPatches := ApplyFunc(godotenv.Load, func(...string) error {
return nil
})
apiPatches := ApplyFunc(api.Start, func(cfg *config.Configuration) (*echo.Echo, error) {
apiStarted = true
return nil, nil
})

defer apiPatches.Reset()
defer loadPatches.Reset()

main.Setup()
assert.Equal(t, apiStarted, true)
assert.Equal(t, tt.args.apiStarted, true)
}

})
}

}
3 changes: 0 additions & 3 deletions daos/dao_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestGetContextExecutor(t *testing.T) {

cases := []struct {
name string
res *sql.Tx
Expand All @@ -23,13 +22,11 @@ func TestGetContextExecutor(t *testing.T) {

// Loop through the test cases
for _, tt := range cases {

t.Run(tt.name, func(t *testing.T) {
response := daos.GetContextExecutor(&sql.Tx{})

// Check if the response is equal to the expected value
assert.Equal(t, response, tt.res)

})
}
}
3 changes: 0 additions & 3 deletions daos/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

func TestCreateRoleTx(t *testing.T) {

cases := []struct {
name string
req models.Role
Expand Down Expand Up @@ -64,7 +63,6 @@ func TestCreateRoleTx(t *testing.T) {
}

func TestFindRoleByID(t *testing.T) {

cases := []struct {
name string
req int
Expand Down Expand Up @@ -104,7 +102,6 @@ func TestFindRoleByID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
_, err := daos.FindRoleByID(tt.req, context.Background())
assert.Equal(t, err, tt.err)

})
}
}
1 change: 0 additions & 1 deletion daos/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ func FindAllUsersWithCount(queryMods []qm.QueryMod, ctx context.Context) (models
queryMods = append(queryMods, qm.Offset(0))
count, err := models.Users(queryMods...).Count(ctx, contextExecutor)
return users, count, err

}
Loading

0 comments on commit f9dd96c

Please sign in to comment.