github.com/sridharv/stencil@v0.0.0-20170626103218-a81b4a7626a1/stencil_test.go (about)

     1  package stencil
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"io/ioutil"
     8  
     9  	"flag"
    10  
    11  	"bytes"
    12  
    13  	"os"
    14  
    15  	"strings"
    16  
    17  	"github.com/pkg/errors"
    18  	"github.com/sridharv/fakegopath"
    19  )
    20  
    21  var updateGoldens = flag.Bool("update-goldens", false, "If true, goldens are updated")
    22  
    23  type outFile struct {
    24  	path   string
    25  	golden string
    26  }
    27  
    28  type testCase struct {
    29  	name    string
    30  	files   []fakegopath.SourceFile
    31  	srcs    []string
    32  	outs    []outFile
    33  	process func([]string) ([]file, error)
    34  }
    35  
    36  func (c testCase) run(t *testing.T) {
    37  	t.Run(c.name, func(t *testing.T) {
    38  		tmp, err := fakegopath.NewTemporaryWithFiles("stencil_"+c.name, c.files)
    39  		if err != nil {
    40  			t.Fatalf("%+v", err)
    41  		}
    42  		defer tmp.Reset()
    43  
    44  		srcs := make([]string, len(c.srcs))
    45  		for i, s := range c.srcs {
    46  			srcs[i] = filepath.Join(tmp.Src, s)
    47  		}
    48  		proc := c.process
    49  		if proc == nil {
    50  			proc = processStencil
    51  		}
    52  		files, err := proc(srcs)
    53  		if err != nil {
    54  			t.Fatalf("%+v", err)
    55  		}
    56  		if len(c.outs) != len(files) {
    57  			t.Fatalf("expected %d files, got %d", len(c.outs), len(files))
    58  		}
    59  		for i, o := range c.outs {
    60  			out := filepath.Join(tmp.Src, o.path)
    61  			f := files[i]
    62  			if !strings.HasSuffix(f.path, out) {
    63  				t.Errorf("expected file %s, got %s", out, f.path)
    64  			}
    65  			if *updateGoldens {
    66  				if err := ioutil.WriteFile(o.golden, f.data, 0644); err != nil {
    67  					t.Error(o.golden, ": failed to update golden", err)
    68  				}
    69  				continue
    70  			}
    71  			golden, err := ioutil.ReadFile(o.golden)
    72  			if err != nil {
    73  				t.Fatal(o.golden, ": could not read golden", err)
    74  			}
    75  			if !bytes.Equal(golden, f.data) {
    76  				t.Errorf("expected output:\n%s\ngot:\n%s", string(golden), string(f.data))
    77  			}
    78  		}
    79  	})
    80  }
    81  
    82  var cases = []testCase{
    83  	{
    84  		name: "Set_String_SingleFile",
    85  		files: []fakegopath.SourceFile{
    86  			{Src: "testdata/set.go", Dest: "collections/set/set.go"},
    87  			{Src: "testdata/set.intersect.go", Dest: "examples/setexamples/intersect.go"},
    88  		},
    89  		srcs: []string{"examples/setexamples/intersect.go"},
    90  		outs: []outFile{
    91  			{
    92  				path:   "examples/setexamples/vendor/collections/set/Element/string/set.go",
    93  				golden: "testdata/set.string.golden",
    94  			},
    95  		},
    96  	},
    97  	{
    98  		name: "Basic_Float32_SingleFile",
    99  		files: []fakegopath.SourceFile{
   100  			{Src: "testdata/basic.go", Dest: "basic/basic.go"},
   101  			{Src: "testdata/basic.use.go", Dest: "use/use.go"},
   102  		},
   103  		srcs: []string{"use/use.go"},
   104  		outs: []outFile{
   105  			{
   106  				path:   "use/vendor/basic/int/float32/basic.go",
   107  				golden: "testdata/basic.float32.golden",
   108  			},
   109  		},
   110  	},
   111  	{
   112  		name: "Set_Interfaces_SingleFile",
   113  		files: []fakegopath.SourceFile{
   114  			{Src: "testdata/interfaces.go", Dest: "ifaces/interfaces.go"},
   115  			{Src: "testdata/interfaces.use.go", Dest: "use/use.go"},
   116  		},
   117  		srcs: []string{"use/use.go"},
   118  		outs: []outFile{
   119  			{
   120  				path:   "use/vendor/ifaces/interface/int/interfaces.go",
   121  				golden: "testdata/interfaces.int.golden",
   122  			},
   123  		},
   124  	},
   125  	{
   126  		name: "Set_Interfaces_MultiFile",
   127  		files: []fakegopath.SourceFile{
   128  			{Src: "testdata/interfaces.go", Dest: "ifaces/interfaces.go"},
   129  			{Src: "testdata/interfacesintersect.go", Dest: "ifaces/interfacesintersect.go"},
   130  			{Src: "testdata/interfaces.use.go", Dest: "use/use.go"},
   131  		},
   132  		srcs: []string{"use/use.go"},
   133  		outs: []outFile{
   134  			{
   135  				path:   "use/vendor/ifaces/interface/int/interfaces.go",
   136  				golden: "testdata/interfaces.int.golden",
   137  			},
   138  			{
   139  				path:   "use/vendor/ifaces/interface/int/interfacesintersect.go",
   140  				golden: "testdata/interfacesintersect.int.golden",
   141  			},
   142  		},
   143  	},
   144  	{
   145  		name: "Set_String_Dir",
   146  		files: []fakegopath.SourceFile{
   147  			{Src: "testdata/set.go", Dest: "collections/set/set.go"},
   148  			{Src: "testdata/set.intersect.go", Dest: "examples/setexamples/intersect.go"},
   149  		},
   150  		srcs: []string{"examples/setexamples/intersect.go"},
   151  		outs: []outFile{
   152  			{
   153  				path:   "examples/setexamples/vendor/collections/set/Element/string/set.go",
   154  				golden: "testdata/set.string.golden",
   155  			},
   156  		},
   157  		process: func(p []string) ([]file, error) {
   158  			d := filepath.Dir(p[0])
   159  			cwd, err := os.Getwd()
   160  			if err != nil {
   161  				return nil, errors.WithStack(err)
   162  			}
   163  			if err = os.Chdir(d); err != nil {
   164  				return nil, errors.WithStack(err)
   165  			}
   166  			defer os.Chdir(cwd)
   167  			return processStencil([]string{})
   168  		},
   169  	},
   170  }
   171  
   172  func TestStencil(t *testing.T) {
   173  	for _, c := range cases {
   174  		c.run(t)
   175  	}
   176  }