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

     1  package wat
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestPopulate(t *testing.T) {
    14  	f := newWatFixture(t)
    15  	defer f.tearDown()
    16  	ws := f.watInit()
    17  
    18  	oldBuiltins := builtins
    19  	defer func() { builtins = oldBuiltins }()
    20  	builtins = []plugin{pluginAB{}}
    21  
    22  	// Instead of paths to executables, this "plugin" just echos results straight to stdout.
    23  	f.write(filepath.Join(kWatDirName, fnameUserPlugins),
    24  		fmt.Sprintf("echo '%s'", MustJson([]WatCommand{cmdC})))
    25  
    26  	cmds, err := List(context.Background(), ws, 0)
    27  	if err != nil {
    28  		f.t.Fatal("Populate", err)
    29  	}
    30  	count := len(cmds.Commands)
    31  	if count != 3 {
    32  		f.t.Fatalf("Expected 3 commands to be populated, got %d", count)
    33  	}
    34  	exists, err := ws.Exists(fnameList)
    35  	if err != nil {
    36  		f.t.Fatal("Exists", err)
    37  	}
    38  	if !exists {
    39  		f.t.Fatal("Expect list file to exist")
    40  	}
    41  
    42  }
    43  
    44  // Make sure we don't explode if the list file doesn't exist yet.
    45  func TestCommandListNotExists(t *testing.T) {
    46  	f := newWatFixture(t)
    47  	defer f.tearDown()
    48  	ws := f.watInit()
    49  	ctx := context.Background()
    50  	cmdListOut, err := List(ctx, ws, listTTL)
    51  	if err != nil {
    52  		f.t.Fatal("List()", err)
    53  	}
    54  
    55  	assertCommandList(f.t, CommandList{}, cmdListOut)
    56  }
    57  
    58  func TestCommandListWriteAndRead(t *testing.T) {
    59  	f := newWatFixture(t)
    60  	defer f.tearDown()
    61  	ws := f.watInit()
    62  	ctx := context.Background()
    63  
    64  	cmds := []WatCommand{
    65  		WatCommand{
    66  			Command:     "echo hello",
    67  			FilePattern: "abc",
    68  		},
    69  	}
    70  	cmdListIn := CommandList{
    71  		time.Now(),
    72  		cmds,
    73  	}
    74  
    75  	err := cmdListIn.toFile(ws)
    76  	if err != nil {
    77  		f.t.Fatal("cmdList.toFile", err)
    78  	}
    79  
    80  	cmdListOut, err := List(ctx, ws, listTTL)
    81  	if err != nil {
    82  		f.t.Fatal("List()", err)
    83  	}
    84  
    85  	assertCommandList(f.t, cmdListIn, cmdListOut)
    86  }
    87  
    88  func TestListRepopulatesStaleInfo(t *testing.T) {
    89  	f := newWatFixture(t)
    90  	defer f.tearDown()
    91  	ws := f.watInit()
    92  
    93  	oldData := CommandList{
    94  		Timestamp: time.Now().Add(time.Hour * -60),
    95  		Commands: []WatCommand{
    96  			WatCommand{
    97  				Command:     "echo hello",
    98  				FilePattern: "abc",
    99  			},
   100  		},
   101  	}
   102  	err := oldData.toFile(ws)
   103  	if err != nil {
   104  		f.t.Fatal("CommandList.toFile()", err)
   105  	}
   106  
   107  	f.write("src/github.com/fake/repo/repo.go", `
   108  package repo
   109  
   110  func One() { return 1 }
   111  `)
   112  	f.write("src/github.com/fake/repo/repo_test.go", `
   113  package repo
   114  
   115  import "testing"
   116  
   117  func TestOne(t *testing.T) {}
   118  `)
   119  	os.Setenv("GOPATH", f.root.Path())
   120  
   121  	actual, err := List(f.ctx, ws, listTTL)
   122  
   123  	expectedCommands := []WatCommand{
   124  		WatCommand{
   125  			FilePattern: "src/github.com/fake/repo/*",
   126  			Command:     "go test github.com/fake/repo",
   127  		},
   128  	}
   129  
   130  	if !reflect.DeepEqual(actual.Commands, expectedCommands) {
   131  		f.t.Fatalf("expected: %v, actual: %v", expectedCommands, actual)
   132  	}
   133  	if !actual.Timestamp.After(oldData.Timestamp) {
   134  		f.t.Fatal("Timestamp was not updated.")
   135  	}
   136  }