Skip to content

Commit 6a7ffe0

Browse files
authored
feat: Review architecture and add tests (#12)
* Change name to allow testing * Remove all local var from collection * Remove all local var from sources * Remove all local var from specific sources * Use parameters for config in sources * Review collection source and fix tests * Changes for config * Fix collection tests * doc: Add information about tests * Add some test for main package * Remove useless printed variables * Working test for HomeHandler! * Use mock for everything * Create a TinShop struct * Remove some globals * Remove latest global var * Handle duplicate in collection * Add test for world entry point * Update to ginkgo v2 * Use DescribeTable of ginkgo v2! * Finally first row of tests for middleware! * Cleaner use for Expect * Finished test for Tinfoil Middleware
1 parent 76713da commit 6a7ffe0

34 files changed

+1580
-321
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ linters:
4242
# - wsl
4343
- exportloopref
4444
- golint
45+
- gochecknoglobals
4546
disable:
4647
- noctx
4748
- scopelint
48-
- gochecknoglobals
4949
- errorlint
5050

5151
disable-all: false

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ Dead simple, thanks to Golang!
7777

7878
If you change an interface (or add a new one), do not forget to execute `./update_mocks.sh` to generate up-to-date mocks for tests.
7979

80+
## What to launch tests?
81+
82+
You can run `ginkgo -r` for one shot or `ginkgo watch -r` during development.
8083
# Roadmap
8184

8285
You can see the [roadmap here](https://github.com/DblK/tinshop/projects/1).

config/config.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,24 @@ type File struct {
4141
ShopProtocol string `mapstructure:"protocol"`
4242
ShopPort int `mapstructure:"port"`
4343
Debug debug `mapstructure:"debug"`
44-
AllSources repository.Sources `mapstructure:"sources"`
44+
AllSources repository.ConfigSources `mapstructure:"sources"`
4545
Name string `mapstructure:"name"`
4646
Security security `mapstructure:"security"`
4747
CustomTitleDB map[string]repository.TitleDBEntry `mapstructure:"customTitledb"`
4848
NSP nsp `mapsstructure:"nsp"`
4949
shopTemplateData repository.ShopTemplate
50+
51+
allHooks []func(repository.Config)
52+
beforeAllHooks []func(repository.Config)
5053
}
5154

52-
var serverConfig File
53-
var allHooks []func(repository.Config)
54-
var beforeAllHooks []func(repository.Config)
55+
// New returns a new configuration
56+
func New() repository.Config {
57+
return &File{}
58+
}
5559

5660
// LoadConfig handles viper under the hood
57-
func LoadConfig() {
61+
func (cfg *File) LoadConfig() {
5862
viper.SetConfigName("config") // name of config file (without extension)
5963
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
6064
viper.AddConfigPath(".") // optionally look for config in the working directory
@@ -72,42 +76,48 @@ func LoadConfig() {
7276

7377
viper.OnConfigChange(func(e fsnotify.Event) {
7478
log.Println("Config file changed, update new configuration...")
75-
configChange()
79+
cfg.configChange()
7680
})
7781
viper.WatchConfig()
7882

79-
configChange()
83+
cfg.configChange()
8084
}
8185

82-
func configChange() {
86+
func (cfg *File) configChange() {
8387
// Call all before hooks
84-
for _, hook := range beforeAllHooks {
85-
hook(&serverConfig)
88+
for _, hook := range cfg.beforeAllHooks {
89+
hook(cfg)
8690
}
8791

88-
serverConfig = loadAndCompute()
92+
newConfig := loadAndCompute()
93+
cfg.rootShop = newConfig.rootShop
94+
cfg.ShopHost = newConfig.ShopHost
95+
cfg.ShopProtocol = newConfig.ShopProtocol
96+
cfg.ShopPort = newConfig.ShopPort
97+
cfg.Debug = newConfig.Debug
98+
cfg.AllSources = newConfig.AllSources
99+
cfg.Name = newConfig.Name
100+
cfg.Security = newConfig.Security
101+
cfg.CustomTitleDB = newConfig.CustomTitleDB
102+
cfg.NSP = newConfig.NSP
103+
cfg.shopTemplateData = newConfig.shopTemplateData
89104

90105
// Call all hooks
91-
for _, hook := range allHooks {
92-
hook(&serverConfig)
106+
for _, hook := range cfg.allHooks {
107+
hook(cfg)
93108
}
94109
}
95110

96-
// GetConfig returns the current configuration
97-
func GetConfig() repository.Config {
98-
return &serverConfig
99-
}
100-
101-
func loadAndCompute() File {
102-
serverConfig = File{}
103-
err := viper.Unmarshal(&serverConfig)
111+
func loadAndCompute() *File {
112+
var loadedConfig = &File{}
113+
err := viper.Unmarshal(&loadedConfig)
104114

105115
if err != nil {
106116
log.Fatalln(err)
107117
}
108-
ComputeDefaultValues(&serverConfig)
118+
ComputeDefaultValues(loadedConfig)
109119

110-
return serverConfig
120+
return loadedConfig
111121
}
112122

113123
// ComputeDefaultValues change the value taken from the config file
@@ -153,13 +163,13 @@ func ComputeDefaultValues(config repository.Config) repository.Config {
153163
}
154164

155165
// AddHook Add hook function on change config
156-
func AddHook(f func(repository.Config)) {
157-
allHooks = append(allHooks, f)
166+
func (cfg *File) AddHook(f func(repository.Config)) {
167+
cfg.allHooks = append(cfg.allHooks, f)
158168
}
159169

160170
// AddBeforeHook Add hook function before on change config
161-
func AddBeforeHook(f func(repository.Config)) {
162-
beforeAllHooks = append(beforeAllHooks, f)
171+
func (cfg *File) AddBeforeHook(f func(repository.Config)) {
172+
cfg.beforeAllHooks = append(cfg.beforeAllHooks, f)
163173
}
164174

165175
// SetRootShop allow to change the root url of the shop
@@ -218,7 +228,7 @@ func (cfg *File) NfsShares() []string {
218228
}
219229

220230
// Sources returns all available sources
221-
func (cfg *File) Sources() repository.Sources {
231+
func (cfg *File) Sources() repository.ConfigSources {
222232
return cfg.AllSources
223233
}
224234

config/config_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package config_test
33
import (
44
"testing"
55

6-
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
88
)
99

config/config_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ package config_test
22

33
import (
44
"github.com/golang/mock/gomock"
5-
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/ginkgo/v2"
66
. "github.com/onsi/gomega"
77

88
"github.com/DblK/tinshop/config"
99
"github.com/DblK/tinshop/mock_repository"
10+
"github.com/DblK/tinshop/repository"
1011
)
1112

1213
var _ = Describe("Config", func() {
14+
var testConfig repository.Config
15+
BeforeEach(func() {
16+
testConfig = config.New()
17+
})
18+
1319
It("Ensure to be able to set a RootShop", func() {
14-
config.GetConfig().SetRootShop("http://tinshop.example.com")
15-
cfg := config.GetConfig()
20+
testConfig.SetRootShop("http://tinshop.example.com")
1621

17-
Expect(cfg.RootShop()).To(Equal("http://tinshop.example.com"))
22+
Expect(testConfig.RootShop()).To(Equal("http://tinshop.example.com"))
1823
})
1924
Context("ComputeDefaultValues", func() {
2025
var (

gameid/gameid_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package gameid_test
33
import (
44
"testing"
55

6-
. "github.com/onsi/ginkgo"
6+
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
88
)
99

gameid/gameid_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package gameid_test
22

33
import (
4-
. "github.com/onsi/ginkgo"
4+
. "github.com/onsi/ginkgo/v2"
55
. "github.com/onsi/gomega"
66

77
"github.com/DblK/tinshop/gameid"

0 commit comments

Comments
 (0)