From dfbf33a5a6cb6ed2956d26cdec14e3ef46c001a9 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Thu, 21 Jan 2021 17:49:28 +0100 Subject: [PATCH 1/2] Fix lib list --all not returning library includes in json output --- arduino/libraries/libraries.go | 18 +++++++++++++++--- commands/compile/compile.go | 31 +++++++++++++++---------------- commands/lib/list.go | 6 +++++- test/test_lib.py | 21 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/arduino/libraries/libraries.go b/arduino/libraries/libraries.go index 8d83e9b93cc..4759ba4f4bb 100644 --- a/arduino/libraries/libraries.go +++ b/arduino/libraries/libraries.go @@ -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 "" @@ -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, @@ -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: diff --git a/commands/compile/compile.go b/commands/compile/compile.go index c8ea46a0a09..e8da8448383 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -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) @@ -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 } diff --git a/commands/lib/list.go b/commands/lib/list.go index de23709bf14..f1e5b40bfbb 100644 --- a/commands/lib/list.go +++ b/commands/lib/list.go @@ -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, }) } diff --git a/test/test_lib.py b/test/test_lib.py index 6dfe0495dde..4acde592b90 100644 --- a/test/test_lib.py +++ b/test/test_lib.py @@ -145,6 +145,27 @@ def test_list_with_fqbn(run_command): assert data[0]["library"]["compatible_with"]["arduino:avr:uno"] +def test_list_all_with_fqbn(run_command): + assert run_command("update") + + # Install core + assert run_command("core install arduino:avr") + + result = run_command("lib list --all --fqbn arduino:avr:uno --format json") + assert result.ok + assert "" == result.stderr + + data = json.loads(result.stdout) + assert 5 == 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"] + + def test_lib_download(run_command, downloads_dir): # Download a specific lib version From 8bcb2f088c59ac0149b592b9f6178c3070174826 Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Fri, 22 Jan 2021 10:41:46 +0100 Subject: [PATCH 2/2] [skip changelog] Enhance integration test --- test/test_lib.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/test_lib.py b/test/test_lib.py index 4acde592b90..8625b306e95 100644 --- a/test/test_lib.py +++ b/test/test_lib.py @@ -145,18 +145,22 @@ def test_list_with_fqbn(run_command): assert data[0]["library"]["compatible_with"]["arduino:avr:uno"] -def test_list_all_with_fqbn(run_command): +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") + 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 5 == len(data) + assert 6 == len(data) libs = {l["library"]["name"]: l["library"]["provides_includes"] for l in data} assert ["SoftwareSerial.h"] == libs["SoftwareSerial"] @@ -164,6 +168,7 @@ def test_list_all_with_fqbn(run_command): 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):