Skip to content

Commit

Permalink
(config): Add tests for the config parser
Browse files Browse the repository at this point in the history
Signed-off-by: Erfan Besharat <[email protected]>
  • Loading branch information
erbesharat committed Jan 20, 2020
1 parent f481bbc commit e1485b0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 102 deletions.
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Config struct {
GoBinary string `json:"gobinary,omitempty"`
Workers int `json:"workers,omitempty"`
Cache Cache `json:"cache,omitempty"`
Fs afero.Fs
fs afero.Fs
}

type Cache struct {
Expand All @@ -30,7 +30,7 @@ const (
// SetDefaults sets the default values for gomods config
// if the fields are empty
func (conf *Config) SetDefaults() {
conf.Fs = afero.NewOsFs()
conf.fs = afero.NewOsFs()
if conf.GoBinary == "" {
conf.GoBinary = DefaultGoBinaryPath
}
Expand All @@ -39,7 +39,7 @@ func (conf *Config) SetDefaults() {
conf.Cache.Type = DefaultGomodsCacheType
}
if conf.Cache.Path == "" {
conf.Cache.Path = afero.GetTempDir(conf.Fs, "")
conf.Cache.Path = afero.GetTempDir(conf.fs, "")
}
}
if conf.Workers == 0 {
Expand Down
4 changes: 2 additions & 2 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ModuleHandler interface {
}

func (m Module) fetch(r *http.Request, c Config) (download.Protocol, error) {
fetcher, err := module.NewGoGetFetcher(c.GoBinary, nil, c.Fs)
fetcher, err := module.NewGoGetFetcher(c.GoBinary, nil, c.fs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func (m Module) storage(c Config) (storage.Backend, error) {
}

func (m Module) dp(fetcher module.Fetcher, s storage.Backend, c Config) download.Protocol {
lister := module.NewVCSLister(c.GoBinary, nil, c.Fs)
lister := module.NewVCSLister(c.GoBinary, nil, c.fs)
st := stash.New(fetcher, s, stash.WithPool(c.Workers), stash.WithSingleflight)
dpOpts := &download.Opts{
Storage: s,
Expand Down
211 changes: 114 additions & 97 deletions setup_test.go
Original file line number Diff line number Diff line change
@@ -1,103 +1,120 @@
package gomods

// import (
// "testing"
import (
"bytes"
"testing"

// "github.com/caddyserver/caddy"
// "github.com/spf13/afero"
// )
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"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)
// }
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
workers 5
cache {
type local
path /my/cache/path
}
}
`,
Config{
GoBinary: "/my/go/binary",
Cache: Cache{
Enable: true,
Type: "local",
Path: "/my/cache/path",
},
Workers: 5,
},
},
}
for _, test := range tests {
buf := bytes.NewBuffer([]byte(test.configFile))
block, err := caddyfile.Parse("Caddyfile", buf)
if err != nil {
t.Errorf("Couldn't read the config: %s", err.Error())
}

// // 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, "")
// }
// Extract the config tokens from the server blocks
var tokens []caddyfile.Token
for _, segment := range block[0].Segments {
for _, token := range segment {
tokens = append(tokens, token)
}
}

// if config != test.config {
// t.Errorf("Expected config to be %+v, but got %+v", test.config, config)
// }
// }
// }
d := caddyfile.NewDispenser(tokens)
g := &Gomods{}

if err := g.UnmarshalCaddyfile(d); err != nil {
t.Errorf("Couldn't parse the config: %s", err.Error())
}

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

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

0 comments on commit e1485b0

Please sign in to comment.