-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make fileset take optional list of paths to list (#1684)
## Changes Before this change, the fileset library would take a single root path and list all files in it. To support an allowlist of paths to list (much like a Git `pathspec` without patterns; see [pathspec](pathspec)), this change introduces an optional argument to `fileset.New` where the caller can specify paths to list. If not specified, this argument defaults to list `.` (i.e. list all files in the root). The motivation for this change is that we wish to expose this pattern in bundles. Users should be able to specify which paths to synchronize instead of always only synchronizing the bundle root directory. [pathspec]: https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec ## Tests New and existing unit tests.
- Loading branch information
Showing
16 changed files
with
256 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package fileset | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/databricks/cli/libs/vfs" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFileSet_NoPaths(t *testing.T) { | ||
fs := New(vfs.MustNew("testdata")) | ||
files, err := fs.Files() | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Len(t, files, 4) | ||
assert.Equal(t, "dir1/a", files[0].Relative) | ||
assert.Equal(t, "dir1/b", files[1].Relative) | ||
assert.Equal(t, "dir2/a", files[2].Relative) | ||
assert.Equal(t, "dir2/b", files[3].Relative) | ||
} | ||
|
||
func TestFileSet_ParentPath(t *testing.T) { | ||
fs := New(vfs.MustNew("testdata"), []string{".."}) | ||
_, err := fs.Files() | ||
|
||
// It is impossible to escape the root directory. | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestFileSet_DuplicatePaths(t *testing.T) { | ||
fs := New(vfs.MustNew("testdata"), []string{"dir1", "dir1"}) | ||
files, err := fs.Files() | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Len(t, files, 2) | ||
assert.Equal(t, "dir1/a", files[0].Relative) | ||
assert.Equal(t, "dir1/b", files[1].Relative) | ||
} | ||
|
||
func TestFileSet_OverlappingPaths(t *testing.T) { | ||
fs := New(vfs.MustNew("testdata"), []string{"dir1", "dir1/a"}) | ||
files, err := fs.Files() | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Len(t, files, 2) | ||
assert.Equal(t, "dir1/a", files[0].Relative) | ||
assert.Equal(t, "dir1/b", files[1].Relative) | ||
} | ||
|
||
func TestFileSet_IgnoreDirError(t *testing.T) { | ||
testError := errors.New("test error") | ||
fs := New(vfs.MustNew("testdata")) | ||
fs.SetIgnorer(testIgnorer{dirErr: testError}) | ||
_, err := fs.Files() | ||
assert.ErrorIs(t, err, testError) | ||
} | ||
|
||
func TestFileSet_IgnoreDir(t *testing.T) { | ||
fs := New(vfs.MustNew("testdata")) | ||
fs.SetIgnorer(testIgnorer{dir: []string{"dir1"}}) | ||
files, err := fs.Files() | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Len(t, files, 2) | ||
assert.Equal(t, "dir2/a", files[0].Relative) | ||
assert.Equal(t, "dir2/b", files[1].Relative) | ||
} | ||
|
||
func TestFileSet_IgnoreFileError(t *testing.T) { | ||
testError := errors.New("test error") | ||
fs := New(vfs.MustNew("testdata")) | ||
fs.SetIgnorer(testIgnorer{fileErr: testError}) | ||
_, err := fs.Files() | ||
assert.ErrorIs(t, err, testError) | ||
} | ||
|
||
func TestFileSet_IgnoreFile(t *testing.T) { | ||
fs := New(vfs.MustNew("testdata")) | ||
fs.SetIgnorer(testIgnorer{file: []string{"dir1/a"}}) | ||
files, err := fs.Files() | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Len(t, files, 3) | ||
assert.Equal(t, "dir1/b", files[0].Relative) | ||
assert.Equal(t, "dir2/a", files[1].Relative) | ||
assert.Equal(t, "dir2/b", files[2].Relative) | ||
} | ||
|
||
type testIgnorer struct { | ||
// dir is a list of directories to ignore. Strings are compared verbatim. | ||
dir []string | ||
|
||
// dirErr is an error to return when IgnoreDirectory is called. | ||
dirErr error | ||
|
||
// file is a list of files to ignore. Strings are compared verbatim. | ||
file []string | ||
|
||
// fileErr is an error to return when IgnoreFile is called. | ||
fileErr error | ||
} | ||
|
||
// IgnoreDirectory returns true if the path is in the dir list. | ||
// If dirErr is set, it returns dirErr. | ||
func (t testIgnorer) IgnoreDirectory(path string) (bool, error) { | ||
if t.dirErr != nil { | ||
return false, t.dirErr | ||
} | ||
|
||
for _, d := range t.dir { | ||
if d == path { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
// IgnoreFile returns true if the path is in the file list. | ||
// If fileErr is set, it returns fileErr. | ||
func (t testIgnorer) IgnoreFile(path string) (bool, error) { | ||
if t.fileErr != nil { | ||
return false, t.fileErr | ||
} | ||
|
||
for _, f := range t.file { | ||
if f == path { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../dir1/a |
Oops, something went wrong.