From 45108336bb0dfd0abdcec5a582ef14d1ae909961 Mon Sep 17 00:00:00 2001 From: Osama Adam Date: Mon, 25 Dec 2023 23:05:04 +0200 Subject: [PATCH] writing test cases for `FindFiles` --- core/findfiles_test.go | 106 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/core/findfiles_test.go b/core/findfiles_test.go index 5e6f1ce..bf4d58d 100644 --- a/core/findfiles_test.go +++ b/core/findfiles_test.go @@ -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 { @@ -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 }