Skip to content

Commit edcf04a

Browse files
committed
Improve handling of missing package index
Following the convention of the other project types, the user can specify the path to a folder containing a package index. Previously, when no file resembling a package index was found in that folder, the linting process would error. It is correct for this to be considered an error, however the panic process was used for this purpose. Although it printed a friendly error message at the start of the output, a panic also outputs a traceback. That traceback is very useful for debugging the code of the action, but it's not very user friendly. The established convention is that panics are used for errors that should not occur if the tool is operating correctly, which errors caused by the user input are handled only with a friendly error message. The new behavior is to use the rule system to handle this situation. This allows for the addition of additional rules that might provide the user with information about the cause of the error.
1 parent 1c754bd commit edcf04a

File tree

7 files changed

+47
-3
lines changed

7 files changed

+47
-3
lines changed

internal/project/packageindex/packageindex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ func Find(folderPath *paths.Path) (*paths.Path, error) {
8181
}
8282
}
8383

84-
return nil, fmt.Errorf("No package index file found in %s", folderPath)
84+
return nil, nil
8585
}

internal/project/packageindex/packageindex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestFind(t *testing.T) {
7070
{"Single", testDataPath.Join("HasPackageIndex"), testDataPath.Join("HasPackageIndex", "package_foo_index.json"), assert.Nil},
7171
{"Multiple", testDataPath.Join("HasMultiple"), testDataPath.Join("HasMultiple", "package_foo_index.json"), assert.Nil},
7272
{"Valid extension fallback", testDataPath.Join("HasJSON"), testDataPath.Join("HasJSON", "foo.json"), assert.Nil},
73-
{"None", testDataPath.Join("HasNone"), nil, assert.NotNil},
73+
{"None", testDataPath.Join("HasNone"), nil, assert.Nil},
7474
}
7575

7676
for _, testTable := range testTables {

internal/project/projectdata/packageindex.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121

2222
// InitializeForPackageIndex gathers the package index rule data for the specified project.
2323
func InitializeForPackageIndex() {
24-
packageIndex, packageIndexLoadError = packageindex.LoadIndex(ProjectPath())
24+
if ProjectPath() != nil {
25+
packageIndex, packageIndexLoadError = packageindex.LoadIndex(ProjectPath())
26+
}
2527
}
2628

2729
var packageIndex *packageindex.Index

internal/rule/ruleconfiguration/ruleconfiguration.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,22 @@ var configurations = []Type{
14811481
ErrorModes: []rulemode.Type{rulemode.Strict},
14821482
RuleFunction: rulefunction.IncorrectArduinoDotHFileNameCase,
14831483
},
1484+
{
1485+
ProjectType: projecttype.PackageIndex,
1486+
SuperprojectType: projecttype.All,
1487+
Category: "data",
1488+
Subcategory: "general",
1489+
ID: "IS001",
1490+
Brief: "missing",
1491+
Description: "",
1492+
MessageTemplate: "No package index was found in specified project path. See: https://arduino.github.io/arduino-cli/latest/package_index_json-specification/",
1493+
DisableModes: nil,
1494+
EnableModes: []rulemode.Type{rulemode.Default},
1495+
InfoModes: nil,
1496+
WarningModes: nil,
1497+
ErrorModes: []rulemode.Type{rulemode.Default},
1498+
RuleFunction: rulefunction.PackageIndexMissing,
1499+
},
14841500
{
14851501
ProjectType: projecttype.PackageIndex,
14861502
SuperprojectType: projecttype.All,

internal/rule/rulefunction/packageindex.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@ import (
2222

2323
// The rule functions for package indexes.
2424

25+
// PackageIndexMissing checks whether a file resembling a package index was found in the specified project folder.
26+
func PackageIndexMissing() (result ruleresult.Type, output string) {
27+
if projectdata.ProjectPath() == nil {
28+
return ruleresult.Fail, ""
29+
}
30+
31+
return ruleresult.Pass, ""
32+
}
33+
2534
// PackageIndexJSONFormat checks whether the package index file is a valid JSON document.
2635
func PackageIndexJSONFormat() (result ruleresult.Type, output string) {
36+
if projectdata.ProjectPath() == nil {
37+
return ruleresult.NotRun, "Package index not found"
38+
}
39+
2740
if isValidJSON(projectdata.ProjectPath()) {
2841
return ruleresult.Pass, ""
2942
}
@@ -33,6 +46,10 @@ func PackageIndexJSONFormat() (result ruleresult.Type, output string) {
3346

3447
// PackageIndexFormat checks for invalid package index data format.
3548
func PackageIndexFormat() (result ruleresult.Type, output string) {
49+
if projectdata.ProjectPath() == nil {
50+
return ruleresult.NotRun, "Package index not found"
51+
}
52+
3653
if projectdata.PackageIndexLoadError() != nil {
3754
return ruleresult.Fail, projectdata.PackageIndexLoadError().Error()
3855
}

internal/rule/rulefunction/packageindex_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ func checkPackageIndexRuleFunction(ruleFunction Type, testTables []packageIndexR
5959
}
6060
}
6161

62+
func TestPackageIndexMissing(t *testing.T) {
63+
testTables := []packageIndexRuleFunctionTestTable{
64+
{"Missing", "missing", ruleresult.Fail, ""},
65+
{"Present", "valid-package-index", ruleresult.Pass, ""},
66+
}
67+
68+
checkPackageIndexRuleFunction(PackageIndexMissing, testTables, t)
69+
}
70+
6271
func TestPackageIndexJSONFormat(t *testing.T) {
6372
testTables := []packageIndexRuleFunctionTestTable{
6473
{"Invalid JSON", "invalid-JSON", ruleresult.Fail, ""},

internal/rule/rulefunction/testdata/packageindexes/missing/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)