Skip to content

Commit

Permalink
Support Workspaces (#252)
Browse files Browse the repository at this point in the history
* Add initial workspace finder

* Add workspace support in groups/finder

* Adds workspace logic to finder

* Fixes lint errors

* Windows fix

* Fix lockfile->lockfiles
  • Loading branch information
filip-debricked authored Aug 29, 2024
1 parent 707a677 commit 4cd4196
Show file tree
Hide file tree
Showing 14 changed files with 881 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
golang.org/x/tools v0.19.0
gopkg.in/yaml.v3 v3.0.1
lukechampine.com/blake3 v1.2.1
github.com/becheran/wildmatch-go v1.0.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCv
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/becheran/wildmatch-go v1.0.0 h1:mE3dGGkTmpKtT4Z+88t8RStG40yN9T+kFEGj2PZFSzA=
github.com/becheran/wildmatch-go v1.0.0/go.mod h1:gbMvj0NtVdJ15Mg/mH9uxk2R1QCistMyU7d9KFzroX4=
github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc=
github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
Expand Down
2 changes: 1 addition & 1 deletion internal/callgraph/language/java11/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewStrategy(config conf.IConfig, paths []string, exclusions []string, inclu
}

func strategyWarning(errMsg string) {
err := fmt.Errorf(errMsg)
err := fmt.Errorf("%s", errMsg)
warningColor := color.New(color.FgYellow, color.Bold).SprintFunc()
defaultOutputWriter := log.Writer()
log.Println(warningColor("Warning: ") + err.Error())
Expand Down
2 changes: 1 addition & 1 deletion internal/file/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (finder *Finder) GetGroups(options DebrickedOptions) (Groups, error) {
return groups, err
}
}

groups.AddWorkspaceLockFiles()
groups.FilterGroupsByStrictness(options.Strictness)

return groups, err
Expand Down
8 changes: 4 additions & 4 deletions internal/file/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestGetGroups(t *testing.T) {
path := ""

excludedFiles := []string{"testdata/go/go.mod", "testdata/misc/requirements.txt", "testdata/misc/Cargo.lock"}
const nbrOfGroups = 6
const nbrOfGroups = 9

fileGroups, err := finder.GetGroups(
DebrickedOptions{
Expand Down Expand Up @@ -349,15 +349,15 @@ func TestGetGroupsWithStrictFlag(t *testing.T) {
name: "StrictnessSetTo0",
strictness: StrictAll,
testedGroupIndex: 3,
expectedNumberOfGroups: 11,
expectedNumberOfGroups: 14,
expectedManifestFile: "composer.json",
expectedLockFiles: []string{"composer.lock", "go.mod", "Cargo.lock", "requirements.txt.pip.debricked"},
},
{
name: "StrictnessSetTo1",
strictness: StrictLockAndPairs,
testedGroupIndex: 1,
expectedNumberOfGroups: 6,
expectedNumberOfGroups: 9,
expectedManifestFile: "",
expectedLockFiles: []string{
"composer.lock", "composer.lock", "go.mod", "Cargo.lock", "requirements.txt.pip.debricked", "requirements-dev.txt.pip.debricked",
Expand All @@ -367,7 +367,7 @@ func TestGetGroupsWithStrictFlag(t *testing.T) {
name: "StrictnessSetTo2",
strictness: StrictPairs,
testedGroupIndex: 0,
expectedNumberOfGroups: 4,
expectedNumberOfGroups: 7,
expectedManifestFile: "composer.json",
expectedLockFiles: []string{
"composer.lock", "requirements-dev.txt.pip.debricked.lock", "requirements.txt.pip.debricked.lock",
Expand Down
26 changes: 26 additions & 0 deletions internal/file/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,29 @@ func (gs *Groups) GetFiles() []string {

return files
}

func (gs *Groups) matchWorkspace(workspaceManifest WorkspaceManifest) {
for _, g := range gs.groups {
// If group g is missing lockfile and does not have the same manifest
if len(g.LockFiles) == 0 && g.ManifestFile != workspaceManifest.RootManifest {
match := workspaceManifest.matchManifest(g.ManifestFile)
if match {
g.LockFiles = workspaceManifest.LockFiles
}
}
}
}

func (gs *Groups) AddWorkspaceLockFiles() {
for _, group := range gs.groups {
workspaces, err := getWorkspaces(group.ManifestFile)
if err == nil && group.HasLockFiles() {
workspaceManifest := WorkspaceManifest{
LockFiles: group.LockFiles,
RootManifest: group.ManifestFile,
Workspaces: workspaces,
}
gs.matchWorkspace(workspaceManifest)
}
}
}
47 changes: 47 additions & 0 deletions internal/file/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,50 @@ func TestMatchGroupsExpected(t *testing.T) {

assert.Equal(t, len(testData), len(groups.groups))
}

func TestGroupsMatchWorkspaces(t *testing.T) {
g1 := NewGroup("package.json", nil, []string{"package-lock.json"})
g2 := NewGroup("", nil, []string{"lockfile2"})
g3 := NewGroup("package/file3", nil, []string{})
g4 := NewGroup("pack/file3", nil, []string{})

gs := Groups{}
gs.Add(*g1)
gs.Add(*g2)
gs.Add(*g3)
gs.Add(*g4)

workspaceManifest := WorkspaceManifest{
LockFiles: []string{"package-lock.json"},
RootManifest: "package.json",
Workspaces: []string{"package/*"},
}
gs.matchWorkspace(workspaceManifest)
for _, g := range gs.groups {
if g.ManifestFile == "package/file3" {
assert.Equal(t, g.LockFiles[0], g1.LockFiles[0])
}
if g.ManifestFile == "pack/file3" {
assert.Equal(t, len(g3.LockFiles), 0)
}
}

}

func TestAddWorkspaceLockFiles(t *testing.T) {
g1 := NewGroup("testdata/workspace/package.json", nil, []string{"testdata/workspace/package-lock.json"})
g2 := NewGroup("testdata/workspace/packages/package_one/package.json", nil, []string{})
g3 := NewGroup("testdata/workspace/packages/package_two/package.json", nil, []string{})

gs := Groups{}
gs.Add(*g1)
gs.Add(*g2)
gs.Add(*g3)
gs.AddWorkspaceLockFiles()
for _, g := range gs.groups {
assert.Equal(t, len(g.LockFiles), 1)
if len(g.LockFiles) == 1 {
assert.Equal(t, g.LockFiles[0], g1.LockFiles[0])
}
}
}
Loading

0 comments on commit 4cd4196

Please sign in to comment.