Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/asdf/asdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/git-pkgs/manifests/internal/core"
)

const minToolVersionFields = 2 // name + version

func init() {
core.Register("asdf", core.Manifest, &toolVersionsParser{}, core.ExactMatch(".tool-versions"))
}
Expand All @@ -24,7 +26,7 @@ func (p *toolVersionsParser) Parse(filename string, content []byte) ([]core.Depe
}

fields := strings.Fields(line)
if len(fields) < 2 {
if len(fields) < minToolVersionFields {
continue
}

Expand Down
5 changes: 3 additions & 2 deletions internal/carthage/carthage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ func (p *cartfileParser) Parse(filename string, content []byte) ([]core.Dependen
Direct: true,
})
} else if match := cartfileGitRegex.FindStringSubmatch(line); match != nil {
const versionGroup = 2
name := match[1]
version := ""
if len(match) > 2 {
version = match[2]
if len(match) > versionGroup {
version = match[versionGroup]
}
deps = append(deps, core.Dependency{
Name: name,
Expand Down
67 changes: 19 additions & 48 deletions internal/carthage/carthage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,29 @@ import (
"github.com/git-pkgs/manifests/internal/core"
)

func TestCartfile(t *testing.T) {
content, err := os.ReadFile("../../testdata/carthage/Cartfile")
func testCartfileParse(t *testing.T, filename string, wantCount int, expected map[string]string) {
t.Helper()

content, err := os.ReadFile("../../testdata/carthage/" + filename)
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}

parser := &cartfileParser{}
deps, err := parser.Parse("Cartfile", content)
deps, err := parser.Parse(filename, content)
if err != nil {
t.Fatalf("Parse failed: %v", err)
}

if len(deps) != 8 {
t.Fatalf("expected 8 dependencies, got %d", len(deps))
if len(deps) != wantCount {
t.Fatalf("expected %d dependencies, got %d", wantCount, len(deps))
}

depMap := make(map[string]core.Dependency)
for _, d := range deps {
depMap[d.Name] = d
}

// Sample of packages with versions
expected := map[string]string{
"ReactiveCocoa/ReactiveCocoa": ">=2.3.1",
"Mantle/Mantle": "~>1.0",
"jspahrsummers/libextobjc": "==0.4.1",
}

for name, wantVer := range expected {
dep, ok := depMap[name]
if !ok {
Expand All @@ -47,44 +42,20 @@ func TestCartfile(t *testing.T) {
}
}

func TestCartfilePrivate(t *testing.T) {
content, err := os.ReadFile("../../testdata/carthage/Cartfile.private")
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}

parser := &cartfileParser{}
deps, err := parser.Parse("Cartfile.private", content)
if err != nil {
t.Fatalf("Parse failed: %v", err)
}

if len(deps) != 3 {
t.Fatalf("expected 3 dependencies, got %d", len(deps))
}

depMap := make(map[string]core.Dependency)
for _, d := range deps {
depMap[d.Name] = d
}
func TestCartfile(t *testing.T) {
testCartfileParse(t, "Cartfile", 8, map[string]string{
"ReactiveCocoa/ReactiveCocoa": ">=2.3.1",
"Mantle/Mantle": "~>1.0",
"jspahrsummers/libextobjc": "==0.4.1",
})
}

// All 3 packages
expected := map[string]string{
"Quick/Quick": "~>0.9",
"Quick/Nimble": "~>3.1",
func TestCartfilePrivate(t *testing.T) {
testCartfileParse(t, "Cartfile.private", 3, map[string]string{
"Quick/Quick": "~>0.9",
"Quick/Nimble": "~>3.1",
"jspahrsummers/xcconfigs": "ec5753493605deed7358dec5f9260f503d3ed650",
}

for name, wantVer := range expected {
dep, ok := depMap[name]
if !ok {
t.Errorf("expected %s dependency", name)
continue
}
if dep.Version != wantVer {
t.Errorf("%s version = %q, want %q", name, dep.Version, wantVer)
}
}
})
}

func TestCartfileResolved(t *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions internal/cocoapods/cocoapods.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ func (p *podfileParser) Parse(filename string, content []byte) ([]core.Dependenc
}

if match := podRegex.FindStringSubmatch(line); match != nil {
const versionGroup = 2
name := match[1]
version := ""
if len(match) > 2 {
version = match[2]
if len(match) > versionGroup {
version = match[versionGroup]
}

scope := core.Runtime
Expand Down Expand Up @@ -172,10 +173,11 @@ func (p *podspecParser) Parse(filename string, content []byte) ([]core.Dependenc
text := string(content)

for _, match := range podspecDepRegex.FindAllStringSubmatch(text, -1) {
const versionGroup = 2
name := match[1]
version := ""
if len(match) > 2 {
version = match[2]
if len(match) > versionGroup {
version = match[versionGroup]
}
deps = append(deps, core.Dependency{
Name: name,
Expand Down
6 changes: 4 additions & 2 deletions internal/conan/conan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

const nameVersionParts = 2 // name/version

func init() {
core.Register("conan", core.Manifest, &conanfileTxtParser{}, core.ExactMatch("conanfile.txt"))
core.Register("conan", core.Manifest, &conanfilePyParser{}, core.ExactMatch("conanfile.py"))
Expand Down Expand Up @@ -146,8 +148,8 @@ func parseConanRef(ref string) (name, version string) {
ref = ref[:idx]
}

parts := strings.SplitN(ref, "/", 2)
if len(parts) == 2 {
parts := strings.SplitN(ref, "/", nameVersionParts)
if len(parts) == nameVersionParts {
return parts[0], parts[1]
}
return "", ""
Expand Down
6 changes: 2 additions & 4 deletions internal/conan/conan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ func TestConanfileTxt(t *testing.T) {

if dep, ok := depMap["boost"]; !ok {
t.Error("expected boost dependency")
} else {
if dep.Version != "1.76.0" {
t.Errorf("expected boost version 1.76.0, got %s", dep.Version)
}
} else if dep.Version != "1.76.0" {
t.Errorf("expected boost version 1.76.0, got %s", dep.Version)
}

// Check build dependency
Expand Down
6 changes: 4 additions & 2 deletions internal/conda/conda.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"gopkg.in/yaml.v3"
)

const condaChannelURLParts = 4 // scheme + empty + host + channel

func init() {
core.Register("conda", core.Manifest, &condaEnvParser{}, core.ExactMatch("environment.yml"))
core.Register("conda", core.Manifest, &condaEnvParser{}, core.ExactMatch("environment.yaml"))
Expand Down Expand Up @@ -121,8 +123,8 @@ func (p *condaLockParser) Parse(filename string, content []byte) ([]core.Depende
if strings.Contains(pkg.URL, "conda.anaconda.org") {
// Extract channel: https://conda.anaconda.org/conda-forge/linux-64/...
parts := strings.Split(pkg.URL, "/")
if len(parts) >= 4 {
registryURL = strings.Join(parts[:4], "/")
if len(parts) >= condaChannelURLParts {
registryURL = strings.Join(parts[:condaChannelURLParts], "/")
}
}

Expand Down
16 changes: 11 additions & 5 deletions internal/core/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ func ForEachLine(content string, fn func(line string) bool) {
}
}

const (
estimateBytesPerDep = 50
minEstimatedDeps = 4
maxEstimatedDeps = 1000
)

// EstimateDeps estimates the number of dependencies based on file size.
func EstimateDeps(size int) int {
estimate := size / 50
if estimate < 4 {
return 4
estimate := size / estimateBytesPerDep
if estimate < minEstimatedDeps {
return minEstimatedDeps
}
if estimate > 1000 {
return 1000
if estimate > maxEstimatedDeps {
return maxEstimatedDeps
}
return estimate
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Register(ecosystem string, kind Kind, parser Parser, match func(string) boo
}

// IdentifyParser returns the first matching parser for a filename.
func IdentifyParser(filename string) (Parser, string, Kind) {
func IdentifyParser(filename string) (Parser, string, Kind) { //nolint:ireturn
base := filepath.Base(filename)
for _, reg := range parsers {
if reg.Match(filename) || reg.Match(base) {
Expand Down
8 changes: 5 additions & 3 deletions internal/cpan/cpan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"gopkg.in/yaml.v3"
)

const perlPackage = "perl"

func init() {
core.Register("cpan", core.Manifest, &cpanfileParser{}, core.ExactMatch("cpanfile"))
core.Register("cpan", core.Lockfile, &cpanfileSnapshotParser{}, core.ExactMatch("cpanfile.snapshot"))
Expand Down Expand Up @@ -291,7 +293,7 @@ func parsePerlHashSection(text, section string, scope core.Scope, seen map[strin
version := match[2]

// Skip perl itself and common non-module entries
if name == "perl" || seen[name] {
if name == perlPackage || seen[name] {
continue
}
seen[name] = true
Expand Down Expand Up @@ -349,7 +351,7 @@ func (p *metaJSONParser) Parse(filename string, content []byte) ([]core.Dependen

for _, mods := range requirements {
for name, version := range mods {
if name == "perl" || seen[name] {
if name == perlPackage || seen[name] {
continue
}
seen[name] = true
Expand Down Expand Up @@ -400,7 +402,7 @@ func (p *metaYMLParser) Parse(filename string, content []byte) ([]core.Dependenc
continue
}
for name, ver := range *mods {
if name == "perl" || seen[name] {
if name == perlPackage || seen[name] {
continue
}
seen[name] = true
Expand Down
Loading