|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func TestBasicHashStaleness(t *testing.T) { |
| 16 | + defer func() { |
| 17 | + err := recover() |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("got an expected error: %v", err.(error)) |
| 20 | + } |
| 21 | + }() |
| 22 | + |
| 23 | + h := newHashTester(t) |
| 24 | + |
| 25 | + td := h.tempDir() |
| 26 | + defer os.RemoveAll(td) |
| 27 | + h.setEnv("GOPATH", td) |
| 28 | + h.dir = h.mkdir(td, "src", "example.com", "rubbish") |
| 29 | + h.mkdir(h.dir, "blah") |
| 30 | + h.writeFile("main.go", ` |
| 31 | + package main |
| 32 | + import "example.com/rubbish/blah" |
| 33 | + func main() { |
| 34 | + print(blah.Name) |
| 35 | + } |
| 36 | + `) |
| 37 | + h.writeFile(filepath.Join("blah", "blah.go"), ` |
| 38 | + package blah |
| 39 | + const Name = "blah" |
| 40 | + `) |
| 41 | + m := filepath.Join(td, "bin", "rubbish.js") |
| 42 | + a := filepath.Join(td, "pkg", fmt.Sprintf("%v_js", runtime.GOOS), "example.com", "rubbish", "blah.a") |
| 43 | + |
| 44 | + // variables to hold the current (c) and new (n) archive (a) and main (m) |
| 45 | + // os.FileInfos |
| 46 | + var ca, cm, na, nm os.FileInfo |
| 47 | + |
| 48 | + // at this point neither main nor archive should exist |
| 49 | + if h.statFile(m) != nil { |
| 50 | + t.Fatalf("main %v existed when it shouldn't have", m) |
| 51 | + } |
| 52 | + if h.statFile(a) != nil { |
| 53 | + t.Fatalf("archive %v existed when it shouldn't have", a) |
| 54 | + } |
| 55 | + |
| 56 | + h.run("gopherjs", "install", "example.com/rubbish") |
| 57 | + |
| 58 | + // now both main and the archive should exist |
| 59 | + ca = h.statFile(a) |
| 60 | + if ca == nil { |
| 61 | + t.Fatalf("archive %v should exist but doesn't", a) |
| 62 | + } |
| 63 | + cm = h.statFile(m) |
| 64 | + if cm == nil { |
| 65 | + t.Fatalf("main %v should exist but doesn't", a) |
| 66 | + } |
| 67 | + |
| 68 | + // re-running the install will cause main to be rewritten; not the package archive |
| 69 | + h.run("gopherjs", "install", "example.com/rubbish") |
| 70 | + nm = h.statFile(m) |
| 71 | + if !nm.ModTime().After(cm.ModTime()) { |
| 72 | + t.Fatalf("expected to see modified main file %v; got %v; prev %v", m, nm.ModTime(), cm.ModTime()) |
| 73 | + } |
| 74 | + cm = nm |
| 75 | + if na := h.statFile(a); !na.ModTime().Equal(ca.ModTime()) { |
| 76 | + t.Fatalf("expected not to see modified archive file %v; got %v; want %v", a, na.ModTime(), ca.ModTime()) |
| 77 | + } |
| 78 | + |
| 79 | + // touching the package file should have no effect on the archive |
| 80 | + h.touch(filepath.Join("blah", "blah.go")) |
| 81 | + h.run("gopherjs", "install", "example.com/rubbish/blah") // only install the package here |
| 82 | + if na := h.statFile(a); !na.ModTime().Equal(ca.ModTime()) { |
| 83 | + t.Fatalf("expected not to see modified archive file %v; got %v; want %v", a, na.ModTime(), ca.ModTime()) |
| 84 | + } |
| 85 | + |
| 86 | + // now update package file - should cause modification time change |
| 87 | + h.writeFile(filepath.Join("blah", "blah.go"), ` |
| 88 | + package blah |
| 89 | + const Name = "GopherJS" |
| 90 | + `) |
| 91 | + h.run("gopherjs", "install", "example.com/rubbish") |
| 92 | + na = h.statFile(a) |
| 93 | + if !na.ModTime().After(ca.ModTime()) { |
| 94 | + t.Fatalf("expected to see modified archive file %v; got %v; prev %v", a, na.ModTime(), ca.ModTime()) |
| 95 | + } |
| 96 | + ca = na |
| 97 | + |
| 98 | + // now change build tags - should cause modification time change |
| 99 | + h.run("gopherjs", "install", "--tags", "asdf", "example.com/rubbish") |
| 100 | + na = h.statFile(a) |
| 101 | + if !na.ModTime().After(ca.ModTime()) { |
| 102 | + t.Fatalf("expected to see modified archive file %v; got %v; prev %v", a, na.ModTime(), ca.ModTime()) |
| 103 | + } |
| 104 | + ca = na |
| 105 | +} |
| 106 | + |
| 107 | +type hashTester struct { |
| 108 | + t *testing.T |
| 109 | + dir string |
| 110 | + env []string |
| 111 | +} |
| 112 | + |
| 113 | +func newHashTester(t *testing.T) *hashTester { |
| 114 | + wd, err := os.Getwd() |
| 115 | + if err != nil { |
| 116 | + fatalf("run failed to get working directory: %v", err) |
| 117 | + } |
| 118 | + return &hashTester{ |
| 119 | + t: t, |
| 120 | + dir: wd, |
| 121 | + env: os.Environ(), |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func (h *hashTester) touch(path string) { |
| 126 | + path = filepath.Join(h.dir, path) |
| 127 | + now := time.Now().UTC() |
| 128 | + if err := os.Chtimes(path, now, now); err != nil { |
| 129 | + fatalf("failed to touch %v: %v", path, err) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func (h *hashTester) statFile(path string) os.FileInfo { |
| 134 | + fi, err := os.Stat(path) |
| 135 | + if err != nil { |
| 136 | + if os.IsNotExist(err) { |
| 137 | + return nil |
| 138 | + } |
| 139 | + fatalf("failed to stat %v: %v", path, err) |
| 140 | + } |
| 141 | + |
| 142 | + return fi |
| 143 | +} |
| 144 | + |
| 145 | +func (h *hashTester) setEnv(key, val string) { |
| 146 | + newEnv := []string{fmt.Sprintf("%v=%v", key, val)} |
| 147 | + for _, e := range h.env { |
| 148 | + if !strings.HasPrefix(e, key+"=") { |
| 149 | + newEnv = append(newEnv, e) |
| 150 | + } |
| 151 | + } |
| 152 | + h.env = newEnv |
| 153 | +} |
| 154 | + |
| 155 | +func (h *hashTester) mkdir(dirs ...string) string { |
| 156 | + d := filepath.Join(dirs...) |
| 157 | + if err := os.MkdirAll(d, 0755); err != nil { |
| 158 | + fatalf("failed to mkdir %v: %v\n", d, err) |
| 159 | + } |
| 160 | + return d |
| 161 | +} |
| 162 | + |
| 163 | +func (h *hashTester) writeFile(path, contents string) { |
| 164 | + path = filepath.Join(h.dir, path) |
| 165 | + if err := ioutil.WriteFile(path, []byte(contents), 0644); err != nil { |
| 166 | + fatalf("failed to write file %v: %v", path, err) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func (h *hashTester) tempDir() string { |
| 171 | + h.t.Helper() |
| 172 | + |
| 173 | + td, err := ioutil.TempDir("", "gopherjs_hashTester") |
| 174 | + if err != nil { |
| 175 | + fatalf("failed to create temp dir: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + return td |
| 179 | +} |
| 180 | + |
| 181 | +func (h *hashTester) run(c string, args ...string) { |
| 182 | + h.t.Helper() |
| 183 | + |
| 184 | + cmd := exec.Command(c, args...) |
| 185 | + cmd.Dir = h.dir |
| 186 | + cmd.Env = h.env |
| 187 | + |
| 188 | + out, err := cmd.CombinedOutput() |
| 189 | + if err != nil { |
| 190 | + fullCmd := append([]string{c}, args...) |
| 191 | + fatalf("failed to run %v: %v\n%v", strings.Join(fullCmd, " "), err, string(out)) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func fatalf(format string, args ...interface{}) { |
| 196 | + panic(fmt.Errorf(format, args...)) |
| 197 | +} |
0 commit comments