-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove buggy inner loop from git dirty check (#117)
This change removes an inner loop introduced in `CheckDirDirty` that was not correctly skipping over files we should be ignoring when doing dirty checks of a working directory.
- Loading branch information
1 parent
ca00e47
commit 95d7dd9
Showing
5 changed files
with
118 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This folder is the base of git repositories created in tests. |
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,3 @@ | ||
foo | ||
bar | ||
baz |
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 @@ | ||
another boring 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
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,107 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-git/go-billy/v5" | ||
"github.com/go-git/go-billy/v5/memfs" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/go-git/go-git/v5/storage/memory" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func newTestRepo(t *testing.T) (*git.Repository, billy.Filesystem) { | ||
t.Helper() | ||
|
||
mfs := memfs.New() | ||
|
||
err := filepath.WalkDir("./fixtures", func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if d.IsDir() { | ||
return nil | ||
} | ||
|
||
fixture, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer fixture.Close() | ||
|
||
f, err := mfs.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
_, err = io.Copy(f, fixture) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
require.NoError(t, err, "expected to walk the fixture directory") | ||
|
||
storage := memory.NewStorage() | ||
repo, err := git.Init(storage, mfs) | ||
require.NoError(t, err, "expected empty repo to be initialized") | ||
|
||
wt, err := repo.Worktree() | ||
require.NoError(t, err, "expected to get worktree") | ||
|
||
_, err = wt.Add(".") | ||
require.NoError(t, err, "expected to add all files") | ||
|
||
_, err = wt.Commit("initial commit", &git.CommitOptions{ | ||
Author: &object.Signature{ | ||
Name: "Test User", | ||
Email: "test@example.com", | ||
When: time.Unix(0, 0), | ||
}, | ||
}) | ||
require.NoError(t, err, "expected to commit all files") | ||
|
||
return repo, mfs | ||
} | ||
|
||
func TestGit_CheckDirDirty(t *testing.T) { | ||
repo, mfs := newTestRepo(t) | ||
|
||
f, err := mfs.Create("dirty-file") | ||
require.NoError(t, err, "expected to create a dirty file") | ||
defer f.Close() | ||
fmt.Fprintln(f, "sample content") | ||
|
||
g := Git{repo: repo} | ||
dirty, str, err := g.CheckDirDirty(".", map[string]string{}) | ||
require.NoError(t, err, "expected to check the directory") | ||
|
||
require.Equal(t, `new file found: []string{"dirty-file"}`, str) | ||
require.True(t, dirty, "expected the directory to be dirty") | ||
} | ||
|
||
func TestGit_CheckDirDirty_IgnoredFiles(t *testing.T) { | ||
repo, mfs := newTestRepo(t) | ||
|
||
f, err := mfs.Create("workflow.lock") | ||
require.NoError(t, err, "expected to create a dirty file") | ||
defer f.Close() | ||
fmt.Fprintln(f, "sample content") | ||
|
||
g := Git{repo: repo} | ||
dirty, str, err := g.CheckDirDirty(".", map[string]string{}) | ||
require.NoError(t, err, "expected to check the directory") | ||
|
||
require.Equal(t, "", str, "expected no dirty files reported") | ||
require.False(t, dirty, "expected the directory to be clean") | ||
} |