Skip to content
Open
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
12 changes: 8 additions & 4 deletions src/packagedcode/go_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ def parse_gomod(location):
req = preprocess(req)
if ')' in req:
break
if not req:
continue
parsed_dep_link = parse_dep_link(req)
ns_name = parsed_dep_link.group('ns_name')
namespace, _, name = ns_name.rpartition('/')
if parsed_dep_link:
ns_name = parsed_dep_link.group('ns_name')
namespace, _, name = ns_name.rpartition('/')
require.append(GoModule(
namespace=namespace,
name=name,
Expand All @@ -146,10 +148,12 @@ def parse_gomod(location):
exc = preprocess(exc)
if ')' in exc:
break
if not exc:
continue
parsed_dep_link = parse_dep_link(exc)
ns_name = parsed_dep_link.group('ns_name')
namespace, _, name = ns_name.rpartition('/')
if parsed_dep_link:
ns_name = parsed_dep_link.group('ns_name')
namespace, _, name = ns_name.rpartition('/')
exclude.append(GoModule(
namespace=namespace,
name=name,
Expand Down
30 changes: 30 additions & 0 deletions tests/packagedcode/test_golang.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ def test_parse_gomod_user_agent(self):
package = golang.GoModHandler.parse(test_file)
self.check_packages_data(package, expected_loc, regen=REGEN_TEST_FIXTURES)

def test_parse_gomod_with_comments_and_blank_lines(self):
test_file = self.get_temp_file('go.mod')
with open(test_file, 'w') as f:
f.write(
'module example.com/hello\n\n'
'go 1.21\n\n'
'require (\n'
' // Direct dependencies\n'
' github.com/stretchr/testify v1.8.4\n\n'
' // Indirect dependencies\n'
' github.com/davecgh/go-spew v1.1.1\n'
')\n\n'
'exclude (\n'
' // Excluded versions\n'
' github.com/bad/pkg v1.0.0\n\n'
' github.com/another/bad v2.0.0\n'
')\n'
)
packages = list(golang.GoModHandler.parse(test_file))
assert len(packages) == 1
pkg = packages[0]
assert pkg.name == 'hello'
purls = [d.purl for d in pkg.dependencies]
assert purls == [
'pkg:golang/github.com/stretchr/testify@v1.8.4',
'pkg:golang/github.com/davecgh/go-spew@v1.1.1',
'pkg:golang/github.com/bad/pkg@v1.0.0',
'pkg:golang/github.com/another/bad@v2.0.0',
]

def test_gosum_is_package_data_file(self):
test_file = self.get_test_loc('golang/gosum/sample1/go.sum')
assert golang.GoSumHandler.is_datafile(test_file)
Expand Down
Loading