Skip to content

Commit 0c3b358

Browse files
committed
Make package index loading function handle missing file
Each possible problem with the project is handled by a dedicated rule. One such problem is that the project was not found. Each rule must be configured so that unrelated problems with the project will not cause them to break or provide incorrect results. This can result in a lot of redundant boilerplate code. Before running a rule against the package index data, it is always necessary to check whether the data is available. If not, running the rule is abandoned with a "NotRun" result. This which results in just such boilerplate, but is likely to be unavoidable without breaking from the modular architecture of the rules system. If the package index file itself was not found, then the loading of the data can not be accomplished, and thus a missing package index should also be cause for abandoning the run of data rules. The check for data can serve as an implicit check for the existence of the data source, meaning additional rule function boilerplate for file existence check can be avoided.
1 parent 757c8e2 commit 0c3b358

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Diff for: internal/project/packageindex/packageindex.go

+4-1
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+
return nil, err
3841
}
3942
var indexData map[string]interface{}
4043
err = json.Unmarshal(rawIndex, &indexData)

Diff for: internal/project/packageindex/packageindex_test.go

+7-3
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) {

0 commit comments

Comments
 (0)