-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
199 additions
and
0 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
Empty file.
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,14 @@ | ||
module my-library | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/BurntSushi/toml v1.0.0 | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) | ||
|
||
require ( | ||
github.com/mattn/go-colorable v0.1.9 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect | ||
) |
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 @@ | ||
this is not a go.mod file! |
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,5 @@ | ||
module my-library | ||
|
||
require ( | ||
github.com/BurntSushi/toml v1.0.0 | ||
) |
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,8 @@ | ||
module my-library | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/BurntSushi/toml v1.0.0 | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) |
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,36 @@ | ||
package parsers | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/mod/modfile" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
const GoEcosystem Ecosystem = "Go" | ||
|
||
func ParseGoLock(pathToLockfile string) ([]PackageDetails, error) { | ||
lockfileContents, err := ioutil.ReadFile(pathToLockfile) | ||
|
||
if err != nil { | ||
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err) | ||
} | ||
|
||
parsedLockfile, err := modfile.Parse(pathToLockfile, lockfileContents, nil) | ||
|
||
if err != nil { | ||
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err) | ||
} | ||
|
||
packages := make([]PackageDetails, 0, len(parsedLockfile.Require)) | ||
|
||
for _, require := range parsedLockfile.Require { | ||
packages = append(packages, PackageDetails{ | ||
Name: require.Mod.Path, | ||
Version: strings.TrimPrefix(require.Mod.Version, "v"), | ||
Ecosystem: GoEcosystem, | ||
}) | ||
} | ||
|
||
return packages, nil | ||
} |
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,115 @@ | ||
package parsers_test | ||
|
||
import ( | ||
"osv-detector/detector/parsers" | ||
"testing" | ||
) | ||
|
||
func TestParseGoLock_FileDoesNotExist(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := parsers.ParseGoLock("fixtures/go/does-not-exist") | ||
|
||
expectErrContaining(t, err, "could not read") | ||
expectPackages(t, packages, []parsers.PackageDetails{}) | ||
} | ||
|
||
func TestParseGoLock_Invalid(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := parsers.ParseGoLock("fixtures/go/not-go-mod.txt") | ||
|
||
expectErrContaining(t, err, "could not parse") | ||
expectPackages(t, packages, []parsers.PackageDetails{}) | ||
} | ||
|
||
func TestParseGoLock_NoPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := parsers.ParseGoLock("fixtures/go/empty.mod") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []parsers.PackageDetails{}) | ||
} | ||
|
||
func TestParseGoLock_OnePackage(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := parsers.ParseGoLock("fixtures/go/one-package.mod") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []parsers.PackageDetails{ | ||
{ | ||
Name: "github.com/BurntSushi/toml", | ||
Version: "1.0.0", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseGoLock_TwoPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := parsers.ParseGoLock("fixtures/go/two-packages.mod") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []parsers.PackageDetails{ | ||
{ | ||
Name: "github.com/BurntSushi/toml", | ||
Version: "1.0.0", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
{ | ||
Name: "gopkg.in/yaml.v2", | ||
Version: "2.4.0", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseGoLock_IndirectPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := parsers.ParseGoLock("fixtures/go/indirect-packages.mod") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []parsers.PackageDetails{ | ||
{ | ||
Name: "github.com/BurntSushi/toml", | ||
Version: "1.0.0", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
{ | ||
Name: "gopkg.in/yaml.v2", | ||
Version: "2.4.0", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
{ | ||
Name: "github.com/mattn/go-colorable", | ||
Version: "0.1.9", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
{ | ||
Name: "github.com/mattn/go-isatty", | ||
Version: "0.0.14", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
{ | ||
Name: "golang.org/x/sys", | ||
Version: "0.0.0-20210630005230-0f9fa26af87c", | ||
Ecosystem: parsers.GoEcosystem, | ||
}, | ||
}) | ||
} |
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
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