Skip to content

Commit a445d9f

Browse files
Add basics of tests for the coderunner.CompileCode() function
1 parent c45a156 commit a445d9f

1 file changed

Lines changed: 44 additions & 48 deletions

File tree

compile_code_test.go

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package coderunner
22

33
import (
44
"context"
5-
"debug/elf"
65
"io/ioutil"
7-
"os"
8-
"path/filepath"
96
"testing"
107

118
mapset "github.com/deckarep/golang-set"
@@ -15,49 +12,48 @@ import (
1512
)
1613

1714
func TestCompileCode(test *testing.T) {
18-
const code = `package main; func main() { fmt.Println("Hello, World!") }`
19-
pathToCode, err := systemutils.SaveTemporaryText(code, ".go")
20-
require.NoError(test, err)
21-
22-
pathToExecutable, err := CompileCode(context.Background(), pathToCode, nil)
23-
require.NoError(test, err)
24-
25-
codeContent, err := ioutil.ReadFile(pathToCode)
26-
require.NoError(test, err)
27-
28-
// we do not use filepath.Split() because it leaves the separator
29-
dir, file := filepath.Dir(pathToExecutable), filepath.Base(pathToExecutable)
30-
assert.Equal(test, os.TempDir(), filepath.Dir(dir))
31-
assert.Regexp(test, `text\d+`, filepath.Base(dir))
32-
assert.Equal(test, "text", file)
33-
34-
const wantedCodeContent = "package main\n\n" +
35-
"import \"fmt\"\n\n" +
36-
"func main() { fmt.Println(\"Hello, World!\") }\n"
37-
assert.Equal(test, wantedCodeContent, string(codeContent))
38-
39-
_, err = elf.Open(pathToExecutable)
40-
assert.NoError(test, err)
41-
}
42-
43-
func TestCompileCode_withDisallowedImport(test *testing.T) {
44-
const code = `package main; func main() { fmt.Println("Hello, World!") }`
45-
pathToCode, err := systemutils.SaveTemporaryText(code, ".go")
46-
require.NoError(test, err)
47-
48-
pathToExecutable, compileErr :=
49-
CompileCode(context.Background(), pathToCode, mapset.NewSet("log"))
50-
51-
codeContent, err := ioutil.ReadFile(pathToCode)
52-
require.NoError(test, err)
53-
54-
const wantedCodeContent = "package main\n\n" +
55-
"import \"fmt\"\n\n" +
56-
"func main() { fmt.Println(\"Hello, World!\") }\n"
57-
assert.Equal(test, wantedCodeContent, string(codeContent))
58-
59-
const wantedCompileErrMessage = "failed import checking: " +
60-
`disallowed import "fmt"`
61-
assert.Empty(test, pathToExecutable)
62-
assert.EqualError(test, compileErr, wantedCompileErrMessage)
15+
type args struct {
16+
ctx context.Context
17+
code string
18+
allowedImports mapset.Set
19+
input string
20+
}
21+
22+
for _, data := range []struct {
23+
name string
24+
args args
25+
wantedErr assert.ErrorAssertionFunc
26+
wantPreparedCode string
27+
wantOutput string
28+
}{
29+
// TODO: Add test cases.
30+
} {
31+
test.Run(data.name, func(test *testing.T) {
32+
pathToCode, err := systemutils.SaveTemporaryText(data.args.code, ".go")
33+
require.NoError(test, err)
34+
35+
pathToExecutable, receivedErr :=
36+
CompileCode(data.args.ctx, pathToCode, data.args.allowedImports)
37+
38+
data.wantedErr(test, receivedErr)
39+
40+
if data.wantPreparedCode != "" {
41+
preparedCode, err := ioutil.ReadFile(pathToCode)
42+
require.NoError(test, err)
43+
44+
assert.Equal(test, data.wantPreparedCode, string(preparedCode))
45+
}
46+
47+
if data.wantOutput != "" {
48+
output, err := systemutils.RunCommand(
49+
context.Background(),
50+
data.args.input,
51+
pathToExecutable,
52+
)
53+
require.NoError(test, err)
54+
55+
assert.Equal(test, data.wantOutput, output)
56+
}
57+
})
58+
}
6359
}

0 commit comments

Comments
 (0)