Skip to content

Commit

Permalink
Prevent fatal error when handling non-directory paths
Browse files Browse the repository at this point in the history
  • Loading branch information
sfowl committed Mar 1, 2022
1 parent 3786a0e commit bf77059
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
12 changes: 8 additions & 4 deletions deplist.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deplist

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -295,21 +296,24 @@ func getDeps(fullPath string) ([]Dependency, Bitmask, error) {
}

// findBaseDir walks a directory tree through empty subdirs til it finds a directory with content
func findBaseDir(fullPath string) string {
func findBaseDir(fullPath string) (string, error) {
log.Debugf("Checking %s", fullPath)
files, err := ioutil.ReadDir(fullPath)
if err != nil {
log.Fatal(err)
return "", fmt.Errorf("Could not read: %s", err)
}
if len(files) == 1 && files[0].IsDir() {
return findBaseDir(filepath.Join(fullPath, files[0].Name()))
}
return fullPath
return fullPath, nil
}

// GetDeps scans a given repository and returns all dependencies found in a DependencyList struct.
func GetDeps(fullPath string) ([]Dependency, Bitmask, error) {
fullPath = findBaseDir(fullPath)
fullPath, err := findBaseDir(fullPath)
if err != nil {
return nil, 0, err
}

deps, foundTypes, err := getDeps(fullPath)
if err != nil {
Expand Down
78 changes: 78 additions & 0 deletions deplist_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package deplist

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -316,3 +319,78 @@ func TestGetDeps(t *testing.T) {
}
}
}

func TestFindBaseDir(t *testing.T) {
type TestCase struct {
Input string
Expected string
Err bool
}

tests := make([]TestCase, 5)

top := t.TempDir()
tests[0] = TestCase{
Input: "non-existent directory",
Expected: "",
Err: true,
}

dirpath := filepath.Join(top, "baz")
os.MkdirAll(dirpath, 0755)
tests[1] = TestCase{
Input: dirpath,
Expected: dirpath,
Err: false,
}

tempFile, err := ioutil.TempFile(top, "bar")
if err != nil {
t.Error(err)
}
tests[2] = TestCase{
Input: tempFile.Name(),
Expected: "",
Err: true,
}

dirpath = filepath.Join(top, "foo/bar/foo/bar/foo/bar")
err = os.MkdirAll(dirpath, 0755)
if err != nil {
t.Error(err)
}
tests[3] = TestCase{
Input: filepath.Join(top, "foo"),
Expected: dirpath,
Err: false,
}

top = t.TempDir()
dirpath = filepath.Join(top, "foo/bar/foo/bar/foo/bar")
err = os.MkdirAll(dirpath, 0755)
if err != nil {
t.Error(err)
}
tempFile, err = ioutil.TempFile(filepath.Join(top, "foo/bar/foo"), "baz")
if err != nil {
t.Error(err)
}
tests[4] = TestCase{
Input: filepath.Join(top, "foo"),
Expected: filepath.Join(top, "foo/bar/foo"),
Err: false,
}

for i, test := range tests {
dir, err := findBaseDir(test.Input)

if test.Err {
if err == nil {
t.Errorf("%d: Expected error reading directory: %s but didn't get one", i, dir)
}
}
if test.Expected != dir {
t.Errorf("%d: Expected %s, got %s", i, test.Expected, dir)
}
}
}

0 comments on commit bf77059

Please sign in to comment.