Skip to content

Commit

Permalink
Improves workspace file finding (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked authored Sep 2, 2024
1 parent 66358ca commit e0cca5b
Show file tree
Hide file tree
Showing 13 changed files with 826 additions and 37 deletions.
10 changes: 5 additions & 5 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 = 9
const nbrOfGroups = 12

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: 14,
expectedNumberOfGroups: 11,
expectedManifestFile: "composer.json",
expectedLockFiles: []string{"composer.lock", "go.mod", "Cargo.lock", "requirements.txt.pip.debricked"},
},
{
name: "StrictnessSetTo1",
strictness: StrictLockAndPairs,
testedGroupIndex: 1,
expectedNumberOfGroups: 9,
expectedNumberOfGroups: 6,
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: 7,
expectedNumberOfGroups: 4,
expectedManifestFile: "composer.json",
expectedLockFiles: []string{
"composer.lock", "requirements-dev.txt.pip.debricked.lock", "requirements.txt.pip.debricked.lock",
Expand All @@ -381,7 +381,7 @@ func TestGetGroupsWithStrictFlag(t *testing.T) {
fileGroups, err := finder.GetGroups(
DebrickedOptions{
RootPath: filePath,
Exclusions: []string{"**/node_modules/**"},
Exclusions: []string{"**/node_modules/**", "**/workspace/**"},
Inclusions: []string{},
LockFileOnly: false,
Strictness: c.strictness,
Expand Down
8 changes: 4 additions & 4 deletions internal/file/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ func (gs *Groups) matchWorkspace(workspaceManifest WorkspaceManifest) {

func (gs *Groups) AddWorkspaceLockFiles() {
for _, group := range gs.groups {
workspaces, err := getWorkspaces(group.ManifestFile)
workspaces, err := getPackageJSONWorkspaces(group.ManifestFile)
if err == nil && group.HasLockFiles() {
workspaceManifest := WorkspaceManifest{
LockFiles: group.LockFiles,
RootManifest: group.ManifestFile,
Workspaces: workspaces,
LockFiles: group.LockFiles,
RootManifest: group.ManifestFile,
WorkspacePatterns: workspaces,
}
gs.matchWorkspace(workspaceManifest)
}
Expand Down
24 changes: 16 additions & 8 deletions internal/file/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,18 @@ func TestGroupsMatchWorkspaces(t *testing.T) {
gs.Add(*g4)

workspaceManifest := WorkspaceManifest{
LockFiles: []string{"package-lock.json"},
RootManifest: "package.json",
Workspaces: []string{"package/*"},
LockFiles: []string{"package-lock.json"},
RootManifest: "package.json",
WorkspacePatterns: []string{"package/*", "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.HasLockFiles() {
assert.Equal(t, g.LockFiles[0], g1.LockFiles[0])
} else {
assert.True(t, false)
}
}
if g.ManifestFile == "pack/file3" {
assert.Equal(t, len(g3.LockFiles), 0)
Expand All @@ -215,17 +219,21 @@ func TestGroupsMatchWorkspaces(t *testing.T) {
}

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{})
g1 := NewGroup(
"testdata/workspace/common/package.json",
nil,
[]string{"testdata/workspace/common/package-lock.json"},
)
g2 := NewGroup("testdata/workspace/common/packages/package_one/package.json", nil, []string{})
g3 := NewGroup("testdata/workspace/common/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)
assert.Equal(t, 1, len(g.LockFiles))
if len(g.LockFiles) == 1 {
assert.Equal(t, g.LockFiles[0], g1.LockFiles[0])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"typescript": "5.5.4"
},
"workspaces": [
"testdata/workspace/packages/*", "testdata\\workspace\\packages\\*"
"packages/*"
]
}
Loading

0 comments on commit e0cca5b

Please sign in to comment.