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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
)
func TestConvertCommandErrorToResultType(t *testing.T) {
tests := []struct {
name string
err error
defaultStatus string
want string
}{
{"nil error returns success", nil, resultRuntimeError, resultSuccess},
{"DeadlineExceeded returns timeout", context.DeadlineExceeded, resultRuntimeError, resultTimeout},
{"other error returns default status", os.ErrNotExist, resultCompileError, resultCompileError},
{"other error returns runtime_error default", os.ErrPermission, resultRuntimeError, resultRuntimeError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := convertCommandErrorToResultType(tt.err, tt.defaultStatus)
if got != tt.want {
t.Errorf("convertCommandErrorToResultType() = %q, want %q", got, tt.want)
}
})
}
}
func TestExecCommandWithTimeout_Success(t *testing.T) {
stdout, stderr, err := execCommandWithTimeout(
context.Background(),
t.TempDir(),
5*time.Second,
func(ctx context.Context) *exec.Cmd {
return exec.CommandContext(ctx, "echo", "hello")
},
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if stdout != "hello\n" {
t.Errorf("stdout = %q, want %q", stdout, "hello\n")
}
if stderr != "" {
t.Errorf("stderr = %q, want empty", stderr)
}
}
func TestExecCommandWithTimeout_Failure(t *testing.T) {
_, _, err := execCommandWithTimeout(
context.Background(),
t.TempDir(),
5*time.Second,
func(ctx context.Context) *exec.Cmd {
return exec.CommandContext(ctx, "false")
},
)
if err == nil {
t.Fatal("expected error, got nil")
}
}
func TestExecCommandWithTimeout_Timeout(t *testing.T) {
_, _, err := execCommandWithTimeout(
context.Background(),
t.TempDir(),
50*time.Millisecond,
func(ctx context.Context) *exec.Cmd {
return exec.CommandContext(ctx, "sleep", "10")
},
)
if err != context.DeadlineExceeded {
t.Errorf("expected DeadlineExceeded, got %v", err)
}
}
func TestExecCommandWithTimeout_Stderr(t *testing.T) {
_, stderr, _ := execCommandWithTimeout(
context.Background(),
t.TempDir(),
5*time.Second,
func(ctx context.Context) *exec.Cmd {
return exec.CommandContext(ctx, "sh", "-c", "echo errmsg >&2")
},
)
if stderr != "errmsg\n" {
t.Errorf("stderr = %q, want %q", stderr, "errmsg\n")
}
}
func TestPrepareWorkingDir(t *testing.T) {
dir := filepath.Join(t.TempDir(), "subdir")
res := prepareWorkingDir(dir)
if res.Status != resultSuccess {
t.Fatalf("prepareWorkingDir() status = %q, want %q", res.Status, resultSuccess)
}
info, err := os.Stat(dir)
if err != nil {
t.Fatalf("directory not created: %v", err)
}
if !info.IsDir() {
t.Fatalf("expected directory, got file")
}
}
func TestPutSwiftSourceFile(t *testing.T) {
t.Run("writes file when Sources/ exists", func(t *testing.T) {
dir := t.TempDir()
sourcesDir := filepath.Join(dir, "Sources")
if err := os.MkdirAll(sourcesDir, 0755); err != nil {
t.Fatalf("failed to create Sources dir: %v", err)
}
res := putSwiftSourceFile(dir, "print(\"hello\")")
if res.Status != resultSuccess {
t.Fatalf("putSwiftSourceFile() status = %q, want %q", res.Status, resultSuccess)
}
content, err := os.ReadFile(filepath.Join(sourcesDir, "main.swift"))
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
if string(content) != "print(\"hello\")" {
t.Errorf("file content = %q, want %q", string(content), "print(\"hello\")")
}
})
t.Run("returns error when Sources/ does not exist", func(t *testing.T) {
dir := t.TempDir()
res := putSwiftSourceFile(dir, "print(\"hello\")")
if res.Status == resultSuccess {
t.Fatal("expected error status, got success")
}
})
}
func TestRemoveWorkingDir(t *testing.T) {
dir := filepath.Join(t.TempDir(), "toremove")
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("failed to create dir: %v", err)
}
removeWorkingDir(dir)
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Errorf("directory still exists after removeWorkingDir")
}
}
|