github.com/tilt-dev/wat@v0.0.2-0.20180626175338-9349b638e250/cli/wat/train_test.go (about)

     1  package wat
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestTrainMatch(t *testing.T) {
    13  	f := newWatFixture(t)
    14  	defer f.tearDown()
    15  	ws := f.watInit()
    16  
    17  	f.write("src/github.com/fake/repo/repo_test.go", `
    18  package repo
    19  
    20  import "testing"
    21  
    22  func TestOne(t *testing.T) {}
    23  `)
    24  
    25  	cmd := WatCommand{
    26  		Command:     "go test github.com/fake/repo",
    27  		FilePattern: "src/github.com/fake/repo/*.go",
    28  	}
    29  	cmds := []WatCommand{cmd}
    30  	groups, err := trainAt(context.Background(), ws, cmds)
    31  	if err != nil {
    32  		t.Fatalf("Train: %v", err)
    33  	}
    34  
    35  	if len(groups) != 1 {
    36  		t.Fatalf("Expected 1 groups. Actual: %+v", groups)
    37  	}
    38  
    39  	for _, g := range groups {
    40  		if len(g.Logs) != 1 {
    41  			t.Fatalf("Expected each group to have 1 log. Actual: %+v", g.Logs)
    42  		}
    43  	}
    44  }
    45  
    46  func TestTrainToFile(t *testing.T) {
    47  	f := newWatFixture(t)
    48  	defer f.tearDown()
    49  	ws := f.watInit()
    50  
    51  	f.write("src/github.com/fake/repo/repo_test.go", `
    52  package repo
    53  
    54  import "testing"
    55  
    56  func TestOne(t *testing.T) {}
    57  `)
    58  
    59  	cmd := WatCommand{
    60  		Command:     "go test github.com/fake/repo",
    61  		FilePattern: "src/github.com/fake/repo/*.go",
    62  	}
    63  	cmds := []WatCommand{cmd}
    64  	_, err := Train(context.Background(), ws, cmds, 0)
    65  	if err != nil {
    66  		t.Fatalf("Train: %v", err)
    67  	}
    68  
    69  	// Make sure Train() wrote to a file.
    70  	groups, err := ReadCmdLogGroups(ws)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	if len(groups) != 1 {
    76  		t.Fatalf("Expected 1 groups. Actual: %+v", groups)
    77  	}
    78  
    79  	for _, g := range groups {
    80  		if len(g.Logs) != 1 {
    81  			t.Fatalf("Expected each group to have 1 log. Actual: %+v", g.Logs)
    82  		}
    83  	}
    84  
    85  	// Make sure Train() re-uses the files on-disk.
    86  	groups2, err := Train(context.Background(), ws, []WatCommand{}, time.Hour)
    87  	if err != nil {
    88  		t.Fatalf("Train: %v", err)
    89  	}
    90  
    91  	if !reflect.DeepEqual(groups, groups2) {
    92  		t.Fatalf("Train must not have reused files on disk because the results were different:\n%v\n%v", groups, groups2)
    93  	}
    94  }
    95  
    96  func TestTrainNoMatch(t *testing.T) {
    97  	f := newWatFixture(t)
    98  	defer f.tearDown()
    99  	ws := f.watInit()
   100  
   101  	cmd := WatCommand{
   102  		Command:     "go test github.com/fake/repo",
   103  		FilePattern: "src/github.com/fake/repo/*.go",
   104  	}
   105  	cmds := []WatCommand{cmd}
   106  	groups, err := trainAt(context.Background(), ws, cmds)
   107  	if err != nil {
   108  		t.Fatalf("Train: %v", err)
   109  	}
   110  
   111  	if len(groups) != 1 {
   112  		t.Fatalf("Expected 1 groups. Actual: %+v", groups)
   113  	}
   114  
   115  	for _, g := range groups {
   116  		if len(g.Logs) != 1 {
   117  			t.Fatalf("Expected each group to have 1 log. Actual: %+v", g.Logs)
   118  		}
   119  	}
   120  }
   121  
   122  func TestTrainFuzz(t *testing.T) {
   123  	f := newWatFixture(t)
   124  	defer f.tearDown()
   125  
   126  	f.write("src/github.com/fake/repo/repo.go", `
   127  package repo
   128  
   129  func F() bool { return false }
   130  `)
   131  	f.write("src/github.com/fake/repo/repo_test.go", `
   132  package repo
   133  
   134  import "testing"
   135  
   136  func TestF(t *testing.T) {
   137    if F() {
   138      t.Fatal("Expected false, got true")
   139    }
   140  }
   141  `)
   142  
   143  	cmd := WatCommand{
   144  		Command:     "go test github.com/fake/repo",
   145  		FilePattern: "src/github.com/fake/repo/*.go",
   146  	}
   147  	cmds := []WatCommand{cmd}
   148  	os.Setenv("GOPATH", f.root.Path())
   149  
   150  	group, err := fuzzAndRun(context.Background(), cmds, f.root.Path(), "src/github.com/fake/repo/repo.go")
   151  	if err != nil {
   152  		t.Fatalf("fuzzAndRun: %v", err)
   153  	}
   154  
   155  	if len(group.Logs) != 1 || group.Logs[0].Success {
   156  		t.Fatalf("Expected failure. Actual log group: %+v", group)
   157  	}
   158  
   159  	group, err = fuzzAndRun(context.Background(), cmds, f.root.Path(), "src/github.com/fake/repo/repo_test.go")
   160  	if err != nil {
   161  		t.Fatalf("fuzzAndRun: %v", err)
   162  	}
   163  
   164  	if len(group.Logs) != 1 || !group.Logs[0].Success {
   165  		t.Fatalf("Expected success. Actual log group: %+v", group)
   166  	}
   167  }
   168  
   169  type fuzzCase struct {
   170  	original string
   171  	expected string
   172  }
   173  
   174  func TestFuzz(t *testing.T) {
   175  	cases := []fuzzCase{
   176  		fuzzCase{"x := false", "x := true"},
   177  		fuzzCase{"x := 0", "x := 1"},
   178  		fuzzCase{"x := 100", "x := 100"},
   179  		fuzzCase{"x := falsey", "x := falsey"},
   180  	}
   181  
   182  	for i, c := range cases {
   183  		c := c
   184  		t.Run(fmt.Sprintf("TestFuzz%d", i), func(t *testing.T) {
   185  			actual := string(fuzz([]byte(c.original)))
   186  			if c.expected != actual {
   187  				t.Fatalf("Expected fuzz(%q) = %q. Actual: %q", c.original, c.expected, actual)
   188  			}
   189  		})
   190  	}
   191  }