|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestWhydeadcode(t *testing.T) { |
| 13 | + for _, c := range []struct { |
| 14 | + fixtureName string |
| 15 | + tgt Path |
| 16 | + }{ |
| 17 | + {"valuemethod", Path{"main.F", "main.main"}}, |
| 18 | + {"typemethod", Path{"main.F", "main.main"}}, |
| 19 | + {"reachmeth", Path{"github.com/aarzilli/whydeadcode/_fixtures/pkg1.(*Astruct).ReflectMethodByName", "github.com/aarzilli/whydeadcode/_fixtures/pkg1.(*Astruct).ReflectMethodByName·f", "main.f", "main.main"}}, |
| 20 | + } { |
| 21 | + t.Run(c.fixtureName, func(t *testing.T) { |
| 22 | + paths, _ := Whydeadcode(buildFixture(t, c.fixtureName)) |
| 23 | + t.Logf("%q -> %q", c.fixtureName, paths[0]) |
| 24 | + if len(paths[0]) < len(c.tgt) { |
| 25 | + t.Error("output path not long enough") |
| 26 | + } |
| 27 | + for i := range c.tgt { |
| 28 | + if c.tgt[i] != paths[0][i] { |
| 29 | + t.Errorf("mismatch at index %d (expected %q got %q)", i, c.tgt[i], paths[0][i]) |
| 30 | + break |
| 31 | + } |
| 32 | + } |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func buildFixture(t *testing.T, name string) io.Reader { |
| 38 | + t.Helper() |
| 39 | + cmd := exec.Command("go", "build", "-o", "_debug", "-ldflags=-dumpdep", filepath.Join("_fixtures", name)+".go") |
| 40 | + out, err := cmd.CombinedOutput() |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("compilation failed for %q: %v", name, err) |
| 43 | + } |
| 44 | + os.Remove("_debug") |
| 45 | + return bytes.NewReader(out) |
| 46 | +} |
0 commit comments