Skip to content

Commit

Permalink
respect --whitelist options when doing a full vendor cycle
Browse files Browse the repository at this point in the history
Commit b1caa00 added a `--whitelist`
flag to allow ignoring paths in the vendor directory when cleaning up
unused packages/files.

However, 4b11a0f added an optimization
to clean the whole vendor directory when doing a full vendor cycle.
Unfortunately, that change did not take the `--whitelist` option into
account, resulting in whitelisted packages/files to be deleted when doing
a full vendor cycle.

This patch checks if a whitelist option is set when doing a full vendor
cycle, and if so, ignores the specified paths when cleaning.

Using the "v18.06.0-ce" branch on github.com/docker/engine, which has a custom `archive/tar` package in the vendor directory;

    $ git checkout v18.06.0-ce
    $ vndr --whitelist 'archive/tar/.*'
    2020/01/20 18:50:51 Collecting initial packages
    2020/01/20 18:50:52 Download dependencies
    2020/01/20 18:50:55 Starting whole vndr cycle because no package specified
    2020/01/20 18:50:55 Ignoring paths matching "archive/tar/.*"
    ...
    020/01/20 18:53:46  Finished clone github.com/aws/aws-sdk-go
    2020/01/20 18:53:46 Finished clone github.com/hashicorp/consul
    2020/01/20 18:53:46 Dependencies downloaded. Download time: 2m50.469326342s
    2020/01/20 18:53:46 Collecting all dependencies
    2020/01/20 18:53:57 Clean vendor dir from unused packages
    2020/01/20 18:53:57 Ignoring paths matching "archive/tar/.*"
    2020/01/20 18:53:59 Success
    2020/01/20 18:53:59 Running time: 3m7.731082301s

After vendoring completed, verify that the `archive/tar` directory was kept:

    ls vendor/archive/tar
    LICENSE         README.md       common.go       format.go       reader.go       stat_actime1.go stat_actime2.go stat_unix.go    strconv.go      writer.go

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah authored and LK4D4 committed Jan 21, 2020
1 parent d4692b7 commit 85886e1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,17 @@ func main() {
cfgDeps = []depEntry{flagDep}
} else {
log.Println("Starting whole vndr cycle because no package specified")
if err := os.RemoveAll(vd); err != nil {
log.Fatal(err)
if len(cleanWhitelist) > 0 {
for _, regex := range cleanWhitelist {
log.Printf("\tIgnoring paths matching %q", regex.String())
}
if err := cleanVendor(vd, nil); err != nil {
log.Fatal(err)
}
} else {
if err := os.RemoveAll(vd); err != nil {
log.Fatal(err)
}
}
}
startDownload := time.Now()
Expand Down
70 changes: 70 additions & 0 deletions test/vndr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,76 @@ github.com/projectatomic/skopeo master`)
}
}

func TestCleanWhitelistFullCycle(t *testing.T) {
vndrBin, err := exec.LookPath("vndr")
if err != nil {
t.Fatal(err)
}
tmp, err := ioutil.TempDir("", "test-vndr-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
repoDir := filepath.Join(tmp, "src", testRepo)
if err := os.MkdirAll(repoDir, 0700); err != nil {
t.Fatal(err)
}
depDir := filepath.Join(repoDir, "vendor", "archive", "tar")
if err := os.MkdirAll(depDir, 0700); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(depDir, "LICENSE"), []byte("foo"), 0644); err != nil {
t.Fatal(err)
}

content := []byte(`github.com/AkihiroSuda/dummy-vndr-46 c5613b87bafaaf105fd3857dcae7ef23c931feec
`)
vendorConf := filepath.Join(repoDir, "vendor.conf")
if err := ioutil.WriteFile(vendorConf, content, 0666); err != nil {
t.Fatal(err)
}
vndrCmd := exec.Command(vndrBin, "-whitelist", `archive/tar/.*`)
vndrCmd.Dir = repoDir
setGopath(vndrCmd, tmp)

out, err := vndrCmd.CombinedOutput()
if err != nil {
t.Logf("output: %v", string(out))
t.Fatalf("error was not expected: %v", err)
}

if !bytes.Contains(out, []byte(fmt.Sprintf(`Ignoring paths matching %q`, `archive/tar/.*`))) {
t.Logf("output: %v", string(out))
t.Errorf(`output missing regular expression "archive/tar/.*"`)
}

// Make sure that the files were not "cleaned".
if _, err := os.Lstat(depDir); err != nil {
t.Errorf("%s was cleaned but shouldn't have been", depDir)
}

// Run again to make sure the above will be cleaned.
vndrCmd = exec.Command(vndrBin)
vndrCmd.Dir = repoDir
setGopath(vndrCmd, tmp)

out, err = vndrCmd.CombinedOutput()
if err != nil {
t.Logf("output: %v", string(out))
t.Fatalf("[no -whitelist] error was not expected: %v", err)
}

if bytes.Contains(out, []byte(fmt.Sprintf(`Ignoring paths matching %q`, `archive/tar/.*`))) {
t.Logf("output: %v", string(out))
t.Errorf(`[no -whitelist] output should not contain regular expression "archive/tar/.*"`)
}

// Make sure that the files were "cleaned".
if _, err := os.Lstat(depDir); err == nil {
t.Errorf("[no -whitelist] %s was NOT cleaned but should have been", depDir)
}
}

func TestUnused(t *testing.T) {
vndrBin, err := exec.LookPath("vndr")
if err != nil {
Expand Down

0 comments on commit 85886e1

Please sign in to comment.