77 | 77 |
return test, nil
|
78 | 78 |
}
|
79 | 79 |
|
80 | |
func extractTxtarToTempDir(arc *txtar.Archive) (dir string, err error) {
|
|
80 |
func extractTxtarToTempDir(arc *txtar.Archive) (dir string, cleanup func(), err error) {
|
81 | 81 |
dir, err = ioutil.TempDir("", "zip_test-*")
|
82 | 82 |
if err != nil {
|
83 | |
return "", err
|
|
83 |
return "", func() {}, err
|
|
84 |
}
|
|
85 |
cleanup = func() {
|
|
86 |
os.RemoveAll(dir)
|
84 | 87 |
}
|
85 | 88 |
defer func() {
|
86 | 89 |
if err != nil {
|
87 | |
os.RemoveAll(dir)
|
|
90 |
cleanup()
|
88 | 91 |
}
|
89 | 92 |
}()
|
90 | 93 |
for _, f := range arc.Files {
|
91 | 94 |
filePath := filepath.Join(dir, f.Name)
|
92 | 95 |
if err := os.MkdirAll(filepath.Dir(filePath), 0777); err != nil {
|
93 | |
return "", err
|
|
96 |
return "", func() {}, err
|
94 | 97 |
}
|
95 | 98 |
if err := ioutil.WriteFile(filePath, f.Data, 0666); err != nil {
|
96 | |
return "", err
|
97 | |
}
|
98 | |
}
|
99 | |
return dir, nil
|
|
99 |
return "", func() {}, err
|
|
100 |
}
|
|
101 |
}
|
|
102 |
return dir, cleanup, nil
|
100 | 103 |
}
|
101 | 104 |
|
102 | 105 |
func extractTxtarToTempZip(arc *txtar.Archive) (zipPath string, err error) {
|
|
268 | 271 |
break
|
269 | 272 |
}
|
270 | 273 |
}
|
271 | |
tmpDir, err := extractTxtarToTempDir(test.archive)
|
272 | |
if err != nil {
|
273 | |
t.Fatal(err)
|
274 | |
}
|
275 | |
defer func() {
|
276 | |
if err := os.RemoveAll(tmpDir); err != nil {
|
277 | |
t.Errorf("removing temp directory: %v", err)
|
278 | |
}
|
279 | |
}()
|
|
274 |
tmpDir, cleanup, err := extractTxtarToTempDir(test.archive)
|
|
275 |
if err != nil {
|
|
276 |
t.Fatal(err)
|
|
277 |
}
|
|
278 |
defer cleanup()
|
280 | 279 |
|
281 | 280 |
// Check the directory.
|
282 | 281 |
cf, err := modzip.CheckDir(tmpDir)
|
|
460 | 459 |
}
|
461 | 460 |
|
462 | 461 |
// Write files to a temporary directory.
|
463 | |
tmpDir, err := extractTxtarToTempDir(test.archive)
|
464 | |
if err != nil {
|
465 | |
t.Fatal(err)
|
466 | |
}
|
467 | |
defer func() {
|
468 | |
if err := os.RemoveAll(tmpDir); err != nil {
|
469 | |
t.Errorf("removing temp directory: %v", err)
|
470 | |
}
|
471 | |
}()
|
|
462 |
tmpDir, cleanup, err := extractTxtarToTempDir(test.archive)
|
|
463 |
if err != nil {
|
|
464 |
t.Fatal(err)
|
|
465 |
}
|
|
466 |
defer cleanup()
|
472 | 467 |
|
473 | 468 |
// Create zip from the directory.
|
474 | 469 |
tmpZip, err := ioutil.TempFile("", "TestCreateFromDir-*.zip")
|
|
1467 | 1462 |
func (f zipFile) Path() string { return f.name }
|
1468 | 1463 |
func (f zipFile) Lstat() (os.FileInfo, error) { return f.f.FileInfo(), nil }
|
1469 | 1464 |
func (f zipFile) Open() (io.ReadCloser, error) { return f.f.Open() }
|
|
1465 |
|
|
1466 |
func TestCreateFromVCS_basic(t *testing.T) {
|
|
1467 |
// Write files to a temporary directory.
|
|
1468 |
tmpDir, cleanup, err := extractTxtarToTempDir(txtar.Parse([]byte(`-- go.mod --
|
|
1469 |
module example.com/foo/bar
|
|
1470 |
|
|
1471 |
go 1.12
|
|
1472 |
-- a.go --
|
|
1473 |
package a
|
|
1474 |
|
|
1475 |
var A = 5
|
|
1476 |
-- b.go --
|
|
1477 |
package a
|
|
1478 |
|
|
1479 |
var B = 5
|
|
1480 |
-- c/c.go --
|
|
1481 |
package c
|
|
1482 |
|
|
1483 |
var C = 5
|
|
1484 |
-- d/d.go --
|
|
1485 |
package c
|
|
1486 |
|
|
1487 |
var D = 5
|
|
1488 |
-- .gitignore --
|
|
1489 |
b.go
|
|
1490 |
c/`)))
|
|
1491 |
if err != nil {
|
|
1492 |
t.Fatal(err)
|
|
1493 |
}
|
|
1494 |
defer cleanup()
|
|
1495 |
|
|
1496 |
gitInit(t, tmpDir)
|
|
1497 |
gitCommit(t, tmpDir)
|
|
1498 |
|
|
1499 |
for _, tc := range []struct {
|
|
1500 |
desc string
|
|
1501 |
subdir string
|
|
1502 |
wantFiles []string
|
|
1503 |
}{
|
|
1504 |
{
|
|
1505 |
desc: "from root",
|
|
1506 |
subdir: "",
|
|
1507 |
wantFiles: []string{"go.mod", "a.go", "d/d.go", ".gitignore"},
|
|
1508 |
},
|
|
1509 |
{
|
|
1510 |
desc: "from subdir",
|
|
1511 |
subdir: "d/",
|
|
1512 |
// Note: File paths are zipped as if the subdir were the root. ie d.go instead of d/d.go.
|
|
1513 |
wantFiles: []string{"d.go"},
|
|
1514 |
},
|
|
1515 |
} {
|
|
1516 |
t.Run(tc.desc, func(t *testing.T) {
|
|
1517 |
// Create zip from the directory.
|
|
1518 |
tmpZip := &bytes.Buffer{}
|
|
1519 |
|
|
1520 |
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}
|
|
1521 |
|
|
1522 |
if err := modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", tc.subdir); err != nil {
|
|
1523 |
t.Fatal(err)
|
|
1524 |
}
|
|
1525 |
|
|
1526 |
readerAt := bytes.NewReader(tmpZip.Bytes())
|
|
1527 |
r, err := zip.NewReader(readerAt, int64(tmpZip.Len()))
|
|
1528 |
if err != nil {
|
|
1529 |
t.Fatal(err)
|
|
1530 |
}
|
|
1531 |
var gotFiles []string
|
|
1532 |
gotMap := map[string]bool{}
|
|
1533 |
for _, f := range r.File {
|
|
1534 |
gotMap[f.Name] = true
|
|
1535 |
gotFiles = append(gotFiles, f.Name)
|
|
1536 |
}
|
|
1537 |
wantMap := map[string]bool{}
|
|
1538 |
for _, f := range tc.wantFiles {
|
|
1539 |
p := filepath.Join("example.com", "foo", "bar@v0.0.1", f)
|
|
1540 |
wantMap[p] = true
|
|
1541 |
}
|
|
1542 |
|
|
1543 |
// The things that should be there.
|
|
1544 |
for f := range gotMap {
|
|
1545 |
if !wantMap[f] {
|
|
1546 |
t.Errorf("CreatedFromVCS: zipped file contains %s, but expected it not to", f)
|
|
1547 |
}
|
|
1548 |
}
|
|
1549 |
|
|
1550 |
// The things that are missing.
|
|
1551 |
for f := range wantMap {
|
|
1552 |
if !gotMap[f] {
|
|
1553 |
t.Errorf("CreatedFromVCS: zipped file doesn't contain %s, but expected it to. all files: %v", f, gotFiles)
|
|
1554 |
}
|
|
1555 |
}
|
|
1556 |
})
|
|
1557 |
}
|
|
1558 |
}
|
|
1559 |
|
|
1560 |
// Test what the experience of creating a zip from a v2 module is like.
|
|
1561 |
func TestCreateFromVCS_v2(t *testing.T) {
|
|
1562 |
// Write files to a temporary directory.
|
|
1563 |
tmpDir, cleanup, err := extractTxtarToTempDir(txtar.Parse([]byte(`-- go.mod --
|
|
1564 |
module example.com/foo/bar
|
|
1565 |
|
|
1566 |
go 1.12
|
|
1567 |
-- a.go --
|
|
1568 |
package a
|
|
1569 |
|
|
1570 |
var A = 5
|
|
1571 |
-- b.go --
|
|
1572 |
package a
|
|
1573 |
|
|
1574 |
var B = 5
|
|
1575 |
-- go.mod --
|
|
1576 |
module example.com/foo/bar
|
|
1577 |
|
|
1578 |
go 1.12
|
|
1579 |
-- gaz/v2/a_2.go --
|
|
1580 |
package a
|
|
1581 |
|
|
1582 |
var C = 9
|
|
1583 |
-- gaz/v2/b_2.go --
|
|
1584 |
package a
|
|
1585 |
|
|
1586 |
var B = 11
|
|
1587 |
-- gaz/v2/go.mod --
|
|
1588 |
module example.com/foo/bar/v2
|
|
1589 |
|
|
1590 |
go 1.12
|
|
1591 |
-- .gitignore --
|
|
1592 |
`)))
|
|
1593 |
if err != nil {
|
|
1594 |
t.Fatal(err)
|
|
1595 |
}
|
|
1596 |
defer cleanup()
|
|
1597 |
|
|
1598 |
gitInit(t, tmpDir)
|
|
1599 |
gitCommit(t, tmpDir)
|
|
1600 |
|
|
1601 |
// Create zip from the directory.
|
|
1602 |
tmpZip := &bytes.Buffer{}
|
|
1603 |
|
|
1604 |
m := module.Version{Path: "example.com/foo/bar/v2", Version: "v2.0.0"}
|
|
1605 |
|
|
1606 |
if err := modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", "gaz/v2"); err != nil {
|
|
1607 |
t.Fatal(err)
|
|
1608 |
}
|
|
1609 |
|
|
1610 |
readerAt := bytes.NewReader(tmpZip.Bytes())
|
|
1611 |
r, err := zip.NewReader(readerAt, int64(tmpZip.Len()))
|
|
1612 |
if err != nil {
|
|
1613 |
t.Fatal(err)
|
|
1614 |
}
|
|
1615 |
var gotFiles []string
|
|
1616 |
gotMap := map[string]bool{}
|
|
1617 |
for _, f := range r.File {
|
|
1618 |
gotMap[f.Name] = true
|
|
1619 |
gotFiles = append(gotFiles, f.Name)
|
|
1620 |
}
|
|
1621 |
wantMap := map[string]bool{
|
|
1622 |
"example.com/foo/bar/v2@v2.0.0/a_2.go": true,
|
|
1623 |
"example.com/foo/bar/v2@v2.0.0/b_2.go": true,
|
|
1624 |
"example.com/foo/bar/v2@v2.0.0/go.mod": true,
|
|
1625 |
}
|
|
1626 |
|
|
1627 |
// The things that should be there.
|
|
1628 |
for f := range gotMap {
|
|
1629 |
if !wantMap[f] {
|
|
1630 |
t.Errorf("CreatedFromVCS: zipped file contains %s, but expected it not to", f)
|
|
1631 |
}
|
|
1632 |
}
|
|
1633 |
|
|
1634 |
// The things that are missing.
|
|
1635 |
for f := range wantMap {
|
|
1636 |
if !gotMap[f] {
|
|
1637 |
t.Errorf("CreatedFromVCS: zipped file doesn't contain %s, but expected it to. all files: %v", f, gotFiles)
|
|
1638 |
}
|
|
1639 |
}
|
|
1640 |
}
|
|
1641 |
|
|
1642 |
func TestCreateFromVCS_nonGitDir(t *testing.T) {
|
|
1643 |
// Write files to a temporary directory.
|
|
1644 |
tmpDir, cleanup, err := extractTxtarToTempDir(txtar.Parse([]byte(`-- go.mod --
|
|
1645 |
module example.com/foo/bar
|
|
1646 |
|
|
1647 |
go 1.12
|
|
1648 |
-- a.go --
|
|
1649 |
package a
|
|
1650 |
|
|
1651 |
var A = 5
|
|
1652 |
`)))
|
|
1653 |
if err != nil {
|
|
1654 |
t.Fatal(err)
|
|
1655 |
}
|
|
1656 |
defer cleanup()
|
|
1657 |
|
|
1658 |
// Create zip from the directory.
|
|
1659 |
tmpZip, err := ioutil.TempFile("", "TestCreateFromDir-*.zip")
|
|
1660 |
if err != nil {
|
|
1661 |
t.Fatal(err)
|
|
1662 |
}
|
|
1663 |
tmpZipPath := tmpZip.Name()
|
|
1664 |
defer func() {
|
|
1665 |
tmpZip.Close()
|
|
1666 |
os.Remove(tmpZipPath)
|
|
1667 |
}()
|
|
1668 |
|
|
1669 |
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}
|
|
1670 |
|
|
1671 |
if err := modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", ""); err == nil {
|
|
1672 |
t.Error("CreateFromVCS: expected error, got nil")
|
|
1673 |
}
|
|
1674 |
}
|
|
1675 |
|
|
1676 |
func TestCreateFromVCS_zeroCommitsGitDir(t *testing.T) {
|
|
1677 |
// Write files to a temporary directory.
|
|
1678 |
tmpDir, cleanup, err := extractTxtarToTempDir(txtar.Parse([]byte(`-- go.mod --
|
|
1679 |
module example.com/foo/bar
|
|
1680 |
|
|
1681 |
go 1.12
|
|
1682 |
-- a.go --
|
|
1683 |
package a
|
|
1684 |
|
|
1685 |
var A = 5
|
|
1686 |
`)))
|
|
1687 |
if err != nil {
|
|
1688 |
t.Fatal(err)
|
|
1689 |
}
|
|
1690 |
defer cleanup()
|
|
1691 |
|
|
1692 |
gitInit(t, tmpDir)
|
|
1693 |
|
|
1694 |
// Create zip from the directory.
|
|
1695 |
tmpZip, err := ioutil.TempFile("", "TestCreateFromDir-*.zip")
|
|
1696 |
if err != nil {
|
|
1697 |
t.Fatal(err)
|
|
1698 |
}
|
|
1699 |
tmpZipPath := tmpZip.Name()
|
|
1700 |
defer func() {
|
|
1701 |
tmpZip.Close()
|
|
1702 |
os.Remove(tmpZipPath)
|
|
1703 |
}()
|
|
1704 |
|
|
1705 |
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}
|
|
1706 |
|
|
1707 |
if err := modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", ""); err == nil {
|
|
1708 |
t.Error("CreateFromVCS: expected error, got nil")
|
|
1709 |
}
|
|
1710 |
}
|
|
1711 |
|
|
1712 |
// gitInit runs "git init" at the specified dir.
|
|
1713 |
//
|
|
1714 |
// Note: some environments - and trybots - don't have git installed. This
|
|
1715 |
// function will cause the calling test to be skipped if that's the case.
|
|
1716 |
func gitInit(t *testing.T, dir string) {
|
|
1717 |
t.Helper()
|
|
1718 |
|
|
1719 |
if _, err := exec.LookPath("git"); err != nil {
|
|
1720 |
t.Skip("PATH does not contain git")
|
|
1721 |
}
|
|
1722 |
|
|
1723 |
cmd := exec.Command("git", "init")
|
|
1724 |
cmd.Dir = dir
|
|
1725 |
cmd.Stderr = os.Stderr
|
|
1726 |
if err := cmd.Run(); err != nil {
|
|
1727 |
t.Fatal(err)
|
|
1728 |
}
|
|
1729 |
|
|
1730 |
cmd = exec.Command("git", "config", "user.email", "testing@golangtests.com")
|
|
1731 |
cmd.Dir = dir
|
|
1732 |
cmd.Stderr = os.Stderr
|
|
1733 |
if err := cmd.Run(); err != nil {
|
|
1734 |
t.Fatal(err)
|
|
1735 |
}
|
|
1736 |
|
|
1737 |
cmd = exec.Command("git", "config", "user.name", "This is the zip Go tests")
|
|
1738 |
cmd.Dir = dir
|
|
1739 |
cmd.Stderr = os.Stderr
|
|
1740 |
if err := cmd.Run(); err != nil {
|
|
1741 |
t.Fatal(err)
|
|
1742 |
}
|
|
1743 |
}
|
|
1744 |
|
|
1745 |
func gitCommit(t *testing.T, dir string) {
|
|
1746 |
t.Helper()
|
|
1747 |
|
|
1748 |
if _, err := exec.LookPath("git"); err != nil {
|
|
1749 |
t.Skip("PATH does not contain git")
|
|
1750 |
}
|
|
1751 |
|
|
1752 |
cmd := exec.Command("git", "add", "-A")
|
|
1753 |
cmd.Dir = dir
|
|
1754 |
cmd.Stderr = os.Stderr
|
|
1755 |
if err := cmd.Run(); err != nil {
|
|
1756 |
t.Skip("git executable is not available on this machine")
|
|
1757 |
}
|
|
1758 |
|
|
1759 |
cmd = exec.Command("git", "commit", "-m", "some commit")
|
|
1760 |
cmd.Dir = dir
|
|
1761 |
cmd.Stderr = os.Stderr
|
|
1762 |
if err := cmd.Run(); err != nil {
|
|
1763 |
t.Fatal(err)
|
|
1764 |
}
|
|
1765 |
}
|