Skip to content

Commit 6461a42

Browse files
committed
Create infra for sharing directories between tests
1 parent c784da7 commit 6461a42

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

Diff for: go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ require (
5757
gopkg.in/yaml.v2 v2.4.0
5858
)
5959

60-
require go.bug.st/testifyjson v1.1.1
60+
require (
61+
github.com/rogpeppe/go-internal v1.3.0
62+
go.bug.st/testifyjson v1.1.1
63+
)
6164

6265
require (
6366
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect

Diff for: go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5H
310310
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
311311
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
312312
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
313+
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
313314
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
314315
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
315316
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

Diff for: internal/integrationtest/arduino-cli.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ func NewArduinoCliWithinEnvironment(env *Environment, config *ArduinoCLIConfig)
100100
workingDir: env.RootDir(),
101101
}
102102
if config.UseSharedStagingFolder {
103-
cli.stagingDir = env.SharedDownloadsDir()
103+
sharedDir := env.SharedDownloadsDir()
104+
cli.stagingDir = sharedDir.Lock()
105+
env.RegisterCleanUpCallback(func() {
106+
sharedDir.Unlock()
107+
})
104108
}
105109

106110
cli.cliEnvVars = map[string]string{

Diff for: internal/integrationtest/environment.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,14 @@ var ProjectName = "cli"
2828
// Environment is a test environment for the test suite.
2929
type Environment struct {
3030
rootDir *paths.Path
31-
downloadsDir *paths.Path
31+
downloadsDir *SharedDir
3232
t *testing.T
3333
cleanUp func()
3434
}
3535

36-
// SharedDir returns the shared downloads directory.
37-
func SharedDir(t *testing.T, id string) *paths.Path {
38-
downloadsDir := paths.TempDir().Join(ProjectName + "-" + id)
39-
require.NoError(t, downloadsDir.MkdirAll())
40-
return downloadsDir
41-
}
42-
4336
// NewEnvironment creates a new test environment.
4437
func NewEnvironment(t *testing.T) *Environment {
45-
downloadsDir := SharedDir(t, "downloads")
38+
downloadsDir := NewSharedDir(t, "downloads")
4639
rootDir, err := paths.MkTempDir("", ProjectName)
4740
require.NoError(t, err)
4841
return &Environment{
@@ -75,7 +68,7 @@ func (e *Environment) RootDir() *paths.Path {
7568
}
7669

7770
// SharedDownloadsDir return the shared directory for downloads
78-
func (e *Environment) SharedDownloadsDir() *paths.Path {
71+
func (e *Environment) SharedDownloadsDir() *SharedDir {
7972
return e.downloadsDir
8073
}
8174

Diff for: internal/integrationtest/shared_directory.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package integrationtest
17+
18+
import (
19+
"sync"
20+
"testing"
21+
22+
"github.com/arduino/go-paths-helper"
23+
"github.com/rogpeppe/go-internal/lockedfile"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
// SharedDir is a directory that is shared between multiple tests.
28+
type SharedDir struct {
29+
dir *paths.Path
30+
t *testing.T
31+
mux sync.Mutex
32+
fileLock *lockedfile.File
33+
}
34+
35+
// Lock locks the shared directory for exclusive access and return the path to the directory.
36+
func (d *SharedDir) Lock() *paths.Path {
37+
d.mux.Lock()
38+
defer d.mux.Unlock()
39+
if d.fileLock != nil {
40+
panic("SharedDir already locked")
41+
}
42+
fileLock, err := lockedfile.Create(d.dir.Join(".lock").String())
43+
require.NoError(d.t, err)
44+
d.fileLock = fileLock
45+
return d.dir
46+
}
47+
48+
// Unlock unlocks the shared directory.
49+
func (d *SharedDir) Unlock() {
50+
d.mux.Lock()
51+
defer d.mux.Unlock()
52+
if d.fileLock == nil {
53+
panic("SharedDir already unlocked")
54+
}
55+
require.NoError(d.t, d.fileLock.Close())
56+
d.fileLock = nil
57+
}
58+
59+
// NewSharedDir creates a new shared directory.
60+
func NewSharedDir(t *testing.T, id string) *SharedDir {
61+
dir := paths.TempDir().Join(ProjectName + "-" + id)
62+
require.NoError(t, dir.MkdirAll())
63+
return &SharedDir{
64+
dir: dir,
65+
t: t,
66+
}
67+
}

0 commit comments

Comments
 (0)