1
0
mirror of https://github.com/moby/buildkit.git synced 2025-04-18 18:04:03 +03:00
buildkit/source/git/identifier_test.go
Justin Chadwell 9a51bb0ff1 git: use custom giturl type to preserve original remote
This resolves a regression introduced in
50e75e3565b0e301a364d64000c4739e1abdc5f1. In this previous patch, I'd
incorrectly assumed that scp-like URLs can express a subset of
"standard"-URLs and so we can always safely convert them for
consistency. This isn't true - the URL "git@example.com:foo" should be
resolved to the home directory of the host, however, the converted URL
"ssh://git@example.com/foo" will be resolved to the root of the host.

To resolve this, we need to not perform this conversion. However, we
also need preserve the behaviour of firm distinction between SCP and
normal URL types (so as to keep proper port parsing).

To do this, we add a new GitURL type to the gitutil package. This new
type contains all useful fields shared in common between the standard
libraries url package and our custom scp-style url parsing package. This
keeps the previous property of a single clean interface to all GitURLs,
while also ensuring that we preserve the original URL to pass to the Git
CLI (making sure we strip fragments out, which are used as
buildkit-level metadata).

As a side-effect of this, the client-side calling code for parsing
git urls is simplified (so we don't have to do fragment wrangling at
every call point).

Signed-off-by: Justin Chadwell <me@jedevc.com>
2023-10-12 19:17:11 +01:00

121 lines
2.9 KiB
Go

package git
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestNewGitIdentifier(t *testing.T) {
tests := []struct {
url string
expected GitIdentifier
}{
{
url: "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git",
expected: GitIdentifier{
Remote: "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git",
},
},
{
url: "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git#main",
expected: GitIdentifier{
Remote: "ssh://root@subdomain.example.hostname:2222/root/my/really/weird/path/foo.git",
Ref: "main",
},
},
{
url: "git@github.com:moby/buildkit.git",
expected: GitIdentifier{
Remote: "git@github.com:moby/buildkit.git",
},
},
{
url: "github.com/moby/buildkit.git#main",
expected: GitIdentifier{
Remote: "https://github.com/moby/buildkit.git",
Ref: "main",
},
},
{
url: "git://github.com/user/repo.git",
expected: GitIdentifier{
Remote: "git://github.com/user/repo.git",
},
},
{
url: "git://github.com/user/repo.git#mybranch:mydir/mysubdir/",
expected: GitIdentifier{
Remote: "git://github.com/user/repo.git",
Ref: "mybranch",
Subdir: "mydir/mysubdir/",
},
},
{
url: "git://github.com/user/repo.git#:mydir/mysubdir/",
expected: GitIdentifier{
Remote: "git://github.com/user/repo.git",
Subdir: "mydir/mysubdir/",
},
},
{
url: "https://github.com/user/repo.git",
expected: GitIdentifier{
Remote: "https://github.com/user/repo.git",
},
},
{
url: "https://github.com/user/repo.git#mybranch:mydir/mysubdir/",
expected: GitIdentifier{
Remote: "https://github.com/user/repo.git",
Ref: "mybranch",
Subdir: "mydir/mysubdir/",
},
},
{
url: "git@github.com:user/repo.git",
expected: GitIdentifier{
Remote: "git@github.com:user/repo.git",
},
},
{
url: "git@github.com:user/repo.git#mybranch:mydir/mysubdir/",
expected: GitIdentifier{
Remote: "git@github.com:user/repo.git",
Ref: "mybranch",
Subdir: "mydir/mysubdir/",
},
},
{
url: "ssh://github.com/user/repo.git",
expected: GitIdentifier{
Remote: "ssh://github.com/user/repo.git",
},
},
{
url: "ssh://github.com/user/repo.git#mybranch:mydir/mysubdir/",
expected: GitIdentifier{
Remote: "ssh://github.com/user/repo.git",
Ref: "mybranch",
Subdir: "mydir/mysubdir/",
},
},
{
url: "ssh://foo%40barcorp.com@github.com/user/repo.git#mybranch:mydir/mysubdir/",
expected: GitIdentifier{
Remote: "ssh://foo%40barcorp.com@github.com/user/repo.git",
Ref: "mybranch",
Subdir: "mydir/mysubdir/",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.url, func(t *testing.T) {
gi, err := NewGitIdentifier(tt.url)
require.NoError(t, err)
require.Equal(t, tt.expected, *gi)
})
}
}