Skip to content

Commit

Permalink
fix: test cases for subpath tag
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
  • Loading branch information
89luca89 committed Feb 13, 2024
1 parent b97748e commit b4a913e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
1 change: 0 additions & 1 deletion cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (cmd *UpCmd) Run(
if client.WorkspaceConfig().Source.GitSubPath != "" {
result.SubstitutionContext.ContainerWorkspaceFolder = filepath.Join(result.SubstitutionContext.ContainerWorkspaceFolder, client.WorkspaceConfig().Source.GitSubPath)
workdir = result.SubstitutionContext.ContainerWorkspaceFolder

}

// configure container ssh
Expand Down
34 changes: 17 additions & 17 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
)

const (
CommitDelimiter string = "@sha256:"
PullRequestReference string = "pull/([0-9]+)/head"
SubPathDelimiter string = "@subpath:"
CommitDelimiter string = "@sha256:"
PullRequestReference string = "pull/([0-9]+)/head"
SubPathDelimiter string = "@subpath:"
)

var (
branchRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@/]+/?[^@/]+)@([a-zA-Z0-9\./\-\_]+)$`)
commitRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@]+)` + regexp.QuoteMeta(CommitDelimiter) + `([a-zA-Z0-9]+)$`)
prReferenceRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@]+)@(` + PullRequestReference + `)$`)
subPathRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@]+)` + regexp.QuoteMeta(SubPathDelimiter) + `([a-zA-Z0-9\./\-\_]+)$`)
branchRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@/]+/?[^@/]+)@([a-zA-Z0-9\./\-\_]+)$`)
commitRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@]+)` + regexp.QuoteMeta(CommitDelimiter) + `([a-zA-Z0-9]+)$`)
prReferenceRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@]+)@(` + PullRequestReference + `)$`)
subPathRegEx = regexp.MustCompile(`^([^@]*(?:git@)?[^@/]+/[^@]+)` + regexp.QuoteMeta(SubPathDelimiter) + `([a-zA-Z0-9\./\-\_]+)$`)
)

func CommandContext(ctx context.Context, args ...string) *exec.Cmd {
Expand Down Expand Up @@ -91,20 +91,20 @@ func GetBranchNameForPR(ref string) string {
}

type GitInfo struct {
Repository string
Branch string
Commit string
PR string
SubPath string
Repository string
Branch string
Commit string
PR string
SubPath string
}

func NewGitInfo(repository, branch, commit, pr, subpath string) *GitInfo {
return &GitInfo{
Repository: repository,
Branch: branch,
Commit: commit,
PR: pr,
SubPath: subpath,
Repository: repository,
Branch: branch,
Commit: commit,
PR: pr,
SubPath: subpath,
}
}

Expand Down
27 changes: 26 additions & 1 deletion pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type testCaseNormalizeRepository struct {
expectedRepo string
expectedBranch string
expectedCommit string
expectedSubpath string
}

type testCaseGetBranchNameForPR struct {
Expand All @@ -28,113 +29,137 @@ func TestNormalizeRepository(t *testing.T) {
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "ssh://git@github.com/loft-sh/devpod.git",
expectedRepo: "ssh://git@github.com/loft-sh/devpod.git",
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "git@github.com/loft-sh/devpod-without-branch.git",
expectedRepo: "git@github.com/loft-sh/devpod-without-branch.git",
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "https://github.com/loft-sh/devpod.git",
expectedRepo: "https://github.com/loft-sh/devpod.git",
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "github.com/loft-sh/devpod.git",
expectedRepo: "https://github.com/loft-sh/devpod.git",
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "github.com/loft-sh/devpod.git@test-branch",
expectedRepo: "https://github.com/loft-sh/devpod.git",
expectedPRReference: "",
expectedBranch: "test-branch",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "git@github.com/loft-sh/devpod-with-branch.git@test-branch",
expectedRepo: "git@github.com/loft-sh/devpod-with-branch.git",
expectedPRReference: "",
expectedBranch: "test-branch",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "git@github.com/loft-sh/devpod-with-branch.git@test_branch",
expectedRepo: "git@github.com/loft-sh/devpod-with-branch.git",
expectedPRReference: "",
expectedBranch: "test_branch",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "ssh://git@github.com/loft-sh/devpod.git@test_branch",
expectedRepo: "ssh://git@github.com/loft-sh/devpod.git",
expectedPRReference: "",
expectedBranch: "test_branch",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "github.com/loft-sh/devpod-without-protocol-with-slash.git@user/branch",
expectedRepo: "https://github.com/loft-sh/devpod-without-protocol-with-slash.git",
expectedPRReference: "",
expectedBranch: "user/branch",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "git@github.com/loft-sh/devpod-with-slash.git@user/branch",
expectedRepo: "git@github.com/loft-sh/devpod-with-slash.git",
expectedPRReference: "",
expectedBranch: "user/branch",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "github.com/loft-sh/devpod.git@sha256:905ffb0",
expectedRepo: "https://github.com/loft-sh/devpod.git",
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "905ffb0",
expectedSubpath: "",
},
{
in: "git@github.com:loft-sh/devpod.git@sha256:905ffb0",
expectedRepo: "git@github.com:loft-sh/devpod.git",
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "905ffb0",
expectedSubpath: "",
},
{
in: "github.com/loft-sh/devpod.git@pull/996/head",
expectedRepo: "https://github.com/loft-sh/devpod.git",
expectedPRReference: "pull/996/head",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "git@github.com:loft-sh/devpod.git@pull/996/head",
expectedRepo: "git@github.com:loft-sh/devpod.git",
expectedPRReference: "pull/996/head",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "",
},
{
in: "github.com/loft-sh/devpod-without-protocol-with-slash.git@subpath:/test/path",
expectedRepo: "https://github.com/loft-sh/devpod-without-protocol-with-slash.git",
expectedPRReference: "",
expectedBranch: "",
expectedCommit: "",
expectedSubpath: "/test/path",
},
}

for _, testCase := range testCases {
outRepo, outPRReference, outBranch, outCommit := NormalizeRepository(testCase.in)
outRepo, outPRReference, outBranch, outCommit, outSubpath := NormalizeRepository(testCase.in)
assert.Check(t, cmp.Equal(testCase.expectedRepo, outRepo))
assert.Check(t, cmp.Equal(testCase.expectedPRReference, outPRReference))
assert.Check(t, cmp.Equal(testCase.expectedBranch, outBranch))
assert.Check(t, cmp.Equal(testCase.expectedCommit, outCommit))
assert.Check(t, cmp.Equal(testCase.expectedSubpath, outSubpath))
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func ParseWorkspaceSource(source string) *WorkspaceSource {
GitPRReference: gitPRReference,
GitBranch: gitBranch,
GitCommit: gitCommit,
GitSubPath: gitSubdir,
GitSubPath: gitSubdir,
}
} else if strings.HasPrefix(source, WorkspaceSourceLocal) {
return &WorkspaceSource{
Expand Down
8 changes: 5 additions & 3 deletions pkg/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func resolve(
GitPRReference: gitPRReference,
GitBranch: gitBranch,
GitCommit: gitCommit,
GitSubPath: gitSubdir,
GitSubPath: gitSubdir,
}
return workspace, nil
}
Expand Down Expand Up @@ -525,8 +525,10 @@ func getProjectImage(link string) string {
return ""
}

var workspaceIDRegEx1 = regexp.MustCompile(`[^\w\-]`)
var workspaceIDRegEx2 = regexp.MustCompile(`[^0-9a-z\-]+`)
var (
workspaceIDRegEx1 = regexp.MustCompile(`[^\w\-]`)
workspaceIDRegEx2 = regexp.MustCompile(`[^0-9a-z\-]+`)
)

func ToID(str string) string {
str = strings.ToLower(filepath.ToSlash(str))
Expand Down

0 comments on commit b4a913e

Please sign in to comment.