Skip to content

Commit

Permalink
Merge branch '4_ignore_node_modules_and_bundle_dirs' into 'master'
Browse files Browse the repository at this point in the history
Files in "node_modules" and ".bundle" must be ignored when scanning current folder.


See merge request gemnasium/toolbelt!9
  • Loading branch information
groulot committed Jan 11, 2018
2 parents 5376e79 + 0fe7ff8 commit afbcf75
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 20 deletions.
67 changes: 48 additions & 19 deletions dependency/dependency_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
34 changes: 33 additions & 1 deletion dependency/dependency_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"testing"

"github.com/gemnasium/toolbelt/api"
"path/filepath"
"reflect"
"encoding/json"
"github.com/gemnasium/toolbelt/config"
)

type TestFile struct {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")},
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit afbcf75

Please sign in to comment.