forked from Privado-Inc/goastgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
117 lines (108 loc) · 4.67 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
package main
import (
"fmt"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
func TestProcessRequestWithSingleFileUseCase(t *testing.T) {
// Get the temporary directory path
tempDir := os.TempDir()
// Create a new folder in the temporary directory
newFolder := filepath.Join(tempDir, uuid.New().String())
err := os.Mkdir(newFolder, 0755)
if err != nil {
fmt.Println("Failed to create folder:", err)
return
}
srcFile := filepath.Join(newFolder, "hello.go")
file, errf := os.Create(srcFile)
if errf != nil {
fmt.Println("Failed to create file:", errf)
return
}
code := "package main \n" +
"import \"fmt\"\n" +
"func main() {\n" +
"fmt.Println(\"Hello World\")\n" +
"}"
file.WriteString(code)
processRequest(InputConfig{out: ".ast", inputPath: srcFile, includeFiles: "", excludeFiles: ""})
expectedJsonFileLocation := filepath.Join(newFolder, ".ast", "hello.go.json")
_, err = os.Stat(expectedJsonFileLocation)
assert.Nil(t, err, "check the ast output is generated at expected location")
diffOutLocation := filepath.Join(tempDir, uuid.New().String())
processRequest(InputConfig{out: diffOutLocation, inputPath: srcFile, includeFiles: "", excludeFiles: ""})
expectedJsonFileLocation = filepath.Join(diffOutLocation, "hello.go.json")
_, err = os.Stat(expectedJsonFileLocation)
assert.Nil(t, err, "check the ast output is generated at expected location")
}
func TestProcessRequestWithMultipleFileDiffFolderStructureUsecase(t *testing.T) {
// Get the temporary directory path
tempDir := os.TempDir()
// Create a new folder in the temporary directory
newFolder := filepath.Join(tempDir, uuid.New().String())
err := os.Mkdir(newFolder, 0755)
if err != nil {
fmt.Println("Failed to create folder:", err)
return
}
srcFile := filepath.Join(newFolder, "hello.go")
file, errf := os.Create(srcFile)
if errf != nil {
fmt.Println("Failed to create file:", errf)
return
}
code := "package main \n" +
"import \"fmt\"\n" +
"func main() {\n" +
"fmt.Println(\"Hello World\")\n" +
"}"
file.WriteString(code)
subDir := filepath.Join(newFolder, "subdir")
err = os.Mkdir(subDir, 0755)
if err != nil {
fmt.Println("Failed to create folder:", err)
return
}
subSrcFile := filepath.Join(subDir, "hellosub.go")
file, errf = os.Create(subSrcFile)
if errf != nil {
fmt.Println("Failed to create file:", errf)
return
}
file.WriteString(code)
processRequest(InputConfig{out: ".ast", inputPath: newFolder, includeFiles: "", excludeFiles: ""})
expectedJsonFileLocationone := filepath.Join(newFolder, ".ast", "hello.go.json")
_, err = os.Stat(expectedJsonFileLocationone)
assert.Nil(t, err, "check the ast output is generated at expected location")
expectedJsonFileLocationtwo := filepath.Join(newFolder, ".ast", "subdir", "hellosub.go.json")
_, err = os.Stat(expectedJsonFileLocationtwo)
assert.Nil(t, err, "check the ast output is generated at expected location")
diffOutLocation := filepath.Join(tempDir, uuid.New().String())
processRequest(InputConfig{out: diffOutLocation, inputPath: newFolder, includeFiles: "", excludeFiles: ""})
expectedJsonFileLocationone = filepath.Join(diffOutLocation, "hello.go.json")
_, err = os.Stat(expectedJsonFileLocationone)
assert.Nil(t, err, "check the ast output is generated at expected location")
expectedJsonFileLocationtwo = filepath.Join(diffOutLocation, "subdir", "hellosub.go.json")
_, err = os.Stat(expectedJsonFileLocationtwo)
assert.Nil(t, err, "check the ast output is generated at expected location")
}
func TestWindowsGetIncludePackageFolders(t *testing.T) {
// Get the temporary directory path
tempDir := os.TempDir()
// Create a new folder in the temporary directory
newFolder := filepath.Join(tempDir, uuid.New().String())
results := getIncludePackageFolders(InputConfig{out: ".ast", inputPath: newFolder, includeFiles: "", excludeFiles: "", includePackages: "/, /pkg, /cmd"})
assert.Equal(t, 3, results.Size(), "result size as expected")
assert.Equal(t, true, results.Contains(filepath.Join(newFolder, "")), "first result")
assert.Equal(t, true, results.Contains(filepath.Join(newFolder, "pkg")), "second result")
assert.Equal(t, true, results.Contains(filepath.Join(newFolder, "cmd")), "third result")
results = getIncludePackageFolders(InputConfig{out: ".ast", inputPath: newFolder, includeFiles: "", excludeFiles: "", includePackages: "/, /pkg/, /cmd/"})
assert.Equal(t, 3, results.Size(), "result size as expected")
assert.Equal(t, true, results.Contains(filepath.Join(newFolder, "")), "first result")
assert.Equal(t, true, results.Contains(filepath.Join(newFolder, "pkg")), "second result")
assert.Equal(t, true, results.Contains(filepath.Join(newFolder, "cmd")), "third result")
}