|
1 | 1 | package client
|
2 | 2 |
|
3 |
| -// import ( |
4 |
| -// "context" |
5 |
| -// "io/ioutil" |
6 |
| -// "os" |
7 |
| - |
8 |
| -// "github.com/mitchellh/go-testing-interface" |
9 |
| -// "github.com/stretchr/testify/require" |
10 |
| - |
11 |
| -// configpkg "github.com/hashicorp/vagrant/internal/config" |
12 |
| -// pb "github.com/hashicorp/vagrant/internal/server/gen" |
13 |
| -// "github.com/hashicorp/vagrant/internal/server/singleprocess" |
14 |
| -// ) |
15 |
| - |
16 |
| -// // TestProject returns an initialized client pointing to an in-memory test |
17 |
| -// // server. This will close automatically on test completion. |
18 |
| -// // |
19 |
| -// // This will also change the working directory to a temporary directory |
20 |
| -// // so that any side effect file creation doesn't impact the real working |
21 |
| -// // directory. If you need to use your working directory, query it before |
22 |
| -// // calling this. |
23 |
| -// func TestProject(t testing.T, opts ...Option) *Project { |
24 |
| -// require := require.New(t) |
25 |
| -// client := singleprocess.TestServer(t) |
26 |
| - |
27 |
| -// ctx := context.Background() |
28 |
| - |
29 |
| -// basis, err := NewBasis(ctx, WithClient(client), WithLocal()) |
30 |
| -// require.NoError(err) |
31 |
| - |
32 |
| -// // Initialize our client |
33 |
| -// result, err := New(ctx, append([]Option{ |
34 |
| -// WithBasis(basis), |
35 |
| -// WithProjectRef(&pb.Ref_Project{Project: "test_p"}), |
36 |
| -// }, opts...)...) |
37 |
| -// require.NoError(err) |
38 |
| - |
39 |
| -// // Move into a temporary directory |
40 |
| -// td := testTempDir(t) |
41 |
| -// testChdir(t, td) |
42 |
| - |
43 |
| -// // Create a valid vagrant configuration file |
44 |
| -// configpkg.TestConfigFile(t, configpkg.TestSource(t)) |
45 |
| - |
46 |
| -// return result |
47 |
| -// } |
48 |
| - |
49 |
| -// // TestApp returns an app reference that can be used for testing. |
50 |
| -// func TestApp(t testing.T, c *Project) string { |
51 |
| -// // Initialize our app |
52 |
| -// singleprocess.TestApp(t, c.Client(), c.App("test_a").Ref()) |
53 |
| - |
54 |
| -// return "test_a" |
55 |
| -// } |
56 |
| - |
57 |
| -// func testChdir(t testing.T, dir string) { |
58 |
| -// pwd, err := os.Getwd() |
59 |
| -// require.NoError(t, err) |
60 |
| -// require.NoError(t, os.Chdir(dir)) |
61 |
| -// t.Cleanup(func() { require.NoError(t, os.Chdir(pwd)) }) |
62 |
| -// } |
63 |
| - |
64 |
| -// func testTempDir(t testing.T) string { |
65 |
| -// dir, err := ioutil.TempDir("", "vagrant-test") |
66 |
| -// require.NoError(t, err) |
67 |
| -// t.Cleanup(func() { os.RemoveAll(dir) }) |
68 |
| -// return dir |
69 |
| -// } |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/mitchellh/go-testing-interface" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/hashicorp/vagrant/internal/server/singleprocess" |
| 12 | +) |
| 13 | + |
| 14 | +// TestBasis returns an initialized client pointing to an in-memory test |
| 15 | +// server. This will close automatically on test completion. |
| 16 | +// |
| 17 | +// This will also change the working directory to a temporary directory |
| 18 | +// so that any side effect file creation doesn't impact the real working |
| 19 | +// directory. If you need to use your working directory, query it before |
| 20 | +// calling this. |
| 21 | +func TestBasis(t testing.T, opts ...Option) *Basis { |
| 22 | + require := require.New(t) |
| 23 | + client := singleprocess.TestServer(t) |
| 24 | + |
| 25 | + ctx := context.Background() |
| 26 | + |
| 27 | + basis, err := New(ctx, WithClient(client), WithLocal()) |
| 28 | + require.NoError(err) |
| 29 | + |
| 30 | + // // Initialize our client |
| 31 | + // result, err := New(ctx, append([]Option{ |
| 32 | + // WithBasis(basis), |
| 33 | + // WithProjectRef(&pb.Ref_Project{Project: "test_p"}), |
| 34 | + // }, opts...)...) |
| 35 | + // require.NoError(err) |
| 36 | + |
| 37 | + // Move into a temporary directory |
| 38 | + td := testTempDir(t) |
| 39 | + testChdir(t, td) |
| 40 | + |
| 41 | + // Create a valid vagrant configuration file |
| 42 | + // configpkg.TestConfigFile(t, configpkg.TestSource(t)) |
| 43 | + |
| 44 | + return basis |
| 45 | +} |
| 46 | + |
| 47 | +func testChdir(t testing.T, dir string) { |
| 48 | + pwd, err := os.Getwd() |
| 49 | + require.NoError(t, err) |
| 50 | + require.NoError(t, os.Chdir(dir)) |
| 51 | + t.Cleanup(func() { require.NoError(t, os.Chdir(pwd)) }) |
| 52 | +} |
| 53 | + |
| 54 | +func testTempDir(t testing.T) string { |
| 55 | + dir, err := ioutil.TempDir("", "vagrant-test") |
| 56 | + require.NoError(t, err) |
| 57 | + t.Cleanup(func() { os.RemoveAll(dir) }) |
| 58 | + return dir |
| 59 | +} |
0 commit comments