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

feat: [#491] Fix the errors thrown by Lint/nilaway CI #642

Merged
merged 41 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c78885a
solve potential nil error for console
kkumar-gcc Sep 12, 2024
610ac59
solve potential nil error for progress bar
kkumar-gcc Sep 12, 2024
51de4fe
solve potential nil error for str.When
kkumar-gcc Sep 12, 2024
b4ff711
resolve nil error for filesystem
kkumar-gcc Sep 12, 2024
45b921e
resolve nil errors
kkumar-gcc Sep 12, 2024
07dd54a
resolve nil errors
kkumar-gcc Sep 12, 2024
58d507a
resolve nil errors
kkumar-gcc Sep 12, 2024
188012d
fix:lint error
kkumar-gcc Sep 12, 2024
7f0dbc9
fix:test error
kkumar-gcc Sep 12, 2024
e0d739c
fix nil error
kkumar-gcc Sep 12, 2024
6495460
fix nil error
kkumar-gcc Sep 12, 2024
8f402dd
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 12, 2024
d04bada
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 13, 2024
4148d38
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 14, 2024
a94e968
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 15, 2024
e359bb7
fix nilaway erro
kkumar-gcc Sep 15, 2024
1599d09
fix nil errors
kkumar-gcc Sep 15, 2024
c9670a9
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 16, 2024
70f858b
fix nil errors
kkumar-gcc Sep 17, 2024
9393c30
optimize file facade setting
kkumar-gcc Sep 17, 2024
f131c24
.
kkumar-gcc Sep 17, 2024
066ae54
.
kkumar-gcc Sep 17, 2024
209f95d
.
kkumar-gcc Sep 17, 2024
00616ce
remove nil error from facades
kkumar-gcc Sep 17, 2024
e9cbd2e
update crypt facade
kkumar-gcc Sep 17, 2024
c670ff7
nilaway:fix for session
kkumar-gcc Sep 17, 2024
0ed6949
nilaway:fix for file
kkumar-gcc Sep 17, 2024
5b43804
nilaway:fix for file
kkumar-gcc Sep 17, 2024
fa97fd2
nilaway:fix for json
kkumar-gcc Sep 17, 2024
4a2f4aa
fix:lint
kkumar-gcc Sep 18, 2024
f505eed
fix:test
kkumar-gcc Sep 18, 2024
0f06b18
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 24, 2024
8e75482
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 26, 2024
c01fbc9
resolve comments
kkumar-gcc Sep 27, 2024
70f0754
fix:test
kkumar-gcc Sep 27, 2024
58b5a40
fix:nilaway
kkumar-gcc Sep 27, 2024
1697335
update recommendation
kkumar-gcc Sep 29, 2024
bdcffb4
update aes registration
kkumar-gcc Sep 29, 2024
bb2b1af
.
kkumar-gcc Sep 29, 2024
403dc12
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 29, 2024
346ae15
Merge branch 'master' into kkumar-gcc/#491
kkumar-gcc Sep 29, 2024
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 auth/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]contractconsole.Command{
app.Commands([]contractconsole.Command{

Check warning on line 35 in auth/service_provider.go

View check run for this annotation

Codecov / codecov/patch

auth/service_provider.go#L35

Added line #L35 was not covered by tests
console.NewJwtSecretCommand(app.MakeConfig()),
console.NewPolicyMakeCommand(),
})
Expand Down
2 changes: 1 addition & 1 deletion cache/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]contractsconsole.Command{
app.Commands([]contractsconsole.Command{

Check warning on line 29 in cache/service_provider.go

View check run for this annotation

Codecov / codecov/patch

cache/service_provider.go#L29

Added line #L29 was not covered by tests
console.NewClearCommand(app.MakeCache()),
})
}
12 changes: 12 additions & 0 deletions console/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,31 @@

// Call Run an Artisan console command by name.
func (c *Application) Call(command string) {
if len(os.Args) == 0 {
return

Check warning on line 51 in console/application.go

View check run for this annotation

Codecov / codecov/patch

console/application.go#L51

Added line #L51 was not covered by tests
}

commands := []string{os.Args[0]}

if c.isArtisan {
commands = append(commands, "artisan")
}

c.Run(append(commands, strings.Split(command, " ")...), false)
}

// CallAndExit Run an Artisan console command by name and exit.
func (c *Application) CallAndExit(command string) {
if len(os.Args) == 0 {
return

Check warning on line 66 in console/application.go

View check run for this annotation

Codecov / codecov/patch

console/application.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

commands := []string{os.Args[0]}

if c.isArtisan {
commands = append(commands, "artisan")
}

c.Run(append(commands, strings.Split(command, " ")...), true)
}

Expand Down
10 changes: 8 additions & 2 deletions console/progress_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
}

func (r *ProgressBar) Advance(step ...int) {
var instance *pterm.ProgressbarPrinter

Check warning on line 22 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L22

Added line #L22 was not covered by tests

if len(step) > 0 {
r.instance = r.instance.Add(step[0])
instance = r.instance.Add(step[0])

Check warning on line 25 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L25

Added line #L25 was not covered by tests
} else {
r.instance = r.instance.Increment()
instance = r.instance.Increment()

Check warning on line 27 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L27

Added line #L27 was not covered by tests
}

if instance != nil {
r.instance = instance

Check warning on line 31 in console/progress_bar.go

View check run for this annotation

Codecov / codecov/patch

console/progress_bar.go#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}

Expand Down
23 changes: 17 additions & 6 deletions console/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"github.com/goravel/framework/console/console"
consolecontract "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/support/color"
)

const Binding = "goravel.console"
Expand All @@ -25,12 +26,22 @@
}

func (receiver *ServiceProvider) registerCommands(app foundation.Application) {
artisan := app.MakeArtisan()
config := app.MakeConfig()
artisan.Register([]consolecontract.Command{
console.NewListCommand(artisan),
console.NewKeyGenerateCommand(config),
artisanFacade := app.MakeArtisan()
if artisanFacade == nil {
color.Yellow().Println("Warning: Artisan Facade is not initialized. Skipping command registration.")
return

Check warning on line 32 in console/service_provider.go

View check run for this annotation

Codecov / codecov/patch

console/service_provider.go#L29-L32

Added lines #L29 - L32 were not covered by tests
}

configFacade := app.MakeConfig()
if configFacade == nil {
color.Yellow().Println("Warning: Config Facade is not initialized. Skipping certain command registrations.")
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
return

Check warning on line 38 in console/service_provider.go

View check run for this annotation

Codecov / codecov/patch

console/service_provider.go#L35-L38

Added lines #L35 - L38 were not covered by tests
}

artisanFacade.Register([]consolecontract.Command{
console.NewListCommand(artisanFacade),
console.NewKeyGenerateCommand(configFacade),

Check warning on line 43 in console/service_provider.go

View check run for this annotation

Codecov / codecov/patch

console/service_provider.go#L41-L43

Added lines #L41 - L43 were not covered by tests
console.NewMakeCommand(),
console.NewBuildCommand(config),
console.NewBuildCommand(configFacade),

Check warning on line 45 in console/service_provider.go

View check run for this annotation

Codecov / codecov/patch

console/service_provider.go#L45

Added line #L45 was not covered by tests
})
}
16 changes: 10 additions & 6 deletions crypt/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"

"github.com/goravel/framework/contracts/config"
Expand All @@ -20,24 +21,27 @@
}

// NewAES returns a new AES hasher.
func NewAES(config config.Config, json foundation.Json) *AES {
func NewAES(config config.Config, json foundation.Json) (*AES, error) {
key := config.GetString("app.key")

// Don't use AES in artisan when the key is empty.
if support.Env == support.EnvArtisan && len(key) == 0 {
return nil
return nil, ErrAppKeyNotSetInArtisan

Check warning on line 29 in crypt/aes.go

View check run for this annotation

Codecov / codecov/patch

crypt/aes.go#L29

Added line #L29 was not covered by tests
}

keyLength := len(key)
// check key length before using it
if len(key) != 16 && len(key) != 24 && len(key) != 32 {
color.Red().Println("[Crypt] Empty or invalid APP_KEY, please reset it.\nExample command:\ngo run . artisan key:generate")
return nil
if keyLength != 16 && keyLength != 24 && keyLength != 32 {
color.Red().Printf("[Crypt] Invalid APP_KEY length. Expected 16, 24, or 32 bytes, but got %d bytes.\n", len(key))
color.Red().Println("Please reset it using the following command:\ngo run . artisan key:generate")
return nil, fmt.Errorf("%w: %d bytes", ErrInvalidAppKeyLength, keyLength)

Check warning on line 37 in crypt/aes.go

View check run for this annotation

Codecov / codecov/patch

crypt/aes.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}

keyBytes := []byte(key)
return &AES{
key: keyBytes,
json: json,
}
}, nil
}

// EncryptString encrypts the given string, and returns the iv and ciphertext as base64 encoded strings.
Expand Down
19 changes: 16 additions & 3 deletions crypt/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crypt
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/goravel/framework/foundation/json"
Expand All @@ -17,8 +18,12 @@ type AesTestSuite struct {
func TestAesTestSuite(t *testing.T) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes, err := NewAES(mockConfig, json.NewJson())

assert.NoError(t, err)

suite.Run(t, &AesTestSuite{
aes: NewAES(mockConfig, json.NewJson()),
aes: aes,
})
mockConfig.AssertExpectations(t)
}
Expand Down Expand Up @@ -61,7 +66,11 @@ func (s *AesTestSuite) TestDecryptString() {
func Benchmark_EncryptString(b *testing.B) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes := NewAES(mockConfig, json.NewJson())
aes, err := NewAES(mockConfig, json.NewJson())
if err != nil {
b.Fatal(err)
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -75,7 +84,11 @@ func Benchmark_EncryptString(b *testing.B) {
func Benchmark_DecryptString(b *testing.B) {
mockConfig := &configmock.Config{}
mockConfig.On("GetString", "app.key").Return("11111111111111111111111111111111").Once()
aes := NewAES(mockConfig, json.NewJson())
aes, err := NewAES(mockConfig, json.NewJson())
if err != nil {
b.Fatal(err)
}

payload, err := aes.EncryptString("Goravel")
if err != nil {
b.Error(err)
Expand Down
12 changes: 12 additions & 0 deletions crypt/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package crypt

import (
"errors"
)

var (
ErrConfigNotSet = errors.New("config must not be nil")
ErrJsonParserNotSet = errors.New("JSON parser must not be nil")
ErrAppKeyNotSetInArtisan = errors.New("APP_KEY is required in artisan environment")
ErrInvalidAppKeyLength = errors.New("invalid APP_KEY length")
)
12 changes: 11 additions & 1 deletion crypt/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@

func (crypt *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
return NewAES(app.MakeConfig(), app.GetJson()), nil
c := app.MakeConfig()
if c == nil {
return nil, ErrConfigNotSet

Check warning on line 16 in crypt/service_provider.go

View check run for this annotation

Codecov / codecov/patch

crypt/service_provider.go#L14-L16

Added lines #L14 - L16 were not covered by tests
}

j := app.GetJson()
if j == nil {
return nil, ErrJsonParserNotSet

Check warning on line 21 in crypt/service_provider.go

View check run for this annotation

Codecov / codecov/patch

crypt/service_provider.go#L19-L21

Added lines #L19 - L21 were not covered by tests
}

return NewAES(c, j)

Check warning on line 24 in crypt/service_provider.go

View check run for this annotation

Codecov / codecov/patch

crypt/service_provider.go#L24

Added line #L24 was not covered by tests
})
}

Expand Down
12 changes: 8 additions & 4 deletions database/gorm/dialector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func (s *DialectorTestSuite) TestMysql() {
s.mockConfig.On("GetString", "database.connections.mysql.loc").
Return("Local").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(mysql.New(mysql.Config{
DSN: fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=%t&loc=%s&multiStatements=true",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "utf8mb4", true, "Local"),
}), dialectors[0])
s.Nil(err)
}

func (s *DialectorTestSuite) TestPostgres() {
Expand All @@ -62,20 +63,22 @@ func (s *DialectorTestSuite) TestPostgres() {
s.mockConfig.On("GetString", "database.connections.postgres.timezone").
Return("UTC").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(postgres.New(postgres.Config{
DSN: fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "disable", "UTC"),
}), dialectors[0])
s.Nil(err)
}

func (s *DialectorTestSuite) TestSqlite() {
dialector := NewDialectorImpl(s.mockConfig, orm.DriverSqlite.String())
s.mockConfig.On("GetString", "database.connections.sqlite.driver").
Return(orm.DriverSqlite.String()).Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Equal(sqlite.Open(fmt.Sprintf("%s?multi_stmts=true", s.config.Database)), dialectors[0])
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(sqlite.Open(fmt.Sprintf("%s?multi_stmts=true", s.config.Database)), dialectors[0])
}

func (s *DialectorTestSuite) TestSqlserver() {
Expand All @@ -85,9 +88,10 @@ func (s *DialectorTestSuite) TestSqlserver() {
s.mockConfig.On("GetString", "database.connections.sqlserver.charset").
Return("utf8mb4").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Nil(err)
s.NotEmpty(dialectors)
s.Equal(sqlserver.New(sqlserver.Config{
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s&charset=%s&MultipleActiveResultSets=true",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "utf8mb4"),
}), dialectors[0])
s.Nil(err)
}
4 changes: 4 additions & 0 deletions database/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
return nil, fmt.Errorf("init gorm dialector error: %v", err)
}

if len(writeDialectors) == 0 {
return nil, errors.New("no write dialectors found")

Check warning on line 56 in database/gorm/gorm.go

View check run for this annotation

Codecov / codecov/patch

database/gorm/gorm.go#L56

Added line #L56 was not covered by tests
}

if err := r.init(writeDialectors[0]); err != nil {
kkumar-gcc marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
Expand Down
35 changes: 18 additions & 17 deletions database/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,22 @@
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
config := app.MakeConfig()
seeder := app.MakeSeeder()
artisan := app.MakeArtisan()
app.MakeArtisan().Register([]consolecontract.Command{
console.NewMigrateMakeCommand(config),
console.NewMigrateCommand(config),
console.NewMigrateRollbackCommand(config),
console.NewMigrateResetCommand(config),
console.NewMigrateRefreshCommand(config, artisan),
console.NewMigrateFreshCommand(config, artisan),
console.NewMigrateStatusCommand(config),
console.NewModelMakeCommand(),
console.NewObserverMakeCommand(),
console.NewSeedCommand(config, seeder),
console.NewSeederMakeCommand(),
console.NewFactoryMakeCommand(),
})
if artisanFacade := app.MakeArtisan(); artisanFacade != nil {
config := app.MakeConfig()
seeder := app.MakeSeeder()
artisanFacade.Register([]consolecontract.Command{
console.NewMigrateMakeCommand(config),
console.NewMigrateCommand(config),
console.NewMigrateRollbackCommand(config),
console.NewMigrateResetCommand(config),
console.NewMigrateRefreshCommand(config, artisanFacade),
console.NewMigrateFreshCommand(config, artisanFacade),
console.NewMigrateStatusCommand(config),
console.NewModelMakeCommand(),
console.NewObserverMakeCommand(),
console.NewSeedCommand(config, seeder),
console.NewSeederMakeCommand(),
console.NewFactoryMakeCommand(),
})

Check warning on line 70 in database/service_provider.go

View check run for this annotation

Codecov / codecov/patch

database/service_provider.go#L54-L70

Added lines #L54 - L70 were not covered by tests
}
}
2 changes: 1 addition & 1 deletion event/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}

func (receiver *ServiceProvider) registerCommands(app foundation.Application) {
app.MakeArtisan().Register([]console.Command{
app.Commands([]console.Command{

Check warning on line 25 in event/service_provider.go

View check run for this annotation

Codecov / codecov/patch

event/service_provider.go#L25

Added line #L25 was not covered by tests
&eventConsole.EventMakeCommand{},
&eventConsole.ListenerMakeCommand{},
})
Expand Down
6 changes: 5 additions & 1 deletion facades/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ import (
)

func App() foundationcontract.Application {
return foundation.App
if foundation.App == nil {
panic(ErrApplicationNotSet)
} else {
return foundation.App
}
}
7 changes: 7 additions & 0 deletions facades/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package facades

import "errors"

var (
ErrApplicationNotSet = errors.New("application instance not initialized")
)
7 changes: 7 additions & 0 deletions filesystem/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package filesystem

import "errors"

var (
ErrStorageFacadeNotSet = errors.New("storage facade not set")
)
Loading
Loading