diff options
author | Paulo Gomes <paulo.gomes@suse.com> | 2023-12-08 08:55:30 +0000 |
---|---|---|
committer | Paulo Gomes <paulo.gomes@suse.com> | 2023-12-08 09:17:42 +0000 |
commit | b2c19824771bbcbb21abb51abb319c1a610aa6b3 (patch) | |
tree | 33cbc5438381ea137e910cb7ee702b431ae16415 /worktree_test.go | |
parent | cec7da63ca0412fce55a0bf0715b7ba44a41eaa2 (diff) | |
download | go-git-b2c19824771bbcbb21abb51abb319c1a610aa6b3.tar.gz |
git: worktree, Align validation with upstream rules
Some worktree validation rules observed upstream are not checked
by go-git, leading to scenarios in which what seems to be a valid
repository for go-git is not necessarily the case for the git cli.
Signed-off-by: Paulo Gomes <paulo.gomes@suse.com>
Diffstat (limited to 'worktree_test.go')
-rw-r--r-- | worktree_test.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/worktree_test.go b/worktree_test.go index 3e057a7..50ff189 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "fmt" "io" "os" "path/filepath" @@ -2747,3 +2748,77 @@ func (s *WorktreeSuite) TestLinkedWorktree(c *C) { c.Assert(err, Equals, ErrRepositoryIncomplete) } } + +func TestValidPath(t *testing.T) { + type testcase struct { + path string + wantErr bool + } + + tests := []testcase{ + {".git", true}, + {".git/b", true}, + {".git\\b", true}, + {"git~1", true}, + {"a/../b", true}, + {"a\\..\\b", true}, + {".gitmodules", false}, + {".gitignore", false}, + {"a..b", false}, + {".", false}, + {"a/.git", false}, + {"a\\.git", false}, + {"a/.git/b", false}, + {"a\\.git\\b", false}, + } + + if runtime.GOOS == "windows" { + tests = append(tests, []testcase{ + {"\\\\a\\b", true}, + {"C:\\a\\b", true}, + {".git . . .", true}, + {".git . . ", true}, + {".git ", true}, + {".git.", true}, + {".git::$INDEX_ALLOCATION", true}, + }...) + } + + for _, tc := range tests { + t.Run(fmt.Sprintf("%s", tc.path), func(t *testing.T) { + err := validPath(tc.path) + if tc.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestWindowsValidPath(t *testing.T) { + tests := []struct { + path string + want bool + }{ + {".git", false}, + {".git . . .", false}, + {".git ", false}, + {".git ", false}, + {".git . .", false}, + {".git . .", false}, + {".git::$INDEX_ALLOCATION", false}, + {".git:", false}, + {"a", true}, + {"a\\b", true}, + {"a/b", true}, + {".gitm", true}, + } + + for _, tc := range tests { + t.Run(fmt.Sprintf("%s", tc.path), func(t *testing.T) { + got := windowsValidPath(tc.path) + assert.Equal(t, tc.want, got) + }) + } +} |