Skip to content

Commit

Permalink
allow for more file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ck3rk3y committed Oct 9, 2023
1 parent 078d650 commit 67efe6b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
emptyTagBranchOrCommit = ""

packageRootPrefixIndicatorInRelativeLocators = "/"
substrNotPresent = -1
extensionCharacter = "."
)

// ParsedGitURL an object representing a parsed moduleURL
Expand Down Expand Up @@ -130,16 +132,28 @@ func parseOutTagBranchOrCommit(input string) (string, string) {
cleanInput := path.Clean(input)
pathWithoutVersion, maybeTagBranchOrCommitWithFile, _ := strings.Cut(cleanInput, tagBranchOrCommitDelimiter)

// input can have been set with version in two diff ways
// input can have been set with version in few diff ways
// 1- github.com/kurtosis-tech/sample-dependency-package/main.star@branch-or-version (when is called from cli run command)
// 2- github.com/kurtosis-tech/sample-dependency-package@branch-or-version/main.star (when is declared in the replace section of the kurtosis.yml file)
// 3- github.com/kurtosis-tech/sample-dependency-package/main.star@foo/bar - here the tag is foo/bar;
// 3- github.com/kurtosis-tech/sample-dependency-package@foo/bar/mains.tar - here the tag is foo/bar; while file is /kurtosis-tech/sample-dependency-package/main.star
// we check if there is a file in maybeTagBranchOrCommitWithFile and then add it to pathWithoutVersion
maybeTagBranchOrCommit, maybeFileNameAndExtension, _ := strings.Cut(maybeTagBranchOrCommitWithFile, urlPathSeparator)
maybeTagBranchOrCommit, lastSectionOfTagBranchCommitWithFile, _ := cutLast(maybeTagBranchOrCommitWithFile, urlPathSeparator)

if maybeFileNameAndExtension != "" {
if lastSectionOfTagBranchCommitWithFile != "" && strings.Contains(lastSectionOfTagBranchCommitWithFile, extensionCharacter) {
// we assume pathWithoutVersion does not contain a file inside yet
pathWithoutVersion = path.Join(pathWithoutVersion, maybeFileNameAndExtension)
pathWithoutVersion = path.Join(pathWithoutVersion, lastSectionOfTagBranchCommitWithFile)
} else if lastSectionOfTagBranchCommitWithFile != "" && !strings.Contains(lastSectionOfTagBranchCommitWithFile, extensionCharacter) {
maybeTagBranchOrCommit = path.Join(maybeTagBranchOrCommit, lastSectionOfTagBranchCommitWithFile)
}

return pathWithoutVersion, maybeTagBranchOrCommit
}

func cutLast(pathToCut string, separator string) (string, string, bool) {
lastIndex := strings.LastIndex(pathToCut, separator)
if lastIndex == substrNotPresent {
return pathToCut, "", false
}
return pathToCut[:lastIndex], pathToCut[lastIndex+1:], false
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
githubSampleURL = "github.com/" + testModuleAuthor + "/" + testModuleName + "/" + testFileName
githubSampleUrlWithTag = githubSampleURL + "@5.33.2"
githubSampleUrlWithBranchContainingVersioningDelimiter = githubSampleURL + "@my@favorite-branch"
githubSampleUrlWithVersionWithSlash = "github.com/kurtosis-tech/sample-startosis-load/sample.star@foo/bar"
githubSampleUrlWithVersionWithSlashAndFile = "github.com/kurtosis-tech/sample-startosis-load@foo/bar/main.star"
)

func TestParsedGitURL_SimpleParse(t *testing.T) {
Expand Down Expand Up @@ -153,3 +155,15 @@ func TestParsedGitUrl_ResolvesRelativeUrlForUrlWithTag(t *testing.T) {
expected = "github.com/kurtosis-tech/sample-startosis-load/src/lib.star"
require.Equal(t, expected, absoluteUrl)
}

func TestParsedGitUrl_ResolvesWithUrlWithVersionBranchWithSlash(t *testing.T) {
parsedUrl, err := parseGitURL(githubSampleUrlWithVersionWithSlash)
require.Nil(t, err)

require.Equal(t, "foo/bar", parsedUrl.tagBranchOrCommit)

parsedUrl, err = parseGitURL(githubSampleUrlWithVersionWithSlashAndFile)
require.Nil(t, err)
require.Equal(t, "foo/bar", parsedUrl.tagBranchOrCommit)
require.Equal(t, "kurtosis-tech/sample-startosis-load/main.star", parsedUrl.relativeFilePath)
}

0 comments on commit 67efe6b

Please sign in to comment.