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

En/upgrade linter #115

Merged
merged 10 commits into from
Dec 12, 2023
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,10 @@ jobs:
code_quality:
name: Code Quality🎖️
runs-on: ubuntu-latest
container: "golangci/golangci-lint:v1.52.2"
container: "golangci/golangci-lint:v1.55.2"
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Get dependencies
run: go get -v -t -d ./...
- name: GolangCI-Lint
Expand Down
8 changes: 5 additions & 3 deletions cmd/gofr/addRoute/addRoute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func Test_addHandlerImport(t *testing.T) {
}

func Test_populateMain(t *testing.T) {
const testPath = "/testEntity"

dir := t.TempDir()

ctrl := gomock.NewController(t)
Expand Down Expand Up @@ -218,7 +220,7 @@ func Test_populateMain(t *testing.T) {
wantErr bool
}{
{"Error chdir", args{"package main"}, []*gomock.Call{
c.EXPECT().Getwd().Return(dir+"/testEntity", nil).MaxTimes(5),
c.EXPECT().Getwd().Return(dir+testPath, nil).MaxTimes(5),
c.EXPECT().Chdir(gomock.AssignableToTypeOf(dir)).Return(errors.New("test error")),
}, true},

Expand All @@ -238,7 +240,7 @@ func Test_populateMain(t *testing.T) {
}, true},

{"Success OpenFile", args{"package main"}, []*gomock.Call{
c.EXPECT().Getwd().Return(dir+"/testEntity", nil).MaxTimes(1),
c.EXPECT().Getwd().Return(dir+testPath, nil).MaxTimes(1),
c.EXPECT().Chdir(gomock.AssignableToTypeOf(dir)).Return(nil).MaxTimes(1),
c.EXPECT().OpenFile("main.go", gomock.Any(), gomock.Any()).Return(file, nil).Times(2),
}, false},
Expand Down Expand Up @@ -407,7 +409,7 @@ func Test_AddRoute(t *testing.T) {
for i, tc := range tests {
_ = os.Chdir(dir)

req := httptest.NewRequest("", setQueryParams(tc.params), nil)
req := httptest.NewRequest("", setQueryParams(tc.params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

res, err := AddRoute(ctx)
Expand Down
30 changes: 16 additions & 14 deletions cmd/gofr/dockerize/dockerize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dockerize

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
Expand Down Expand Up @@ -124,7 +125,7 @@ func Test_Dockerize(t *testing.T) {
}

for i, tc := range testCases {
req := httptest.NewRequest("", setQueryParams(tc.params), nil)
req := httptest.NewRequest("", setQueryParams(tc.params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), app)

resp, err := h.Dockerize(ctx)
Expand All @@ -144,7 +145,7 @@ func TestDockerize_Error(t *testing.T) {
expErr := &errors.Response{Reason: fmt.Sprintf(`unknown parameter(s) [` + strings.Join([]string{"tags"}, ",") + `]. ` +
`Run gofr <command_name> -h for help of the command.`)}

req := httptest.NewRequest("", setQueryParams(map[string]string{"tags": "commit"}), nil)
req := httptest.NewRequest("", setQueryParams(map[string]string{"tags": "commit"}), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), app)

resp, err := h.Dockerize(ctx)
Expand All @@ -160,7 +161,7 @@ func TestDockerize_Fail(t *testing.T) {
params := map[string]string{"invalid": "testTag"}
expErr := &errors.Response{Reason: "unknown parameter(s) [invalid]. Run gofr <command_name> -h for help of the command."}

req := httptest.NewRequest("", setQueryParams(params), nil)
req := httptest.NewRequest("", setQueryParams(params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Run(ctx)
Expand All @@ -174,7 +175,7 @@ func Test_Dockerize_getImageFailure(t *testing.T) {

h := New("gofr-app", "1.0.0")

req := httptest.NewRequest("", setQueryParams(map[string]string{"tag": "commit"}), nil)
req := httptest.NewRequest("", setQueryParams(map[string]string{"tag": "commit"}), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Dockerize(ctx)
Expand All @@ -191,7 +192,7 @@ func Test_Dockerize_buildFailure(t *testing.T) {

h := New("gofr-app", "1.0.0")

req := httptest.NewRequest("", "/dummy", nil)
req := httptest.NewRequest("", "/dummy", http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Dockerize(ctx)
Expand All @@ -207,7 +208,7 @@ func Test_Dockerize_buildDockerFailure(t *testing.T) {

h := New("gofr-app", "1.0.0")

req := httptest.NewRequest("", "/dummy", nil)
req := httptest.NewRequest("", "/dummy", http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Dockerize(ctx)
Expand Down Expand Up @@ -241,7 +242,7 @@ func TestRun(t *testing.T) {
}

for i, tc := range testCases {
req := httptest.NewRequest("", setQueryParams(tc.params), nil)
req := httptest.NewRequest("", setQueryParams(tc.params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), app)

resp, err := h.Run(ctx)
Expand All @@ -258,7 +259,7 @@ func TestRun_Fail(t *testing.T) {
params := map[string]string{"invalid": "testTag"}
expErr := &errors.Response{Reason: "unknown parameter(s) [invalid]. Run gofr <command_name> -h for help of the command."}

req := httptest.NewRequest("", setQueryParams(params), nil)
req := httptest.NewRequest("", setQueryParams(params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Run(ctx)
Expand All @@ -281,7 +282,7 @@ func Test_populateFlags(t *testing.T) {
}

for i, tc := range testcases {
req := httptest.NewRequest("", setQueryParams(tc.params), nil)
req := httptest.NewRequest("", setQueryParams(tc.params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

res := populateFlags(ctx, "gofr-app")
Expand All @@ -305,7 +306,8 @@ func Test_getImageName(t *testing.T) {
}

for i, tc := range testCases {
imageName, err := getImageName(&tc.flag, "dev")
testFlag := tc.flag
imageName, err := getImageName(&testFlag, "dev")

assert.Contains(t, imageName, tc.expRes, "[TESTCASE %d] Failed Desc: %v\nexpected %v\tgot %v\n", i+1, tc.desc, tc.expRes, imageName)
assert.Nil(t, err, "[TESTCASE %d] Failed Desc: %v\nexpected %v\tgot %v\n", i+1, tc.desc, nil, err)
Expand Down Expand Up @@ -354,7 +356,7 @@ func Test_Run_getImageFailure(t *testing.T) {

h := New("gofr-app", "1.0.0")

req := httptest.NewRequest("", setQueryParams(map[string]string{"tag": "commit"}), nil)
req := httptest.NewRequest("", setQueryParams(map[string]string{"tag": "commit"}), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Run(ctx)
Expand All @@ -371,7 +373,7 @@ func Test_Run_buildFailure(t *testing.T) {

h := New("gofr-app", "1.0.0")

req := httptest.NewRequest("", "/dummy", nil)
req := httptest.NewRequest("", "/dummy", http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Run(ctx)
Expand All @@ -387,7 +389,7 @@ func Test_Run_buildDockerFailure(t *testing.T) {

h := New("gofr-app", "1.0.0")

req := httptest.NewRequest("", "/dummy", nil)
req := httptest.NewRequest("", "/dummy", http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Run(ctx)
Expand All @@ -401,7 +403,7 @@ func Test_Run_runDockerFailure(t *testing.T) {

h := New("gofr-app", "1.0.0")

req := httptest.NewRequest("", setQueryParams(map[string]string{"image": "invalid"}), nil)
req := httptest.NewRequest("", setQueryParams(map[string]string{"image": "invalid"}), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

resp, err := h.Run(ctx)
Expand Down
8 changes: 5 additions & 3 deletions cmd/gofr/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
type Handler struct {
}

const goExtension = ".go"

// Getwd returns a rooted path name corresponding to the current directory
func (h Handler) Getwd() (string, error) {
return os.Getwd()
Expand Down Expand Up @@ -135,7 +137,7 @@ func addCore(f fileSystem, projectDirectory, entity string) error {
return err
}
// create the interfaceFile , interface.go, for core layer
interfaceFile, err := f.OpenFile("interface.go", os.O_APPEND|os.O_CREATE|os.O_WRONLY, migration.RWMode)
interfaceFile, err := f.OpenFile("interface"+goExtension, os.O_APPEND|os.O_CREATE|os.O_WRONLY, migration.RWMode)
if err != nil {
return err
}
Expand Down Expand Up @@ -175,7 +177,7 @@ func addComposite(f fileSystem, projectDirectory, entity string) error {
return err
}

interfaceFile, err := f.OpenFile("interface.go", os.O_APPEND|os.O_CREATE|os.O_WRONLY, migration.RWMode)
interfaceFile, err := f.OpenFile("interface"+goExtension, os.O_APPEND|os.O_CREATE|os.O_WRONLY, migration.RWMode)
if err != nil {
return err
}
Expand Down Expand Up @@ -213,7 +215,7 @@ func addConsumer(f fileSystem, projectDirectory, entity string) error {
return err
}

filePtr, err := f.OpenFile(entity+".go", os.O_APPEND|os.O_CREATE|os.O_WRONLY, migration.RWMode)
filePtr, err := f.OpenFile(entity+goExtension, os.O_APPEND|os.O_CREATE|os.O_WRONLY, migration.RWMode)
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/gofr/entity/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package entity
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
Expand Down Expand Up @@ -297,7 +298,7 @@ func Test_AddEntity(t *testing.T) {
for i, tc := range tests {
_ = os.Chdir(path)

req := httptest.NewRequest("", setQueryParams(tc.params), nil)
req := httptest.NewRequest("", setQueryParams(tc.params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

res, err := AddEntity(ctx)
Expand Down
3 changes: 2 additions & 1 deletion cmd/gofr/initialize/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package initialize
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
Expand Down Expand Up @@ -159,7 +160,7 @@ func Test_Init(t *testing.T) {
for i, tc := range tests {
_ = os.Chdir(path)

req := httptest.NewRequest("", setQueryParams(tc.params), nil)
req := httptest.NewRequest("", setQueryParams(tc.params), http.NoBody)
ctx := gofr.NewContext(nil, request.NewHTTPRequest(req), gofr.New())

res, err := Init(ctx)
Expand Down
28 changes: 15 additions & 13 deletions cmd/gofr/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"gofr.dev/pkg/gofr/assert"
)

const RWXOwner = 0700
const RWXOwner, testProject = 0700, "/testGoProject"

// initializeTest function to create a sample go project in temporary directory
func initializeTest(t *testing.T) {
Expand Down Expand Up @@ -149,43 +149,43 @@ func TestCLI(t *testing.T) {

assert.CMDOutputContains(t, main, "gofr init -name=testGoProject", "Successfully created project: testGoProject")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr init -namee=testGoProject",
"unknown parameter(s) [namee]. Run gofr <command_name> -h for help of the command.")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr add -methods=all -path=/foo", "Added route: /foo")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr add -method=all -path=/foo",
"unknown parameter(s) [method]. Run gofr <command_name> -h for help of the command.")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr add -methods= -path=/foo",
"Parameter methods is required for this request")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr add -methods=all -path=", "Parameter path is required for this request")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr add -methods=all -path=/foo", "route foo is already present")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr entity -type=core -name=person", "Successfully created entity: person")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr entity -type= -name=person",
"Parameter type is required for this request")

_ = os.Chdir(dir + "/testGoProject")
_ = os.Chdir(dir + testProject)

assert.CMDOutputContains(t, main, "gofr entity -typee=core -namee=person",
"unknown parameter(s) [namee,typee]. Run gofr <command_name> -h for help of the command.")
Expand Down Expand Up @@ -428,6 +428,8 @@ paths:
'400':
description: Sample API Hello`

const gofrTestHost = "gofr test -host="

d1 := []byte(ymlStr)

tempFile, err := os.CreateTemp(t.TempDir(), "dat1.yml")
Expand All @@ -445,7 +447,7 @@ paths:
_ = json.NewEncoder(w).Encode("{}")
}))
hostPort := strings.Replace(server.URL, "http://", "", 1)
assert.CMDOutputContains(t, main, "gofr test -host="+hostPort+" -source="+tempFile.Name(), "failed")
assert.CMDOutputContains(t, main, gofrTestHost+hostPort+" -source="+tempFile.Name(), "failed")

// case to check test help
assert.CMDOutputContains(t, main, "gofr test -h", "runs integration test for a given configuration")
Expand All @@ -455,11 +457,11 @@ paths:
"unknown parameter(s) [hosts]. Run gofr <command_name> -h for help of the command.")

// case when source not specified
assert.CMDOutputContains(t, main, "gofr test -host="+hostPort, "Parameter source is required for this request")
assert.CMDOutputContains(t, main, gofrTestHost+hostPort, "Parameter source is required for this request")

// case when host not specified
assert.CMDOutputContains(t, main, "gofr test -source="+tempFile.Name(), "Parameter host is required for this request")

// case when source is incorrect
assert.CMDOutputContains(t, main, "gofr test -host="+hostPort+" -source=/some/fake/path/data.yml", "no such file or directory")
assert.CMDOutputContains(t, main, gofrTestHost+hostPort+" -source=/some/fake/path/data.yml", "no such file or directory")
}
9 changes: 5 additions & 4 deletions cmd/gofr/migration/dbMigration/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (c *Cassandra) Run(m Migrator, app, name, method string, logger log.Logger)
}

func (c *Cassandra) preRun(app, method, name string) error {
migrationTableSchema := "CREATE TABLE IF NOT EXISTS gofr_migrations ( " +
"app text, version bigint, start_time timestamp, end_time timestamp, method text, PRIMARY KEY (app, version, method) )"
const migrationTableSchema = `CREATE TABLE IF NOT EXISTS gofr_migrations (app text, version bigint,` +
`start_time timestamp, end_time timestamp, method text, PRIMARY KEY (app, version, method) )`

err := c.session.Query(migrationTableSchema).Exec()
if err != nil {
Expand Down Expand Up @@ -152,13 +152,14 @@ func (c *Cassandra) GetAllMigrations(app string) (upMigrations, downMigrations [

// FinishMigration completes the migration
func (c *Cassandra) FinishMigration() error {
const query = "INSERT INTO gofr_migrations(app, version, method, start_time, end_time) VALUES (?, ?, ?, ?, ?)"

if c.session == nil {
return errors.DataStoreNotInitialized{DBName: datastore.CassandraStore}
}

for _, l := range c.newMigrations {
err := c.session.Query("INSERT INTO gofr_migrations(app, version, method, start_time, end_time) "+
"VALUES (?, ?, ?, ?, ?)", l.App, l.Version, l.Method, l.StartTime, l.EndTime).Exec()
err := c.session.Query(query, l.App, l.Version, l.Method, l.StartTime, l.EndTime).Exec()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gofr/migration/dbMigration/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func initRedisTests() *Redis {
Password: c.Get("REDIS_PASS"),
}

db, _ := datastore.NewRedis(logger, configs)
db, _ := datastore.NewRedis(logger, &configs)
redis := NewRedis(db)

return redis
Expand Down