Skip to content

Commit

Permalink
writing test cases for FindFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
osamaadam committed Dec 25, 2023
1 parent 75453b6 commit 4510833
Showing 1 changed file with 104 additions and 2 deletions.
106 changes: 104 additions & 2 deletions core/findfiles_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package core

import "testing"
import (
"os"
"path/filepath"
"testing"

cf "github.com/osamaadam/cfgrr/configfile"
"github.com/osamaadam/cfgrr/helpers"
"github.com/osamaadam/cfgrr/ignorefile"
)

func TestCheckGlobsMatch(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -30,5 +38,99 @@ func TestCheckGlobsMatch(t *testing.T) {
}

func TestFindFiles(t *testing.T) {
t.Skip("TODO: I don't know how to test this function without creating a bunch of files and directories in the test directory. I don't want to do that.")
backupDir := t.TempDir()
_createFiles(
backupDir,
".vimrc",
".bashrc",
".zshrc",
".config/nvim/init.vim",
".config/nvim/coc-settings.json",
)

tests := []struct {
name string
filesToIgnore []string
patterns []string
expectedFiles []string
expectErr bool
}{
{"no patterns", []string{}, []string{}, []string{}, true},
{"accept all, ignore none", []string{}, []string{"**/*"}, []string{
".vimrc",
".bashrc",
".zshrc",
".config/nvim/init.vim",
".config/nvim/coc-settings.json",
}, false},
{"ignore specific patterns", []string{"**/*rc"}, []string{"**/*"}, []string{
".config/nvim/init.vim",
".config/nvim/coc-settings.json",
}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
igContainer := _createIgnoreFile(t.TempDir(), tt.filesToIgnore...)

files, err := FindFiles(backupDir, igContainer, tt.patterns...)
if tt.expectErr && err != nil {
return
} else if tt.expectErr && err == nil {
t.Fatalf("expected error, got nil")
} else if !tt.expectErr && err != nil {
t.Fatalf("unexpected error: %v", err)
}

if len(files) != len(tt.expectedFiles) {
t.Fatalf("expected %d files, got %d", len(tt.expectedFiles), len(files))
}

for _, file := range files {
relativePath, err := filepath.Rel(backupDir, file.PathAbs())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !helpers.Contains(tt.expectedFiles, relativePath) {
t.Errorf("expected to find %s but didn't", relativePath)
}
}
})
}
}

func _createFiles(dir string, names ...string) []*cf.ConfigFile {
numOfFiles := len(names)
files := make([]*cf.ConfigFile, numOfFiles)

for i, name := range names {
filePath := filepath.Join(dir, name)
fileDir := filepath.Dir(filePath)
if err := os.MkdirAll(fileDir, 0755); err != nil {
panic(err)
}
f, err := os.Create(filePath)
if err != nil {
panic(err)
}
f.Close()
files[i], _ = cf.NewConfigFile(f.Name())
}

return files
}

func _createIgnoreFile(backupDir string, patterns ...string) ignorefile.IIgnoresContainer {
ignFile, err := ignorefile.InitDefaultIgnoreFile()
if err != nil {
panic(err)
}

if err := ignFile.WriteLines(patterns...); err != nil {
panic(err)
}

ignContainer := ignorefile.NewIgnoresContainer(filepath.Base(ignFile.Path()))

return ignContainer
}

0 comments on commit 4510833

Please sign in to comment.