Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Keep legal files when vendoring tools (#8)
Browse files Browse the repository at this point in the history
* Keep legal files when vendoring tools

* Bump version to v1.1.0

* Use a broader pile of legal file hints
  • Loading branch information
spenczar authored Mar 22, 2017
1 parent 7f20cb1 commit 42cdcc3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
61 changes: 55 additions & 6 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,12 @@ func clean(pkgs []string) {
return err
}

// Delete files in packages that aren't marked as "keep",
// and any non-go or test files.
// Delete files in packages that aren't in packages we need to build the
// tools, and any non-go, non-legal files in packages we *do* need.
if info.Mode().IsRegular() {
pkg = filepath.Dir(pkg)
_, keptPkg := keep[pkg]
isGo := strings.HasSuffix(path, ".go")
isAssembly := strings.HasSuffix(path, ".s")
isTest := strings.HasSuffix(path, "_test.go")
if !keptPkg || !(isGo || isAssembly) || isTest {
if !(keptPkg && keepFile(path)) {
toDelete = append(toDelete, path)
}
return nil
Expand Down Expand Up @@ -113,3 +110,55 @@ func clean(pkgs []string) {
}
}
}

func keepFile(filename string) bool {
if strings.HasSuffix(filename, "_test.go") {
return false
}
if strings.HasSuffix(filename, ".go") {
return true
}
if strings.HasSuffix(filename, ".s") {
return true
}
if isLegalFile(filename) {
return true
}
return false
}

var commonLegalFilePrefixes = []string{
"licence", // UK spelling
"license", // US spelling
"copying",
"unlicense",
"copyright",
"copyleft",
"authors",
"contributors",
"readme", // often has a license inline
}

var commonLegalFileSubstrings = []string{
"legal",
"notice",
"disclaimer",
"patent",
"third-party",
"thirdparty",
}

func isLegalFile(filename string) bool {
base := strings.ToLower(filepath.Base(filename))
for _, p := range commonLegalFilePrefixes {
if strings.HasPrefix(base, p) {
return true
}
}
for _, s := range commonLegalFileSubstrings {
if strings.Contains(base, s) {
return true
}
}
return false
}
54 changes: 54 additions & 0 deletions clean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"path/filepath"
"testing"
)

func TestIsLegalFile(t *testing.T) {
testcase := func(filename string, want bool) {
t.Run(filename, func(t *testing.T) {
have := isLegalFile(filename)
if have != want {
t.Fail()
}
})
}

testcase("license.md", true)
testcase("license.txt", true)
testcase("LICENSE", true)
testcase("LICENCE", true)
testcase("LICENSE.md", true)
testcase(filepath.Join("pkg", "LICENSE.md"), true)
testcase("LEGAL", true)
testcase("README", true)
testcase("COPYING", true)
testcase("COPYRIGHT", true)
testcase("UNLICENSE", true)
testcase("PATENTS", true)

testcase(filepath.Join("pat", "ents"), false)
testcase("picture.jpeg", false)
}

func TestKeep(t *testing.T) {
testcase := func(filename string, want bool) {
t.Run(filename, func(t *testing.T) {
have := keepFile(filename)
if have != want {
t.Fail()
}
})
}

testcase("program.go", true)
testcase("program_test.go", false)
testcase(filepath.Join("pkg", "program.go"), true)
testcase(filepath.Join("pkg", "program_test.go"), false)

testcase("assembly.s", true)
testcase("notassembly.as", false)
testcase("picture.gif", false)
testcase("LICENSE.md", true)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
)

const version = "v1.0.3"
const version = "v1.1.0"

var cacheDir = ""

Expand Down
6 changes: 6 additions & 0 deletions retool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ func TestRetool(t *testing.T) {

// Now the binary should be installed
assertBinInstalled(t, dir, "retool")

// Legal files should be kept around
_, err = os.Stat(filepath.Join(dir, "_tools", "src", "github.com", "twitchtv", "retool", "LICENSE"))
if err != nil {
t.Error("missing license file")
}
})

t.Run("dep_added", func(t *testing.T) {
Expand Down

0 comments on commit 42cdcc3

Please sign in to comment.