-
Notifications
You must be signed in to change notification settings - Fork 11
/
rewrite_test.go
45 lines (35 loc) · 870 Bytes
/
rewrite_test.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
package main
import (
"go/parser"
"go/token"
"io/ioutil"
"os"
"strconv"
"testing"
)
func Test_rewriteGoFile(t *testing.T) {
f, _ := ioutil.TempFile("", "rewriteGoFile.go")
f.Close()
c := `package main
import(
"github.com/octokit/go-octokit"
)
func main() {
fmt.Println("hi")
}
`
ioutil.WriteFile(f.Name(), []byte(c), os.ModePerm)
err := rewriteGoFile(f.Name(), "github.com/gophergala/nut", []string{"github.com/octokit/go-octokit"})
if err != nil {
t.Fatal("err isn't nil for rewriteGoFile")
}
fset := token.NewFileSet()
ff, _ := parser.ParseFile(fset, f.Name(), nil, parser.ParseComments)
if len(ff.Imports) != 1 {
t.Fatal("there should be only one import")
}
i, _ := strconv.Unquote(ff.Imports[0].Path.Value)
if i != "github.com/gophergala/nut/vendor/_nuts/github.com/octokit/go-octokit" {
t.Fatalf("import is wrong: %s", i)
}
}