Skip to content

Commit 0bc964f

Browse files
committed
Add convenience functions for detecting whether a path contains library files
1 parent c65d0cc commit 0bc964f

File tree

7 files changed

+93
-10
lines changed

7 files changed

+93
-10
lines changed

Diff for: check/checkfunctions/library.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -904,16 +904,8 @@ func LibraryPropertiesMisspelledOptionalField() (result checkresult.Type, output
904904

905905
// LibraryInvalid checks whether the provided path is a valid library.
906906
func LibraryInvalid() (result checkresult.Type, output string) {
907-
directoryListing, err := checkdata.LoadedLibrary().SourceDir.ReadDir()
908-
if err != nil {
909-
panic(err)
910-
}
911-
912-
directoryListing.FilterOutDirs()
913-
for _, potentialHeaderFile := range directoryListing {
914-
if library.HasHeaderFileValidExtension(potentialHeaderFile) {
915-
return checkresult.Pass, ""
916-
}
907+
if library.ContainsHeaderFile(checkdata.LoadedLibrary().SourceDir) {
908+
return checkresult.Pass, ""
917909
}
918910

919911
return checkresult.Fail, ""

Diff for: project/library/library.go

+50
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package library
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/arduino/go-paths-helper"
2123
)
2224

@@ -35,6 +37,30 @@ func HasHeaderFileValidExtension(filePath *paths.Path) bool {
3537
return hasHeaderFileValidExtension
3638
}
3739

40+
// ContainsHeaderFile checks whether the provided path contains a file with valid header extension.
41+
func ContainsHeaderFile(searchPath *paths.Path) bool {
42+
if searchPath.NotExist() {
43+
panic(fmt.Sprintf("Error: provided path %s does not exist.", searchPath))
44+
}
45+
if searchPath.IsNotDir() {
46+
panic(fmt.Sprintf("Error: provided path %s is not a directory.", searchPath))
47+
}
48+
49+
directoryListing, err := searchPath.ReadDir()
50+
if err != nil {
51+
panic(err)
52+
}
53+
54+
directoryListing.FilterOutDirs()
55+
for _, potentialHeaderFile := range directoryListing {
56+
if HasHeaderFileValidExtension(potentialHeaderFile) {
57+
return true
58+
}
59+
}
60+
61+
return false
62+
}
63+
3864
// See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-metadata
3965
var metadataFilenames = map[string]struct{}{
4066
"library.properties": empty,
@@ -49,6 +75,30 @@ func IsMetadataFile(filePath *paths.Path) bool {
4975
return false
5076
}
5177

78+
// ContainsMetadataFile checks whether the provided path contains an Arduino library metadata file.
79+
func ContainsMetadataFile(searchPath *paths.Path) bool {
80+
if searchPath.NotExist() {
81+
panic(fmt.Sprintf("Error: provided path %s does not exist.", searchPath))
82+
}
83+
if searchPath.IsNotDir() {
84+
panic(fmt.Sprintf("Error: provided path %s is not a directory.", searchPath))
85+
}
86+
87+
directoryListing, err := searchPath.ReadDir()
88+
if err != nil {
89+
panic(err)
90+
}
91+
92+
directoryListing.FilterOutDirs()
93+
for _, potentialMetadataFile := range directoryListing {
94+
if IsMetadataFile(potentialMetadataFile) {
95+
return true
96+
}
97+
}
98+
99+
return false
100+
}
101+
52102
// See: https://arduino.github.io/arduino-cli/latest/library-specification/#library-examples
53103
var examplesFolderValidNames = map[string]struct{}{
54104
"examples": empty,

Diff for: project/library/library_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 [email protected].
15+
16+
package library
17+
18+
import (
19+
"os"
20+
"testing"
21+
22+
"github.com/arduino/go-paths-helper"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
var testDataPath *paths.Path
27+
28+
func init() {
29+
workingDirectory, _ := os.Getwd()
30+
testDataPath = paths.New(workingDirectory, "testdata")
31+
}
32+
33+
func TestContainsHeaderFile(t *testing.T) {
34+
assert.True(t, ContainsHeaderFile(testDataPath.Join("ContainsHeaderFile")))
35+
assert.False(t, ContainsHeaderFile(testDataPath.Join("ContainsNoHeaderFile")))
36+
}
37+
38+
func TestContainsMetadataFile(t *testing.T) {
39+
assert.True(t, ContainsMetadataFile(testDataPath.Join("ContainsMetadataFile")))
40+
assert.False(t, ContainsMetadataFile(testDataPath.Join("ContainsNoMetadataFile")))
41+
}

Diff for: project/library/testdata/ContainsHeaderFile/foo.h

Whitespace-only changes.

Diff for: project/library/testdata/ContainsMetadataFile/library.properties

Whitespace-only changes.

Diff for: project/library/testdata/ContainsNoHeaderFile/foo.bar

Whitespace-only changes.

Diff for: project/library/testdata/ContainsNoMetadataFile/foo.bar

Whitespace-only changes.

0 commit comments

Comments
 (0)