Skip to content

Commit

Permalink
chore: linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Jun 13, 2024
1 parent 73d6ce7 commit 51327aa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ run:
skip-files:
- ".+_test.go"
- "corpx/faker.go"

issues:
exclude:
- "Set is deprecated: use context-based WithConfigValue instead"
- "SetDefaultIdentitySchemaFromRaw is deprecated: Use context-based WithDefaultIdentitySchemaFromRaw instead"
- "SetDefaultIdentitySchema is deprecated: Use context-based WithDefaultIdentitySchema instead"
33 changes: 15 additions & 18 deletions driver/config/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,42 @@ import (

"github.com/knadh/koanf/maps"

"github.com/knadh/koanf/v2"

"github.com/ory/kratos/embedx"
"github.com/ory/x/configx"
"github.com/ory/x/contextx"
)

type (
TestConfigProvider struct {
contextx.Contextualizer
Options []configx.OptionModifier
}
contextKey int
mapProvider map[string]any
)

func (t *TestConfigProvider) NewProvider(ctx context.Context, opts ...configx.OptionModifier) (*configx.Provider, error) {
return configx.New(ctx, []byte(embedx.ConfigSchema), append(t.Options, opts...)...)
}

func (t *TestConfigProvider) Config(ctx context.Context, config *configx.Provider) *configx.Provider {
config = t.Contextualizer.Config(ctx, config)
k := config.Copy()
if values, ok := ctx.Value(contextConfigKey).(mapProvider); ok && values != nil {
// our trusty provider never errors
_ = k.Load(values, nil)
values, ok := ctx.Value(contextConfigKey).(mapProvider)
if !ok {
return config
}
c := *config
c.Koanf = k
return &c
config, err := t.NewProvider(ctx, configx.WithValues(values))
if err != nil {
// This is not production code. The provider is only used in tests.
panic(err)

Check warning on line 39 in driver/config/test_config.go

View check run for this annotation

Codecov / codecov/patch

driver/config/test_config.go#L39

Added line #L39 was not covered by tests
}
return config
}

const contextConfigKey contextKey = 1

var (
_ contextx.Contextualizer = (*TestConfigProvider)(nil)
_ koanf.Provider = (*mapProvider)(nil)
)

func WithConfigValue(ctx context.Context, key string, value any) context.Context {
Expand Down Expand Up @@ -68,11 +73,3 @@ func WithConfigValues(ctx context.Context, newValues map[string]any) context.Con

return context.WithValue(ctx, contextConfigKey, values)
}

func (m mapProvider) ReadBytes() ([]byte, error) {
return nil, nil
}

func (m mapProvider) Read() (map[string]any, error) {
return m, nil
}
37 changes: 19 additions & 18 deletions internal/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,26 @@ func init() {
}

func NewConfigurationWithDefaults(t testing.TB, opts ...configx.OptionModifier) *config.Config {
configOpts := append([]configx.OptionModifier{
configx.WithValues(map[string]interface{}{
"log.level": "error",
config.ViperKeyDSN: dbal.NewSQLiteTestDatabase(t),
config.ViperKeyHasherArgon2ConfigMemory: 16384,
config.ViperKeyHasherArgon2ConfigIterations: 1,
config.ViperKeyHasherArgon2ConfigParallelism: 1,
config.ViperKeyHasherArgon2ConfigSaltLength: 16,
config.ViperKeyHasherBcryptCost: 4,
config.ViperKeyHasherArgon2ConfigKeyLength: 16,
config.ViperKeyCourierSMTPURL: "smtp://foo:[email protected]/",
config.ViperKeySelfServiceBrowserDefaultReturnTo: "https://www.ory.sh/redirect-not-set",
config.ViperKeySecretsCipher: []string{"secret-thirty-two-character-long"},
}),
configx.SkipValidation(),
}, opts...)
c := config.MustNew(t, logrusx.New("", ""),
os.Stderr,
&config.TestConfigProvider{&contextx.Default{}},
append([]configx.OptionModifier{
configx.WithValues(map[string]interface{}{
"log.level": "error",
config.ViperKeyDSN: dbal.NewSQLiteTestDatabase(t),
config.ViperKeyHasherArgon2ConfigMemory: 16384,
config.ViperKeyHasherArgon2ConfigIterations: 1,
config.ViperKeyHasherArgon2ConfigParallelism: 1,
config.ViperKeyHasherArgon2ConfigSaltLength: 16,
config.ViperKeyHasherBcryptCost: 4,
config.ViperKeyHasherArgon2ConfigKeyLength: 16,
config.ViperKeyCourierSMTPURL: "smtp://foo:[email protected]/",
config.ViperKeySelfServiceBrowserDefaultReturnTo: "https://www.ory.sh/redirect-not-set",
config.ViperKeySecretsCipher: []string{"secret-thirty-two-character-long"},
}),
configx.SkipValidation(),
}, opts...)...,
&config.TestConfigProvider{Contextualizer: &contextx.Default{}, Options: configOpts},
configOpts...,
)
return c
}
Expand Down Expand Up @@ -90,7 +91,7 @@ func NewRegistryDefaultWithDSN(t testing.TB, dsn string, opts ...configx.OptionM
require.NoError(t, err)
pool := jsonnetsecure.NewProcessPool(runtime.GOMAXPROCS(0))
t.Cleanup(pool.Close)
require.NoError(t, reg.Init(context.Background(), &config.TestConfigProvider{&contextx.Default{}}, driver.SkipNetworkInit, driver.WithDisabledMigrationLogging(), driver.WithJsonnetPool(pool)))
require.NoError(t, reg.Init(context.Background(), &config.TestConfigProvider{Contextualizer: &contextx.Default{}}, driver.SkipNetworkInit, driver.WithDisabledMigrationLogging(), driver.WithJsonnetPool(pool)))
require.NoError(t, reg.Persister().MigrateUp(context.Background())) // always migrate up

actual, err := reg.Persister().DetermineNetwork(context.Background())
Expand Down
6 changes: 4 additions & 2 deletions internal/testhelpers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"encoding/base64"
"testing"

"github.com/ory/kratos/driver/config"

"github.com/spf13/pflag"
"github.com/stretchr/testify/require"

"github.com/ory/kratos/driver/config"
"github.com/ory/x/configx"
"github.com/ory/x/randx"
)
Expand Down Expand Up @@ -78,6 +77,9 @@ func UseIdentitySchema(t *testing.T, conf *config.Config, url string) (id string
ID: id,
URL: url,
}))
t.Cleanup(func() {
conf.MustSet(context.Background(), config.ViperKeyIdentitySchemas, schemas)
})
return id
}

Expand Down
2 changes: 1 addition & 1 deletion persistence/sql/persister_hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var _ persisterDependencies = &logRegistryOnly{}

func TestPersisterHMAC(t *testing.T) {
ctx := context.Background()
conf := config.MustNew(t, logrusx.New("", ""), os.Stderr, &config.TestConfigProvider{&contextx.Default{}}, configx.SkipValidation())
conf := config.MustNew(t, logrusx.New("", ""), os.Stderr, &contextx.Default{}, configx.SkipValidation())
conf.MustSet(ctx, config.ViperKeySecretsDefault, []string{"foobarbaz"})
c, err := pop.NewConnection(&pop.ConnectionDetails{URL: "sqlite://foo?mode=memory"})
require.NoError(t, err)
Expand Down

0 comments on commit 51327aa

Please sign in to comment.