Skip to content

Commit 2fd5512

Browse files
committed
Fix logic for comment parse, resolve conflics
2 parents 12f0eed + caf9d09 commit 2fd5512

File tree

7 files changed

+127
-33
lines changed

7 files changed

+127
-33
lines changed

test-crawler/collector/collector.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package collector
22

33
import (
4+
"bytes"
5+
"fmt"
46
"os"
57
"path/filepath"
68
"strings"
9+
"unicode"
710

811
a "testsuites/annotations"
912
)
@@ -126,3 +129,35 @@ func ListGoFilesInFolder(root string, ignore []string) (system string, files []s
126129

127130
return "", nil, nil
128131
}
132+
133+
// GenerateMethodName generates name adding underscore and usecase index
134+
// ex: name: ListenProofRequest -> LISTEN_PROOF_REQUEST_001
135+
func GenerateMethodName(funcName string) string {
136+
usecase := "001" // in the future it will be autoincremented depending how many return cases we have.
137+
buf := &bytes.Buffer{}
138+
for i, rune := range funcName {
139+
if unicode.IsUpper(rune) && i > 0 {
140+
buf.WriteRune('_')
141+
}
142+
buf.WriteRune(rune)
143+
}
144+
145+
return fmt.Sprintf("%s_%s", strings.ToUpper(buf.String()), usecase)
146+
}
147+
148+
// GenerateMethodDescription generates description based on inputParams, returnParams
149+
func GenerateMethodDescription(f FunctionAnnotation) string {
150+
if f.Description == "" {
151+
dsc := "Function description not set."
152+
if (f.InputParams != "" && f.InputParams != "()") && f.ReturnValues != "" {
153+
dsc = fmt.Sprintf(`Given a %s, returns %s`, f.InputParams, f.ReturnValues)
154+
} else if (f.InputParams == "" || f.InputParams == "()") && f.ReturnValues != "" {
155+
dsc = fmt.Sprintf(`Returns %s`, f.ReturnValues)
156+
}
157+
return dsc
158+
} else {
159+
return f.Description
160+
}
161+
162+
return "Function description not set."
163+
}

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/crawler_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ func TestCrawlSingleFileForFunctions(t *testing.T) {
2222
}
2323

2424
assert.Equal(t, "// HelloEvent simple method that just formats message.", fnsAnn[0].Description)
25-
assert.Equal(t, "HelloEvent", fnsAnn[0].Name)
25+
assert.Equal(t, "HELLO_EVENT_001", fnsAnn[0].Name)
2626
assert.Equal(t, "()", fnsAnn[0].InputParams) // input param
2727
assert.Equal(t, "string", fnsAnn[0].ReturnValues) // return param
2828

2929
assert.Equal(t, "// HelloEventWithParameter accepts one param that got formated in message.", fnsAnn[1].Description)
30-
assert.Equal(t, "HelloEventWithParameter", fnsAnn[1].Name)
30+
assert.Equal(t, "HELLO_EVENT_WITH_PARAMETER_001", fnsAnn[1].Name)
3131
assert.Equal(t, "(param string)", fnsAnn[1].InputParams)
3232
assert.Equal(t, "(string, error)", fnsAnn[1].ReturnValues)
3333

3434
assert.Equal(t, "// FunctionWithoutParameters...", fnsAnn[2].Description)
35-
assert.Equal(t, "FunctionWithoutParameters", fnsAnn[2].Name)
35+
assert.Equal(t, "FUNCTION_WITHOUT_PARAMETERS_001", fnsAnn[2].Name)
3636
assert.Equal(t, "()", fnsAnn[2].InputParams)
3737
assert.Equal(t, "", fnsAnn[2].ReturnValues)
3838

3939
assert.Equal(t, "// FunctionWithPointerReturnValue returns a simple pointer value.", fnsAnn[3].Description)
40-
assert.Equal(t, "FunctionWithPointerReturnValue", fnsAnn[3].Name)
40+
assert.Equal(t, "FUNCTION_WITH_POINTER_RETURN_VALUE_001", fnsAnn[3].Name)
41+
4142
assert.Equal(t, "()", fnsAnn[3].InputParams)
4243
assert.Equal(t, "*Event", fnsAnn[3].ReturnValues)
4344
}

test-crawler/extractor/extractor.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,14 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
260260
child.Child(4): parameter_list: (<-chan *types2.RequestEvent, error)
261261
*/
262262
if child.Type() == string(METHOD_DECLARATION) {
263-
comment := content[prevNode.StartByte():prevNode.EndByte()]
263+
comment := ""
264264
if prevNode.Type() == string(COMMENT) {
265+
comment = content[prevNode.StartByte():prevNode.EndByte()]
265266
value, annotationType, _ := parser.Parse(comment)
266267
if value != nil && annotationType == a.Ignore {
267268
isIgnored = value.(bool)
268269
}
269270
}
270-
271271
funcName = content[child.Child(2).StartByte():child.Child(2).EndByte()]
272272
params := ""
273273
returnValues := ""
@@ -286,15 +286,19 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
286286
returnValueType == string(TYPE_IDENTIFIER) {
287287
returnValues = content[child.Child(4).StartByte():child.Child(4).EndByte()]
288288
}
289+
funcAnn := c.FunctionAnnotation{
290+
Description: comment,
291+
Name: funcName,
292+
Public: isPublic(funcName),
293+
InputParams: params,
294+
ReturnValues: returnValues,
295+
}
296+
funcAnn.Name = c.GenerateMethodName(funcAnn.Name)
297+
funcAnn.Description = c.GenerateMethodDescription(funcAnn)
298+
289299
funcAnnoPair = append(funcAnnoPair, FunctionAnnotationNode{
290-
Node: child,
291-
Function: c.FunctionAnnotation{
292-
Description: comment,
293-
Name: funcName,
294-
Public: isPublic(funcName),
295-
InputParams: params,
296-
ReturnValues: returnValues,
297-
},
300+
Node: child,
301+
Function: funcAnn,
298302
})
299303
}
300304
prevNode = child

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)