-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainPackage_test.go
121 lines (95 loc) · 2.58 KB
/
mainPackage_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
package astra
import (
"github.com/stretchr/testify/require"
"os"
"path"
"testing"
)
func TestService_setupTempMainPackage(t *testing.T) {
t.Run("creates temp main package", func(t *testing.T) {
service := &Service{
WorkDir: "./",
}
err := service.setupAstraDir()
require.NoError(t, err)
defer func() {
err = service.cleanupAstraDir()
require.NoError(t, err)
}()
err = service.setupTempMainPackage()
require.NoError(t, err)
require.NotEmpty(t, service.tempMainPackageName)
})
t.Run("copies all .go files in working directory to temp directory", func(t *testing.T) {
service := &Service{
WorkDir: "./",
}
err := service.setupAstraDir()
require.NoError(t, err)
defer func() {
err = service.cleanupAstraDir()
require.NoError(t, err)
}()
err = service.setupTempMainPackage()
require.NoError(t, err)
wdFiles, err := os.ReadDir("./")
require.NoError(t, err)
files, err := os.ReadDir("./.astra/astramain")
require.NoError(t, err)
filesByName := make([]string, len(files))
for i, file := range files {
filesByName[i] = file.Name()
}
require.NotEmpty(t, files)
for _, file := range wdFiles {
if path.Ext(file.Name()) == ".go" {
require.Contains(t, filesByName, file.Name())
}
}
})
}
func TestService_cleanupTempMainPackage(t *testing.T) {
service := &Service{
WorkDir: "./",
}
err := service.setupAstraDir()
require.NoError(t, err)
defer func() {
err = service.cleanupAstraDir()
require.NoError(t, err)
}()
err = service.setupTempMainPackage()
require.NoError(t, err)
err = service.cleanupTempMainPackage()
require.NoError(t, err)
_, err = os.Stat(path.Join(service.getAstraDirPath(), mainPackageReplacement))
require.Error(t, err)
}
func TestService_GetMainPackageName(t *testing.T) {
t.Run("sets up the temp main package if it doesn't exist", func(t *testing.T) {
service := &Service{
WorkDir: "./",
}
err := service.setupAstraDir()
require.NoError(t, err)
defer func() {
err = service.cleanupAstraDir()
require.NoError(t, err)
}()
require.Empty(t, service.tempMainPackageName)
mainPkgName, err := service.GetMainPackageName()
require.NoError(t, err)
require.NotEmpty(t, mainPkgName)
require.NotEmpty(t, service.tempMainPackageName)
require.Equal(t, service.tempMainPackageName, mainPkgName)
})
t.Run("returns the name of the temp main package", func(t *testing.T) {
service := &Service{
WorkDir: "./",
tempMainPackageName: "test",
}
mainPkgName, err := service.GetMainPackageName()
require.NoError(t, err)
require.Equal(t, "test", mainPkgName)
})
}