Skip to content

Commit

Permalink
Rename configuration module to config
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed May 21, 2023
1 parent 2267b71 commit 273475d
Show file tree
Hide file tree
Showing 31 changed files with 206 additions and 206 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package configuration
package config

import (
"fmt"

"github.com/evg4b/uncors/internal/configuration/hooks"
"github.com/evg4b/uncors/internal/config/hooks"
"github.com/mitchellh/mapstructure"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -61,7 +61,7 @@ func LoadConfiguration(viperInstance *viper.Viper, args []string) (*UncorsConfig
))

if err := viperInstance.Unmarshal(configuration, configOption); err != nil {
return nil, fmt.Errorf("filed parsing configuration: %w", err)
return nil, fmt.Errorf("filed parsing config: %w", err)
}

if err := readURLMapping(viperInstance, configuration); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package configuration_test
package config_test

import (
"testing"

"github.com/evg4b/uncors/internal/configuration"
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testutils"
"github.com/evg4b/uncors/testing/testutils/params"
Expand All @@ -16,40 +16,40 @@ func TestLoadConfiguration(t *testing.T) {
viperInstance := viper.New()
viperInstance.SetFs(fs)

t.Run("correctly parse configuration", func(t *testing.T) {
t.Run("correctly parse config", func(t *testing.T) {
tests := []struct {
name string
args []string
expected *configuration.UncorsConfig
expected *config.UncorsConfig
}{
{
name: "return default config",
args: []string{},
expected: &configuration.UncorsConfig{
expected: &config.UncorsConfig{
HTTPPort: 80,
HTTPSPort: 443,
Mappings: []configuration.URLMapping{},
Mocks: []configuration.Mock{},
Mappings: []config.URLMapping{},
Mocks: []config.Mock{},
},
},
{
name: "minimal config is set",
args: []string{params.Config, "/minimal-config.yaml"},
expected: &configuration.UncorsConfig{
expected: &config.UncorsConfig{
HTTPPort: 8080,
HTTPSPort: 443,
Mappings: []configuration.URLMapping{
Mappings: []config.URLMapping{
{From: "http://demo", To: "https://demo.com"},
},
Mocks: []configuration.Mock{},
Mocks: []config.Mock{},
},
},
{
name: "read all fields from config file config is set",
args: []string{params.Config, "/full-config.yaml"},
expected: &configuration.UncorsConfig{
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: []configuration.URLMapping{
Mappings: []config.URLMapping{
{From: "http://demo1", To: "https://demo1.com"},
{From: "http://other-demo2", To: "https://demo2.io"},
},
Expand All @@ -58,7 +58,7 @@ func TestLoadConfiguration(t *testing.T) {
HTTPSPort: 8081,
CertFile: "/cert-file.pem",
KeyFile: "/key-file.key",
Mocks: []configuration.Mock{
Mocks: []config.Mock{
{
Path: "/demo",
Method: "POST",
Expand All @@ -68,7 +68,7 @@ func TestLoadConfiguration(t *testing.T) {
Headers: map[string]string{
"accept-encoding": "deflate",
},
Response: configuration.Response{
Response: config.Response{
Code: 201,
Headers: map[string]string{
"accept-encoding": "deflate",
Expand All @@ -88,9 +88,9 @@ func TestLoadConfiguration(t *testing.T) {
params.From, mocks.SourceHost2, params.To, mocks.TargetHost2,
params.From, mocks.SourceHost3, params.To, mocks.TargetHost3,
},
expected: &configuration.UncorsConfig{
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: []configuration.URLMapping{
Mappings: []config.URLMapping{
{From: "http://demo1", To: "https://demo1.com"},
{From: "http://other-demo2", To: "https://demo2.io"},
{From: mocks.SourceHost1, To: mocks.TargetHost1},
Expand All @@ -102,7 +102,7 @@ func TestLoadConfiguration(t *testing.T) {
HTTPSPort: 8081,
CertFile: "/cert-file.pem",
KeyFile: "/key-file.key",
Mocks: []configuration.Mock{
Mocks: []config.Mock{
{
Path: "/demo",
Method: "POST",
Expand All @@ -112,7 +112,7 @@ func TestLoadConfiguration(t *testing.T) {
Headers: map[string]string{
"accept-encoding": "deflate",
},
Response: configuration.Response{
Response: config.Response{
Code: 201,
Headers: map[string]string{
"accept-encoding": "deflate",
Expand All @@ -127,15 +127,15 @@ func TestLoadConfiguration(t *testing.T) {
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
config, err := configuration.LoadConfiguration(viperInstance, testCase.args)
config, err := config.LoadConfiguration(viperInstance, testCase.args)

assert.NoError(t, err)
assert.Equal(t, testCase.expected, config)
})
}
})

t.Run("parse configuration with error", func(t *testing.T) {
t.Run("parse config with error", func(t *testing.T) {
tests := []struct {
name string
args []string
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestLoadConfiguration(t *testing.T) {
},
},
{
name: "configuration file doesn't exist",
name: "config file doesn't exist",
args: []string{
params.Config, "/not-exist-config.yaml",
},
Expand All @@ -190,7 +190,7 @@ func TestLoadConfiguration(t *testing.T) {
},
},
{
name: "configuration file is corrupted",
name: "config file is corrupted",
args: []string{
params.Config, "/corrupted-config.yaml",
},
Expand All @@ -215,14 +215,14 @@ func TestLoadConfiguration(t *testing.T) {
params.Config, "/incorrect-config.yaml",
},
expected: []string{
"filed parsing configuration: 1 error(s) decoding:\n\n* cannot parse 'http-port' as int:" +
"filed parsing config: 1 error(s) decoding:\n\n* cannot parse 'http-port' as int:" +
" strconv.ParseInt: parsing \"xxx\": invalid syntax",
},
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
config, err := configuration.LoadConfiguration(viperInstance, testCase.args)
config, err := config.LoadConfiguration(viperInstance, testCase.args)

assert.Nil(t, config)
for _, expected := range testCase.expected {
Expand All @@ -236,17 +236,17 @@ func TestLoadConfiguration(t *testing.T) {
func TestUncorsConfigIsHTTPSEnabled(t *testing.T) {
tests := []struct {
name string
config *configuration.UncorsConfig
config *config.UncorsConfig
expected bool
}{
{
name: "false by default",
config: &configuration.UncorsConfig{},
config: &config.UncorsConfig{},
expected: false,
},
{
name: "false by default",
config: &configuration.UncorsConfig{},
config: &config.UncorsConfig{},
expected: false,
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configuration
package config

import (
"errors"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

"github.com/evg4b/uncors/internal/configuration/hooks"
"github.com/evg4b/uncors/internal/config/hooks"
"github.com/evg4b/uncors/testing/testutils"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configuration
package config

import (
"time"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configuration
package config

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package configuration_test
package config_test

import (
"testing"

"github.com/evg4b/uncors/internal/configuration"
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/testing/testutils"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand All @@ -19,13 +19,13 @@ const (
func TestStaticDirMappingHookFunc(t *testing.T) {
const configFile = "config.yaml"
type testType struct {
Statics configuration.StaticDirMappings `mapstructure:"statics"`
Statics config.StaticDirMappings `mapstructure:"statics"`
}

tests := []struct {
name string
config string
expected configuration.StaticDirMappings
expected config.StaticDirMappings
}{
{
name: "decode plan mapping",
Expand All @@ -34,7 +34,7 @@ statics:
/path: /static-dir
/another-path: /another-static-dir
`,
expected: configuration.StaticDirMappings{
expected: config.StaticDirMappings{
{Path: anotherPath, Dir: anotherStaticDir},
{Path: path, Dir: staticDir},
},
Expand All @@ -46,7 +46,7 @@ statics:
/path: { dir: /static-dir }
/another-path: { dir: /another-static-dir }
`,
expected: configuration.StaticDirMappings{
expected: config.StaticDirMappings{
{Path: path, Dir: staticDir},
{Path: anotherPath, Dir: anotherStaticDir},
},
Expand All @@ -58,7 +58,7 @@ statics:
/path: { dir: /static-dir, index: index.html }
/another-path: { dir: /another-static-dir, index: default.html }
`,
expected: configuration.StaticDirMappings{
expected: config.StaticDirMappings{
{Path: path, Dir: staticDir, Index: "index.html"},
{Path: anotherPath, Dir: anotherStaticDir, Index: "default.html"},
},
Expand All @@ -70,7 +70,7 @@ statics:
/path: { dir: /static-dir, index: index.html }
/another-path: /another-static-dir
`,
expected: configuration.StaticDirMappings{
expected: config.StaticDirMappings{
{Path: path, Dir: staticDir, Index: "index.html"},
{Path: anotherPath, Dir: anotherStaticDir},
},
Expand All @@ -89,7 +89,7 @@ statics:
actual := testType{}

err = viperInstance.Unmarshal(&actual, viper.DecodeHook(
configuration.StaticDirMappingHookFunc(),
config.StaticDirMappingHookFunc(),
))
testutils.CheckNoError(t, err)

Expand All @@ -101,28 +101,28 @@ statics:
func TestStaticDirMappingClone(t *testing.T) {
tests := []struct {
name string
expected configuration.StaticDirMapping
expected config.StaticDirMapping
}{
{
name: "empty structure",
expected: configuration.StaticDirMapping{},
expected: config.StaticDirMapping{},
},
{
name: "structure with 1 field",
expected: configuration.StaticDirMapping{
expected: config.StaticDirMapping{
Dir: "dir",
},
},
{
name: "structure with 2 field",
expected: configuration.StaticDirMapping{
expected: config.StaticDirMapping{
Dir: "dir",
Path: "/some-path",
},
},
{
name: "structure with all field",
expected: configuration.StaticDirMapping{
expected: config.StaticDirMapping{
Dir: "dir",
Path: "/one-more-path",
Index: "index.html",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package configuration
package config

import (
"reflect"
Expand Down
Loading

0 comments on commit 273475d

Please sign in to comment.