Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 81d18c6

Browse files
committed
Expand TestToReachMaps with internal rm checks
1 parent afba47f commit 81d18c6

File tree

1 file changed

+176
-44
lines changed

1 file changed

+176
-44
lines changed

analysis_test.go

Lines changed: 176 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,19 +1459,33 @@ func TestToReachMaps(t *testing.T) {
14591459
t.Fatalf("ListPackages failed on varied test case: %s", err)
14601460
}
14611461

1462+
// Helper to add github.com/varied/example prefix
1463+
b := func(s string) string {
1464+
if s == "" {
1465+
return "github.com/example/varied"
1466+
}
1467+
return "github.com/example/varied/" + s
1468+
}
1469+
bl := func(parts ...string) string {
1470+
for k, s := range parts {
1471+
parts[k] = b(s)
1472+
}
1473+
return strings.Join(parts, " ")
1474+
}
1475+
14621476
// Set up vars for validate closure
1463-
var expect map[string][]string
1477+
var wantex, wantin map[string][]string
14641478
var name string
14651479
var main, tests bool
14661480
var ignore map[string]bool
14671481

14681482
validate := func() {
1469-
result, _ := vptree.ToReachMaps(main, tests, ignore)
1470-
if !reflect.DeepEqual(expect, result) {
1483+
gotex, gotin := vptree.ToReachMaps(main, tests, ignore)
1484+
if !reflect.DeepEqual(wantex, gotex) {
14711485
seen := make(map[string]bool)
1472-
for ip, epkgs := range expect {
1486+
for ip, epkgs := range wantex {
14731487
seen[ip] = true
1474-
if pkgs, exists := result[ip]; !exists {
1488+
if pkgs, exists := gotex[ip]; !exists {
14751489
t.Errorf("ver(%q): expected import path %s was not present in result", name, ip)
14761490
} else {
14771491
if !reflect.DeepEqual(pkgs, epkgs) {
@@ -1480,46 +1494,87 @@ func TestToReachMaps(t *testing.T) {
14801494
}
14811495
}
14821496

1483-
for ip, pkgs := range result {
1497+
for ip, pkgs := range gotex {
14841498
if seen[ip] {
14851499
continue
14861500
}
14871501
t.Errorf("ver(%q): Got packages for import path %s, but none were expected:\n\t%s", name, ip, pkgs)
14881502
}
14891503
}
1504+
1505+
if !reflect.DeepEqual(wantin, gotin) {
1506+
seen := make(map[string]bool)
1507+
for ip, epkgs := range wantin {
1508+
seen[ip] = true
1509+
if pkgs, exists := gotin[ip]; !exists {
1510+
t.Errorf("ver(%q): expected internal import path %s was not present in result", name, ip)
1511+
} else {
1512+
if !reflect.DeepEqual(pkgs, epkgs) {
1513+
t.Errorf("ver(%q): did not get expected internal package set for import path %s:\n\t(GOT): %s\n\t(WNT): %s", name, ip, pkgs, epkgs)
1514+
}
1515+
}
1516+
}
1517+
1518+
for ip, pkgs := range gotin {
1519+
if seen[ip] {
1520+
continue
1521+
}
1522+
t.Errorf("ver(%q): Got internal packages for import path %s, but none were expected:\n\t%s", name, ip, pkgs)
1523+
}
1524+
}
1525+
}
1526+
1527+
// maps of each internal package, and their expected external and internal
1528+
// imports in the maximal case.
1529+
allex := map[string][]string{
1530+
b(""): {"encoding/binary", "github.com/Masterminds/semver", "github.com/sdboyer/gps", "go/parser", "hash", "net/http", "os", "sort"},
1531+
b("m1p"): {"github.com/sdboyer/gps", "os", "sort"},
1532+
b("namemismatch"): {"github.com/Masterminds/semver", "os"},
1533+
b("otherpath"): {"github.com/sdboyer/gps", "os", "sort"},
1534+
b("simple"): {"encoding/binary", "github.com/sdboyer/gps", "go/parser", "hash", "os", "sort"},
1535+
b("simple/another"): {"encoding/binary", "github.com/sdboyer/gps", "hash", "os", "sort"},
14901536
}
14911537

1492-
all := map[string][]string{
1493-
"github.com/example/varied": {"encoding/binary", "github.com/Masterminds/semver", "github.com/sdboyer/gps", "go/parser", "hash", "net/http", "os", "sort"},
1494-
"github.com/example/varied/m1p": {"github.com/sdboyer/gps", "os", "sort"},
1495-
"github.com/example/varied/namemismatch": {"github.com/Masterminds/semver", "os"},
1496-
"github.com/example/varied/otherpath": {"github.com/sdboyer/gps", "os", "sort"},
1497-
"github.com/example/varied/simple": {"encoding/binary", "github.com/sdboyer/gps", "go/parser", "hash", "os", "sort"},
1498-
"github.com/example/varied/simple/another": {"encoding/binary", "github.com/sdboyer/gps", "hash", "os", "sort"},
1538+
allin := map[string][]string{
1539+
b(""): {b("m1p"), b("namemismatch"), b("otherpath"), b("simple"), b("simple/another")},
1540+
b("m1p"): {},
1541+
b("namemismatch"): {},
1542+
b("otherpath"): {b("m1p")},
1543+
b("simple"): {b("m1p"), b("simple/another")},
1544+
b("simple/another"): {b("m1p")},
14991545
}
1546+
15001547
// build a map to validate the exception inputs. do this because shit is
15011548
// hard enough to keep track of that it's preferable not to have silent
15021549
// success if a typo creeps in and we're trying to except an import that
15031550
// isn't in a pkg in the first place
15041551
valid := make(map[string]map[string]bool)
1505-
for ip, expkgs := range all {
1552+
for ip, expkgs := range allex {
15061553
m := make(map[string]bool)
15071554
for _, pkg := range expkgs {
15081555
m[pkg] = true
15091556
}
15101557
valid[ip] = m
15111558
}
1559+
validin := make(map[string]map[string]bool)
1560+
for ip, inpkgs := range allin {
1561+
m := make(map[string]bool)
1562+
for _, pkg := range inpkgs {
1563+
m[pkg] = true
1564+
}
1565+
validin[ip] = m
1566+
}
15121567

1513-
// helper to compose expect, excepting specific packages
1568+
// helper to compose wantex, excepting specific packages
15141569
//
15151570
// this makes it easier to see what we're taking out on each test
15161571
except := func(pkgig ...string) {
15171572
// reinit expect with everything from all
1518-
expect = make(map[string][]string)
1519-
for ip, expkgs := range all {
1573+
wantex = make(map[string][]string)
1574+
for ip, expkgs := range allex {
15201575
sl := make([]string, len(expkgs))
15211576
copy(sl, expkgs)
1522-
expect[ip] = sl
1577+
wantex[ip] = sl
15231578
}
15241579

15251580
// now build the dropmap
@@ -1536,7 +1591,7 @@ func TestToReachMaps(t *testing.T) {
15361591

15371592
// if only a single elem was passed, though, drop the whole thing
15381593
if len(not) == 0 {
1539-
delete(expect, ip)
1594+
delete(wantex, ip)
15401595
continue
15411596
}
15421597

@@ -1551,34 +1606,89 @@ func TestToReachMaps(t *testing.T) {
15511606
drop[ip] = m
15521607
}
15531608

1554-
for ip, pkgs := range expect {
1609+
for ip, pkgs := range wantex {
15551610
var npkgs []string
15561611
for _, imp := range pkgs {
15571612
if !drop[ip][imp] {
15581613
npkgs = append(npkgs, imp)
15591614
}
15601615
}
15611616

1562-
expect[ip] = npkgs
1617+
wantex[ip] = npkgs
15631618
}
15641619
}
15651620

1621+
// same as above, but for internal reachmap
1622+
exceptin := func(pkgig ...string) {
1623+
// reinit expect with everything from all
1624+
wantin = make(map[string][]string)
1625+
for ip, inpkgs := range allin {
1626+
sl := make([]string, len(inpkgs))
1627+
copy(sl, inpkgs)
1628+
wantin[ip] = sl
1629+
}
1630+
1631+
// now build the dropmap
1632+
drop := make(map[string]map[string]bool)
1633+
for _, igstr := range pkgig {
1634+
// split on space; first elem is import path to pkg, the rest are
1635+
// the imports to drop.
1636+
not := strings.Split(igstr, " ")
1637+
var ip string
1638+
ip, not = not[0], not[1:]
1639+
if _, exists := validin[ip]; !exists {
1640+
t.Fatalf("%s is not a package name we're working with, doofus", ip)
1641+
}
1642+
1643+
// if only a single elem was passed, though, drop the whole thing
1644+
if len(not) == 0 {
1645+
delete(wantin, ip)
1646+
continue
1647+
}
1648+
1649+
m := make(map[string]bool)
1650+
for _, imp := range not {
1651+
if !validin[ip][imp] {
1652+
t.Fatalf("%s is not a reachable import of %s, even in the all case", imp, ip)
1653+
}
1654+
m[imp] = true
1655+
}
1656+
1657+
drop[ip] = m
1658+
}
1659+
1660+
for ip, pkgs := range wantin {
1661+
var npkgs []string
1662+
for _, imp := range pkgs {
1663+
if !drop[ip][imp] {
1664+
npkgs = append(npkgs, imp)
1665+
}
1666+
}
1667+
1668+
wantin[ip] = npkgs
1669+
}
1670+
}
1671+
1672+
/* PREP IS DONE, BEGIN ACTUAL TESTING */
1673+
15661674
// first, validate all
15671675
name = "all"
15681676
main, tests = true, true
15691677
except()
1678+
exceptin()
15701679
validate()
15711680

15721681
// turn off main pkgs, which necessarily doesn't affect anything else
15731682
name = "no main"
15741683
main = false
1575-
except("github.com/example/varied")
1684+
except(b(""))
1685+
exceptin(b(""))
15761686
validate()
15771687

15781688
// ignoring the "varied" pkg has same effect as disabling main pkgs
15791689
name = "ignore root"
15801690
ignore = map[string]bool{
1581-
"github.com/example/varied": true,
1691+
b(""): true,
15821692
}
15831693
main = true
15841694
validate()
@@ -1590,20 +1700,24 @@ func TestToReachMaps(t *testing.T) {
15901700
tests = false
15911701
ignore = nil
15921702
except(
1593-
"github.com/example/varied encoding/binary",
1594-
"github.com/example/varied/simple encoding/binary",
1595-
"github.com/example/varied/simple/another encoding/binary",
1596-
"github.com/example/varied/otherpath github.com/sdboyer/gps os sort",
1703+
b("")+" encoding/binary",
1704+
b("simple")+" encoding/binary",
1705+
b("simple/another")+" encoding/binary",
1706+
b("otherpath")+" github.com/sdboyer/gps os sort",
15971707
)
15981708

15991709
// almost the same as previous, but varied just goes away completely
16001710
name = "no main or tests"
16011711
main = false
16021712
except(
1603-
"github.com/example/varied",
1604-
"github.com/example/varied/simple encoding/binary",
1605-
"github.com/example/varied/simple/another encoding/binary",
1606-
"github.com/example/varied/otherpath github.com/sdboyer/gps os sort",
1713+
b(""),
1714+
b("simple")+" encoding/binary",
1715+
b("simple/another")+" encoding/binary",
1716+
b("otherpath")+" github.com/sdboyer/gps os sort",
1717+
)
1718+
exceptin(
1719+
b(""),
1720+
bl("otherpath", "m1p"),
16071721
)
16081722
validate()
16091723

@@ -1614,38 +1728,56 @@ func TestToReachMaps(t *testing.T) {
16141728
// varied/simple
16151729
name = "ignore varied/simple"
16161730
ignore = map[string]bool{
1617-
"github.com/example/varied/simple": true,
1731+
b("simple"): true,
16181732
}
16191733
except(
16201734
// root pkg loses on everything in varied/simple/another
1621-
"github.com/example/varied hash encoding/binary go/parser",
1622-
"github.com/example/varied/simple",
1735+
b("")+" hash encoding/binary go/parser",
1736+
b("simple"),
1737+
)
1738+
exceptin(
1739+
// FIXME this is a bit odd, but should probably exclude m1p as well,
1740+
// because it actually shouldn't be valid to import a package that only
1741+
// has tests. This whole model misses that nuance right now, though.
1742+
bl("", "simple", "simple/another"),
1743+
b("simple"),
16231744
)
16241745
validate()
16251746

16261747
// widen the hole by excluding otherpath
16271748
name = "ignore varied/{otherpath,simple}"
16281749
ignore = map[string]bool{
1629-
"github.com/example/varied/otherpath": true,
1630-
"github.com/example/varied/simple": true,
1750+
b("otherpath"): true,
1751+
b("simple"): true,
16311752
}
16321753
except(
16331754
// root pkg loses on everything in varied/simple/another and varied/m1p
1634-
"github.com/example/varied hash encoding/binary go/parser github.com/sdboyer/gps sort",
1635-
"github.com/example/varied/otherpath",
1636-
"github.com/example/varied/simple",
1755+
b("")+" hash encoding/binary go/parser github.com/sdboyer/gps sort",
1756+
b("otherpath"),
1757+
b("simple"),
1758+
)
1759+
exceptin(
1760+
bl("", "simple", "simple/another", "m1p", "otherpath"),
1761+
b("otherpath"),
1762+
b("simple"),
16371763
)
16381764
validate()
16391765

16401766
// remove namemismatch, though we're mostly beating a dead horse now
16411767
name = "ignore varied/{otherpath,simple,namemismatch}"
1642-
ignore["github.com/example/varied/namemismatch"] = true
1768+
ignore[b("namemismatch")] = true
16431769
except(
16441770
// root pkg loses on everything in varied/simple/another and varied/m1p
1645-
"github.com/example/varied hash encoding/binary go/parser github.com/sdboyer/gps sort os github.com/Masterminds/semver",
1646-
"github.com/example/varied/otherpath",
1647-
"github.com/example/varied/simple",
1648-
"github.com/example/varied/namemismatch",
1771+
b("")+" hash encoding/binary go/parser github.com/sdboyer/gps sort os github.com/Masterminds/semver",
1772+
b("otherpath"),
1773+
b("simple"),
1774+
b("namemismatch"),
1775+
)
1776+
exceptin(
1777+
bl("", "simple", "simple/another", "m1p", "otherpath", "namemismatch"),
1778+
b("otherpath"),
1779+
b("simple"),
1780+
b("namemismatch"),
16491781
)
16501782
validate()
16511783
}

0 commit comments

Comments
 (0)