Skip to content

Always discover subprojects #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func FindProjects() ([]Type, error) {

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

// If targetPath is a file, targetPath itself is the project, so it's only necessary to determine/verify the type.
if targetPath.IsNotDir() {
Expand Down Expand Up @@ -82,36 +82,33 @@ func findProjects(targetPath *paths.Path) ([]Type, error) {
ProjectType: projectType,
SuperprojectType: projectType,
}
foundProjects = append(foundProjects, foundProject)

foundProjects = append(foundProjects, findSubprojects(foundProject, projectType)...)

return foundProjects, nil
foundParentProjects = append(foundParentProjects, foundProject)
}
} else {
if configuration.SuperprojectTypeFilter() == projecttype.All || configuration.Recursive() {
// Project discovery and/or type detection is required.
foundParentProjects = findProjectsUnderPath(targetPath, configuration.SuperprojectTypeFilter(), configuration.Recursive())
} else {
// Project was explicitly defined by user.
foundParentProjects = append(foundParentProjects,
Type{
Path: targetPath,
ProjectType: configuration.SuperprojectTypeFilter(),
SuperprojectType: configuration.SuperprojectTypeFilter(),
},
)
}

return nil, fmt.Errorf("Specified path %s is not an Arduino project", targetPath)
}

if configuration.SuperprojectTypeFilter() == projecttype.All || configuration.Recursive() {
// Project discovery and/or type detection is required.
foundParentProjects := findProjectsUnderPath(targetPath, configuration.SuperprojectTypeFilter(), configuration.Recursive())
for _, foundParentProject := range foundParentProjects {
foundProjects = append(foundProjects, foundParentProject)
foundProjects = append(foundProjects, findSubprojects(foundParentProject, foundParentProject.ProjectType)...)
}
} else {
// Project was explicitly defined by user.
foundProjects = append(foundProjects,
Type{
Path: targetPath,
ProjectType: configuration.SuperprojectTypeFilter(),
SuperprojectType: configuration.SuperprojectTypeFilter(),
},
)
// Discover subprojects of all found projects.
var foundProjects []Type
for _, foundParentProject := range foundParentProjects {
foundProjects = append(foundProjects, foundParentProject)
foundProjects = append(foundProjects, findSubprojects(foundParentProject, foundParentProject.ProjectType)...)
}

if foundProjects == nil {
return nil, fmt.Errorf("No projects found under %s", targetPath)
return nil, fmt.Errorf("No projects found with project path %s", targetPath)
}

return foundProjects, nil
Expand Down
144 changes: 86 additions & 58 deletions internal/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package project

import (
"fmt"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -53,16 +54,16 @@ func TestFindProjects(t *testing.T) {

testTables := []struct {
testName string
superprojectTypeFilter string
recursive string
superprojectTypeFilter []string
recursive []string
projectPaths []string
errorAssertion assert.ErrorAssertionFunc
expectedProjects []Type
}{
{
"Sketch file",
"all",
"",
[]string{"all", "sketch"},
[]string{"true", "false"},
[]string{sketchPath.Join("Sketch.ino").String()},
assert.NoError,
[]Type{
Expand All @@ -75,8 +76,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Library file",
"all",
"",
[]string{"all", "library"},
[]string{"true", "false"},
[]string{libraryPath.Join("Library.h").String()},
assert.NoError,
[]Type{
Expand All @@ -94,8 +95,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Platform file",
"all",
"",
[]string{"all", "platform"},
[]string{"true", "false"},
[]string{platformPath.Join("boards.txt").String()},
assert.NoError,
[]Type{
Expand All @@ -118,8 +119,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Package index file",
"all",
"",
[]string{"all", "package-index"},
[]string{"true", "false"},
[]string{packageIndexFilePath.String()},
assert.NoError,
[]Type{
Expand All @@ -132,8 +133,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Explicit file",
"sketch",
"",
[]string{"sketch"},
[]string{"true", "false"},
[]string{libraryPath.Join("Library.h").String()},
assert.NoError,
[]Type{
Expand All @@ -146,8 +147,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Sketch folder",
"all",
"",
[]string{"all", "sketch"},
[]string{"true", "false"},
[]string{sketchPath.String()},
assert.NoError,
[]Type{
Expand All @@ -160,8 +161,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Library folder",
"all",
"",
[]string{"all", "library"},
[]string{"true", "false"},
[]string{libraryPath.String()},
assert.NoError,
[]Type{
Expand All @@ -179,8 +180,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Platform folder",
"all",
"",
[]string{"all", "platform"},
[]string{"true", "false"},
[]string{platformPath.String()},
assert.NoError,
[]Type{
Expand All @@ -203,8 +204,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Package index folder",
"all",
"",
[]string{"all", "package-index"},
[]string{"true", "false"},
[]string{packageIndexFolderPath.String()},
assert.NoError,
[]Type{
Expand All @@ -217,8 +218,8 @@ func TestFindProjects(t *testing.T) {
},
{
"Explicit folder",
"sketch",
"false",
[]string{"sketch"},
[]string{"false"},
[]string{libraryPath.String()},
assert.NoError,
[]Type{
Expand All @@ -230,9 +231,23 @@ func TestFindProjects(t *testing.T) {
},
},
{
"Projects folder, recursive",
"all",
"true",
"Explicit folder",
[]string{"sketch"},
[]string{"true"},
[]string{libraryPath.String()},
assert.NoError,
[]Type{
{
Path: libraryExamplePath,
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Sketch,
},
},
},
{
"Projects folder",
[]string{"all"},
[]string{"true"},
[]string{projectsPath.String()},
assert.NoError,
[]Type{
Expand All @@ -253,18 +268,37 @@ func TestFindProjects(t *testing.T) {
},
},
},
{
"Projects folder",
[]string{"sketch"},
[]string{"true"},
[]string{projectsPath.String()},
assert.NoError,
[]Type{
{
Path: projectsPathLibraryExample,
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Sketch,
},
{
Path: projectsPathSketch,
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Sketch,
},
},
},
{
"Projects folder, non-recursive",
"all",
"false",
[]string{"all"},
[]string{"false"},
[]string{projectsPath.String()},
assert.Error,
[]Type{},
},
{
"Multiple target folders",
"all",
"true",
[]string{"all"},
[]string{"true"},
[]string{projectsPath.String(), sketchPath.String()},
assert.NoError,
[]Type{
Expand All @@ -290,38 +324,32 @@ func TestFindProjects(t *testing.T) {
},
},
},
{
"superproject type filter",
"sketch",
"true",
[]string{projectsPath.String()},
assert.NoError,
[]Type{
{
Path: projectsPathLibraryExample,
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Sketch,
},
{
Path: projectsPathSketch,
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Sketch,
},
},
},
}

for _, testTable := range testTables {
flags := test.ConfigurationFlags()
flags.Set("project-type", testTable.superprojectTypeFilter)
if testTable.recursive != "" {
flags.Set("recursive", testTable.recursive)
}
configuration.Initialize(flags, testTable.projectPaths)
foundProjects, err := FindProjects()
testTable.errorAssertion(t, err)
if err == nil {
assert.True(t, reflect.DeepEqual(foundProjects, testTable.expectedProjects), testTable.testName)
for _, superprojectTypeFilter := range testTable.superprojectTypeFilter {
for _, recursive := range testTable.recursive {
flags := test.ConfigurationFlags()
flags.Set("project-type", superprojectTypeFilter)
if recursive != "" {
flags.Set("recursive", recursive)
}
configuration.Initialize(flags, testTable.projectPaths)
foundProjects, err := FindProjects()
testTable.errorAssertion(t, err)
if err == nil {
assert.True(
t,
reflect.DeepEqual(foundProjects, testTable.expectedProjects),
fmt.Sprintf(
"%s (%s project-type=%s recursive=%s)",
testTable.testName,
testTable.projectPaths,
superprojectTypeFilter, recursive,
),
)
}
}
}
}
}