Skip to content

Commit 1378952

Browse files
VEN-8 | Add flags for different crawling options, add new cli commands (#967)
* VEN-8 | Add flags for different crawling options, add new cli commands
1 parent caf9d09 commit 1378952

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

test-crawler/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@ run:
33
go run main.go config.go
44

55
copy-output:
6-
go run main.go config.go > ../frontend/apps/web/src/tests.json
6+
go run main.go config.go > ../frontend/apps/web/src/tests.json
7+
8+
run_bcg:
9+
go run . -crawl=src
10+
11+
go-unit-test:
12+
go test ./...
13+
14+
go-integration-test:
15+
./integrations_tests/*.sh
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
go run . -crawl=src
3+
# for FILE in "outputs/*"; do echo $FILE | cat $FILE; done
4+
if [ -z "$(ls -A outputs)" ]; then
5+
echo "No expected files in output directory"
6+
exit 1
7+
fi

test-crawler/main.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
8+
"flag"
79
"fmt"
810
"io/ioutil"
911
"os"
@@ -29,8 +31,17 @@ func main() {
2931

3032
config := NewConfig()
3133

32-
crawlRepoBehaviorsAndSaveToJSON(config)
34+
var crawl string
35+
flag.StringVar(&crawl, "crawl", "behavior", "what should this crawler do? -crawl=src (source files), -crawl=behavior (test files)")
36+
flag.Parse()
3337

38+
if crawl == "behavior" {
39+
crawlRepoBehaviorsAndSaveToJSON(config)
40+
} else if crawl == "src" {
41+
if err := crawlRepoSourceCodeAndSaveToYaml(config); err != nil {
42+
fmt.Println(err.Error())
43+
}
44+
}
3445
}
3546

3647
func crawlRepoBehaviorsAndSaveToJSON(config Config) {
@@ -81,6 +92,22 @@ func crawlRepoBehaviorsAndSaveToJSON(config Config) {
8192
Save(result, config.TestCrawlerOutputMode, config.TestCrawlerOutputDir, config.TestCrawlerIndentJSON)
8293
}
8394

95+
func crawlRepoSourceCodeAndSaveToYaml(config Config) error {
96+
ctx := context.Background()
97+
for _, path := range config.BehaviorGenPaths {
98+
systemMethods, err := crawlFolderForSystemMethods(path)
99+
if err != nil {
100+
return err
101+
}
102+
103+
if err := makeYAMLFromSystemMethods(ctx, config, *systemMethods); err != nil {
104+
return fmt.Errorf("error %w saving to yaml for system %s", err, systemMethods.System)
105+
}
106+
}
107+
108+
return nil
109+
}
110+
84111
// crawlSingleFileForMethods accepts path of single go file,
85112
// and prints extracted methods out of it.
86113
func crawlSingleFileForFunctions(ctx context.Context, path string) ([]c.FunctionAnnotation, error) {
@@ -118,29 +145,34 @@ func crawlFolderForSystemMethods(system string) (*c.SystemMethods, error) {
118145
return &systemFunctions, nil
119146
}
120147

121-
// makeYAML will make yaml file from public methods.
122-
func makeYAML(ctx context.Context, filePath string) error {
148+
// makeYAMLFromSystemMethods will make yaml file from public methods.
149+
func makeYAMLFromSystemMethods(ctx context.Context, config Config, systemMethods c.SystemMethods) error {
123150

124-
publicMethods, err := crawlSingleFileForFunctions(ctx, filePath)
125-
if err != nil {
126-
fmt.Print(err)
127-
os.Exit(1)
151+
if _, err := os.Stat(config.BehaviorGenOutputDir); errors.Is(err, os.ErrNotExist) {
152+
err2 := os.Mkdir(config.BehaviorGenOutputDir, os.ModePerm)
153+
if err2 != nil {
154+
return fmt.Errorf("error create dir: %w", err)
155+
}
128156
}
129157

130-
for i := 0; i < len(publicMethods); i++ {
131-
publicMethods[i].ID = i
158+
yamlData, err := y.Marshal(&systemMethods)
159+
if err != nil {
160+
return fmt.Errorf("error marhaling: %w", err)
132161
}
133162

134-
yamlData, err := y.Marshal(&publicMethods)
163+
filename := fmt.Sprintf("%s/%s.yaml", config.BehaviorGenOutputDir, systemMethods.System)
164+
file, err := os.Create(filename)
135165
if err != nil {
136-
fmt.Printf("Error while Marshaling. %v", err)
166+
return fmt.Errorf("error create file: %w", err)
137167
}
168+
defer file.Close()
169+
fmt.Println("File generated: ", filename)
138170

139-
fileName := "test.yaml"
140-
err = ioutil.WriteFile(fileName, yamlData, 0644)
171+
err = ioutil.WriteFile(filename, yamlData, 0644)
141172
if err != nil {
142-
panic("Unable to write data into the file")
173+
return fmt.Errorf("error write to file: %w", err)
143174
}
175+
144176
return nil
145177
}
146178

0 commit comments

Comments
 (0)