...

Source file src/github.com/shurcooL/vfsgen/generator_test.go

Documentation: github.com/shurcooL/vfsgen

     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  // This code will generate an assets_vfsdata.go file with
    19  // `var assets http.FileSystem = ...`
    20  // that statically implements the contents of "assets" directory.
    21  //
    22  // vfsgen is great to use with go generate directives. This code can go in an assets_gen.go file, which can
    23  // then be invoked via "//go:generate go run assets_gen.go". The input virtual filesystem can read directly
    24  // from disk, or it can be more involved.
    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  // Verify that all possible combinations of {non-compressed,compressed} files build
    35  // successfully, and have no gofmt issues.
    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 // Nil function means want nil error.
    43  	}{
    44  		{
    45  			// Empty.
    46  			filename: "empty.go",
    47  			fs:       union.New(nil),
    48  		},
    49  		{
    50  			// Test that vfsgen.Generate returns an error when there is
    51  			// an error reading from the input filesystem.
    52  			filename:  "notexist.go",
    53  			fs:        http.Dir("notexist"),
    54  			wantError: os.IsNotExist,
    55  		},
    56  		{
    57  			// No compressed files.
    58  			filename: "nocompressed.go",
    59  			fs: httpfs.New(mapfs.New(map[string]string{
    60  				"not-compressable-file.txt": "Not compressable.",
    61  			})),
    62  		},
    63  		{
    64  			// Only compressed files.
    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  			// Both non-compressed and compressed files.
    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