github.com/windmilleng/wat@v0.0.2-0.20180626175338-9349b638e250/cli/wat/fs_test.go (about)

     1  package wat
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestWatRootCurDir(t *testing.T) {
    13  	f := newWatFixture(t)
    14  	defer f.tearDown()
    15  
    16  	// wat root at f.root
    17  	f.watInit()
    18  
    19  	rt, err := watRoot(f.root.Path())
    20  	if err != nil {
    21  		f.t.Fatalf("Got error: %v", err)
    22  	}
    23  	if rt != f.root.Path() {
    24  		f.t.Fatalf("Expected wat root '%s', got '%s'", f.root.Path(), rt)
    25  	}
    26  }
    27  
    28  func TestWatRootParentDir(t *testing.T) {
    29  	f := newWatFixture(t)
    30  	defer f.tearDown()
    31  
    32  	// wat root at f.root
    33  	f.watInit()
    34  
    35  	f.writeFile("foo/bar/baz")
    36  	childDir := filepath.Join(f.root.Path(), "foo/bar")
    37  
    38  	rt, err := watRoot(childDir)
    39  	if err != nil {
    40  		f.t.Fatalf("Got error: %v", err)
    41  	}
    42  	if rt != f.root.Path() {
    43  		f.t.Fatalf("Expected wat root '%s', got '%s'", f.root.Path(), rt)
    44  	}
    45  }
    46  
    47  func TestWatRootNotExists(t *testing.T) {
    48  	f := newWatFixture(t)
    49  	defer f.tearDown()
    50  
    51  	_, err := watRoot(f.root.Path())
    52  	if err == nil || err != ErrNoWatRoot {
    53  		f.t.Fatalf("Expected 'no wat root found' error but instead got error: %v", err)
    54  	}
    55  }
    56  
    57  func TestRead(t *testing.T) {
    58  	f := newWatFixture(t)
    59  	defer f.tearDown()
    60  	ws := f.watInit()
    61  
    62  	for i := 0; i < 5; i++ {
    63  		f.write(filepath.Join(kWatDirName, strconv.Itoa(i)), strconv.Itoa(i))
    64  	}
    65  
    66  	for i := 0; i < 5; i++ {
    67  		contents, err := ws.Read(strconv.Itoa(i))
    68  		if err != nil {
    69  			f.t.Fatalf("[wat.Read] Got error reading file '%d': %v", i, err)
    70  		}
    71  		if string(contents) != strconv.Itoa(i) {
    72  			f.t.Fatalf("Expected file '%d' to have contents '%d'; got '%s'", i, i, contents)
    73  		}
    74  	}
    75  }
    76  
    77  func TestWrite(t *testing.T) {
    78  	f := newWatFixture(t)
    79  	defer f.tearDown()
    80  	ws := f.watInit()
    81  
    82  	for i := 0; i < 5; i++ {
    83  		err := ws.Write(strconv.Itoa(i), strconv.Itoa(i))
    84  		if err != nil {
    85  			f.t.Fatalf("Got error writing file '%d': %v", i, err)
    86  		}
    87  	}
    88  
    89  	for i := 0; i < 5; i++ {
    90  		expectedPath := filepath.Join(kWatDirName, strconv.Itoa(i))
    91  		contents, err := ioutil.ReadFile(expectedPath)
    92  		if err != nil {
    93  			f.t.Fatalf("[ioutil.ReadFile]: %v", err)
    94  		}
    95  		if string(contents) != strconv.Itoa(i) {
    96  			f.t.Fatalf("Expected file '%d' to have contents '%d'; got '%s'", i, i, contents)
    97  		}
    98  	}
    99  }
   100  
   101  func TestWriteNested(t *testing.T) {
   102  	f := newWatFixture(t)
   103  	defer f.tearDown()
   104  	ws := f.watInit()
   105  
   106  	for i := 0; i < 5; i++ {
   107  		path := filepath.Join(strconv.Itoa(i), strconv.Itoa(i))
   108  		err := ws.Write(path, strconv.Itoa(i))
   109  		if err != nil {
   110  			f.t.Fatalf("[wat.write]: %v", err)
   111  		}
   112  	}
   113  
   114  	for i := 0; i < 5; i++ {
   115  		expectedPath := filepath.Join(kWatDirName, strconv.Itoa(i), strconv.Itoa(i))
   116  		contents, err := ioutil.ReadFile(expectedPath)
   117  		if err != nil {
   118  			f.t.Fatalf("[ioutil.ReadFile] %v", err)
   119  		}
   120  		if string(contents) != strconv.Itoa(i) {
   121  			f.t.Fatalf("Expected file '%d' to have contents '%d'; got '%s'",
   122  				i, i, contents)
   123  		}
   124  	}
   125  }
   126  
   127  func TestAppend(t *testing.T) {
   128  	f := newWatFixture(t)
   129  	defer f.tearDown()
   130  	ws := f.watInit()
   131  
   132  	fullPath := filepath.Join(kWatDirName, "test")
   133  	f.write(fullPath, "foo")
   134  
   135  	err := ws.Append("test", "bar")
   136  	if err != nil {
   137  		f.t.Fatalf("[Append] %v", err)
   138  	}
   139  	err = ws.Append("test", "baz")
   140  	if err != nil {
   141  		f.t.Fatalf("[Append] %v", err)
   142  	}
   143  
   144  	expectedContents := "foobarbaz"
   145  	contents, err := ioutil.ReadFile(fullPath)
   146  	if err != nil {
   147  		f.t.Fatalf("[ioutil.ReadFile]: %v", err)
   148  	}
   149  	if string(contents) != expectedContents {
   150  		f.t.Fatalf("Expected contents '%s'; got '%s'", expectedContents, contents)
   151  	}
   152  }
   153  
   154  func TestAppendNonexistentFile(t *testing.T) {
   155  	f := newWatFixture(t)
   156  	defer f.tearDown()
   157  	ws := f.watInit()
   158  
   159  	// Appending to file that doesn't exist yet
   160  	err := ws.Append("test", "foobarbaz")
   161  
   162  	fullPath := filepath.Join(kWatDirName, "test")
   163  	expectedContents := "foobarbaz"
   164  	contents, err := ioutil.ReadFile(fullPath)
   165  	if err != nil {
   166  		f.t.Fatalf("[ioutil.ReadFile]: %v", err)
   167  	}
   168  	if string(contents) != expectedContents {
   169  		f.t.Fatalf("Expected contents '%s'; got '%s'", expectedContents, contents)
   170  	}
   171  }
   172  
   173  func TestAppendLine(t *testing.T) {
   174  	f := newWatFixture(t)
   175  	defer f.tearDown()
   176  	ws := f.watInit()
   177  
   178  	fullPath := filepath.Join(kWatDirName, "test")
   179  	f.write(fullPath, `foo
   180  `)
   181  
   182  	err := ws.AppendLine("test", "bar")
   183  	if err != nil {
   184  		f.t.Fatalf("[AppendLine] %v", err)
   185  	}
   186  	err = ws.AppendLine("test", "baz")
   187  	if err != nil {
   188  		f.t.Fatalf("[AppendLine] %v", err)
   189  	}
   190  
   191  	expectedContents := `foo
   192  bar
   193  baz
   194  `
   195  	contents, err := ioutil.ReadFile(fullPath)
   196  	if err != nil {
   197  		f.t.Fatalf("[ioutil.ReadFile]: %v", err)
   198  	}
   199  	if string(contents) != expectedContents {
   200  		f.t.Fatalf("Expected contents '%s'; got '%s'", expectedContents, contents)
   201  	}
   202  }
   203  
   204  func TestCmdLogsToFile(t *testing.T) {
   205  	f := newWatFixture(t)
   206  	defer f.tearDown()
   207  	ws := f.watInit()
   208  
   209  	groupA := CommandLogGroup{
   210  		Context: LogContext{
   211  			RecentEdits: []string{"a.txt", "b.txt"},
   212  			StartTime:   time.Now(),
   213  			Source:      LogSourceBootstrap,
   214  		},
   215  		Logs: []CommandLog{
   216  			{
   217  				Command:  "cat foo",
   218  				Success:  true,
   219  				Duration: time.Minute,
   220  			},
   221  		},
   222  	}
   223  	groupB := CommandLogGroup{
   224  		Context: LogContext{
   225  			RecentEdits: []string{"a.txt", "c.txt"},
   226  			StartTime:   time.Now(),
   227  			Source:      LogSourceBootstrap,
   228  		},
   229  		Logs: []CommandLog{
   230  			{
   231  				Command:  "cat bar",
   232  				Success:  false,
   233  				Duration: time.Minute * 2,
   234  			},
   235  		},
   236  	}
   237  
   238  	err := CmdLogGroupsToFile(ws, []CommandLogGroup{groupA, groupB})
   239  	if err != nil {
   240  		f.t.Fatalf("[CmdLogGroupsToFile]: %v", err)
   241  	}
   242  
   243  	expectedContents := fmt.Sprintf("%s\n%s\n", MustJson(groupA), MustJson(groupB))
   244  	contents, err := ioutil.ReadFile(filepath.Join(kWatDirName, fnameCmdLog))
   245  	if err != nil {
   246  		f.t.Fatalf("[ioutil.ReadFile]: %v", err)
   247  	}
   248  	if string(contents) != expectedContents {
   249  		f.t.Fatalf("Expected contents '%s'; got '%s'", expectedContents, contents)
   250  	}
   251  }