From d114648c1690bc972d2578fe389557c59d2ed35f Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 1 Mar 2015 12:31:13 +0100 Subject: [PATCH] fixup alterFiles --- test/util.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/test/util.go b/test/util.go index 5c941bc06..2f2cf345f 100644 --- a/test/util.go +++ b/test/util.go @@ -114,7 +114,7 @@ func alterFiles(dir string) error { return nil } - info, err = os.Stat(path); + info, err = os.Stat(path) if err != nil { // Something we deleted while walking. Ignore. return nil @@ -131,17 +131,24 @@ func alterFiles(dir string) error { return nil } - r := rand.Float64() + // File structure is base/x/xy/xyz12345... + // comps == 1: base (don't touch) + // comps == 2: base/x (must be dir) + // comps == 3: base/x/xy (must be dir) + // comps > 3: base/x/xy/xyz12345... (can be dir or file) + comps := len(strings.Split(path, string(os.PathSeparator))) + + r := rand.Intn(10) switch { - case r < 0.1 && comps > 2: + case r == 0 && comps > 2: // Delete every tenth file or directory, except top levels err := removeAll(path) if err != nil { return err } - case r < 0.2 && info.Mode().IsRegular(): + case r == 1 && info.Mode().IsRegular(): if info.Mode()&0200 != 0200 { // Not owner writable. Fix. err = os.Chmod(path, 0644) @@ -169,13 +176,14 @@ func alterFiles(dir string) error { if err != nil { return err } - case r < 0.3 && comps > 2 && rand.Float64() < 0.2: + + case r == 2 && comps > 3 && rand.Float64() < 0.2: if !info.Mode().IsRegular() { err = removeAll(path) if err != nil { return err } - d1 := []byte("I used to be a dir: "+path) + d1 := []byte("I used to be a dir: " + path) err := ioutil.WriteFile(path, d1, 0644) if err != nil { return err @@ -189,9 +197,10 @@ func alterFiles(dir string) error { if err != nil { return err } - generateFiles(path, 100, 20, "../LICENSE") + generateFiles(path, 10, 20, "../LICENSE") } - case r < 0.3 && comps > 1 && (info.Mode().IsRegular() || rand.Float64() < 0.2): + + case r == 3 && comps > 2 && (info.Mode().IsRegular() || rand.Float64() < 0.2): rpath := filepath.Dir(path) if rand.Float64() < 0.2 { for move := rand.Intn(comps - 1); move > 0; move-- { @@ -209,8 +218,7 @@ func alterFiles(dir string) error { return err } - // Create 100 new files - return generateFiles(dir, 100, 20, "../LICENSE") + return generateFiles(dir, 25, 20, "../LICENSE") } func ReadRand(bs []byte) (int, error) {