Reduce indentation and long lines in test

This commit is contained in:
William Elwood 2019-11-06 04:37:29 +00:00 committed by Frank Denis
parent 0aea5f81ef
commit 77a4a3da90
1 changed files with 89 additions and 70 deletions

View File

@ -84,13 +84,14 @@ func writeSourceCache(t *testing.T, basePath string, fixtures []SourceFixture) {
if err := ioutil.WriteFile(path, f.content, perms); err != nil { if err := ioutil.WriteFile(path, f.content, perms); err != nil {
t.Fatalf("Unable to write cache file %s: %v", path, err) t.Fatalf("Unable to write cache file %s: %v", path, err)
} }
if !f.mtime.IsZero() { if f.mtime.IsZero() {
continue
}
if err := os.Chtimes(path, f.mtime, f.mtime); err != nil { if err := os.Chtimes(path, f.mtime, f.mtime); err != nil {
t.Fatalf("Unable to touch cache file %s to %v: %v", path, f.mtime, err) t.Fatalf("Unable to touch cache file %s to %v: %v", path, f.mtime, err)
} }
} }
} }
}
func checkSourceCache(c *check.C, basePath string, fixtures []SourceFixture) { func checkSourceCache(c *check.C, basePath string, fixtures []SourceFixture) {
for _, f := range fixtures { for _, f := range fixtures {
@ -122,22 +123,15 @@ func loadTestSourceNames(t *testing.T, d *SourceTestData) {
} }
} }
func loadFixtures(t *testing.T, d *SourceTestData) { func generateFixtureState(t *testing.T, d *SourceTestData, suffix, file string, state SourceTestState) {
d.fixtures = map[SourceTestState]map[string]SourceFixture{TestStateCorrect: map[string]SourceFixture{}}
for _, source := range d.sources {
for _, suffix := range [...]string{"", ".minisig"} {
file := source + suffix
d.fixtures[TestStateCorrect][file] = SourceFixture{suffix: suffix, content: readFixture(t, filepath.Join("sources", file)), mtime: d.timeNow}
for _, state := range [...]SourceTestState{TestStateExpired, TestStatePartial, TestStateReadErr, TestStateOpenErr,
TestStatePartialSig, TestStateMissingSig, TestStateReadSigErr, TestStateOpenSigErr} {
if _, ok := d.fixtures[state]; !ok { if _, ok := d.fixtures[state]; !ok {
d.fixtures[state] = map[string]SourceFixture{} d.fixtures[state] = map[string]SourceFixture{}
} }
if suffix != ".minisig" {
switch state { switch state {
case TestStatePartialSig, TestStateMissingSig, TestStateReadSigErr, TestStateOpenSigErr: case TestStatePartialSig, TestStateMissingSig, TestStateReadSigErr, TestStateOpenSigErr:
if suffix != ".minisig" {
d.fixtures[state][file] = d.fixtures[TestStateCorrect][file] d.fixtures[state][file] = d.fixtures[TestStateCorrect][file]
continue return
} }
} }
f := SourceFixture{suffix: suffix, mtime: d.timeNow} f := SourceFixture{suffix: suffix, mtime: d.timeNow}
@ -153,6 +147,29 @@ func loadFixtures(t *testing.T, d *SourceTestData) {
} }
d.fixtures[state][file] = f d.fixtures[state][file] = f
} }
func loadFixtures(t *testing.T, d *SourceTestData) {
d.fixtures = map[SourceTestState]map[string]SourceFixture{TestStateCorrect: map[string]SourceFixture{}}
for _, source := range d.sources {
for _, suffix := range [...]string{"", ".minisig"} {
file := source + suffix
d.fixtures[TestStateCorrect][file] = SourceFixture{
suffix: suffix,
content: readFixture(t, filepath.Join("sources", file)),
mtime: d.timeNow,
}
for _, state := range [...]SourceTestState{
TestStateExpired,
TestStatePartial,
TestStateReadErr,
TestStateOpenErr,
TestStatePartialSig,
TestStateMissingSig,
TestStateReadSigErr,
TestStateOpenSigErr,
} {
generateFixtureState(t, d, suffix, file, state)
}
} }
} }
} }
@ -261,22 +278,11 @@ func prepSourceTestCache(t *testing.T, d *SourceTestData, e *SourceTestExpect, s
} }
func prepSourceTestDownload(t *testing.T, d *SourceTestData, e *SourceTestExpect, source string, downloadTest []SourceTestState) { func prepSourceTestDownload(t *testing.T, d *SourceTestData, e *SourceTestExpect, source string, downloadTest []SourceTestState) {
if len(downloadTest) > 0 { if len(downloadTest) == 0 {
return
}
for _, state := range downloadTest { for _, state := range downloadTest {
path := "/" + strconv.FormatUint(uint64(state), 10) + "/" + source path := "/" + strconv.FormatUint(uint64(state), 10) + "/" + source
if !e.success {
switch state {
case TestStateCorrect:
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
e.Source.in, e.success = e.cache[0].content, true
fallthrough
case TestStateMissingSig, TestStatePartial, TestStatePartialSig, TestStateReadSigErr:
d.reqExpect[path+".minisig"]++
fallthrough
case TestStateMissing, TestStateReadErr:
d.reqExpect[path]++
}
}
switch state { switch state {
case TestStateMissing, TestStateMissingSig: case TestStateMissing, TestStateMissingSig:
e.err = "404 Not Found" e.err = "404 Not Found"
@ -294,6 +300,20 @@ func prepSourceTestDownload(t *testing.T, d *SourceTestData, e *SourceTestExpect
e.Source.urls = append(e.Source.urls, u) e.Source.urls = append(e.Source.urls, u)
} }
e.urls = append(e.urls, d.server.URL+path) e.urls = append(e.urls, d.server.URL+path)
if e.success {
continue
}
switch state {
case TestStateCorrect:
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
e.Source.in, e.success = e.cache[0].content, true
fallthrough
case TestStateMissingSig, TestStatePartial, TestStatePartialSig, TestStateReadSigErr:
d.reqExpect[path+".minisig"]++
fallthrough
case TestStateMissing, TestStateReadErr:
d.reqExpect[path]++
}
} }
if e.success { if e.success {
e.err = "" e.err = ""
@ -307,7 +327,6 @@ func prepSourceTestDownload(t *testing.T, d *SourceTestData, e *SourceTestExpect
e.success = false e.success = false
} }
} }
}
func setupSourceTestCase(t *testing.T, d *SourceTestData, i int, func setupSourceTestCase(t *testing.T, d *SourceTestData, i int,
cacheTest *SourceTestState, downloadTest []SourceTestState) (id string, e *SourceTestExpect) { cacheTest *SourceTestState, downloadTest []SourceTestState) (id string, e *SourceTestExpect) {