Skip to content

Commit caf9d09

Browse files
VEN-7 | Refactor and improve config file (#958)
* VEN-7 | Refactor and improve config file
1 parent 9687154 commit caf9d09

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

test-crawler/config.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ const (
1515
)
1616

1717
type Config struct {
18-
Paths []string `yaml:"paths"`
19-
OutputMode OutputMode `yaml:"output"`
20-
OutputDir string `yaml:"output_dir"`
21-
Language string `yaml:"lang_mode"`
22-
IndentJSON bool `yaml:"indent_json"`
23-
Ignore []string `yaml:"ignore"`
18+
TestCrawlerPaths []string `yaml:"test_crawler_paths"`
19+
TestCrawlerOutputMode OutputMode `yaml:"test_crawler_output"`
20+
TestCrawlerOutputDir string `yaml:"test_crawler_output_dir"`
21+
TestCrawlerLanguage string `yaml:"test_crawler_lang_mode"`
22+
TestCrawlerIndentJSON bool `yaml:"test_crawler_indent_json"`
23+
TestCrawlerIgnore []string `yaml:"test_crawler_ignore"`
24+
25+
BehaviorGenPaths []string `yaml:"behavior_gen_paths"`
26+
BehaviorGenIgnore []string `yaml:"behavior_gen_ignore"`
27+
BehaviorGenOutputDir string `yaml:"behavior_gen_output_dir"`
2428
}
2529

2630
const filename string = "config.yaml"
@@ -50,9 +54,9 @@ func NewConfig() Config {
5054

5155
func Default() Config {
5256
return Config{
53-
Paths: []string{"_modules/lotus"},
54-
OutputMode: "stdout",
55-
Language: "auto",
56-
IndentJSON: false,
57+
TestCrawlerPaths: []string{"_modules/lotus"},
58+
TestCrawlerOutputMode: "stdout",
59+
TestCrawlerLanguage: "auto",
60+
TestCrawlerIndentJSON: false,
5761
}
5862
}

test-crawler/config.yaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# list of paths to directories that should be crawled
2-
paths: ["../repo-to-crawl"]
2+
test_crawler_paths: ["../repo-to-crawl"]
33
# stdout or file(json)
4-
output: stdout
4+
test_crawler_output: stdout
55
# if output is file, choose directory
6-
output_dir: outputs
6+
test_crawler_output_dir: outputs
77
# auto or one of the supported languages for specific repo
8-
lang_mode: go
8+
test_crawler_lang_mode: go
99
# should the output JSON be indented
10-
indent_json: true
10+
test_crawler_indent_json: true
1111
# ignore paths
12-
ignore: ["extern"]
12+
test_crawler_ignore: ["extern"]
13+
14+
### Behavior catalogue related vars
15+
behavior_gen_paths: ["../repo-to-crawl"]
16+
behavior_gen_ignore: [".github","cmds","config","docker","docs","extern","integrate_test"]
17+
behavior_gen_output_dir: outputs/catalogue

test-crawler/config_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
"gopkg.in/yaml.v2"
11+
)
12+
13+
func TestNewConfig(t *testing.T) {
14+
cfg := Config{
15+
TestCrawlerPaths: []string{"a", "b"},
16+
TestCrawlerOutputMode: "stdout",
17+
TestCrawlerOutputDir: "output",
18+
TestCrawlerLanguage: "go",
19+
TestCrawlerIndentJSON: true,
20+
TestCrawlerIgnore: []string{"extern"},
21+
22+
BehaviorGenPaths: []string{"c", "d"},
23+
BehaviorGenIgnore: []string{".git"},
24+
BehaviorGenOutputDir: "output/catalogue",
25+
}
26+
27+
f, err := os.CreateTemp("", "config-test.yml")
28+
require.Nil(t, err)
29+
defer f.Close()
30+
defer os.Remove(f.Name())
31+
32+
configBytes, err := yaml.Marshal(&cfg)
33+
assert.NoError(t, err)
34+
35+
assert.NoError(t, ioutil.WriteFile(f.Name(), configBytes, 0644))
36+
37+
var configFromFile Config
38+
fileContent, err := ioutil.ReadFile(f.Name())
39+
assert.NoError(t, err)
40+
41+
err = yaml.Unmarshal(fileContent, &configFromFile)
42+
assert.NoError(t, err)
43+
44+
assert.Equal(t, cfg, configFromFile)
45+
}

test-crawler/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func crawlRepoBehaviorsAndSaveToJSON(config Config) {
3737
allFiles := make(map[c.FileID]*c.TestFile)
3838
var fileFunctions []c.Function
3939

40-
for _, path := range config.Paths {
41-
files, err := c.GetTestFiles(path, config.Ignore)
40+
for _, path := range config.TestCrawlerPaths {
41+
files, err := c.GetTestFiles(path, config.TestCrawlerIgnore)
4242
if err != nil {
4343
fmt.Println(err)
4444
return
@@ -78,7 +78,7 @@ func crawlRepoBehaviorsAndSaveToJSON(config Config) {
7878

7979
result := filterFilesWhereChildIsRoot(allFiles)
8080

81-
Save(result, config.OutputMode, config.OutputDir, config.IndentJSON)
81+
Save(result, config.TestCrawlerOutputMode, config.TestCrawlerOutputDir, config.TestCrawlerIndentJSON)
8282
}
8383

8484
// crawlSingleFileForMethods accepts path of single go file,

0 commit comments

Comments
 (0)