Skip to content

Commit

Permalink
Add directory structure of includes (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhenrik13 authored Oct 9, 2024
1 parent 8ba20e2 commit dd3542f
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 39 deletions.
43 changes: 39 additions & 4 deletions filemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (m *FileManager) SnapshotLocal(localPath string) error {
return err
}

if m.matchExcludes(fileName) && !m.matchIncludes(fileName) {
if m.matchExcludes(fileName) && !m.matchIncludes(localPath, fileName) {
return nil
}

Expand All @@ -298,15 +298,50 @@ func (m *FileManager) matchExcludes(path string) bool {
if len(m.excludes) == 0 {
return false
}

return matchRElist(path, m.excludes)
}

// matchIncludes determines if a path matches any of the include filters.
func (m *FileManager) matchIncludes(path string) bool {
func (m *FileManager) matchIncludes(basePath, path string) bool {
if len(m.includes) == 0 {
return true
return false
}

if !matchRElist(path, m.includes) {
return false
}

// add directories to the list of files
dirName := filepath.Dir(path)
baseName := filepath.Base(dirName)

// iterate until dir and base is the same
for dirName != baseName {
// skip if already in the list
if _, ok := m.localFiles[path]; ok {
continue
}

absolutePath := filepath.Join(basePath, dirName)
fileInfo, err := os.Stat(absolutePath)

// this should never happen
if os.IsNotExist(err) {
return false
}

m.localFiles[dirName] = File{
Name: dirName,
IsDir: true,
Path: filepath.Join(basePath, dirName),
Size: int(fileInfo.Size()),
}

dirName = filepath.Dir(dirName)
baseName = filepath.Base(dirName)
}
return matchRElist(path, m.includes)
return true
}

// matchRElist determines if a pattern matches a list of regular expressions.
Expand Down
158 changes: 123 additions & 35 deletions filemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func Test_FileManager_Token_Refresh(t *testing.T) {
if err != nil {
t.Fatal(err)
}
cli.authToken = "invalid"

cli.WithAuthToken("invalid")

m := NewFileManager().WithClient(cli)
if err := m.SnapshotRemote(ctx, "/"); err != nil {
Expand All @@ -29,38 +30,6 @@ func Test_FileManager_Token_Refresh(t *testing.T) {

}

func Test_FileManager_Exclude_Include(t *testing.T) {

if !testingHasCredentials {
t.Skip("Skipping test because QBEE_EMAIL and QBEE_PASSWORD are not set")
}

ctx := context.Background()

cli, err := LoginGetAuthenticatedClient(ctx)
if err != nil {
t.Fatal(err)
}

m := NewFileManager().WithClient(cli).WithDryRun(true).WithExcludes("cmd/,.git").WithIncludes("cmd/filemanager.go")

if err := m.SnapshotLocal("."); err != nil {
t.Fatal(err)
}

list := m.GetLocalSnapshot()
if len(list) == 0 {
t.Fatal("should have 1 file")
}

for _, f := range list {
if f.Path == "cmd/filemanager.go" {
return
}
}
t.Fatal("file not found")
}

func Test_FileManager_Upload_Download(t *testing.T) {
if !testingHasCredentials {
t.Skip("Skipping test because QBEE_EMAIL and QBEE_PASSWORD are not set")
Expand Down Expand Up @@ -119,11 +88,13 @@ func Test_FileManager_Sync(t *testing.T) {

m := NewFileManager().WithClient(cli).WithDelete(true)

if err := m.Sync(ctx, ".github", "/.github"); err != nil {
testDir := createTestDirectoryStructure(t)

if err := m.Sync(ctx, testDir, "/testDir"); err != nil {
t.Fatal(err)
}

if err := m.Sync(ctx, ".github", "/"); err != nil {
if err := m.Sync(ctx, testDir, "/"); err != nil {
t.Fatal(err)
}

Expand All @@ -148,5 +119,122 @@ func Test_FileManager_Sync(t *testing.T) {
if len(files) != 0 {
t.Fatal("files found")
}
}

func Test_FileManager_Exclude(t *testing.T) {

if !testingHasCredentials {
t.Skip("Skipping test because QBEE_EMAIL and QBEE_PASSWORD are not set")
}

ctx := context.Background()

cli, err := LoginGetAuthenticatedClient(ctx)
if err != nil {
t.Fatal(err)
}

testDir := createTestDirectoryStructure(t)

m := NewFileManager().WithClient(cli).WithDryRun(true).WithExcludes("subdir")

if err := m.SnapshotLocal(testDir); err != nil {
t.Fatal(err)
}

list := m.GetLocalSnapshot()

tt := []struct {
path string
shouldExist bool
}{
{"testdir/subdir", false},
{"testdir", true},
{"testdir/subdir/testfile2.txt", false},
{"testdir/subdir/testfile3.txt", false},
}

for _, tc := range tt {
if _, ok := list[tc.path]; ok != tc.shouldExist {
t.Fatalf("expected %s to exist: %v", tc.path, tc.shouldExist)
}
}

}

func Test_FileManager_Exlude_Include(t *testing.T) {
if !testingHasCredentials {
t.Skip("Skipping test because QBEE_EMAIL and QBEE_PASSWORD are not set")
}

ctx := context.Background()

cli, err := LoginGetAuthenticatedClient(ctx)
if err != nil {
t.Fatal(err)
}

testDir := createTestDirectoryStructure(t)

m := NewFileManager().
WithClient(cli).
WithDryRun(true).
WithExcludes("subdir").
WithIncludes("subdir/testfile2.txt")

if err := m.SnapshotLocal(testDir); err != nil {
t.Fatal(err)
}

list := m.GetLocalSnapshot()
if len(list) == 0 {
t.Fatal("should have 1 file")
}

tt := []struct {
path string
shouldExist bool
}{
{"testdir/subdir", true},
{"testdir", true},
{"testdir/subdir/testfile2.txt", true},
{"testdir/subdir/testfile3.txt", false},
}

for _, tc := range tt {
if _, ok := list[tc.path]; ok != tc.shouldExist {
t.Fatalf("expected %s to exist: %v", tc.path, tc.shouldExist)
}
}
}

func createTestDirectoryStructure(t *testing.T) string {

tmpDir := t.TempDir()

testDir := filepath.Join(tmpDir, "testdir")

if err := os.MkdirAll(testDir, 0755); err != nil {
t.Fatal(err)
}

if err := os.WriteFile(filepath.Join(testDir, "testfile.txt"), []byte("testfile"), 0600); err != nil {
t.Fatal(err)
}

testSubDir := filepath.Join(testDir, "subdir")

if err := os.MkdirAll(testSubDir, 0755); err != nil {
t.Fatal(err)
}

if err := os.WriteFile(filepath.Join(testSubDir, "testfile2.txt"), []byte("testfile2"), 0600); err != nil {
t.Fatal(err)
}

if err := os.WriteFile(filepath.Join(testSubDir, "testfile3.txt"), []byte("testfile2"), 0600); err != nil {
t.Fatal(err)
}

return tmpDir
}

0 comments on commit dd3542f

Please sign in to comment.