-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(config): Add tests for the config parser
Signed-off-by: Erfan Besharat <[email protected]>
- Loading branch information
1 parent
f481bbc
commit e1485b0
Showing
3 changed files
with
119 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |