Skip to content
Permalink
Browse files

(setup): Add tests for config parser

  • Loading branch information...
erbesharat committed Sep 6, 2019
1 parent b9a55e8 commit 75266e7e1d356e4968fd6e1af876e884f5a66af7
Showing with 103 additions and 0 deletions.
  1. +103 −0 setup_test.go
@@ -0,0 +1,103 @@
package gomods

import (
"testing"

"github.com/caddyserver/caddy"
"github.com/spf13/afero"
)

func Test_parse(t *testing.T) {
type args struct {
c *caddy.Controller
}
tests := []struct {
configFile string
config Config
}{
{
`
gomods
`,
Config{
GoBinary: DefaultGoBinaryPath,
Workers: DefaultGomodsWorkers,
},
},
{
`
gomods {
cache
}
`,
Config{
GoBinary: DefaultGoBinaryPath,
Workers: DefaultGomodsWorkers,
Cache: Cache{
Enable: true,
Type: DefaultGomodsCacheType,
Path: "/tmp/txtdirect/gomods",
},
},
},
{
`
gomods {
gobinary /my/go/binary
cache {
type local
path /my/cache/path
}
}
`,
Config{
GoBinary: "/my/go/binary",
Workers: DefaultGomodsWorkers,
Cache: Cache{
Enable: true,
Type: "local",
Path: "/my/cache/path",
},
},
},
{
`
gomods {
gobinary /my/go/binary
cache {
type local
path /my/cache/path
}
workers 5
}
`,
Config{
GoBinary: "/my/go/binary",
Cache: Cache{
Enable: true,
Type: "local",
Path: "/my/cache/path",
},
Workers: 5,
},
},
}
for _, test := range tests {
c := caddy.NewTestController("http", test.configFile)
config, err := parse(c)
if err != nil {
t.Error(err)
}

// Fs field gets filled by default when parsing the config
test.config.Fs = config.Fs
// Set the default cache path for expected config if cache type is tmp
if config.Cache.Type == "tmp" {
test.config.Cache.Path = afero.GetTempDir(test.config.Fs, "")
}

if config != test.config {
t.Errorf("Expected config to be %+v, but got %+v", test.config, config)
}
}
}

0 comments on commit 75266e7

Please sign in to comment.
You can’t perform that action at this time.