Skip to content

Commit f4361a0

Browse files
authored
Merge pull request #945 from filecoin-project/feature/STM2
VEN-2 | Generate a basic YAML file
2 parents d517192 + 279c8aa commit f4361a0

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

test-crawler/collector/collector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Function struct {
2929
}
3030

3131
type FunctionAnnotation struct {
32+
ID int `yaml:"id"`
3233
Name string `yaml:"name"`
3334
InputParams string `yaml:"inputParams"`
3435
ReturnValues string `yaml:"returnValues"`

test-crawler/main.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"io/ioutil"
89
"os"
910
"os/exec"
1011
"strings"
@@ -13,6 +14,8 @@ import (
1314
a "testsuites/annotations"
1415
c "testsuites/collector"
1516
ex "testsuites/extractor"
17+
18+
y "gopkg.in/yaml.v2"
1619
)
1720

1821
type FnLink struct {
@@ -27,6 +30,7 @@ func main() {
2730
config := NewConfig()
2831

2932
crawlRepoBehaviorsAndSaveToJSON(config)
33+
3034
}
3135

3236
func crawlRepoBehaviorsAndSaveToJSON(config Config) {
@@ -87,7 +91,8 @@ func crawlSingleFileForMethods(path string) {
8791
}
8892
for _, fn := range fns {
8993
fmt.Printf(
90-
"name %s : public %v : params: %s : return values : %s\n",
94+
"ID %d : name %s : public %v : params: %s : return values : %s\n",
95+
fn.ID,
9196
fn.Name,
9297
fn.Public,
9398
fn.InputParams,
@@ -100,6 +105,32 @@ func extractPublicMethodsFromFile(ctx context.Context, filePath string) ([]c.Fun
100105
return ex.GetExportedFunctions(ctx, filePath)
101106
}
102107

108+
// makeYAML will make yaml file from public methods.
109+
func makeYAML(ctx context.Context, filePath string) error {
110+
111+
publicMethods, err := extractPublicMethodsFromFile(ctx, filePath)
112+
if err != nil {
113+
fmt.Print(err)
114+
os.Exit(1)
115+
}
116+
117+
for i := 0; i < len(publicMethods); i++ {
118+
publicMethods[i].ID = i
119+
}
120+
121+
yamlData, err := y.Marshal(&publicMethods)
122+
if err != nil {
123+
fmt.Printf("Error while Marshaling. %v", err)
124+
}
125+
126+
fileName := "test.yaml"
127+
err = ioutil.WriteFile(fileName, yamlData, 0644)
128+
if err != nil {
129+
panic("Unable to write data into the file")
130+
}
131+
return nil
132+
}
133+
103134
func linkFiles(flist []c.Function) (links [][]FnLink) {
104135

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

test-crawler/make_yaml_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
c "testsuites/collector"
6+
7+
y "gopkg.in/yaml.v2"
8+
9+
"github.com/stretchr/testify/assert"
10+
11+
"io/ioutil"
12+
)
13+
14+
func TestMakeYAML(t *testing.T) {
15+
16+
yaml := []c.FunctionAnnotation{
17+
{
18+
ID: 1,
19+
Name: "SomeName",
20+
InputParams: "(ctx context.Context, param Parameters)",
21+
ReturnValues: "error",
22+
Description: "",
23+
Public: true,
24+
},
25+
{
26+
ID: 2,
27+
Name: "SomeName2",
28+
InputParams: "(ctx context.Context, param2 Parameters2)",
29+
ReturnValues: "error",
30+
Description: "",
31+
Public: true,
32+
},
33+
}
34+
35+
yamlData, err := y.Marshal(&yaml)
36+
if err != nil {
37+
t.Errorf("got error: %v", err.Error())
38+
}
39+
fileName := "test.yaml"
40+
err = ioutil.WriteFile(fileName, yamlData, 0644)
41+
if err != nil {
42+
panic("Unable to write data into the file")
43+
}
44+
45+
bytes, err := ioutil.ReadFile(fileName)
46+
if err != nil {
47+
t.Errorf("got error: %v", err.Error())
48+
}
49+
50+
assert.Equal(t, yamlData, bytes)
51+
52+
}

0 commit comments

Comments
 (0)