-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevcontainer_test.go
More file actions
114 lines (105 loc) · 2.93 KB
/
devcontainer_test.go
File metadata and controls
114 lines (105 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package devcontainer
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
func writeFixture(t *testing.T, body string) (workspace, configPath string) {
t.Helper()
dir := t.TempDir()
dcDir := filepath.Join(dir, ".devcontainer")
if err := os.MkdirAll(dcDir, 0755); err != nil {
t.Fatal(err)
}
cfg := filepath.Join(dcDir, "devcontainer.json")
if err := os.WriteFile(cfg, []byte(body), 0644); err != nil {
t.Fatal(err)
}
return dir, cfg
}
func TestResolve_Discovery(t *testing.T) {
ws, cfg := writeFixture(t, `{"image":"alpine"}`)
got, err := Resolve(context.Background(), ResolveOptions{
LocalWorkspaceFolder: ws,
})
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if got.LocalWorkspaceFolder != ws {
t.Errorf("LocalWorkspaceFolder = %q", got.LocalWorkspaceFolder)
}
if got.DevcontainerID == "" {
t.Error("DevcontainerID should be populated")
}
_ = cfg // path used implicitly via discovery
}
func TestResolve_DotDevcontainerJson(t *testing.T) {
dir := t.TempDir()
cfg := filepath.Join(dir, ".devcontainer.json")
if err := os.WriteFile(cfg, []byte(`{"image":"alpine"}`), 0644); err != nil {
t.Fatal(err)
}
if _, err := Resolve(context.Background(), ResolveOptions{LocalWorkspaceFolder: dir}); err != nil {
t.Fatalf("Resolve: %v", err)
}
}
func TestResolve_MissingFile(t *testing.T) {
dir := t.TempDir()
_, err := Resolve(context.Background(), ResolveOptions{LocalWorkspaceFolder: dir})
if err == nil {
t.Fatal("expected error when no devcontainer.json exists")
}
if !strings.Contains(err.Error(), "no devcontainer.json") {
t.Errorf("unexpected error: %v", err)
}
}
func TestResolve_RequiresAbsolute(t *testing.T) {
_, err := Resolve(context.Background(), ResolveOptions{LocalWorkspaceFolder: "relative/path"})
if err == nil {
t.Fatal("expected error for relative path")
}
}
func TestResolve_LocalEnvOverride(t *testing.T) {
ws, _ := writeFixture(t, `{"image":"${localEnv:CUSTOM}"}`)
got, err := Resolve(context.Background(), ResolveOptions{
LocalWorkspaceFolder: ws,
LocalEnv: map[string]string{"CUSTOM": "frobnicate:1"},
})
if err != nil {
t.Fatalf("Resolve: %v", err)
}
src := got.Source
if src == nil {
t.Fatal("Source nil")
}
}
func TestResolve_DevcontainerIDOverride(t *testing.T) {
ws, _ := writeFixture(t, `{"image":"alpine"}`)
called := false
got, err := Resolve(context.Background(), ResolveOptions{
LocalWorkspaceFolder: ws,
DevcontainerIDFunc: func(local, cfg string) string {
called = true
return "deadbeef"
},
})
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if !called {
t.Error("DevcontainerIDFunc not invoked")
}
if got.DevcontainerID != "deadbeef" {
t.Errorf("DevcontainerID = %q", got.DevcontainerID)
}
}
func TestResolve_CtxCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := Resolve(ctx, ResolveOptions{LocalWorkspaceFolder: "/tmp"})
if err == nil {
t.Fatal("expected ctx cancelled error")
}
}