Skip to content

Use Arduino Lint to validate releases #18

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 3 commits into from
May 14, 2021
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
14 changes: 14 additions & 0 deletions .github/workflows/test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ jobs:
with:
go-version: "1.14"

- name: Install Arduino Lint
run: |
ARDUINO_LINT_INSTALLATION_PATH="${{ runner.temp }}/arduino-lint"
mkdir --parents "$ARDUINO_LINT_INSTALLATION_PATH"
curl \
-fsSL \
https://raw.githubusercontent.com/arduino/arduino-lint/main/etc/install.sh \
| \
BINDIR="$ARDUINO_LINT_INSTALLATION_PATH" \
sh

# Add installation folder to path to path
echo "$ARDUINO_LINT_INSTALLATION_PATH" >> "$GITHUB_PATH"

- name: Install Taskfile
uses: arduino/actions/setup-taskfile@master
with:
Expand Down
100 changes: 100 additions & 0 deletions libraries/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package libraries

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)

var empty struct{}

var officialTypes = map[string]struct{}{
"Arduino": empty,
}

func official(metadata *Repo) bool {
for _, libraryType := range metadata.Types {
_, isOfficial := officialTypes[libraryType]
if isOfficial {
return true
}
}
return false
}

// RunArduinoLint runs Arduino Lint on the library and returns the report in the event of error or warnings.
func RunArduinoLint(folder string, metadata *Repo) ([]byte, error) {
JSONReportFolder, err := ioutil.TempDir("", "arduino-lint-report-")
if err != nil {
panic(err)
}
JSONReportPath := filepath.Join(JSONReportFolder, "report.json")
defer os.RemoveAll(JSONReportPath)

// See: https://arduino.github.io/arduino-lint/latest/commands/arduino-lint/
cmd := exec.Command(
"arduino-lint",
"--compliance=permissive",
"--format=text",
"--project-type=library",
"--recursive=false",
"--report-file="+JSONReportPath,
folder,
)
// See: https://arduino.github.io/arduino-lint/latest/#environment-variables
cmd.Env = modifyEnv(os.Environ(), "ARDUINO_LINT_LIBRARY_MANAGER_INDEXING", "true")
cmd.Env = modifyEnv(cmd.Env, "ARDUINO_LINT_OFFICIAL", fmt.Sprintf("%t", official(metadata)))

textReport, lintErr := cmd.CombinedOutput()
if lintErr != nil {
return textReport, lintErr
}

// Read report.
rawJSONReport, err := ioutil.ReadFile(JSONReportPath)
if err != nil {
panic(err)
}
var JSONReport map[string]interface{}
if err := json.Unmarshal(rawJSONReport, &JSONReport); err != nil {
panic(err)
}

// Check warning count.
reportSummary := JSONReport["summary"].(map[string]interface{})
warningCount := reportSummary["warningCount"].(float64)

// Report should be displayed when there are warnings.
if warningCount > 0 {
return textReport, lintErr
}

// No warnings.
return nil, nil
}
108 changes: 108 additions & 0 deletions libraries/lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// This file is part of libraries-repository-engine.
//
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package libraries

import (
"os"
"path/filepath"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

var testDataPath string

func init() {
workingDirectory, err := os.Getwd()
if err != nil {
panic(err)
}
testDataPath = filepath.Join(workingDirectory, "testdata")
}

func TestRunArduinoLint(t *testing.T) {
testTables := []struct {
testName string
folder string
official bool
reportRegexp string
errorAssertion assert.ErrorAssertionFunc
}{
{
"update",
"Arduino_MKRRGB",
true,
"^$",
assert.NoError,
},
{
"official",
"Arduino_TestOff",
true,
"^$",
assert.NoError,
},
{
"unofficial",
"Arduino_Test3rd",
false,
"LP012",
assert.NoError,
},
{
"error",
"Arduino_TestErr",
true,
"LS006",
assert.Error,
},
{
"warning",
"Arduino_TestWarn",
true,
"LP015",
assert.NoError,
},
{
"pass",
"Arduino_TestPass",
true,
"^$",
assert.NoError,
},
}

for _, testTable := range testTables {
var metadata Repo
if testTable.official {
metadata.Types = []string{"Arduino"}
} else {
metadata.Types = []string{"Contributed"}
}
report, err := RunArduinoLint(filepath.Join(testDataPath, "libraries", testTable.folder), &metadata)
assert.Regexp(t, regexp.MustCompile(testTable.reportRegexp), string(report), testTable.testName)
testTable.errorAssertion(t, err, testTable.testName)
}
}
105 changes: 0 additions & 105 deletions libraries/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import (
"context"
"encoding/base64"
"errors"
"regexp"
"strings"

"github.com/google/go-github/github"
ini "github.com/vaughan0/go-ini"
Expand All @@ -64,109 +62,6 @@ type LibraryMetadata struct {
Depends string
}

const categoryUcategorized string = "Uncategorized"

var validCategories = []string{
"Display",
"Communication",
"Signal Input/Output",
"Sensors",
"Device Control",
"Timing",
"Data Storage",
"Data Processing",
"Other",
categoryUcategorized,
}

// IsValidCategory checks if category is a valid category
func IsValidCategory(category string) bool {
for _, c := range validCategories {
if category == c {
return true
}
}
return false
}

// Validate checks LibraryMetadata for errors, returns an array of the errors found
func (library *LibraryMetadata) Validate() []error {
var errorsAccumulator []error

// Check lib name
if !IsValidLibraryName(library.Name) {
errorsAccumulator = append(errorsAccumulator, errors.New("Invalid 'name' field: "+library.Name))
}

// Check author and maintainer existence
if library.Author == "" {
errorsAccumulator = append(errorsAccumulator, errors.New("'author' field must be defined"))
}
if library.Maintainer == "" {
library.Maintainer = library.Author
}

// Check sentence and paragraph and url existence
if library.Sentence == "" || library.URL == "" {
errorsAccumulator = append(errorsAccumulator, errors.New("'sentence' and 'url' fields must be defined"))
}

newVersion, err := VersionToSemverCompliant(library.Version)
if err != nil {
errorsAccumulator = append(errorsAccumulator, err)
}
library.Version = newVersion

// Check if the category is valid and set to "Uncategorized" if not
if !IsValidCategory(library.Category) {
library.Category = categoryUcategorized
}

// Check if 'depends' field is correctly written
if !IsValidDependency(library.Depends) {
errorsAccumulator = append(errorsAccumulator, errors.New("Invalid 'depends' field: "+library.Depends))
}
return errorsAccumulator
}

// IsValidLibraryName checks if a string is a valid library name
func IsValidLibraryName(name string) bool {
if len(name) == 0 {
return false
}
if name[0] == '-' || name[0] == '_' || name[0] == ' ' {
return false
}
for _, char := range name {
if !strings.Contains("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ", string(char)) {
return false
}
}
return true
}

var re = regexp.MustCompile("^([a-zA-Z0-9](?:[a-zA-Z0-9._\\- ]*[a-zA-Z0-9])?) *(?: \\(([^()]*)\\))?$")

// IsValidDependency checks if the `depends` field of library.properties is correctly formatted
func IsValidDependency(depends string) bool {
// TODO: merge this method with db.ExtractDependenciesList
depends = strings.TrimSpace(depends)
if depends == "" {
return true
}
for _, dep := range strings.Split(depends, ",") {
dep = strings.TrimSpace(dep)
if dep == "" {
return false
}
matches := re.FindAllStringSubmatch(dep, -1)
if matches == nil {
return false
}
}
return true
}

// ParsePullRequest makes a LibraryMetadata by reading library.properties from a github.PullRequest
func ParsePullRequest(gh *github.Client, pull *github.PullRequest) (*LibraryMetadata, error) {
head := *pull.Head
Expand Down
45 changes: 0 additions & 45 deletions libraries/metadata/metadata_test.go

This file was deleted.

Loading