Skip to content

[WE-168] Failing test fix for tools download dir discovery and vendor update #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions tools/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,29 +319,61 @@ func stringInSlice(str string, list []string) bool {
return false
}

func findBaseDir(dirList []string) string {
if len(dirList) == 1 {
return filepath.Dir(dirList[0]) + "/"
}
baseDir := ""
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
dontdiff := []string{"pax_global_header"}
for index, _ := range dirList {
if stringInSlice(dirList[index], dontdiff) {
continue
func CommonPrefix(sep byte, paths []string) string {
// Handle special cases.
switch len(paths) {
case 0:
return ""
case 1:
return path.Clean(paths[0])
}

c := []byte(path.Clean(paths[0]))

// We add a trailing sep to handle: common prefix directory is included in the path list
// (e.g. /home/user1, /home/user1/foo, /home/user1/bar).
// path.Clean will have cleaned off trailing / separators with
// the exception of the root directory, "/" making it "//"
// but this will get fixed up to "/" below).
c = append(c, sep)

// Ignore the first path since it's already in c
for _, v := range paths[1:] {
// Clean up each path before testing it
v = path.Clean(v) + string(sep)

// Find the first non-common byte and truncate c
if len(v) < len(c) {
c = c[:len(v)]
}
candidateBaseDir := dirList[index]
for i := index; i < len(dirList); i++ {
if !strings.Contains(dirList[i], candidateBaseDir) {
return baseDir
for i := 0; i < len(c); i++ {
if v[i] != c[i] {
c = c[:i]
break
}
}
// avoid setting the candidate if it is the last file
if dirList[len(dirList)-1] != candidateBaseDir {
baseDir = candidateBaseDir
}

// Remove trailing non-separator characters and the final separator
for i := len(c) - 1; i >= 0; i-- {
if c[i] == sep {
c = c[:i]
break
}
}
return baseDir

return string(c)
}

func findBaseDir(dirList []string) string {
if len(dirList) == 1 {
return filepath.Dir(dirList[0]) + "/"
}
commonBaseDir := CommonPrefix(os.PathSeparator, dirList)
if commonBaseDir != "" {
commonBaseDir = commonBaseDir + "/"
}
return commonBaseDir
}

func extractZip(body []byte, location string) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions tools/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ func Test_findBaseDir(t *testing.T) {
{[]string{"bin/bossac"}, "bin/"},
{[]string{"bin/", "bin/bossac"}, "bin/"},
{[]string{"bin/", "bin/bossac", "example"}, ""},
{[]string{"avrdude/bin/avrdude", "avrdude/etc/avrdude.conf"}, "avrdude"},
{[]string{"avrdude/bin/avrdude", "avrdude/etc/avrdude.conf"}, "avrdude/"},
}
for _, tt := range cases {
t.Run(fmt.Sprintln(tt.dirList), func(t *testing.T) {
if got := findBaseDir(tt.dirList); got != tt.want {
t.Errorf("findBaseDir() = %v, want %v", got, tt.want)
t.Errorf("findBaseDir() = got %v, want %v", got, tt.want)
}
})
}
Expand Down