github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/cmd/reactVet/reactVet_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"syscall"
    10  	"testing"
    11  
    12  	"github.com/kisielk/gotool"
    13  )
    14  
    15  func TestImmutableVetter(t *testing.T) {
    16  
    17  	var expected = `_testFiles/example.go:16:15: argument must be a constant string
    18  _testFiles/example.go:20:19: argument must be a constant string
    19  _testFiles/example.go:24:19: argument must be a constant string`
    20  
    21  	wd, err := os.Getwd()
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  
    26  	specs := gotool.ImportPaths([]string{
    27  		"myitcv.io/react/cmd/reactVet/_testFiles",
    28  	})
    29  
    30  	emsgs := vet(wd, specs)
    31  
    32  	bfr := bytes.NewBuffer(nil)
    33  
    34  	for _, msg := range emsgs {
    35  		fmt.Fprintf(bfr, "%v\n", msg)
    36  	}
    37  
    38  	diff := strDiff(expected, bfr.String())
    39  	if diff != "" {
    40  		fmt.Println(bfr.String())
    41  		t.Errorf("Expected no diff; got:\n%v", diff)
    42  	}
    43  }
    44  
    45  func mustTmpFile(dir string, prefix string) *os.File {
    46  	res, err := ioutil.TempFile(dir, prefix)
    47  
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  
    52  	return res
    53  }
    54  
    55  func strDiff(exp, act string) string {
    56  	actFn := mustTmpFile("", "").Name()
    57  	expFn := mustTmpFile("", "").Name()
    58  
    59  	defer func() {
    60  		os.Remove(actFn)
    61  		os.Remove(expFn)
    62  	}()
    63  
    64  	err := ioutil.WriteFile(actFn, []byte(act), 077)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	err = ioutil.WriteFile(expFn, []byte(exp), 077)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  
    73  	cmd := exec.Command("diff", "-wu", expFn, actFn)
    74  	res, err := cmd.CombinedOutput()
    75  	if err != nil {
    76  		ec := cmd.ProcessState.Sys().(syscall.WaitStatus)
    77  		if ec.ExitStatus() != 1 {
    78  			panic(err)
    79  		}
    80  	}
    81  
    82  	return string(res)
    83  }