Skip to content

Commit c10727e

Browse files
committed
Integrated 'go.bug.st/testsuite'
It doesn't make much sense to keep it separate in a different library
1 parent 0e0b615 commit c10727e

File tree

6 files changed

+144
-17
lines changed

6 files changed

+144
-17
lines changed

Diff for: go.mod

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

60-
require (
61-
go.bug.st/testifyjson v1.1.1
62-
go.bug.st/testsuite v0.1.0
63-
)
60+
require go.bug.st/testifyjson v1.1.1
6461

6562
require (
6663
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect

Diff for: go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,6 @@ go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2
376376
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
377377
go.bug.st/testifyjson v1.1.1 h1:nHotIMK151LF3vYsU/b2RaoVaWCgrf2kvQeGNoZkGaA=
378378
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
379-
go.bug.st/testsuite v0.1.0 h1:oX4zdIB62+G5A0Kq4dja7Vy8tDiKqKVhhxkzhpMGgog=
380-
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
381379
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
382380
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
383381
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ import (
3333
"github.com/arduino/go-paths-helper"
3434
"github.com/fatih/color"
3535
"github.com/stretchr/testify/require"
36-
"go.bug.st/testsuite"
3736
"google.golang.org/grpc"
3837
)
3938

40-
func init() {
41-
testsuite.ProjectName = "cli"
42-
}
43-
4439
// FindRepositoryRootPath returns the repository root path
4540
func FindRepositoryRootPath(t *testing.T) *paths.Path {
4641
repoRootPath, err := paths.Getwd()
@@ -54,8 +49,8 @@ func FindRepositoryRootPath(t *testing.T) *paths.Path {
5449

5550
// CreateArduinoCLIWithEnvironment performs the minimum amount of actions
5651
// to build the default test environment.
57-
func CreateArduinoCLIWithEnvironment(t *testing.T) (*testsuite.Environment, *ArduinoCLI) {
58-
env := testsuite.NewEnvironment(t)
52+
func CreateArduinoCLIWithEnvironment(t *testing.T) (*Environment, *ArduinoCLI) {
53+
env := NewEnvironment(t)
5954

6055
cli := NewArduinoCliWithinEnvironment(env, &ArduinoCLIConfig{
6156
ArduinoCLIPath: FindRepositoryRootPath(t).Join("arduino-cli"),
@@ -89,7 +84,7 @@ type ArduinoCLIConfig struct {
8984
}
9085

9186
// NewArduinoCliWithinEnvironment creates a new Arduino CLI client inside the given environment.
92-
func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoCLIConfig) *ArduinoCLI {
87+
func NewArduinoCliWithinEnvironment(env *Environment, config *ArduinoCLIConfig) *ArduinoCLI {
9388
color.NoColor = false
9489
cli := &ArduinoCLI{
9590
path: config.ArduinoCLIPath,

Diff for: internal/integrationtest/daemon/daemon_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import (
2020

2121
"github.com/arduino/arduino-cli/internal/integrationtest"
2222
"github.com/stretchr/testify/require"
23-
"go.bug.st/testsuite"
2423
)
2524

2625
// createEnvForDaemon performs the minimum required operations to start the arduino-cli daemon.
2726
// It returns a testsuite.Environment and an ArduinoCLI client to perform the integration tests.
2827
// The Environment must be disposed by calling the CleanUp method via defer.
29-
func createEnvForDaemon(t *testing.T) (*testsuite.Environment, *integrationtest.ArduinoCLI) {
30-
env := testsuite.NewEnvironment(t)
28+
func createEnvForDaemon(t *testing.T) (*integrationtest.Environment, *integrationtest.ArduinoCLI) {
29+
env := integrationtest.NewEnvironment(t)
3130

3231
cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{
3332
ArduinoCLIPath: integrationtest.FindRepositoryRootPath(t).Join("arduino-cli"),

Diff for: internal/integrationtest/environment.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
"testing"
20+
21+
"github.com/arduino/go-paths-helper"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
// ProjectName is the prefix used in the test temp files
26+
var ProjectName = "cli"
27+
28+
// Environment is a test environment for the test suite.
29+
type Environment struct {
30+
rootDir *paths.Path
31+
downloadsDir *paths.Path
32+
t *testing.T
33+
cleanUp func()
34+
}
35+
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+
43+
// NewEnvironment creates a new test environment.
44+
func NewEnvironment(t *testing.T) *Environment {
45+
downloadsDir := SharedDir(t, "downloads")
46+
rootDir, err := paths.MkTempDir("", ProjectName)
47+
require.NoError(t, err)
48+
return &Environment{
49+
rootDir: rootDir,
50+
downloadsDir: downloadsDir,
51+
t: t,
52+
cleanUp: func() {
53+
require.NoError(t, rootDir.RemoveAll())
54+
},
55+
}
56+
}
57+
58+
// RegisterCleanUpCallback adds a clean up function to the clean up chain
59+
func (e *Environment) RegisterCleanUpCallback(newCleanUp func()) {
60+
previousCleanUp := e.cleanUp
61+
e.cleanUp = func() {
62+
newCleanUp()
63+
previousCleanUp()
64+
}
65+
}
66+
67+
// CleanUp removes the test environment.
68+
func (e *Environment) CleanUp() {
69+
e.cleanUp()
70+
}
71+
72+
// RootDir returns the root dir of the environment.
73+
func (e *Environment) RootDir() *paths.Path {
74+
return e.rootDir
75+
}
76+
77+
// SharedDownloadsDir return the shared directory for downloads
78+
func (e *Environment) SharedDownloadsDir() *paths.Path {
79+
return e.downloadsDir
80+
}
81+
82+
// T returns the testing environment
83+
func (e *Environment) T() *testing.T {
84+
return e.t
85+
}

Diff for: internal/integrationtest/http_server.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
"fmt"
20+
"net/http"
21+
"net/url"
22+
23+
"github.com/arduino/go-paths-helper"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
// HTTPServeFile spawn an http server that serve a single file. The server
28+
// is started on the given port. The URL to the file and a cleanup function are returned.
29+
func (env *Environment) HTTPServeFile(port uint16, path *paths.Path) *url.URL {
30+
mux := http.NewServeMux()
31+
mux.HandleFunc("/"+path.Base(), func(w http.ResponseWriter, r *http.Request) {
32+
http.ServeFile(w, r, path.String())
33+
})
34+
server := &http.Server{
35+
Addr: fmt.Sprintf(":%d", port),
36+
Handler: mux,
37+
}
38+
39+
t := env.T()
40+
fileURL, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/%s", port, path.Base()))
41+
require.NoError(t, err)
42+
43+
go func() {
44+
err := server.ListenAndServe()
45+
require.Equal(t, err, http.ErrServerClosed)
46+
}()
47+
48+
env.RegisterCleanUpCallback(func() {
49+
server.Close()
50+
})
51+
52+
return fileURL
53+
}

0 commit comments

Comments
 (0)