Skip to content

Commit

Permalink
feat: Review architecture and add tests (#12)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DblK authored Jan 2, 2022
1 parent 76713da commit 6a7ffe0
Show file tree
Hide file tree
Showing 34 changed files with 1,580 additions and 321 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ linters:
# - wsl
- exportloopref
- golint
- gochecknoglobals
disable:
- noctx
- scopelint
- gochecknoglobals
- errorlint

disable-all: false
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ Dead simple, thanks to Golang!

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.

## What to launch tests?

You can run `ginkgo -r` for one shot or `ginkgo watch -r` during development.
# Roadmap

You can see the [roadmap here](https://github.com/DblK/tinshop/projects/1).
Expand Down
66 changes: 38 additions & 28 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ type File struct {
ShopProtocol string `mapstructure:"protocol"`
ShopPort int `mapstructure:"port"`
Debug debug `mapstructure:"debug"`
AllSources repository.Sources `mapstructure:"sources"`
AllSources repository.ConfigSources `mapstructure:"sources"`
Name string `mapstructure:"name"`
Security security `mapstructure:"security"`
CustomTitleDB map[string]repository.TitleDBEntry `mapstructure:"customTitledb"`
NSP nsp `mapsstructure:"nsp"`
shopTemplateData repository.ShopTemplate

allHooks []func(repository.Config)
beforeAllHooks []func(repository.Config)
}

var serverConfig File
var allHooks []func(repository.Config)
var beforeAllHooks []func(repository.Config)
// New returns a new configuration
func New() repository.Config {
return &File{}
}

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

viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Config file changed, update new configuration...")
configChange()
cfg.configChange()
})
viper.WatchConfig()

configChange()
cfg.configChange()
}

func configChange() {
func (cfg *File) configChange() {
// Call all before hooks
for _, hook := range beforeAllHooks {
hook(&serverConfig)
for _, hook := range cfg.beforeAllHooks {
hook(cfg)
}

serverConfig = loadAndCompute()
newConfig := loadAndCompute()
cfg.rootShop = newConfig.rootShop
cfg.ShopHost = newConfig.ShopHost
cfg.ShopProtocol = newConfig.ShopProtocol
cfg.ShopPort = newConfig.ShopPort
cfg.Debug = newConfig.Debug
cfg.AllSources = newConfig.AllSources
cfg.Name = newConfig.Name
cfg.Security = newConfig.Security
cfg.CustomTitleDB = newConfig.CustomTitleDB
cfg.NSP = newConfig.NSP
cfg.shopTemplateData = newConfig.shopTemplateData

// Call all hooks
for _, hook := range allHooks {
hook(&serverConfig)
for _, hook := range cfg.allHooks {
hook(cfg)
}
}

// GetConfig returns the current configuration
func GetConfig() repository.Config {
return &serverConfig
}

func loadAndCompute() File {
serverConfig = File{}
err := viper.Unmarshal(&serverConfig)
func loadAndCompute() *File {
var loadedConfig = &File{}
err := viper.Unmarshal(&loadedConfig)

if err != nil {
log.Fatalln(err)
}
ComputeDefaultValues(&serverConfig)
ComputeDefaultValues(loadedConfig)

return serverConfig
return loadedConfig
}

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

// AddHook Add hook function on change config
func AddHook(f func(repository.Config)) {
allHooks = append(allHooks, f)
func (cfg *File) AddHook(f func(repository.Config)) {
cfg.allHooks = append(cfg.allHooks, f)
}

// AddBeforeHook Add hook function before on change config
func AddBeforeHook(f func(repository.Config)) {
beforeAllHooks = append(beforeAllHooks, f)
func (cfg *File) AddBeforeHook(f func(repository.Config)) {
cfg.beforeAllHooks = append(cfg.beforeAllHooks, f)
}

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

// Sources returns all available sources
func (cfg *File) Sources() repository.Sources {
func (cfg *File) Sources() repository.ConfigSources {
return cfg.AllSources
}

Expand Down
2 changes: 1 addition & 1 deletion config/config_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config_test
import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down
13 changes: 9 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ package config_test

import (
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/DblK/tinshop/config"
"github.com/DblK/tinshop/mock_repository"
"github.com/DblK/tinshop/repository"
)

var _ = Describe("Config", func() {
var testConfig repository.Config
BeforeEach(func() {
testConfig = config.New()
})

It("Ensure to be able to set a RootShop", func() {
config.GetConfig().SetRootShop("http://tinshop.example.com")
cfg := config.GetConfig()
testConfig.SetRootShop("http://tinshop.example.com")

Expect(cfg.RootShop()).To(Equal("http://tinshop.example.com"))
Expect(testConfig.RootShop()).To(Equal("http://tinshop.example.com"))
})
Context("ComputeDefaultValues", func() {
var (
Expand Down
2 changes: 1 addition & 1 deletion gameid/gameid_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gameid_test
import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down
2 changes: 1 addition & 1 deletion gameid/gameid_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gameid_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/DblK/tinshop/gameid"
Expand Down
Loading

0 comments on commit 6a7ffe0

Please sign in to comment.