@@ -17,7 +17,9 @@ package libraries
17
17
18
18
import (
19
19
"encoding/json"
20
+ "os"
20
21
"testing"
22
+ "time"
21
23
22
24
paths "github.com/arduino/go-paths-helper"
23
25
"github.com/stretchr/testify/require"
@@ -85,3 +87,69 @@ func TestLibrariesLoader(t *testing.T) {
85
87
require .True (t , lib .IsLegacy )
86
88
}
87
89
}
90
+
91
+ func TestSymlinkLoop (t * testing.T ) {
92
+ // Set up directory structure of test library.
93
+ testLib := paths .New ("testdata" , "TestLib" )
94
+ examplesPath := testLib .Join ("examples" )
95
+ require .NoError (t , examplesPath .Mkdir ())
96
+ defer examplesPath .RemoveAll ()
97
+
98
+ // It's probably most friendly for contributors using Windows to create the symlinks needed for the test on demand.
99
+ err := os .Symlink (examplesPath .Join (".." ).String (), examplesPath .Join ("UpGoer1" ).String ())
100
+ require .NoError (t , err , "This test must be run as administrator on Windows to have symlink creation privilege." )
101
+ // It's necessary to have multiple symlinks to a parent directory to create the loop.
102
+ err = os .Symlink (examplesPath .Join (".." ).String (), examplesPath .Join ("UpGoer2" ).String ())
103
+ require .NoError (t , err )
104
+
105
+ // The failure condition is Load() never returning, testing for which requires setting up a timeout.
106
+ done := make (chan bool )
107
+ go func () {
108
+ _ , err = Load (testLib , User )
109
+ done <- true
110
+ }()
111
+ select {
112
+ case <- done :
113
+ case <- time .After (2 * time .Second ):
114
+ require .FailNow (t , "Load didn't complete in the allocated time." )
115
+ }
116
+ require .Error (t , err )
117
+ }
118
+
119
+ func TestLegacySymlinkLoop (t * testing.T ) {
120
+ // Set up directory structure of test library.
121
+ testLib := paths .New ("testdata" , "LegacyLib" )
122
+ examplesPath := testLib .Join ("examples" )
123
+ require .NoError (t , examplesPath .Mkdir ())
124
+ defer examplesPath .RemoveAll ()
125
+
126
+ // It's probably most friendly for contributors using Windows to create the symlinks needed for the test on demand.
127
+ err := os .Symlink (examplesPath .Join (".." ).String (), examplesPath .Join ("UpGoer1" ).String ())
128
+ require .NoError (t , err , "This test must be run as administrator on Windows to have symlink creation privilege." )
129
+ // It's necessary to have multiple symlinks to a parent directory to create the loop.
130
+ err = os .Symlink (examplesPath .Join (".." ).String (), examplesPath .Join ("UpGoer2" ).String ())
131
+ require .NoError (t , err )
132
+
133
+ // The failure condition is Load() never returning, testing for which requires setting up a timeout.
134
+ done := make (chan bool )
135
+ go func () {
136
+ _ , err = Load (testLib , User )
137
+ done <- true
138
+ }()
139
+
140
+ select {
141
+ case <- done :
142
+ case <- time .After (2 * time .Second ):
143
+ require .FailNow (t , "Load didn't complete in the allocated time." )
144
+ }
145
+ require .Error (t , err )
146
+ }
147
+
148
+ func TestLoadExamples (t * testing.T ) {
149
+ example , err := paths .New ("." , "testdata" , "TestLibExamples" , "examples" , "simple" ).Abs ()
150
+ require .NoError (t , err )
151
+ lib , err := Load (paths .New ("testdata" , "TestLibExamples" ), User )
152
+ require .NoError (t , err )
153
+ require .Len (t , lib .Examples , 1 )
154
+ require .True (t , lib .Examples .Contains (example ))
155
+ }
0 commit comments