@@ -1466,6 +1466,8 @@ func TestCreateFromVCS_basic(t *testing.T) {
1466
1466
module example.com/foo/bar
1467
1467
1468
1468
go 1.12
1469
+ -- LICENSE --
1470
+ root license
1469
1471
-- a.go --
1470
1472
package a
1471
1473
@@ -1482,6 +1484,20 @@ var C = 5
1482
1484
package c
1483
1485
1484
1486
var D = 5
1487
+ -- e/LICENSE --
1488
+ e license
1489
+ -- e/e.go --
1490
+ package e
1491
+
1492
+ var E = 5
1493
+ -- f/go.mod --
1494
+ module example.com/foo/bar/f
1495
+
1496
+ go 1.12
1497
+ -- f/f.go --
1498
+ package f
1499
+
1500
+ var F = 5
1485
1501
-- .gitignore --
1486
1502
b.go
1487
1503
c/` )))
@@ -1494,31 +1510,60 @@ c/`)))
1494
1510
1495
1511
for _ , tc := range []struct {
1496
1512
desc string
1513
+ version module.Version
1497
1514
subdir string
1498
1515
wantFiles []string
1516
+ wantData map [string ]string
1499
1517
}{
1500
1518
{
1501
1519
desc : "from root" ,
1520
+ version : module.Version {Path : "example.com/foo/bar" , Version : "v0.0.1" },
1502
1521
subdir : "" ,
1503
- wantFiles : []string {"go.mod" , "a.go" , "d/d.go" , ".gitignore" },
1522
+ wantFiles : []string {"go.mod" , "LICENSE" , "a.go" , "d/d.go" , "e/LICENSE" , "e/e.go" , ".gitignore" },
1523
+ wantData : map [string ]string {"LICENSE" : "root license\n " },
1504
1524
},
1505
1525
{
1506
- desc : "from subdir" ,
1507
- subdir : "d/" ,
1526
+ desc : "from subdir" ,
1527
+ version : module.Version {Path : "example.com/foo/bar" , Version : "v0.0.1" },
1528
+ subdir : "d/" ,
1508
1529
// Note: File paths are zipped as if the subdir were the root. ie d.go instead of d/d.go.
1509
- wantFiles : []string {"d.go" },
1530
+ // subdirs without a license hoist the license from the root
1531
+ wantFiles : []string {"d.go" , "LICENSE" },
1532
+ wantData : map [string ]string {"LICENSE" : "root license\n " },
1533
+ },
1534
+ {
1535
+ desc : "from subdir with license" ,
1536
+ version : module.Version {Path : "example.com/foo/bar" , Version : "v0.0.1" },
1537
+ subdir : "e/" ,
1538
+ // Note: File paths are zipped as if the subdir were the root. ie e.go instead of e/e.go.
1539
+ // subdirs with a license use their own
1540
+ wantFiles : []string {"LICENSE" , "e.go" },
1541
+ wantData : map [string ]string {"LICENSE" : "e license\n " },
1542
+ },
1543
+ {
1544
+ desc : "from submodule subdir" ,
1545
+ version : module.Version {Path : "example.com/foo/bar/f" , Version : "v0.0.1" },
1546
+ subdir : "f/" ,
1547
+ // Note: File paths are zipped as if the subdir were the root. ie f.go instead of f/f.go.
1548
+ // subdirs without a license hoist the license from the root
1549
+ wantFiles : []string {"go.mod" , "f.go" , "LICENSE" },
1550
+ wantData : map [string ]string {"LICENSE" : "root license\n " },
1510
1551
},
1511
1552
} {
1512
1553
t .Run (tc .desc , func (t * testing.T ) {
1513
1554
// Create zip from the directory.
1514
1555
tmpZip := & bytes.Buffer {}
1515
1556
1516
- m := module.Version {Path : "example.com/foo/bar" , Version : "v0.0.1" }
1517
-
1518
- if err := modzip .CreateFromVCS (tmpZip , m , tmpDir , "HEAD" , tc .subdir ); err != nil {
1557
+ if err := modzip .CreateFromVCS (tmpZip , tc .version , tmpDir , "HEAD" , tc .subdir ); err != nil {
1519
1558
t .Fatal (err )
1520
1559
}
1521
1560
1561
+ wantData := map [string ]string {}
1562
+ for f , data := range tc .wantData {
1563
+ p := path .Join (tc .version .String (), f )
1564
+ wantData [p ] = data
1565
+ }
1566
+
1522
1567
readerAt := bytes .NewReader (tmpZip .Bytes ())
1523
1568
r , err := zip .NewReader (readerAt , int64 (tmpZip .Len ()))
1524
1569
if err != nil {
@@ -1529,10 +1574,28 @@ c/`)))
1529
1574
for _ , f := range r .File {
1530
1575
gotMap [f .Name ] = true
1531
1576
gotFiles = append (gotFiles , f .Name )
1577
+
1578
+ if want , ok := wantData [f .Name ]; ok {
1579
+ r , err := f .Open ()
1580
+ if err != nil {
1581
+ t .Errorf ("CreatedFromVCS: error opening %s: %v" , f .Name , err )
1582
+ continue
1583
+ }
1584
+ defer r .Close ()
1585
+ got , err := io .ReadAll (r )
1586
+ if err != nil {
1587
+ t .Errorf ("CreatedFromVCS: error reading %s: %v" , f .Name , err )
1588
+ continue
1589
+ }
1590
+ if want != string (got ) {
1591
+ t .Errorf ("CreatedFromVCS: zipped file %s contains %s, expected %s" , f .Name , string (got ), want )
1592
+ continue
1593
+ }
1594
+ }
1532
1595
}
1533
1596
wantMap := map [string ]bool {}
1534
1597
for _ , f := range tc .wantFiles {
1535
- p := path .
Join (
"example.com" , "foo" , "[email protected] " ,
f )
1598
+ p := path .Join (tc . version . String () , f )
1536
1599
wantMap [p ] = true
1537
1600
}
1538
1601
@@ -1549,6 +1612,11 @@ c/`)))
1549
1612
t .Errorf ("CreatedFromVCS: zipped file doesn't contain %s, but expected it to. all files: %v" , f , gotFiles )
1550
1613
}
1551
1614
}
1615
+ for f := range wantData {
1616
+ if ! gotMap [f ] {
1617
+ t .Errorf ("CreatedFromVCS: zipped file doesn't contain %s, but expected it to. all files: %v" , f , gotFiles )
1618
+ }
1619
+ }
1552
1620
})
1553
1621
}
1554
1622
}
0 commit comments