Skip to content

Commit

Permalink
Fix lib list --all not returning library includes in json output (#1147)
Browse files Browse the repository at this point in the history
* Fix lib list --all not returning library includes in json output

* [skip changelog] Enhance integration test
  • Loading branch information
silvanocerza committed Jan 22, 2021
1 parent 7e55f9e commit d996b15
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
18 changes: 15 additions & 3 deletions arduino/libraries/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (library *Library) String() string {
}

// ToRPCLibrary converts this library into an rpc.Library
func (library *Library) ToRPCLibrary() *rpc.Library {
func (library *Library) ToRPCLibrary() (*rpc.Library, error) {
pathOrEmpty := func(p *paths.Path) string {
if p == nil {
return ""
Expand All @@ -100,6 +100,18 @@ func (library *Library) ToRPCLibrary() *rpc.Library {
}
return p.String()
}

// If the the "includes" property is empty or not included in the "library.properties" file
// we search for headers by reading the library files directly
headers := library.DeclaredHeaders()
if len(headers) == 0 {
var err error
headers, err = library.SourceHeaders()
if err != nil {
return nil, fmt.Errorf("gathering library headers: %w", err)
}
}

return &rpc.Library{
Name: library.Name,
Author: library.Author,
Expand All @@ -124,9 +136,9 @@ func (library *Library) ToRPCLibrary() *rpc.Library {
Version: library.Version.String(),
License: library.License,
Examples: library.Examples.AsStrings(),
ProvidesIncludes: library.DeclaredHeaders(),
ProvidesIncludes: headers,
CompatibleWith: library.CompatibleWith,
}
}, nil
}

// SupportsAnyArchitectureIn returns true if any of the following is true:
Expand Down
31 changes: 15 additions & 16 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,6 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W

builderCtx.SourceOverride = req.GetSourceOverride()

// Use defer() to create an rpc.CompileResp with all the information available at the
// moment of return.
defer func() {
if r != nil {
importedLibs := []*rpc.Library{}
for _, lib := range builderCtx.ImportedLibraries {
importedLibs = append(importedLibs, lib.ToRPCLibrary())
}

r.BuildPath = builderCtx.BuildPath.String()
r.UsedLibraries = importedLibs
r.ExecutableSectionsSize = builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray()
}
}()

// if --preprocess or --show-properties were passed, we can stop here
if req.GetShowProperties() {
return &rpc.CompileResp{}, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
Expand Down Expand Up @@ -259,6 +244,20 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
}
}

importedLibs := []*rpc.Library{}
for _, lib := range builderCtx.ImportedLibraries {
rpcLib, err := lib.ToRPCLibrary()
if err != nil {
return nil, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err)
}
importedLibs = append(importedLibs, rpcLib)
}

logrus.Tracef("Compile %s for %s successful", sketch.Name, fqbnIn)
return &rpc.CompileResp{}, nil

return &rpc.CompileResp{
BuildPath: builderCtx.BuildPath.String(),
UsedLibraries: importedLibs,
ExecutableSectionsSize: builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray(),
}, nil
}
6 changes: 5 additions & 1 deletion commands/lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryList
if lib.Available != nil {
release = lib.Available.ToRPCLibraryRelease()
}
rpcLib, err := lib.Library.ToRPCLibrary()
if err != nil {
return nil, fmt.Errorf("converting library %s to rpc struct: %w", lib.Library.Name, err)
}
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
Library: lib.Library.ToRPCLibrary(),
Library: rpcLib,
Release: release,
})
}
Expand Down
26 changes: 26 additions & 0 deletions test/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ def test_list_with_fqbn(run_command):
assert data[0]["library"]["compatible_with"]["arduino:avr:uno"]


def test_list_provides_includes_fallback(run_command):
# Verifies "provides_includes" field is returned even if libraries don't declare
# the "includes" property in their "library.properties" file
assert run_command("update")

# Install core
assert run_command("core install arduino:avr@1.8.3")
assert run_command("lib install ArduinoJson@6.17.2")

# List all libraries, even the ones installed with the above core
result = run_command("lib list --all --fqbn arduino:avr:uno --format json")
assert result.ok
assert "" == result.stderr

data = json.loads(result.stdout)
assert 6 == len(data)

libs = {l["library"]["name"]: l["library"]["provides_includes"] for l in data}
assert ["SoftwareSerial.h"] == libs["SoftwareSerial"]
assert ["Wire.h"] == libs["Wire"]
assert ["EEPROM.h"] == libs["EEPROM"]
assert ["HID.h"] == libs["HID"]
assert ["SPI.h"] == libs["SPI"]
assert ["ArduinoJson.h", "ArduinoJson.hpp"] == libs["ArduinoJson"]


def test_lib_download(run_command, downloads_dir):

# Download a specific lib version
Expand Down

0 comments on commit d996b15

Please sign in to comment.