-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support parsing
renv.lock
files
- Loading branch information
Showing
15 changed files
with
310 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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 json! |
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,17 @@ | ||
{ | ||
"R": { | ||
"Version": "2.15.2", | ||
"Repositories": [] | ||
}, | ||
"Packages": { | ||
"morning": { | ||
"Package": "morning", | ||
"Version": "0.1.0", | ||
"Repository": "CRAN", | ||
"Requirements": [ | ||
"coffee", | ||
"toast" | ||
] | ||
} | ||
} | ||
} |
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,27 @@ | ||
{ | ||
"R": { | ||
"Version": "4.2.3", | ||
"Repositories": [ | ||
{ | ||
"Name": "CRAN", | ||
"URL": "https://cloud.r-project.org" | ||
} | ||
] | ||
}, | ||
"Packages": { | ||
"markdown": { | ||
"Package": "markdown", | ||
"Version": "1.0", | ||
"Source": "Repository", | ||
"Repository": "CRAN", | ||
"Hash": "4584a57f565dd7987d59dda3a02cfb41" | ||
}, | ||
"mime": { | ||
"Package": "mime", | ||
"Version": "0.7", | ||
"Source": "Repository", | ||
"Repository": "CRAN", | ||
"Hash": "908d95ccbfd1dd274073ef07a7c93934" | ||
} | ||
} | ||
} |
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,29 @@ | ||
{ | ||
"R": { | ||
"Version": "4.1.0", | ||
"Repositories": [ | ||
{ | ||
"Name": "CRAN", | ||
"URL": "https://cran.rstudio.com" | ||
} | ||
] | ||
}, | ||
"Bioconductor": { | ||
"Version": "3.13" | ||
}, | ||
"Packages": { | ||
"BH": { | ||
"Package": "BH", | ||
"Version": "1.75.0-0", | ||
"Source": "Repository", | ||
"Repository": "CRAN", | ||
"Hash": "e4c04affc2cac20c8fec18385cd14691" | ||
}, | ||
"BSgenome": { | ||
"Package": "BSgenome", | ||
"Version": "1.60.0", | ||
"Source": "Bioconductor", | ||
"Hash": "bc39f66b170caed3ea67c03eb6b4b55c" | ||
} | ||
} | ||
} |
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,35 @@ | ||
{ | ||
"R": { | ||
"Version": "4.3.1", | ||
"Repositories": [ | ||
{ | ||
"Name": "CRAN", | ||
"URL": "https://cloud.r-project.org" | ||
} | ||
] | ||
}, | ||
"Packages": { | ||
"markdown": { | ||
"Package": "markdown", | ||
"Version": "1.0", | ||
"Source": "Repository", | ||
"Repository": "CRAN", | ||
"Hash": "4584a57f565dd7987d59dda3a02cfb41" | ||
}, | ||
"mime": { | ||
"Package": "mime", | ||
"Version": "0.12.1", | ||
"Source": "GitHub", | ||
"RemoteType": "github", | ||
"RemoteHost": "api.github.com", | ||
"RemoteUsername": "yihui", | ||
"RemoteRepo": "mime", | ||
"RemoteRef": "main", | ||
"RemoteSha": "1763e0dcb72fb58d97bab97bb834fc71f1e012bc", | ||
"Requirements": [ | ||
"tools" | ||
], | ||
"Hash": "c2772b6269924dad6784aaa1d99dbb86" | ||
} | ||
} | ||
} |
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,16 @@ | ||
{ | ||
"R": { | ||
"Version": "2.15.2", | ||
"Repositories": [] | ||
}, | ||
"Packages": { | ||
"morning": { | ||
"Package": "morning", | ||
"Version": "0.1.0", | ||
"Requirements": [ | ||
"coffee", | ||
"toast" | ||
] | ||
} | ||
} | ||
} |
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,54 @@ | ||
package lockfile | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type RenvPackage struct { | ||
Package string `json:"Package"` | ||
Version string `json:"Version"` | ||
Repository string `json:"Repository"` | ||
} | ||
|
||
type RenvLockfile struct { | ||
Packages map[string]RenvPackage `json:"Packages"` | ||
} | ||
|
||
const CRANEcosystem Ecosystem = "CRAN" | ||
|
||
func ParseRenvLock(pathToLockfile string) ([]PackageDetails, error) { | ||
var parsedLockfile *RenvLockfile | ||
|
||
lockfileContents, err := os.ReadFile(pathToLockfile) | ||
|
||
if err != nil { | ||
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err) | ||
} | ||
|
||
err = json.Unmarshal(lockfileContents, &parsedLockfile) | ||
|
||
if err != nil { | ||
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err) | ||
} | ||
|
||
packages := make([]PackageDetails, 0, len(parsedLockfile.Packages)) | ||
|
||
for _, pkg := range parsedLockfile.Packages { | ||
// currently we assume that unless a package is explicitly for a different | ||
// repository, it is a CRAN package (even if its Source is not Repository) | ||
if pkg.Repository != "" && pkg.Repository != string(CRANEcosystem) { | ||
continue | ||
} | ||
|
||
packages = append(packages, PackageDetails{ | ||
Name: pkg.Package, | ||
Version: pkg.Version, | ||
Ecosystem: CRANEcosystem, | ||
CompareAs: CRANEcosystem, | ||
}) | ||
} | ||
|
||
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,124 @@ | ||
package lockfile_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/g-rath/osv-detector/pkg/lockfile" | ||
) | ||
|
||
func TestParseRenvLock_FileDoesNotExist(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseRenvLock("fixtures/renv/does-not-exist") | ||
|
||
expectErrContaining(t, err, "could not read") | ||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseRenvLock_InvalidJson(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseRenvLock("fixtures/renv/not-json.txt") | ||
|
||
expectErrContaining(t, err, "could not parse") | ||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseRenvLock_NoPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseRenvLock("fixtures/renv/empty.lock") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseRenvLock_OnePackage(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseRenvLock("fixtures/renv/one-package.lock") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "morning", | ||
Version: "0.1.0", | ||
Ecosystem: lockfile.CRANEcosystem, | ||
CompareAs: lockfile.CRANEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseRenvLock_TwoPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseRenvLock("fixtures/renv/two-packages.lock") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "markdown", | ||
Version: "1.0", | ||
Ecosystem: lockfile.CRANEcosystem, | ||
CompareAs: lockfile.CRANEcosystem, | ||
}, | ||
{ | ||
Name: "mime", | ||
Version: "0.7", | ||
Ecosystem: lockfile.CRANEcosystem, | ||
CompareAs: lockfile.CRANEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseRenvLock_WithMixedSources(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseRenvLock("fixtures/renv/with-mixed-sources.lock") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "markdown", | ||
Version: "1.0", | ||
Ecosystem: lockfile.CRANEcosystem, | ||
CompareAs: lockfile.CRANEcosystem, | ||
}, | ||
{ | ||
Name: "mime", | ||
Version: "0.12.1", | ||
Ecosystem: lockfile.CRANEcosystem, | ||
CompareAs: lockfile.CRANEcosystem, | ||
}, | ||
}) | ||
} | ||
func TestParseRenvLock_WithoutRepository(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseRenvLock("fixtures/renv/without-repository.lock") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "morning", | ||
Version: "0.1.0", | ||
Ecosystem: lockfile.CRANEcosystem, | ||
CompareAs: lockfile.CRANEcosystem, | ||
}, | ||
}) | ||
} |
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