pkg/serverinit: fix tests for windows

Fixes: http://camlistore.org/issue/304

Change-Id: Ieb29ffefd1daf8d62704374d8e1eecd9cfea3156
This commit is contained in:
mpl 2014-07-31 20:50:54 +02:00
parent b3637337be
commit 044ac54a20
1 changed files with 32 additions and 1 deletions

View File

@ -26,6 +26,8 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime"
"sort" "sort"
"strings" "strings"
"testing" "testing"
@ -101,7 +103,9 @@ type namedReadSeeker struct {
func (n namedReadSeeker) Name() string { return n.name } func (n namedReadSeeker) Name() string { return n.name }
func (n namedReadSeeker) Close() error { return nil } func (n namedReadSeeker) Close() error { return nil }
// configParser returns a custom jsonconfig ConfigParser whose reader rewrites "/path/to/secring" to the absolute path of the jsonconfig test-secring.gpg file. // configParser returns a custom jsonconfig ConfigParser whose reader rewrites
// "/path/to/secring" to the absolute path of the jsonconfig test-secring.gpg file.
// On windows, it also fixes the slash separated paths.
func configParser() *jsonconfig.ConfigParser { func configParser() *jsonconfig.ConfigParser {
return &jsonconfig.ConfigParser{ return &jsonconfig.ConfigParser{
Open: func(path string) (jsonconfig.File, error) { Open: func(path string) (jsonconfig.File, error) {
@ -109,6 +113,7 @@ func configParser() *jsonconfig.ConfigParser {
if err != nil { if err != nil {
return nil, err return nil, err
} }
slurp = backslashEscape(slurp)
return namedReadSeeker{path, bytes.NewReader(slurp)}, nil return namedReadSeeker{path, bytes.NewReader(slurp)}, nil
}, },
} }
@ -120,13 +125,38 @@ func replaceRingPath(path string) ([]byte, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("Could not get absolute path of %v: %v", relativeRing, err) return nil, fmt.Errorf("Could not get absolute path of %v: %v", relativeRing, err)
} }
secRing = strings.Replace(secRing, `\`, `\\`, -1)
slurpBytes, err := ioutil.ReadFile(path) slurpBytes, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return bytes.Replace(slurpBytes, []byte(secringPlaceholder), []byte(secRing), 1), nil return bytes.Replace(slurpBytes, []byte(secringPlaceholder), []byte(secRing), 1), nil
} }
// We just need to make sure that we don't match the prefix handlers too.
var unixPathPattern = regexp.MustCompile(`"/.*/.+"`)
// backslashEscape, on windows, changes all the slash separated paths (which
// match unixPathPattern, to omit the prefix handler paths) with escaped
// backslashes.
func backslashEscape(b []byte) []byte {
if runtime.GOOS != "windows" {
return b
}
unixPaths := unixPathPattern.FindAll(b, -1)
if unixPaths == nil {
return b
}
var oldNew []string
for _, v := range unixPaths {
bStr := string(v)
oldNew = append(oldNew, bStr, strings.Replace(bStr, `/`, `\\`, -1))
}
r := strings.NewReplacer(oldNew...)
return []byte(r.Replace(string(b)))
}
func testConfig(name string, t *testing.T) { func testConfig(name string, t *testing.T) {
wantedError := func() error { wantedError := func() error {
slurp, err := ioutil.ReadFile(strings.Replace(name, ".json", ".err", 1)) slurp, err := ioutil.ReadFile(strings.Replace(name, ".json", ".err", 1))
@ -142,6 +172,7 @@ func testConfig(name string, t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not read %s: %v", name, err) t.Fatalf("Could not read %s: %v", name, err)
} }
b = backslashEscape(b)
var hiLevelConf serverconfig.Config var hiLevelConf serverconfig.Config
if err := json.Unmarshal(b, &hiLevelConf); err != nil { if err := json.Unmarshal(b, &hiLevelConf); err != nil {
t.Fatalf("Could not unmarshal %s into a serverconfig.Config: %v", name, err) t.Fatalf("Could not unmarshal %s into a serverconfig.Config: %v", name, err)