Skip to content

Commit b69ed33

Browse files
[skip-changelog] Exclude libraries with a missing .h file from lib list (#2083)
* Exclude libraries with a missing `.h` file from `lib` commands * Add TestLibListDoesNotIncludeEmptyLibraries to lib_test.go * Add unit tests for the changes * Update documentation
1 parent 8c2b8e7 commit b69ed33

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

Diff for: arduino/libraries/libraries_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,15 @@ func TestLibrariesLoader(t *testing.T) {
7373
require.Equal(t, "LibWithNonUTF8Properties", lib.Name)
7474
require.Equal(t, "àrduìnò", lib.Author)
7575
}
76+
{
77+
lib, err := Load(paths.New("testdata", "EmptyLib"), User)
78+
require.Error(t, err)
79+
require.Nil(t, lib)
80+
}
81+
{
82+
lib, err := Load(paths.New("testdata", "LegacyLib"), User)
83+
require.NoError(t, err)
84+
require.Equal(t, "LegacyLib", lib.Name)
85+
require.True(t, lib.IsLegacy)
86+
}
7687
}

Diff for: arduino/libraries/loader.go

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"strings"
2121

22+
"github.com/arduino/arduino-cli/arduino/globals"
2223
"github.com/arduino/arduino-cli/arduino/sketch"
2324
"github.com/arduino/go-paths-helper"
2425
properties "github.com/arduino/go-properties-orderedmap"
@@ -126,6 +127,11 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
126127
}
127128

128129
func makeLegacyLibrary(path *paths.Path, location LibraryLocation) (*Library, error) {
130+
if foundHeader, err := containsHeaderFile(path); err != nil {
131+
return nil, err
132+
} else if !foundHeader {
133+
return nil, errors.Errorf(tr("invalid library: no header files found"))
134+
}
129135
library := &Library{
130136
InstallDir: path.Canonical(),
131137
Location: location,
@@ -186,3 +192,19 @@ func addExamplesToPathList(examplesPath *paths.Path, list *paths.PathList) error
186192
}
187193
return nil
188194
}
195+
196+
// containsHeaderFile returns true if the directory contains a ".h" file.
197+
// Returns false otherwise
198+
func containsHeaderFile(d *paths.Path) (bool, error) {
199+
dirContent, err := d.ReadDir()
200+
if err != nil {
201+
return false, fmt.Errorf(tr("reading directory %[1]s content: %[2]w", d, err))
202+
}
203+
dirContent.FilterOutDirs()
204+
headerExtensions := []string{}
205+
for k := range globals.HeaderFilesValidExtensions {
206+
headerExtensions = append(headerExtensions, k)
207+
}
208+
dirContent.FilterSuffix(headerExtensions...)
209+
return len(dirContent) > 0, nil
210+
}

Diff for: arduino/libraries/testdata/LegacyLib/LegacyLib.h

Whitespace-only changes.

Diff for: docs/library-specification.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,6 @@ allowing the compilation to fail in a difficult to understand way):
419419
## Old library format (pre-1.5)
420420

421421
In order to support old libraries (from Arduino IDE 1.0.x), Arduino IDE and Arduino CLI will also compile libraries
422-
missing a library.properties metadata file. As a result, these libraries should behave as they did in Arduino IDE 1.0.x,
423-
although they will be available for all boards, including non-AVR ones (which wouldn’t have been present in Arduino IDE
424-
1.0.x).
422+
missing a library.properties metadata file (the header file is still required). As a result, these libraries should
423+
behave as they did in Arduino IDE 1.0.x, although they will be available for all boards, including non-AVR ones (which
424+
wouldn’t have been present in Arduino IDE 1.0.x).

Diff for: internal/integrationtest/lib/lib_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -1587,3 +1587,40 @@ func TestLibBundlesWhenLibWithTheSameNameIsInstalledGlobally(t *testing.T) {
15871587
requirejson.Parse(t, stdout).Query(`.[].library.examples[1]`).MustContain(`"OTALeds"`)
15881588
}
15891589
}
1590+
1591+
func TestLibListDoesNotIncludeEmptyLibraries(t *testing.T) {
1592+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
1593+
defer env.CleanUp()
1594+
1595+
_, _, err := cli.Run("lib", "update-index")
1596+
require.NoError(t, err)
1597+
1598+
// Create a library that does not have neither a library.properties nor a ".h" file
1599+
emptyLib := cli.SketchbookDir().Join("libraries", "empty")
1600+
require.NoError(t, emptyLib.MkdirAll())
1601+
1602+
// Check that the output of lib list and lib examples is empty
1603+
stdout, _, err := cli.Run("lib", "list", "--format", "json")
1604+
require.NoError(t, err)
1605+
requirejson.Empty(t, stdout)
1606+
1607+
stdout, _, err = cli.Run("lib", "examples", "--format", "json")
1608+
require.NoError(t, err)
1609+
requirejson.Empty(t, stdout)
1610+
1611+
// Create a library with a header
1612+
libWithHeader := cli.SketchbookDir().Join("libraries", "libWithHeader")
1613+
require.NoError(t, libWithHeader.MkdirAll())
1614+
f, err := libWithHeader.Join("libWithHeader.h").Create()
1615+
require.NoError(t, err)
1616+
require.NoError(t, f.Close())
1617+
1618+
// Check that the output of lib list and lib examples contains the library
1619+
stdout, _, err = cli.Run("lib", "list", "--format", "json")
1620+
require.NoError(t, err)
1621+
requirejson.Len(t, stdout, 1)
1622+
1623+
stdout, _, err = cli.Run("lib", "examples", "--format", "json")
1624+
require.NoError(t, err)
1625+
requirejson.Len(t, stdout, 1)
1626+
}

0 commit comments

Comments
 (0)