Skip to content

Commit

Permalink
Add tests for the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoPologruto committed May 3, 2023
1 parent a760b7c commit a34f57d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
68 changes: 68 additions & 0 deletions arduino/libraries/libraries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package libraries

import (
"encoding/json"
"os"
"testing"
"time"

paths "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -85,3 +87,69 @@ func TestLibrariesLoader(t *testing.T) {
require.True(t, lib.IsLegacy)
}
}

func TestSymlinkLoop(t *testing.T) {
// Set up directory structure of test library.
testLib := paths.New("testdata", "TestLib")
examplesPath := testLib.Join("examples")
require.NoError(t, examplesPath.Mkdir())
defer examplesPath.RemoveAll()

// It's probably most friendly for contributors using Windows to create the symlinks needed for the test on demand.
err := os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer1").String())
require.NoError(t, err, "This test must be run as administrator on Windows to have symlink creation privilege.")
// It's necessary to have multiple symlinks to a parent directory to create the loop.
err = os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer2").String())
require.NoError(t, err)

// The failure condition is Load() never returning, testing for which requires setting up a timeout.
done := make(chan bool)
go func() {
_, err = Load(testLib, User)
done <- true
}()
select {
case <-done:
case <-time.After(2 * time.Second):
require.FailNow(t, "Load didn't complete in the allocated time.")
}
require.Error(t, err)
}

func TestLegacySymlinkLoop(t *testing.T) {
// Set up directory structure of test library.
testLib := paths.New("testdata", "LegacyLib")
examplesPath := testLib.Join("examples")
require.NoError(t, examplesPath.Mkdir())
defer examplesPath.RemoveAll()

// It's probably most friendly for contributors using Windows to create the symlinks needed for the test on demand.
err := os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer1").String())
require.NoError(t, err, "This test must be run as administrator on Windows to have symlink creation privilege.")
// It's necessary to have multiple symlinks to a parent directory to create the loop.
err = os.Symlink(examplesPath.Join("..").String(), examplesPath.Join("UpGoer2").String())
require.NoError(t, err)

// The failure condition is Load() never returning, testing for which requires setting up a timeout.
done := make(chan bool)
go func() {
_, err = Load(testLib, User)
done <- true
}()

select {
case <-done:
case <-time.After(2 * time.Second):
require.FailNow(t, "Load didn't complete in the allocated time.")
}
require.Error(t, err)
}

func TestLoadExamples(t *testing.T) {
example, err := paths.New(".", "testdata", "TestLibExamples", "examples", "simple").Abs()
require.NoError(t, err)
lib, err := Load(paths.New("testdata", "TestLibExamples"), User)
require.NoError(t, err)
require.Len(t, lib.Examples, 1)
require.True(t, lib.Examples.Contains(example))
}
Empty file.
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions arduino/libraries/testdata/TestLibExamples/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=TestLib
version=1.0.3
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=A test lib
paragraph=very powerful!
category=Device Control
url=http://www.arduino.cc/en/Reference/TestLib
architectures=avr
Empty file.

0 comments on commit a34f57d

Please sign in to comment.