Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3427584

Browse files
authored
Merge pull request #187 from per1234/load-package-index
Provide natively loaded package index project data
2 parents 757c8e2 + 447d7cd commit 3427584

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

‎internal/project/packageindex/packageindex.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ import (
3232

3333
// Properties parses the package index from the given path and returns the data.
3434
func Properties(packageIndexPath *paths.Path) (map[string]interface{}, error) {
35+
if packageIndexPath == nil {
36+
return nil, fmt.Errorf("Package index path is nil")
37+
}
3538
rawIndex, err := packageIndexPath.ReadFile()
3639
if err != nil {
37-
panic(err)
40+
returnnil, err
3841
}
3942
var indexData map[string]interface{}
4043
err = json.Unmarshal(rawIndex, &indexData)

‎internal/project/packageindex/packageindex_test.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ func init() {
3535
}
3636

3737
func TestProperties(t *testing.T) {
38-
packageIndex, err := Properties(testDataPath.Join("package_valid_index.json"))
39-
require.Nil(t, err)
38+
assert.NotPanics(t, func() { Properties(nil) }, "Don't panic when package index was not found")
39+
packageIndex, err := Properties(nil)
40+
assert.Error(t, err, "Error when package index was not found")
4041

41-
assert.NotNil(t, packageIndex)
42+
packageIndex, err = Properties(testDataPath.Join("package_valid_index.json"))
43+
require.Nil(t, err, "No error on valid package index")
44+
45+
assert.NotNil(t, packageIndex, "Package index data present")
4246
}
4347

4448
func TestHasValidExtension(t *testing.T) {

‎internal/project/projectdata/packageindex.go‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,35 @@
1616
package projectdata
1717

1818
import (
19-
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
19+
clipackageindex "github.com/arduino/arduino-cli/arduino/cores/packageindex"
20+
"github.com/arduino/arduino-lint/internal/project/packageindex"
2021
)
2122

2223
// InitializeForPackageIndex gathers the package index rule data for the specified project.
2324
func InitializeForPackageIndex() {
25+
packageIndex, packageIndexLoadError = packageindex.Properties(ProjectPath())
2426
if ProjectPath() != nil {
25-
packageIndex, packageIndexLoadError = packageindex.LoadIndex(ProjectPath())
27+
_, packageIndexCLILoadError = clipackageindex.LoadIndex(ProjectPath())
2628
}
2729
}
2830

29-
var packageIndex *packageindex.Index
31+
var packageIndex map[string]interface{}
3032

31-
// PackageIndex returns the packageindex.Index object generated by Arduino CLI.
32-
func PackageIndex() *packageindex.Index {
33+
// PackageIndex returns the package index data.
34+
func PackageIndex() map[string]interface{} {
3335
return packageIndex
3436
}
3537

3638
var packageIndexLoadError error
3739

38-
// PackageIndexLoadError returns the error return of packageindex.LoadIndex().
40+
// PackageIndexLoadError returns the error from loading the package index.
3941
func PackageIndexLoadError() error {
4042
return packageIndexLoadError
4143
}
44+
45+
var packageIndexCLILoadError error
46+
47+
// PackageIndexCLILoadError returns the error return of Arduino CLI's packageindex.LoadIndex().
48+
func PackageIndexCLILoadError() error {
49+
return packageIndexCLILoadError
50+
}

‎internal/project/projectdata/packageindex_test.go‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ func init() {
3636

3737
func TestInitializeForPackageIndex(t *testing.T) {
3838
testTables := []struct {
39-
testName string
40-
path *paths.Path
41-
packageIndexAssertion assert.ValueAssertionFunc
42-
packageIndexLoadErrorAssertion assert.ValueAssertionFunc
39+
testName string
40+
path *paths.Path
41+
packageIndexAssertion assert.ValueAssertionFunc
42+
packageIndexLoadErrorAssertion assert.ValueAssertionFunc
43+
packageIndexCLILoadErrorAssertion assert.ValueAssertionFunc
4344
}{
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},
45+
{"Valid", packageIndexTestDataPath.Join("valid-package-index", "package_foo_index.json"), assert.NotNil, assert.Nil, assert.Nil},
46+
{"Invalid package index", packageIndexTestDataPath.Join("invalid-package-index", "package_foo_index.json"), assert.Nil, assert.NotNil, assert.NotNil},
47+
{"Invalid JSON", packageIndexTestDataPath.Join("invalid-JSON", "package_foo_index.json"), assert.Nil, assert.NotNil, assert.NotNil},
4748
}
4849

4950
for _, testTable := range testTables {
@@ -56,6 +57,7 @@ func TestInitializeForPackageIndex(t *testing.T) {
5657
Initialize(testProject)
5758

5859
testTable.packageIndexLoadErrorAssertion(t, PackageIndexLoadError(), testTable.testName)
60+
testTable.packageIndexCLILoadErrorAssertion(t, PackageIndexCLILoadError(), testTable.testName)
5961
if PackageIndexLoadError() == nil {
6062
testTable.packageIndexAssertion(t, PackageIndex(), testTable.testName)
6163
}

‎internal/rule/rulefunction/packageindex.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func PackageIndexFormat() (result ruleresult.Type, output string) {
7777
return ruleresult.NotRun, "Package index not found"
7878
}
7979

80-
if projectdata.PackageIndexLoadError() != nil {
81-
return ruleresult.Fail, projectdata.PackageIndexLoadError().Error()
80+
if projectdata.PackageIndexCLILoadError() != nil {
81+
return ruleresult.Fail, projectdata.PackageIndexCLILoadError().Error()
8282
}
8383

8484
return ruleresult.Pass, ""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /