@@ -1459,19 +1459,33 @@ func TestToReachMaps(t *testing.T) {
1459
1459
t .Fatalf ("ListPackages failed on varied test case: %s" , err )
1460
1460
}
1461
1461
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
+
1462
1476
// Set up vars for validate closure
1463
- var expect map [string ][]string
1477
+ var wantex , wantin map [string ][]string
1464
1478
var name string
1465
1479
var main , tests bool
1466
1480
var ignore map [string ]bool
1467
1481
1468
1482
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 ) {
1471
1485
seen := make (map [string ]bool )
1472
- for ip , epkgs := range expect {
1486
+ for ip , epkgs := range wantex {
1473
1487
seen [ip ] = true
1474
- if pkgs , exists := result [ip ]; ! exists {
1488
+ if pkgs , exists := gotex [ip ]; ! exists {
1475
1489
t .Errorf ("ver(%q): expected import path %s was not present in result" , name , ip )
1476
1490
} else {
1477
1491
if ! reflect .DeepEqual (pkgs , epkgs ) {
@@ -1480,46 +1494,87 @@ func TestToReachMaps(t *testing.T) {
1480
1494
}
1481
1495
}
1482
1496
1483
- for ip , pkgs := range result {
1497
+ for ip , pkgs := range gotex {
1484
1498
if seen [ip ] {
1485
1499
continue
1486
1500
}
1487
1501
t .Errorf ("ver(%q): Got packages for import path %s, but none were expected:\n \t %s" , name , ip , pkgs )
1488
1502
}
1489
1503
}
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" },
1490
1536
}
1491
1537
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" ) },
1499
1545
}
1546
+
1500
1547
// build a map to validate the exception inputs. do this because shit is
1501
1548
// hard enough to keep track of that it's preferable not to have silent
1502
1549
// success if a typo creeps in and we're trying to except an import that
1503
1550
// isn't in a pkg in the first place
1504
1551
valid := make (map [string ]map [string ]bool )
1505
- for ip , expkgs := range all {
1552
+ for ip , expkgs := range allex {
1506
1553
m := make (map [string ]bool )
1507
1554
for _ , pkg := range expkgs {
1508
1555
m [pkg ] = true
1509
1556
}
1510
1557
valid [ip ] = m
1511
1558
}
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
+ }
1512
1567
1513
- // helper to compose expect , excepting specific packages
1568
+ // helper to compose wantex , excepting specific packages
1514
1569
//
1515
1570
// this makes it easier to see what we're taking out on each test
1516
1571
except := func (pkgig ... string ) {
1517
1572
// 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 {
1520
1575
sl := make ([]string , len (expkgs ))
1521
1576
copy (sl , expkgs )
1522
- expect [ip ] = sl
1577
+ wantex [ip ] = sl
1523
1578
}
1524
1579
1525
1580
// now build the dropmap
@@ -1536,7 +1591,7 @@ func TestToReachMaps(t *testing.T) {
1536
1591
1537
1592
// if only a single elem was passed, though, drop the whole thing
1538
1593
if len (not ) == 0 {
1539
- delete (expect , ip )
1594
+ delete (wantex , ip )
1540
1595
continue
1541
1596
}
1542
1597
@@ -1551,34 +1606,89 @@ func TestToReachMaps(t *testing.T) {
1551
1606
drop [ip ] = m
1552
1607
}
1553
1608
1554
- for ip , pkgs := range expect {
1609
+ for ip , pkgs := range wantex {
1555
1610
var npkgs []string
1556
1611
for _ , imp := range pkgs {
1557
1612
if ! drop [ip ][imp ] {
1558
1613
npkgs = append (npkgs , imp )
1559
1614
}
1560
1615
}
1561
1616
1562
- expect [ip ] = npkgs
1617
+ wantex [ip ] = npkgs
1563
1618
}
1564
1619
}
1565
1620
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
+
1566
1674
// first, validate all
1567
1675
name = "all"
1568
1676
main , tests = true , true
1569
1677
except ()
1678
+ exceptin ()
1570
1679
validate ()
1571
1680
1572
1681
// turn off main pkgs, which necessarily doesn't affect anything else
1573
1682
name = "no main"
1574
1683
main = false
1575
- except ("github.com/example/varied" )
1684
+ except (b ("" ))
1685
+ exceptin (b ("" ))
1576
1686
validate ()
1577
1687
1578
1688
// ignoring the "varied" pkg has same effect as disabling main pkgs
1579
1689
name = "ignore root"
1580
1690
ignore = map [string ]bool {
1581
- "github.com/example/varied" : true ,
1691
+ b ( "" ) : true ,
1582
1692
}
1583
1693
main = true
1584
1694
validate ()
@@ -1590,20 +1700,24 @@ func TestToReachMaps(t *testing.T) {
1590
1700
tests = false
1591
1701
ignore = nil
1592
1702
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" ,
1597
1707
)
1598
1708
1599
1709
// almost the same as previous, but varied just goes away completely
1600
1710
name = "no main or tests"
1601
1711
main = false
1602
1712
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" ),
1607
1721
)
1608
1722
validate ()
1609
1723
@@ -1614,38 +1728,56 @@ func TestToReachMaps(t *testing.T) {
1614
1728
// varied/simple
1615
1729
name = "ignore varied/simple"
1616
1730
ignore = map [string ]bool {
1617
- "github.com/example/varied/ simple" : true ,
1731
+ b ( " simple") : true ,
1618
1732
}
1619
1733
except (
1620
1734
// 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" ),
1623
1744
)
1624
1745
validate ()
1625
1746
1626
1747
// widen the hole by excluding otherpath
1627
1748
name = "ignore varied/{otherpath,simple}"
1628
1749
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 ,
1631
1752
}
1632
1753
except (
1633
1754
// 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" ),
1637
1763
)
1638
1764
validate ()
1639
1765
1640
1766
// remove namemismatch, though we're mostly beating a dead horse now
1641
1767
name = "ignore varied/{otherpath,simple,namemismatch}"
1642
- ignore ["github.com/example/varied/ namemismatch" ] = true
1768
+ ignore [b ( " namemismatch") ] = true
1643
1769
except (
1644
1770
// 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" ),
1649
1781
)
1650
1782
validate ()
1651
1783
}
0 commit comments