forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
126 lines (100 loc) · 4.48 KB
/
main_test.go
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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenerate(t *testing.T) {
examplesTmp := t.TempDir()
examplesDocTmp := t.TempDir()
example := Example{
Name: "foo",
Image: "docker.io/example/foo:latest",
}
exampleNameLower := example.Lower()
err := generate(example, examplesTmp, examplesDocTmp)
assert.Nil(t, err)
templatesDir, err := os.ReadDir(filepath.Join(".", "_template"))
assert.Nil(t, err)
exampleDirPath := filepath.Join(examplesTmp, exampleNameLower)
newExampleDir, err := os.ReadDir(exampleDirPath)
assert.Nil(t, err)
exampleDirFileInfo, err := os.Stat(exampleDirPath)
assert.Nil(t, err) // error nil implies the file exist
assert.True(t, exampleDirFileInfo.IsDir())
exampleDocFile := filepath.Join(examplesDocTmp, exampleNameLower+".md")
_, err = os.Stat(exampleDocFile)
assert.Nil(t, err) // error nil implies the file exist
// check the number of template files is equal to examples + 1 (the doc)
assert.Equal(t, len(newExampleDir)+1, len(templatesDir))
assertExampleDocContent(t, example, exampleDocFile)
generatedTemplatesDir := filepath.Join(examplesTmp, exampleNameLower)
assertExampleTestContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+"_test.go"))
assertExampleContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+".go"))
assertGoModContent(t, example, filepath.Join(generatedTemplatesDir, "go.mod"))
assertMakefileContent(t, example, filepath.Join(generatedTemplatesDir, "Makefile"))
assertToolsGoContent(t, example, filepath.Join(generatedTemplatesDir, "tools", "tools.go"))
}
// assert content example file in the docs
func assertExampleDocContent(t *testing.T, example Example, exampleDocFile string) {
content, err := os.ReadFile(exampleDocFile)
assert.Nil(t, err)
lower := example.Lower()
title := example.Title()
data := strings.Split(string(content), "\n")
assert.Equal(t, data[0], "# "+title)
assert.Equal(t, data[2], "<!--codeinclude-->")
assert.Equal(t, data[3], "[Creating a "+title+" container](../../examples/"+lower+"/"+lower+".go)")
assert.Equal(t, data[4], "<!--/codeinclude-->")
assert.Equal(t, data[6], "<!--codeinclude-->")
assert.Equal(t, data[7], "[Test for a "+title+" container](../../examples/"+lower+"/"+lower+"_test.go)")
assert.Equal(t, data[8], "<!--/codeinclude-->")
}
// assert content example test
func assertExampleTestContent(t *testing.T, example Example, exampleTestFile string) {
content, err := os.ReadFile(exampleTestFile)
assert.Nil(t, err)
data := strings.Split(string(content), "\n")
assert.Equal(t, data[0], "package "+example.Lower())
assert.Equal(t, data[7], "func Test"+example.Title()+"(t *testing.T) {")
}
// assert content example
func assertExampleContent(t *testing.T, example Example, exampleFile string) {
content, err := os.ReadFile(exampleFile)
assert.Nil(t, err)
lower := example.Lower()
title := example.Title()
exampleName := example.Name
data := strings.Split(string(content), "\n")
assert.Equal(t, data[0], "package "+lower)
assert.Equal(t, data[8], "// "+lower+"Container represents the "+exampleName+" container type used in the module")
assert.Equal(t, data[9], "type "+lower+"Container struct {")
assert.Equal(t, data[13], "// setup"+title+" creates an instance of the "+exampleName+" container type")
assert.Equal(t, data[14], "func setup"+title+"(ctx context.Context) (*"+lower+"Container, error) {")
assert.Equal(t, data[16], "\t\tImage: \""+example.Image+"\",")
assert.Equal(t, data[26], "\treturn &"+lower+"Container{Container: container}, nil")
}
// assert content go.mod
func assertGoModContent(t *testing.T, example Example, goModFile string) {
content, err := os.ReadFile(goModFile)
assert.Nil(t, err)
data := strings.Split(string(content), "\n")
assert.Equal(t, data[0], "module github.com/testcontainers/testcontainers-go/examples/"+example.Lower())
}
// assert content Makefile
func assertMakefileContent(t *testing.T, example Example, makefile string) {
content, err := os.ReadFile(makefile)
assert.Nil(t, err)
data := strings.Split(string(content), "\n")
assert.Equal(t, data[4], "\t$(MAKE) test-"+example.Lower())
}
// assert content tools/tools.go
func assertToolsGoContent(t *testing.T, example Example, tools string) {
content, err := os.ReadFile(tools)
assert.Nil(t, err)
data := strings.Split(string(content), "\n")
assert.Equal(t, data[3], "// This package contains the tool dependencies of the "+example.Name+" example.")
assert.Equal(t, data[5], "package tools")
}