-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve searcher.ParseVersionedPackage, and reuse in implemetation of…
… devconfig.parseVersionedName
- Loading branch information
Showing
3 changed files
with
87 additions
and
22 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
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 |
---|---|---|
|
@@ -9,10 +9,19 @@ import ( | |
|
||
// ParseVersionedPackage checks if the given package is a versioned package | ||
// (`[email protected]`) and returns its name and version | ||
func ParseVersionedPackage(pkg string) (string, string, bool) { | ||
lastIndex := strings.LastIndex(pkg, "@") | ||
if lastIndex == -1 { | ||
func ParseVersionedPackage(versionedName string) (name string, version string, found bool) { | ||
// use the last @ symbol as the version delimiter, some packages have @ in the name | ||
atSymbolIndex := strings.LastIndex(versionedName, "@") | ||
if atSymbolIndex == -1 { | ||
return "", "", false | ||
} | ||
return pkg[:lastIndex], pkg[lastIndex+1:], true | ||
if atSymbolIndex == len(versionedName)-1 { | ||
// This case handles packages that end with `@` in the name | ||
// example: `emacsPackages.@` | ||
return "", "", false | ||
} | ||
|
||
// Common case: package@version | ||
name, version = versionedName[:atSymbolIndex], versionedName[atSymbolIndex+1:] | ||
return name, version, true | ||
} |
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,66 @@ | ||
package searcher | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseVersionedPackage(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expectedFound bool | ||
expectedName string | ||
expectedVersion string | ||
}{ | ||
{ | ||
name: "no-version", | ||
input: "python", | ||
expectedFound: false, | ||
expectedName: "", | ||
expectedVersion: "", | ||
}, | ||
{ | ||
name: "with-version-latest", | ||
input: "python@latest", | ||
expectedFound: true, | ||
expectedName: "python", | ||
expectedVersion: "latest", | ||
}, | ||
{ | ||
name: "with-version", | ||
input: "[email protected]", | ||
expectedFound: true, | ||
expectedName: "python", | ||
expectedVersion: "1.2.3", | ||
}, | ||
{ | ||
name: "with-two-@-signs", | ||
input: "emacsPackages.@@latest", | ||
expectedFound: true, | ||
expectedName: "emacsPackages.@", | ||
expectedVersion: "latest", | ||
}, | ||
{ | ||
name: "with-trailing-@-sign", | ||
input: "emacsPackages.@", | ||
expectedFound: false, | ||
expectedName: "", | ||
expectedVersion: "", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
name, version, found := ParseVersionedPackage(testCase.input) | ||
if found != testCase.expectedFound { | ||
t.Errorf("expected: %v, got: %v", testCase.expectedFound, found) | ||
} | ||
if name != testCase.expectedName { | ||
t.Errorf("expected: %v, got: %v", testCase.expectedName, name) | ||
} | ||
if version != testCase.expectedVersion { | ||
t.Errorf("expected: %v, got: %v", testCase.expectedVersion, version) | ||
} | ||
}) | ||
} | ||
} |