Skip to content

Commit

Permalink
update fs repo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mesuutt committed May 24, 2023
1 parent 9eb092a commit af0ef2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
4 changes: 3 additions & 1 deletion pkg/repo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func (r fsRepo) AddStats(diff commit.Diff) error {
return fmt.Errorf("changes could not write to file: `%s`. error: %w", filePath, err)
}

return f.Close()
if err := f.Close(); err != nil {
return err
}
}

return nil
Expand Down
57 changes: 26 additions & 31 deletions pkg/repo/fsrepo_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package repo

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/mesuutt/git-mirror/pkg/commit"
)
Expand All @@ -14,58 +13,54 @@ func TestRepo_AddChange(t *testing.T) {
repoPath, _ := os.MkdirTemp("", "")
t.Cleanup(func() { os.RemoveAll(repoPath) })

// commitGen := commit.NewDiffGenerator(filepath.Join(repoPath, "config.toml"))

repo := NewFsRepo(repoPath)

now := time.Now()
tests := []struct {
name string
stats []commit.FileStat
fileName string
content string
name string
changes []commit.Change
}{
{
name: "one commit",
stats: []commit.FileStat{{Insert: 1, Delete: 2, Ext: ".go"}},
fileName: "log.go",
content: "1 insertion(s), 2 deletion(s)\n",
name: "one commit",
changes: []commit.Change{
{Insertion: 1, Deletion: 2, Filename: "log.go", Dir: "2023/01/01", Text: "1 insertion(s), 2 deletion(s)"},
},
},
{
name: "same file type, two commit",
stats: []commit.FileStat{{Insert: 1, Delete: 2, Ext: ".rs"}, {Insert: 2, Delete: 2, Ext: ".rs"}},
fileName: "log.rs",
content: "1 insertion(s), 2 deletion(s)\n2 insertion(s), 2 deletion(s)\n",
name: "two commit",
changes: []commit.Change{
{Insertion: 3, Deletion: 4, Filename: "log.rs", Dir: "2023/01/02", Text: "3 insertion(s), 4 deletion(s)"},
{Insertion: 2, Deletion: 3, Filename: "log.go", Dir: "2023/01/02", Text: "2 insertion(s), 3 deletion(s)"},
},
},
{
name: "file without extension",
stats: []commit.FileStat{{Insert: 1, Delete: 2, Ext: "Makefile"}},
fileName: "Makefile",
content: "1 insertion(s), 2 deletion(s)\n",
name: "file without extension",
changes: []commit.Change{{Insertion: 1, Deletion: 2, Filename: "Makefile", Dir: "2023/01/03", Text: "1 insertion(s), 2 deletion(s)"}},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
dayParts := strings.Split(now.Format("2006-01-02"), "-")
path := filepath.Join(repoPath, dayParts[0], dayParts[1], dayParts[2], tt.fileName)

err := repo.AddStats(tt.stats...)
err := repo.AddStats(commit.Diff{Changes: tt.changes})

if err != nil {
t.Errorf("stat add failed: %v", err)
}

if _, err := os.Stat(path); err != nil {
t.Fatalf("expected stat file not found in repo: %s", path)
}
for _, change := range tt.changes {
path := filepath.Join(repoPath, change.Dir, change.Filename)
if _, err := os.Stat(path); err != nil {
t.Fatalf("expected stat file not found in repo: %s", path)
}

content, _ := os.ReadFile(path)
content, _ := os.ReadFile(path)

if string(content) != tt.content {
t.Fatalf("file content not mathed. want: `%s`, got: `%s`", tt.content, string(content))
expectedContent := fmt.Sprintf("%s\n", change.Text)
if string(content) != expectedContent {
t.Fatalf("file content not mathed. want: `%s`, got: `%s`", expectedContent, string(content))
}
}

})
}
}

0 comments on commit af0ef2a

Please sign in to comment.