...
1 package vfsgen_test
2
3 import (
4 "log"
5 "net/http"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "github.com/shurcooL/httpfs/union"
13 "github.com/shurcooL/vfsgen"
14 "golang.org/x/tools/godoc/vfs/httpfs"
15 "golang.org/x/tools/godoc/vfs/mapfs"
16 )
17
18
19
20
21
22
23
24
25 func Example() {
26 var fs http.FileSystem = http.Dir("assets")
27
28 err := vfsgen.Generate(fs, vfsgen.Options{})
29 if err != nil {
30 log.Fatalln(err)
31 }
32 }
33
34
35
36 func TestGenerate_buildAndGofmt(t *testing.T) {
37 tempDir := t.TempDir()
38
39 tests := []struct {
40 filename string
41 fs http.FileSystem
42 wantError func(error) bool
43 }{
44 {
45
46 filename: "empty.go",
47 fs: union.New(nil),
48 },
49 {
50
51
52 filename: "notexist.go",
53 fs: http.Dir("notexist"),
54 wantError: os.IsNotExist,
55 },
56 {
57
58 filename: "nocompressed.go",
59 fs: httpfs.New(mapfs.New(map[string]string{
60 "not-compressable-file.txt": "Not compressable.",
61 })),
62 },
63 {
64
65 filename: "onlycompressed.go",
66 fs: httpfs.New(mapfs.New(map[string]string{
67 "compressable-file.txt": "This text compresses easily. " + strings.Repeat(" Go!", 128),
68 })),
69 },
70 {
71
72 filename: "both.go",
73 fs: httpfs.New(mapfs.New(map[string]string{
74 "not-compressable-file.txt": "Not compressable.",
75 "compressable-file.txt": "This text compresses easily. " + strings.Repeat(" Go!", 128),
76 })),
77 },
78 }
79
80 for _, test := range tests {
81 filename := filepath.Join(tempDir, test.filename)
82
83 err := vfsgen.Generate(test.fs, vfsgen.Options{
84 Filename: filename,
85 PackageName: "test",
86 })
87 switch {
88 case test.wantError == nil && err != nil:
89 t.Fatalf("%s: vfsgen.Generate returned non-nil error: %v", test.filename, err)
90 case test.wantError != nil && !test.wantError(err):
91 t.Fatalf("%s: vfsgen.Generate returned wrong error: %v", test.filename, err)
92 }
93 if test.wantError != nil {
94 continue
95 }
96
97 if out, err := exec.Command("go", "build", filename).CombinedOutput(); err != nil {
98 t.Errorf("err: %v\nout: %s", err, out)
99 }
100 if out, err := exec.Command("gofmt", "-d", "-s", filename).Output(); err != nil || len(out) != 0 {
101 t.Errorf("gofmt issue\nerr: %v\nout: %s", err, out)
102 }
103 }
104 }
105
View as plain text