-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
103 lines (94 loc) · 2.23 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"bytes"
"errors"
"fmt"
"go/types"
"log"
"os/exec"
"strings"
)
// find type in info according to name
func findDef(info types.Info, name string) types.Type {
for ident, obj := range info.Defs {
// check if equals
if ident.String() == name {
return obj.Type()
}
}
return nil
}
// format sources to replace mark text
func FormatImplementers(sources []string) string {
commaStr := strings.Join(sources, ",")
fmt.Print(commaStr)
// implement joint
target := "var implementers = []dbusutil.Implementer{" + commaStr + "}"
return target
}
// execute unit test file with go test
func ExecuteUnitTest(file string) error {
log.Println("write to file success")
// if update success, execute write DBus xml
cmd := exec.Command("go", "test", file, "-count=1", "-v", "-run=TestDBusWriteXml")
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// run command
if err := cmd.Run(); err != nil {
log.Println("run command failed, out: ", stdout.String())
log.Println("run command failed, err: ", stderr.String())
return errors.New(stdout.String())
} else {
log.Println("run command go test success")
}
return nil
}
func ReplaceText(source string, old string, new string) string {
return strings.Replace(source, old, new, -1)
}
// unique string slice
func UniqueSlice(multi []string) []string {
// create map
uniqueMap := make(map[string]string)
// use map to unique slice
for _, elem := range multi {
// check if elem is empty
if elem == "" {
continue
}
// add elem to map
uniqueMap[elem] = elem
}
// create slice
var uniqueSlice []string
// add map elem to slice
for _, elem := range uniqueMap {
// check if elem is empty
if elem == "" {
continue
}
// add elem to slice
uniqueSlice = append(uniqueSlice, elem)
}
return uniqueSlice
}
// importer package
type importer struct{}
func (v *importer) Import(path0 string) (*types.Package, error) {
return types.NewPackage(path0, ""), nil
}
var ModuleStr = `
replacePackageMark
import (
"pkg.deepin.io/lib/dbusutil"
"testing"
)
replaceInterfaceMark
// go test -count=1 -v -run=TestDBusWriteXml
func TestDBusWriteXml(t *testing.T){
for _, value := range implementers {
dbusutil.WriteXML(value)
}
}`