diff --git a/dependency/dependency_file.go b/dependency/dependency_file.go index 9a0aa63..4046ab7 100644 --- a/dependency/dependency_file.go +++ b/dependency/dependency_file.go @@ -143,36 +143,61 @@ func ListDependencyFiles(p *api.Project) error { return nil } -var getLocalDependencyFiles = func() ([]*api.DependencyFile, error) { +var getLocalDependencyFiles = func(rootPath string) ([]*api.DependencyFile, error) { dfiles := []*api.DependencyFile{} + excludeDirectory :=map[string]bool{ + "node_modules": true, + ".bundle": true, + "vendor": true, + ".git": true, + } searchDeps := func(path string, info os.FileInfo, err error) error { - - // Skip excluded paths - if info.IsDir() && info.Name() == ".git" { - return filepath.SkipDir + // Get path relative to rootPath, we don't want to take wrongly into account + // the elements of rootPath + relativePath, err := filepath.Rel(rootPath, path) + if err != nil { + return err + } + // Skip excluded directories + for _, pathComponent := range filepath.SplitList(relativePath) { + if excludeDirectory[pathComponent] { + return filepath.SkipDir + } } // Skip ignored_pathes - if len(config.IgnoredPaths) > 0 { - for _, path := range config.IgnoredPaths { - matched, err := filepath.Match(filepath.Clean(path), info.Name()) - if err != nil { - return err - } + for _, ignoredPath := range config.IgnoredPaths { + // Old behavior, keep it in case users rely on it + matched1, err := filepath.Match(filepath.Clean(ignoredPath), info.Name()) + if err != nil { + return err + } + // Actual match on the path + matched2, err := filepath.Match(filepath.Clean(ignoredPath), relativePath) + if err != nil { + return err + } - if matched { - fmt.Println("Skipping", info.Name()) - return filepath.SkipDir - } + if matched1 || matched2 { + fmt.Println("Skipping", info.Name()) + return filepath.SkipDir } } if df := depfile.Find(path); df != nil { - fmt.Printf("Found: %s (%s)\n", path, df.Name) - dfiles = append(dfiles, NewDependencyFile(path)) + fmt.Printf("Found: %s (%s)\n", relativePath, df.Name) + dfile := NewDependencyFile(path) + // Remove the rootPath from the path field of dfile to keep things clean. + // we want pathes relative to the project's root + dfile.Path, err = filepath.Rel(rootPath, dfile.Path) + if err != nil { + return err + } + dfiles = append(dfiles, dfile) } return nil } - err := filepath.Walk(".", searchDeps) + // Walk the directory + err := filepath.Walk(rootPath, searchDeps) if err != nil { return dfiles, err } @@ -253,7 +278,11 @@ func LookupDependencyFiles(files []string) (dfiles []*api.DependencyFile, err er } } else { fmt.Println("[warning] No files given, scanning current directory instead.") - files, err := getLocalDependencyFiles() + currentDir, err := os.Getwd() + if err != nil { + return dfiles, err + } + files, err := getLocalDependencyFiles(currentDir) if err != nil { return nil, err } diff --git a/dependency/dependency_file_test.go b/dependency/dependency_file_test.go index 0e452b0..3df0ed8 100644 --- a/dependency/dependency_file_test.go +++ b/dependency/dependency_file_test.go @@ -12,6 +12,10 @@ import ( "testing" "github.com/gemnasium/toolbelt/api" + "path/filepath" + "reflect" + "encoding/json" + "github.com/gemnasium/toolbelt/config" ) type TestFile struct { @@ -152,6 +156,34 @@ func TestGetFileSHA1(t *testing.T) { } } +func TestGetLocalDependencyFiles(t *testing.T) { + wantedResult := []*api.DependencyFile{ + &api.DependencyFile{ + Path: "Gemfile", + SHA: "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", + Content: []uint8{}, + }, + &api.DependencyFile{ + Path: "subdir/gems.rb", + SHA: "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", + Content: []uint8{}, + }, + } + var prettyString = func(v interface{}) string { + b, _ := json.MarshalIndent(v, "", " ") + return string(b) + } + config.IgnoredPaths = []string{"sub1/sub2/Gemfile", "sub3/sub4"} + // Get a list of recognised dependency files from test data + result, err := getLocalDependencyFiles(filepath.Join("testdata", "test_get_local_dependency_files")) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(result, wantedResult) { + t.Errorf("Expected output:\n%s\nGot:\n%s\n", prettyString(wantedResult), prettyString(result)) + } +} + func TestListDependencyFiles(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Header.Set("Content-Type", "application/json") @@ -212,7 +244,7 @@ func TestPushDependencyFiles(t *testing.T) { os.Stdout = w api.APIImpl = api.NewAPIv1(ts.URL, "") - getLocalDependencyFiles = func() ([]*api.DependencyFile, error) { + getLocalDependencyFiles = func(path string) ([]*api.DependencyFile, error) { return []*api.DependencyFile{ &api.DependencyFile{Path: "Gemfile", SHA: "Gemfile SHA-1", Content: []byte("Gemfile.lock base64 encoded content")}, &api.DependencyFile{Path: "Gemfile.lock", SHA: "Gemfile.lock SHA-1", Content: []byte("Gemfile base64 encoded content")}, diff --git a/dependency/testdata/test_get_local_dependency_files/.bundle/package.json b/dependency/testdata/test_get_local_dependency_files/.bundle/package.json new file mode 100644 index 0000000..e69de29 diff --git a/dependency/testdata/test_get_local_dependency_files/Gemfile b/dependency/testdata/test_get_local_dependency_files/Gemfile new file mode 100644 index 0000000..e69de29 diff --git a/dependency/testdata/test_get_local_dependency_files/node_modules/yarn.lock b/dependency/testdata/test_get_local_dependency_files/node_modules/yarn.lock new file mode 100644 index 0000000..e69de29 diff --git a/dependency/testdata/test_get_local_dependency_files/non_dependency_file b/dependency/testdata/test_get_local_dependency_files/non_dependency_file new file mode 100644 index 0000000..e69de29 diff --git a/dependency/testdata/test_get_local_dependency_files/sub1/sub2/Gemfile b/dependency/testdata/test_get_local_dependency_files/sub1/sub2/Gemfile new file mode 100644 index 0000000..e69de29 diff --git a/dependency/testdata/test_get_local_dependency_files/sub3/sub4/Gemfile b/dependency/testdata/test_get_local_dependency_files/sub3/sub4/Gemfile new file mode 100644 index 0000000..e69de29 diff --git a/dependency/testdata/test_get_local_dependency_files/subdir/gems.rb b/dependency/testdata/test_get_local_dependency_files/subdir/gems.rb new file mode 100644 index 0000000..e69de29 diff --git a/dependency/testdata/test_get_local_dependency_files/vendor/Gemfile b/dependency/testdata/test_get_local_dependency_files/vendor/Gemfile new file mode 100644 index 0000000..e69de29