Skip to content

Commit 93bdef0

Browse files
committed
[test] Use testutil for TestLoadWorkspace
1 parent 8931743 commit 93bdef0

File tree

3 files changed

+95
-14
lines changed

3 files changed

+95
-14
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
workspace:
2+
environmentManifest:
3+
- name: "foobar"
4+
command: ["echo", "foobar"]
5+
defaultArgs:
6+
message: "hello root"
7+
8+
components:
9+
- location: wsa/pkg0
10+
packages:
11+
- name: app
12+
type: generic
13+
config:
14+
commands:
15+
- ["echo"]
16+
- location: wsa/pkg1
17+
packages:
18+
- name: app
19+
type: generic
20+
config:
21+
commands:
22+
- ["echo"]
23+
scripts:
24+
- name: echo
25+
script: |
26+
echo ${message}
27+
- location: wsa
28+
packages:
29+
- name: app
30+
type: generic
31+
config:
32+
commands:
33+
- ["echo", "${message}"]
34+
scripts:
35+
- name: echo
36+
script: |
37+
echo ${message}
38+
- location: deeper/pkg0
39+
packages:
40+
- name: app
41+
type: generic
42+
deps:
43+
- wsa/pkg0:app
44+
- wsa/pkg1:app
45+
- wsa:app
46+
config:
47+
commands:
48+
- ["echo"]

pkg/leeway/scripts_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/gitpod-io/leeway/cmd"
13+
"github.com/gitpod-io/leeway/pkg/testutil"
1314
)
1415

1516
var dut = flag.Bool("dut", false, "run command/device under test")
@@ -104,6 +105,8 @@ type CommandFixtureTest struct {
104105
StderrSub string
105106
NoStderrSub string
106107
Eval func(t *testing.T, stdout, stderr string)
108+
Fixture *testutil.Setup
109+
FixturePath string
107110
}
108111

109112
// Run executes the fixture test - do not forget to call this one
@@ -114,6 +117,40 @@ func (ft *CommandFixtureTest) Run() {
114117
}
115118

116119
ft.T.Run(ft.Name, func(t *testing.T) {
120+
loc := "../../"
121+
122+
if ft.FixturePath != "" {
123+
fp, err := os.Open(ft.FixturePath)
124+
if err != nil {
125+
t.Fatalf("cannot load fixture from %s: %v", ft.FixturePath, err)
126+
}
127+
ft.Fixture, err = testutil.LoadFromYAML(fp)
128+
fp.Close()
129+
if err != nil {
130+
t.Fatalf("cannot load fixture from %s: %v", ft.FixturePath, err)
131+
}
132+
}
133+
if ft.Fixture != nil {
134+
var err error
135+
loc, err = ft.Fixture.Materialize()
136+
if err != nil {
137+
t.Fatalf("cannot materialize fixture: %v", err)
138+
}
139+
t.Logf("materialized fixture workspace: %s", loc)
140+
t.Cleanup(func() { os.RemoveAll(loc) })
141+
}
142+
143+
env := os.Environ()
144+
n := 0
145+
for _, x := range env {
146+
if strings.HasPrefix(x, "LEEWAY_") {
147+
continue
148+
}
149+
env[n] = x
150+
n++
151+
}
152+
env = env[:n]
153+
117154
self, err := os.Executable()
118155
if err != nil {
119156
t.Fatalf("cannot identify test binary: %q", err)
@@ -125,7 +162,8 @@ func (ft *CommandFixtureTest) Run() {
125162
)
126163
cmd.Stdout = sout
127164
cmd.Stderr = serr
128-
cmd.Dir = "../../"
165+
cmd.Dir = loc
166+
cmd.Env = env
129167
err = cmd.Run()
130168

131169
var exitCode int

pkg/leeway/workspace_test.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,34 @@ func TestFixtureLoadWorkspace(t *testing.T) {
1616
{
1717
Name: "single workspace packages",
1818
T: t,
19-
Args: []string{"collect", "-w", "fixtures/nested-ws/wsa"},
19+
Args: []string{"collect"},
2020
NoNestedWorkspace: true,
2121
ExitCode: 0,
2222
StdoutSub: "pkg1:app",
23+
FixturePath: "fixtures/load-workspace.yaml",
2324
},
2425
{
2526
Name: "workspace components",
2627
T: t,
27-
Args: []string{"collect", "-w", "fixtures/nested-ws/wsa", "components"},
28+
Args: []string{"collect", "components"},
2829
NoNestedWorkspace: true,
2930
ExitCode: 0,
30-
StdoutSub: "//\npkg0\npkg1",
31-
},
32-
{
33-
Name: "ignore nested workspaces",
34-
T: t,
35-
Args: []string{"collect", "-w", "fixtures/nested-ws", "components"},
36-
NoNestedWorkspace: true,
37-
ExitCode: 1,
38-
StderrSub: "pkg0:app: package \\\"wsa/pkg0:app\\\" is unknown",
31+
StdoutSub: "deeper/pkg0\nwsa\nwsa/pkg0\nwsa/pkg1",
32+
FixturePath: "fixtures/load-workspace.yaml",
3933
},
4034
{
4135
Name: "environment manifest",
4236
T: t,
43-
Args: []string{"describe", "-w", "fixtures/nested-ws/wsa", "environment-manifest"},
37+
Args: []string{"describe", "environment-manifest"},
4438
Eval: func(t *testing.T, stdout, stderr string) {
4539
for _, k := range []string{"os", "arch", "foobar"} {
4640
if !strings.Contains(stdout, fmt.Sprintf("%s: ", k)) {
4741
t.Errorf("missing %s entry in environment manifest", k)
4842
}
4943
}
5044
},
51-
ExitCode: 0,
45+
ExitCode: 0,
46+
FixturePath: "fixtures/load-workspace.yaml",
5247
},
5348
}
5449

0 commit comments

Comments
 (0)