Skip to content

Commit e7d7808

Browse files
committed
Provide check data for package indexes
1 parent 83b63b7 commit e7d7808

File tree

17 files changed

+274
-6
lines changed

17 files changed

+274
-6
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66

77
# Test files
88
/check/checkdata/schema/testdata/invalid-schema.json
9+
/check/checkdata/testdata/packageindexes/invalid-JSON/package_foo_index.json

check/checkdata/checkdata.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package checkdata
2121

2222
import (
2323
"github.com/arduino/arduino-check/project"
24+
"github.com/arduino/arduino-check/project/packageindex"
2425
"github.com/arduino/arduino-check/project/projecttype"
2526
"github.com/arduino/go-paths-helper"
2627
)
@@ -35,6 +36,14 @@ func Initialize(project project.Type, schemasPath *paths.Path) {
3536
InitializeForLibrary(project, schemasPath)
3637
case projecttype.Platform:
3738
case projecttype.PackageIndex:
39+
var err error
40+
// Because a package index project is a file, but project.Path may be a folder, an extra discovery step is needed for this project type.
41+
projectPath, err = packageindex.Find(project.Path)
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
InitializeForPackageIndex()
3847
}
3948
}
4049

check/checkdata/packageindex.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package checkdata
17+
18+
import (
19+
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
20+
)
21+
22+
// InitializeForPackageIndex gathers the package index check data for the specified project.
23+
func InitializeForPackageIndex() {
24+
packageIndex, packageIndexLoadError = packageindex.LoadIndex(ProjectPath())
25+
}
26+
27+
var packageIndex *packageindex.Index
28+
29+
// PackageIndex returns the packageindex.Index object generated by Arduino CLI.
30+
func PackageIndex() *packageindex.Index {
31+
return packageIndex
32+
}
33+
34+
var packageIndexLoadError error
35+
36+
// PackageIndexLoadError returns the error return of packageindex.LoadIndex().
37+
func PackageIndexLoadError() error {
38+
return packageIndexLoadError
39+
}

check/checkdata/packageindex_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package checkdata
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-check/project"
22+
"github.com/arduino/arduino-check/project/projecttype"
23+
"github.com/arduino/go-paths-helper"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
var packageIndexTestDataPath *paths.Path
28+
29+
func init() {
30+
workingDirectory, err := paths.Getwd()
31+
if err != nil {
32+
panic(err)
33+
}
34+
packageIndexTestDataPath = workingDirectory.Join("testdata", "packageindexes")
35+
}
36+
37+
func TestInitializeForPackageIndex(t *testing.T) {
38+
testTables := []struct {
39+
testName string
40+
path *paths.Path
41+
packageIndexAssertion assert.ValueAssertionFunc
42+
packageIndexLoadErrorAssertion assert.ValueAssertionFunc
43+
}{
44+
{"Valid", packageIndexTestDataPath.Join("valid-package-index", "package_foo_index.json"), assert.NotNil, assert.Nil},
45+
{"Invalid package index", packageIndexTestDataPath.Join("invalid-package-index", "package_foo_index.json"), assert.Nil, assert.NotNil},
46+
{"Invalid JSON", packageIndexTestDataPath.Join("invalid-JSON", "package_foo_index.json"), assert.Nil, assert.NotNil},
47+
}
48+
49+
for _, testTable := range testTables {
50+
51+
testProject := project.Type{
52+
Path: testTable.path,
53+
ProjectType: projecttype.PackageIndex,
54+
SuperprojectType: projecttype.PackageIndex,
55+
}
56+
Initialize(testProject, nil)
57+
58+
testTable.packageIndexLoadErrorAssertion(t, PackageIndexLoadError(), testTable.testName)
59+
if PackageIndexLoadError() == nil {
60+
testTable.packageIndexAssertion(t, PackageIndex(), testTable.testName)
61+
}
62+
}
63+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"foo": "bar"
4+
},
5+
{
6+
"baz": "bat"
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"foo": "bar"
4+
},
5+
{
6+
"baz": "bat"
7+
}
8+
]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "myboard",
5+
"maintainer": "Jane Developer",
6+
"websiteURL": "https://github.com/janedeveloper/myboard",
7+
"email": "jane@example.com",
8+
"help": {
9+
"online": "http://example.com/forum/myboard"
10+
},
11+
"platforms": [
12+
{
13+
"name": "My Board",
14+
"architecture": "avr",
15+
"version": "1.0.0",
16+
"category": "Contributed",
17+
"help": {
18+
"online": "http://example.com/forum/myboard"
19+
},
20+
"url": "https://janedeveloper.github.io/myboard/myboard-1.0.0.zip",
21+
"archiveFileName": "myboard-1.0.0.zip",
22+
"checksum": "SHA-256:ec3ff8a1dc96d3ba6f432b9b837a35fd4174a34b3d2927de1d51010e8b94f9f1",
23+
"size": "15005",
24+
"boards": [{ "name": "My Board" }, { "name": "My Board Pro" }],
25+
"toolsDependencies": [
26+
{
27+
"packager": "arduino",
28+
"name": "avr-gcc",
29+
"version": "4.8.1-arduino5"
30+
},
31+
{
32+
"packager": "arduino",
33+
"name": "avrdude",
34+
"version": "6.0.1-arduino5"
35+
}
36+
]
37+
},
38+
{
39+
"name": "My Board",
40+
"architecture": "avr",
41+
"version": "1.0.1",
42+
"category": "Contributed",
43+
"help": {
44+
"online": "http://example.com/forum/myboard"
45+
},
46+
"url": "https://janedeveloper.github.io/myboard/myboard-1.0.1.zip",
47+
"archiveFileName": "myboard-1.0.1.zip",
48+
"checksum": "SHA-256:9c86ee28a7ce9fe33e8b07ec643316131e0031b0d22e63bb398902a5fdadbca9",
49+
"size": "15125",
50+
"boards": [{ "name": "My Board" }, { "name": "My Board Pro" }],
51+
"toolsDependencies": [
52+
{
53+
"packager": "arduino",
54+
"name": "avr-gcc",
55+
"version": "4.8.1-arduino5"
56+
},
57+
{
58+
"packager": "arduino",
59+
"name": "avrdude",
60+
"version": "6.0.1-arduino5"
61+
}
62+
]
63+
}
64+
],
65+
"tools": []
66+
}
67+
]
68+
}

project/packageindex/packageindex.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ See: https://arduino.github.io/arduino-cli/latest/package_index_json-specificati
2020
package packageindex
2121

2222
import (
23+
"fmt"
2324
"regexp"
2425

2526
"github.com/arduino/go-paths-helper"
@@ -51,3 +52,33 @@ func HasValidFilename(filePath *paths.Path, officialCheckMode bool) bool {
5152
filename := filePath.Base()
5253
return regex.MatchString(filename)
5354
}
55+
56+
func Find(folderPath *paths.Path) (*paths.Path, error) {
57+
exist, err := folderPath.ExistCheck()
58+
if !exist {
59+
return nil, fmt.Errorf("Error opening path %s: %s", folderPath, err)
60+
}
61+
62+
if folderPath.IsNotDir() {
63+
return folderPath, nil
64+
}
65+
66+
directoryListing, err := folderPath.ReadDir()
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
directoryListing.FilterOutDirs()
72+
for _, potentialPackageIndexFile := range directoryListing {
73+
if HasValidFilename(potentialPackageIndexFile, true) {
74+
return potentialPackageIndexFile, nil
75+
}
76+
}
77+
for _, potentialPackageIndexFile := range directoryListing {
78+
if HasValidExtension(potentialPackageIndexFile) {
79+
return potentialPackageIndexFile, nil
80+
}
81+
}
82+
83+
return nil, fmt.Errorf("No package index file found in %s", folderPath)
84+
}

project/packageindex/packageindex_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@
1616
package packageindex
1717

1818
import (
19+
"os"
1920
"testing"
2021

2122
"github.com/arduino/go-paths-helper"
2223
"github.com/stretchr/testify/assert"
2324
)
2425

26+
var testDataPath *paths.Path
27+
28+
func init() {
29+
workingDirectory, err := os.Getwd()
30+
if err != nil {
31+
panic(err)
32+
}
33+
testDataPath = paths.New(workingDirectory, "testdata")
34+
}
35+
2536
func TestHasValidExtension(t *testing.T) {
2637
assert.True(t, HasValidExtension(paths.New("/foo", "bar.json")))
2738
assert.False(t, HasValidExtension(paths.New("/foo", "bar.baz")))
@@ -46,3 +57,27 @@ func TestHasValidFilename(t *testing.T) {
4657
testTable.assertion(t, HasValidFilename(paths.New("/foo", testTable.filename), testTable.officialCheckMode), testTable.testName)
4758
}
4859
}
60+
61+
func TestFind(t *testing.T) {
62+
testTables := []struct {
63+
testName string
64+
path *paths.Path
65+
expectedPath *paths.Path
66+
errAssertion assert.ValueAssertionFunc
67+
}{
68+
{"Nonexistent", testDataPath.Join("nonexistent"), nil, assert.NotNil},
69+
{"File", testDataPath.Join("HasPackageIndex", "package_foo_index.json"), testDataPath.Join("HasPackageIndex", "package_foo_index.json"), assert.Nil},
70+
{"Single", testDataPath.Join("HasPackageIndex"), testDataPath.Join("HasPackageIndex", "package_foo_index.json"), assert.Nil},
71+
{"Multiple", testDataPath.Join("HasMultiple"), testDataPath.Join("HasMultiple", "package_foo_index.json"), assert.Nil},
72+
{"Valid extension fallback", testDataPath.Join("HasJSON"), testDataPath.Join("HasJSON", "foo.json"), assert.Nil},
73+
{"None", testDataPath.Join("HasNone"), nil, assert.NotNil},
74+
}
75+
76+
for _, testTable := range testTables {
77+
path, err := Find(testTable.path)
78+
testTable.errAssertion(t, err, testTable.testName)
79+
if err == nil {
80+
assert.Equal(t, testTable.expectedPath, path, testTable.testName)
81+
}
82+
}
83+
}

project/packageindex/testdata/HasJSON/foo.json

Whitespace-only changes.

project/packageindex/testdata/HasMultiple/foo.json

Whitespace-only changes.

project/packageindex/testdata/HasMultiple/package_foo_index.json

Whitespace-only changes.

project/packageindex/testdata/HasNone/foo.bar

Whitespace-only changes.

project/packageindex/testdata/HasPackageIndex/package_foo_index.json

Whitespace-only changes.

project/packageindex/testdata/HasPrimaryPackageIndex/package_index.json

Whitespace-only changes.

project/project.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ func findProjects(targetPath *paths.Path) ([]Type, error) {
6060
// The filename provides additional information about the project type. So rather than using isProject(), which doesn't make use this information, use a specialized function that does.
6161
isProject, projectType := isProjectIndicatorFile(targetPath, configuration.SuperprojectTypeFilter())
6262
if isProject {
63+
var projectPath *paths.Path
64+
if projectType == projecttype.PackageIndex {
65+
projectPath = targetPath // With package indexes the project is the file. When the user has provided the full path to the project, that information should be preserved.
66+
} else {
67+
projectPath = targetPath.Parent()
68+
}
6369
foundProject := Type{
64-
Path: targetPath.Parent(),
70+
Path: projectPath,
6571
ProjectType: projectType,
6672
SuperprojectType: projectType,
6773
}

project/project_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func TestFindProjects(t *testing.T) {
4444
platformPath := testDataPath.Join("Platform")
4545
platformBundledLibraryPath := testDataPath.Join("Platform", "libraries", "Library")
4646
platformBundledLibraryExamplePath := testDataPath.Join("Platform", "libraries", "Library", "examples", "Example")
47-
packageIndexPath := testDataPath.Join("PackageIndex")
47+
packageIndexFolderPath := testDataPath.Join("PackageIndex")
48+
packageIndexFilePath := packageIndexFolderPath.Join("package_foo_index.json")
4849
projectsPath := testDataPath.Join("Projects")
4950
projectsPathSketch := testDataPath.Join("Projects", "Sketch")
5051
projectsPathLibrary := testDataPath.Join("Projects", "Library")
@@ -119,11 +120,11 @@ func TestFindProjects(t *testing.T) {
119120
"Package index file",
120121
"all",
121122
"",
122-
[]string{packageIndexPath.Join("package_foo_index.json").String()},
123+
[]string{packageIndexFilePath.String()},
123124
assert.NoError,
124125
[]Type{
125126
{
126-
Path: packageIndexPath,
127+
Path: packageIndexFilePath,
127128
ProjectType: projecttype.PackageIndex,
128129
SuperprojectType: projecttype.PackageIndex,
129130
},
@@ -190,11 +191,11 @@ func TestFindProjects(t *testing.T) {
190191
"Package index folder",
191192
"all",
192193
"",
193-
[]string{packageIndexPath.String()},
194+
[]string{packageIndexFolderPath.String()},
194195
assert.NoError,
195196
[]Type{
196197
{
197-
Path: packageIndexPath,
198+
Path: packageIndexFolderPath,
198199
ProjectType: projecttype.PackageIndex,
199200
SuperprojectType: projecttype.PackageIndex,
200201
},

0 commit comments

Comments
 (0)