Skip to content

Add check for symlinks in library #56

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 1 commit into from
Nov 25, 2020
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
15 changes: 15 additions & 0 deletions check/checkconfigurations/checkconfigurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,21 @@ var configurations = []Type{
ErrorModes: nil,
CheckFunction: checkfunctions.LibraryHasSubmodule,
},
{
ProjectType: projecttype.Library,
Category: "structure",
Subcategory: "general",
ID: "",
Brief: "symlink",
Description: "",
MessageTemplate: "Symlink(s) found at {{.}}. These block acceptance to the Arduino Library Manager index.",
DisableModes: nil,
EnableModes: []checkmode.Type{checkmode.All},
InfoModes: nil,
WarningModes: []checkmode.Type{checkmode.Default},
ErrorModes: []checkmode.Type{checkmode.LibraryManagerSubmission, checkmode.LibraryManagerIndexed},
CheckFunction: checkfunctions.LibraryContainsSymlinks,
},
{
ProjectType: projecttype.Sketch,
Category: "structure",
Expand Down
28 changes: 28 additions & 0 deletions check/checkfunctions/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package checkfunctions

import (
"net/http"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -864,6 +865,33 @@ func LibraryHasSubmodule() (result checkresult.Type, output string) {
return checkresult.Pass, ""
}

// LibraryContainsSymlinks checks if the library folder contains symbolic links.
func LibraryContainsSymlinks() (result checkresult.Type, output string) {
projectPathListing, err := checkdata.ProjectPath().ReadDirRecursive()
if err != nil {
panic(err)
}
projectPathListing.FilterOutDirs()

symlinkPaths := []string{}
for _, projectPathItem := range projectPathListing {
projectPathItemStat, err := os.Lstat(projectPathItem.String())
if err != nil {
panic(err)
}

if projectPathItemStat.Mode()&os.ModeSymlink != 0 {
symlinkPaths = append(symlinkPaths, projectPathItem.String())
}
}

if len(symlinkPaths) > 0 {
return checkresult.Fail, strings.Join(symlinkPaths, ", ")
}

return checkresult.Pass, ""
}

// spellCheckLibraryPropertiesFieldValue returns the value of the provided library.properties field with commonly misspelled words corrected.
func spellCheckLibraryPropertiesFieldValue(fieldName string) (result checkresult.Type, output string) {
if checkdata.LibraryPropertiesLoadError() != nil {
Expand Down
25 changes: 25 additions & 0 deletions check/checkfunctions/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/arduino/arduino-check/project/projecttype"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var testDataPath *paths.Path
Expand Down Expand Up @@ -200,3 +201,27 @@ func TestLibraryHasSubmodule(t *testing.T) {

checkCheckFunction(LibraryHasSubmodule, testTables, t)
}

func TestLibraryContainsSymlinks(t *testing.T) {
testLibrary := "Recursive"
symlinkPath := testDataPath.Join(testLibrary, "test-symlink")
// It's probably most friendly to developers using Windows to create the symlink needed for the test on demand.
err := os.Symlink(testDataPath.Join(testLibrary, "library.properties").String(), symlinkPath.String())
require.Nil(t, err, "This test must be run as administrator on Windows to have symlink creation privilege.")
defer symlinkPath.RemoveAll() // clean up

testTables := []checkFunctionTestTable{
{"Has symlink", testLibrary, checkresult.Fail, ""},
}

checkCheckFunction(LibraryContainsSymlinks, testTables, t)

err = symlinkPath.RemoveAll()
require.Nil(t, err)

testTables = []checkFunctionTestTable{
{"No symlink", testLibrary, checkresult.Pass, ""},
}

checkCheckFunction(LibraryContainsSymlinks, testTables, t)
}