Skip to content

Commit b0123b0

Browse files
VEN-1 | Parse single file for function declarations
1 parent 6f279fb commit b0123b0

File tree

5 files changed

+108
-8
lines changed

5 files changed

+108
-8
lines changed

test-crawler/collector/collector.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ type Function struct {
2828
IsTesting bool `json:"-"`
2929
}
3030

31+
type FunctionAnnotation struct {
32+
Name string `yaml:"name"`
33+
InputParams string `yaml:"inputParams"`
34+
ReturnValues string `yaml:"returnValues"`
35+
Description string
36+
Public bool
37+
}
38+
3139
func GetTestFiles(root string, ignore []string) (files []TestFile, err error) {
3240
fileArray, err := listTestFiles(root, ignore)
3341
if err != nil {

test-crawler/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Config struct {
2121
Language string `yaml:"lang_mode"`
2222
IndentJSON bool `yaml:"indent_json"`
2323
Ignore []string `yaml:"ignore"`
24+
SingleFile string `yaml:"single_file"`
2425
}
2526

2627
const filename string = "config.yaml"

test-crawler/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ lang_mode: go
1010
indent_json: true
1111
# ignore paths
1212
ignore: ["extern"]
13+
single_file: ../repo-to-crawl/venus-gateway/proofevent/proof_event.go # for example, replace with different one

test-crawler/extractor/extractor.go

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"os"
1010
"strings"
11+
"unicode"
1112

1213
a "testsuites/annotations"
1314
c "testsuites/collector"
@@ -26,6 +27,7 @@ const (
2627
BLOCK NodeType = "block"
2728
CALL_EXPRESSION NodeType = "call_expression"
2829
IDENTIFIER NodeType = "identifier"
30+
METHOD_DECLARATION NodeType = "method_declaration"
2931
)
3032

3133
type Metadata struct {
@@ -81,6 +83,29 @@ func ExtractInfo(file c.TestFile, ctx context.Context, fileID c.FileID) (*FileDa
8183
return fileData, hasNoTests(!checkForExistanceOfTests(fData)), nil
8284
}
8385

86+
func GetExportedFunctions(ctx context.Context, filePath string) ([]c.FunctionAnnotation, error) {
87+
content, err := getFileContent(filePath)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
parser := sitter.NewParser()
93+
parser.SetLanguage(golang.GetLanguage())
94+
95+
tree, err := parser.ParseCtx(ctx, nil, []byte(content))
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
cursor := sitter.NewTreeCursor(tree.RootNode())
101+
fnsAnno, err := parseContentForPublicFunctions(content, cursor)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
return fnsAnno, nil
107+
}
108+
84109
func getFileContent(filePath string) (content string, err error) {
85110

86111
file, err := os.Open(filePath)
@@ -103,17 +128,17 @@ func parseContent(content string, treeCursor *sitter.TreeCursor, filePath string
103128
var annotationParser a.Parser
104129

105130
fileData.Metadata = getMetadata(content, treeCursor, &annotationParser)
106-
functions := getFunctionNodes(content, treeCursor, &annotationParser)
131+
funcNodes := getFunctionNodes(content, treeCursor, &annotationParser)
107132

108-
for _, function := range functions {
133+
for _, function := range funcNodes {
109134

110135
behaviors := findBehaviorsFromNode(content, function.Node)
111136
var callExpressions []string = nil
112137
if len(behaviors) > 0 {
113138
callExpressions = findCallExprFromNode(content, function.Node)
114139
}
115140

116-
fileData.Functions = append(fileData.Functions, makeCollectorScenario(filePath, function.Name, behaviors, callExpressions))
141+
fileData.Functions = append(fileData.Functions, makeCollectorScenario(filePath, function.Function.Name, behaviors, callExpressions))
117142

118143
}
119144

@@ -163,9 +188,12 @@ func getMetadata(content string, treeCursor *sitter.TreeCursor, parser *a.Parser
163188
return &meta
164189
}
165190

191+
// this method extracts functions and methods.
192+
// Function can have 2 types: function_declaration (for example contructor)
193+
// and method_declaration (can be exported and unexported)
166194
func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.Parser) (funcAnnoPair []struct {
167-
Node *sitter.Node
168-
Name string
195+
Node *sitter.Node
196+
Function c.FunctionAnnotation
169197
}) {
170198

171199
numChildsRootNode := treeCursor.CurrentNode().ChildCount()
@@ -204,17 +232,41 @@ func getFunctionNodes(content string, treeCursor *sitter.TreeCursor, parser *a.P
204232
}
205233

206234
funcAnnoPair = append(funcAnnoPair, struct {
207-
Node *sitter.Node
208-
Name string
235+
Node *sitter.Node
236+
Function c.FunctionAnnotation
209237
}{
210238
Node: node,
211-
Name: funcName,
239+
Function: c.FunctionAnnotation{
240+
Name: funcName,
241+
},
212242
})
213243

214244
node = nil
215245
funcName = ""
216246
isIgnored = false
217247
}
248+
if child.Type() == string(METHOD_DECLARATION) {
249+
funcName = content[child.Child(2).StartByte():child.Child(2).EndByte()]
250+
public := unicode.IsUpper(rune(funcName[0]))
251+
params := ""
252+
returnValues := ""
253+
if child.Child(1).Type() == string(PARAMETER_LIST) {
254+
params = content[child.Child(3).StartByte():child.Child(3).EndByte()]
255+
returnValues = content[child.Child(4).StartByte():child.Child(4).EndByte()]
256+
}
257+
funcAnnoPair = append(funcAnnoPair, struct {
258+
Node *sitter.Node
259+
Function c.FunctionAnnotation
260+
}{
261+
Node: child,
262+
Function: c.FunctionAnnotation{
263+
Name: funcName,
264+
Public: public,
265+
InputParams: params,
266+
ReturnValues: returnValues,
267+
},
268+
})
269+
}
218270
prevNode = child
219271
}
220272
}
@@ -301,3 +353,15 @@ func makeID(filePath string, funcName string, behavior string) string {
301353
hash := md5.Sum([]byte(fmt.Sprintf("%s_%s_%s", filePath, funcName, behavior)))
302354
return string(hex.EncodeToString(hash[:]))
303355
}
356+
357+
func parseContentForPublicFunctions(content string, cursor *sitter.TreeCursor) ([]c.FunctionAnnotation, error) {
358+
var annotationParser a.Parser
359+
360+
var fnsAnno []c.FunctionAnnotation
361+
functions := getFunctionNodes(content, cursor, &annotationParser)
362+
for _, f := range functions {
363+
fnsAnno = append(fnsAnno, f.Function)
364+
}
365+
366+
return fnsAnno, nil
367+
}

test-crawler/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func main() {
2626

2727
config := NewConfig()
2828

29+
// crawlRepoBehaviorsAndSaveToJSON(config)
30+
crawlSingleFileForMethods(config)
31+
}
32+
33+
func crawlRepoBehaviorsAndSaveToJSON(config Config) {
2934
allFiles := make(map[c.FileID]*c.TestFile)
3035
var fileFunctions []c.Function
3136

@@ -73,6 +78,27 @@ func main() {
7378
Save(result, config.OutputMode, config.OutputDir, config.IndentJSON)
7479
}
7580

81+
func crawlSingleFileForMethods(config Config) {
82+
fns, err := extractPublicMethodsFromFile(context.Background(), config.SingleFile)
83+
if err != nil {
84+
fmt.Print(err)
85+
os.Exit(1)
86+
}
87+
for _, fn := range fns {
88+
fmt.Printf(
89+
"name %s : public %v : params: %s : return values : %s\n",
90+
fn.Name,
91+
fn.Public,
92+
fn.InputParams,
93+
fn.ReturnValues,
94+
)
95+
}
96+
}
97+
98+
func extractPublicMethodsFromFile(ctx context.Context, filePath string) ([]c.FunctionAnnotation, error) {
99+
return ex.GetExportedFunctions(ctx, filePath)
100+
}
101+
76102
func linkFiles(flist []c.Function) (links [][]FnLink) {
77103

78104
functions := make(map[string]c.Function)

0 commit comments

Comments
 (0)