Skip to content

[skip-changelog] Exclude libraries with a missing .h file from lib list #2083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions arduino/libraries/libraries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,15 @@ func TestLibrariesLoader(t *testing.T) {
require.Equal(t, "LibWithNonUTF8Properties", lib.Name)
require.Equal(t, "àrduìnò", lib.Author)
}
{
lib, err := Load(paths.New("testdata", "EmptyLib"), User)
require.Error(t, err)
require.Nil(t, lib)
}
{
lib, err := Load(paths.New("testdata", "LegacyLib"), User)
require.NoError(t, err)
require.Equal(t, "LegacyLib", lib.Name)
require.True(t, lib.IsLegacy)
}
}
22 changes: 22 additions & 0 deletions arduino/libraries/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strings"

"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
Expand Down Expand Up @@ -126,6 +127,11 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
}

func makeLegacyLibrary(path *paths.Path, location LibraryLocation) (*Library, error) {
if foundHeader, err := containsHeaderFile(path); err != nil {
return nil, err
} else if !foundHeader {
return nil, errors.Errorf(tr("invalid library: no header files found"))
}
library := &Library{
InstallDir: path.Canonical(),
Location: location,
Expand Down Expand Up @@ -186,3 +192,19 @@ func addExamplesToPathList(examplesPath *paths.Path, list *paths.PathList) error
}
return nil
}

// containsHeaderFile returns true if the directory contains a ".h" file.
// Returns false otherwise
func containsHeaderFile(d *paths.Path) (bool, error) {
dirContent, err := d.ReadDir()
if err != nil {
return false, fmt.Errorf(tr("reading directory %[1]s content: %[2]w", d, err))
}
dirContent.FilterOutDirs()
headerExtensions := []string{}
for k := range globals.HeaderFilesValidExtensions {
headerExtensions = append(headerExtensions, k)
}
dirContent.FilterSuffix(headerExtensions...)
return len(dirContent) > 0, nil
}
Empty file.
6 changes: 3 additions & 3 deletions docs/library-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,6 @@ allowing the compilation to fail in a difficult to understand way):
## Old library format (pre-1.5)

In order to support old libraries (from Arduino IDE 1.0.x), Arduino IDE and Arduino CLI will also compile libraries
missing a library.properties metadata file. As a result, these libraries should behave as they did in Arduino IDE 1.0.x,
although they will be available for all boards, including non-AVR ones (which wouldn’t have been present in Arduino IDE
1.0.x).
missing a library.properties metadata file (the header file is still required). As a result, these libraries should
behave as they did in Arduino IDE 1.0.x, although they will be available for all boards, including non-AVR ones (which
wouldn’t have been present in Arduino IDE 1.0.x).
37 changes: 37 additions & 0 deletions internal/integrationtest/lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1587,3 +1587,40 @@ func TestLibBundlesWhenLibWithTheSameNameIsInstalledGlobally(t *testing.T) {
requirejson.Parse(t, stdout).Query(`.[].library.examples[1]`).MustContain(`"OTALeds"`)
}
}

func TestLibListDoesNotIncludeEmptyLibraries(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

_, _, err := cli.Run("lib", "update-index")
require.NoError(t, err)

// Create a library that does not have neither a library.properties nor a ".h" file
emptyLib := cli.SketchbookDir().Join("libraries", "empty")
require.NoError(t, emptyLib.MkdirAll())

// Check that the output of lib list and lib examples is empty
stdout, _, err := cli.Run("lib", "list", "--format", "json")
require.NoError(t, err)
requirejson.Empty(t, stdout)

stdout, _, err = cli.Run("lib", "examples", "--format", "json")
require.NoError(t, err)
requirejson.Empty(t, stdout)

// Create a library with a header
libWithHeader := cli.SketchbookDir().Join("libraries", "libWithHeader")
require.NoError(t, libWithHeader.MkdirAll())
f, err := libWithHeader.Join("libWithHeader.h").Create()
require.NoError(t, err)
require.NoError(t, f.Close())

// Check that the output of lib list and lib examples contains the library
stdout, _, err = cli.Run("lib", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)

stdout, _, err = cli.Run("lib", "examples", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)
}