Skip to content

Commit e04c6fa

Browse files
authored
Merge pull request #167 from per1234/fix-subproject-discovery
Always discover subprojects
2 parents 37a8a38 + 8e5f33a commit e04c6fa

File tree

2 files changed

+108
-83
lines changed

2 files changed

+108
-83
lines changed

Diff for: internal/project/project.go

+22-25
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func FindProjects() ([]Type, error) {
5353

5454
// findProjects handles the recursion for FindProjects().
5555
func findProjects(targetPath *paths.Path) ([]Type, error) {
56-
var foundProjects []Type
56+
var foundParentProjects []Type
5757

5858
// If targetPath is a file, targetPath itself is the project, so it's only necessary to determine/verify the type.
5959
if targetPath.IsNotDir() {
@@ -82,36 +82,33 @@ func findProjects(targetPath *paths.Path) ([]Type, error) {
8282
ProjectType: projectType,
8383
SuperprojectType: projectType,
8484
}
85-
foundProjects = append(foundProjects, foundProject)
86-
87-
foundProjects = append(foundProjects, findSubprojects(foundProject, projectType)...)
88-
89-
return foundProjects, nil
85+
foundParentProjects = append(foundParentProjects, foundProject)
86+
}
87+
} else {
88+
if configuration.SuperprojectTypeFilter() == projecttype.All || configuration.Recursive() {
89+
// Project discovery and/or type detection is required.
90+
foundParentProjects = findProjectsUnderPath(targetPath, configuration.SuperprojectTypeFilter(), configuration.Recursive())
91+
} else {
92+
// Project was explicitly defined by user.
93+
foundParentProjects = append(foundParentProjects,
94+
Type{
95+
Path: targetPath,
96+
ProjectType: configuration.SuperprojectTypeFilter(),
97+
SuperprojectType: configuration.SuperprojectTypeFilter(),
98+
},
99+
)
90100
}
91-
92-
return nil, fmt.Errorf("Specified path %s is not an Arduino project", targetPath)
93101
}
94102

95-
if configuration.SuperprojectTypeFilter() == projecttype.All || configuration.Recursive() {
96-
// Project discovery and/or type detection is required.
97-
foundParentProjects := findProjectsUnderPath(targetPath, configuration.SuperprojectTypeFilter(), configuration.Recursive())
98-
for _, foundParentProject := range foundParentProjects {
99-
foundProjects = append(foundProjects, foundParentProject)
100-
foundProjects = append(foundProjects, findSubprojects(foundParentProject, foundParentProject.ProjectType)...)
101-
}
102-
} else {
103-
// Project was explicitly defined by user.
104-
foundProjects = append(foundProjects,
105-
Type{
106-
Path: targetPath,
107-
ProjectType: configuration.SuperprojectTypeFilter(),
108-
SuperprojectType: configuration.SuperprojectTypeFilter(),
109-
},
110-
)
103+
// Discover subprojects of all found projects.
104+
var foundProjects []Type
105+
for _, foundParentProject := range foundParentProjects {
106+
foundProjects = append(foundProjects, foundParentProject)
107+
foundProjects = append(foundProjects, findSubprojects(foundParentProject, foundParentProject.ProjectType)...)
111108
}
112109

113110
if foundProjects == nil {
114-
return nil, fmt.Errorf("No projects found under %s", targetPath)
111+
return nil, fmt.Errorf("No projects found with project path %s", targetPath)
115112
}
116113

117114
return foundProjects, nil

Diff for: internal/project/project_test.go

+86-58
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package project
1717

1818
import (
19+
"fmt"
1920
"os"
2021
"reflect"
2122
"testing"
@@ -53,16 +54,16 @@ func TestFindProjects(t *testing.T) {
5354

5455
testTables := []struct {
5556
testName string
56-
superprojectTypeFilter string
57-
recursive string
57+
superprojectTypeFilter []string
58+
recursive []string
5859
projectPaths []string
5960
errorAssertion assert.ErrorAssertionFunc
6061
expectedProjects []Type
6162
}{
6263
{
6364
"Sketch file",
64-
"all",
65-
"",
65+
[]string{"all", "sketch"},
66+
[]string{"true", "false"},
6667
[]string{sketchPath.Join("Sketch.ino").String()},
6768
assert.NoError,
6869
[]Type{
@@ -75,8 +76,8 @@ func TestFindProjects(t *testing.T) {
7576
},
7677
{
7778
"Library file",
78-
"all",
79-
"",
79+
[]string{"all", "library"},
80+
[]string{"true", "false"},
8081
[]string{libraryPath.Join("Library.h").String()},
8182
assert.NoError,
8283
[]Type{
@@ -94,8 +95,8 @@ func TestFindProjects(t *testing.T) {
9495
},
9596
{
9697
"Platform file",
97-
"all",
98-
"",
98+
[]string{"all", "platform"},
99+
[]string{"true", "false"},
99100
[]string{platformPath.Join("boards.txt").String()},
100101
assert.NoError,
101102
[]Type{
@@ -118,8 +119,8 @@ func TestFindProjects(t *testing.T) {
118119
},
119120
{
120121
"Package index file",
121-
"all",
122-
"",
122+
[]string{"all", "package-index"},
123+
[]string{"true", "false"},
123124
[]string{packageIndexFilePath.String()},
124125
assert.NoError,
125126
[]Type{
@@ -132,8 +133,8 @@ func TestFindProjects(t *testing.T) {
132133
},
133134
{
134135
"Explicit file",
135-
"sketch",
136-
"",
136+
[]string{"sketch"},
137+
[]string{"true", "false"},
137138
[]string{libraryPath.Join("Library.h").String()},
138139
assert.NoError,
139140
[]Type{
@@ -146,8 +147,8 @@ func TestFindProjects(t *testing.T) {
146147
},
147148
{
148149
"Sketch folder",
149-
"all",
150-
"",
150+
[]string{"all", "sketch"},
151+
[]string{"true", "false"},
151152
[]string{sketchPath.String()},
152153
assert.NoError,
153154
[]Type{
@@ -160,8 +161,8 @@ func TestFindProjects(t *testing.T) {
160161
},
161162
{
162163
"Library folder",
163-
"all",
164-
"",
164+
[]string{"all", "library"},
165+
[]string{"true", "false"},
165166
[]string{libraryPath.String()},
166167
assert.NoError,
167168
[]Type{
@@ -179,8 +180,8 @@ func TestFindProjects(t *testing.T) {
179180
},
180181
{
181182
"Platform folder",
182-
"all",
183-
"",
183+
[]string{"all", "platform"},
184+
[]string{"true", "false"},
184185
[]string{platformPath.String()},
185186
assert.NoError,
186187
[]Type{
@@ -203,8 +204,8 @@ func TestFindProjects(t *testing.T) {
203204
},
204205
{
205206
"Package index folder",
206-
"all",
207-
"",
207+
[]string{"all", "package-index"},
208+
[]string{"true", "false"},
208209
[]string{packageIndexFolderPath.String()},
209210
assert.NoError,
210211
[]Type{
@@ -217,8 +218,8 @@ func TestFindProjects(t *testing.T) {
217218
},
218219
{
219220
"Explicit folder",
220-
"sketch",
221-
"false",
221+
[]string{"sketch"},
222+
[]string{"false"},
222223
[]string{libraryPath.String()},
223224
assert.NoError,
224225
[]Type{
@@ -230,9 +231,23 @@ func TestFindProjects(t *testing.T) {
230231
},
231232
},
232233
{
233-
"Projects folder, recursive",
234-
"all",
235-
"true",
234+
"Explicit folder",
235+
[]string{"sketch"},
236+
[]string{"true"},
237+
[]string{libraryPath.String()},
238+
assert.NoError,
239+
[]Type{
240+
{
241+
Path: libraryExamplePath,
242+
ProjectType: projecttype.Sketch,
243+
SuperprojectType: projecttype.Sketch,
244+
},
245+
},
246+
},
247+
{
248+
"Projects folder",
249+
[]string{"all"},
250+
[]string{"true"},
236251
[]string{projectsPath.String()},
237252
assert.NoError,
238253
[]Type{
@@ -253,18 +268,37 @@ func TestFindProjects(t *testing.T) {
253268
},
254269
},
255270
},
271+
{
272+
"Projects folder",
273+
[]string{"sketch"},
274+
[]string{"true"},
275+
[]string{projectsPath.String()},
276+
assert.NoError,
277+
[]Type{
278+
{
279+
Path: projectsPathLibraryExample,
280+
ProjectType: projecttype.Sketch,
281+
SuperprojectType: projecttype.Sketch,
282+
},
283+
{
284+
Path: projectsPathSketch,
285+
ProjectType: projecttype.Sketch,
286+
SuperprojectType: projecttype.Sketch,
287+
},
288+
},
289+
},
256290
{
257291
"Projects folder, non-recursive",
258-
"all",
259-
"false",
292+
[]string{"all"},
293+
[]string{"false"},
260294
[]string{projectsPath.String()},
261295
assert.Error,
262296
[]Type{},
263297
},
264298
{
265299
"Multiple target folders",
266-
"all",
267-
"true",
300+
[]string{"all"},
301+
[]string{"true"},
268302
[]string{projectsPath.String(), sketchPath.String()},
269303
assert.NoError,
270304
[]Type{
@@ -290,38 +324,32 @@ func TestFindProjects(t *testing.T) {
290324
},
291325
},
292326
},
293-
{
294-
"superproject type filter",
295-
"sketch",
296-
"true",
297-
[]string{projectsPath.String()},
298-
assert.NoError,
299-
[]Type{
300-
{
301-
Path: projectsPathLibraryExample,
302-
ProjectType: projecttype.Sketch,
303-
SuperprojectType: projecttype.Sketch,
304-
},
305-
{
306-
Path: projectsPathSketch,
307-
ProjectType: projecttype.Sketch,
308-
SuperprojectType: projecttype.Sketch,
309-
},
310-
},
311-
},
312327
}
313328

314329
for _, testTable := range testTables {
315-
flags := test.ConfigurationFlags()
316-
flags.Set("project-type", testTable.superprojectTypeFilter)
317-
if testTable.recursive != "" {
318-
flags.Set("recursive", testTable.recursive)
319-
}
320-
configuration.Initialize(flags, testTable.projectPaths)
321-
foundProjects, err := FindProjects()
322-
testTable.errorAssertion(t, err)
323-
if err == nil {
324-
assert.True(t, reflect.DeepEqual(foundProjects, testTable.expectedProjects), testTable.testName)
330+
for _, superprojectTypeFilter := range testTable.superprojectTypeFilter {
331+
for _, recursive := range testTable.recursive {
332+
flags := test.ConfigurationFlags()
333+
flags.Set("project-type", superprojectTypeFilter)
334+
if recursive != "" {
335+
flags.Set("recursive", recursive)
336+
}
337+
configuration.Initialize(flags, testTable.projectPaths)
338+
foundProjects, err := FindProjects()
339+
testTable.errorAssertion(t, err)
340+
if err == nil {
341+
assert.True(
342+
t,
343+
reflect.DeepEqual(foundProjects, testTable.expectedProjects),
344+
fmt.Sprintf(
345+
"%s (%s project-type=%s recursive=%s)",
346+
testTable.testName,
347+
testTable.projectPaths,
348+
superprojectTypeFilter, recursive,
349+
),
350+
)
351+
}
352+
}
325353
}
326354
}
327355
}

0 commit comments

Comments
 (0)