github.com/windmilleng/wat@v0.0.2-0.20180626175338-9349b638e250/cli/wat/utils_test.go (about) 1 package wat 2 3 import ( 4 "context" 5 "encoding/json" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "reflect" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/windmilleng/wat/cli/analytics" 15 "github.com/windmilleng/wat/os/ospath" 16 "github.com/windmilleng/wat/os/temp" 17 ) 18 19 type watFixture struct { 20 t *testing.T 21 ctx context.Context 22 root *temp.TempDir 23 origWd string // For resetting wd at teardown 24 a analytics.Analytics 25 } 26 27 func newWatFixture(t *testing.T) *watFixture { 28 wd, err := ospath.Realwd() 29 if err != nil { 30 t.Fatalf("Error getting wd: %v", err) 31 } 32 33 root, err := temp.NewDir(t.Name()) 34 if err != nil { 35 t.Fatalf("Error making temp dir: %v", err) 36 } 37 38 err = os.Chdir(root.Path()) 39 if err != nil { 40 t.Fatalf("Error setting wd to temp dir: %v", err) 41 } 42 43 return &watFixture{ 44 t: t, 45 ctx: context.Background(), 46 root: root, 47 origWd: wd, 48 a: &analytics.MemoryAnalytics{}, 49 } 50 } 51 52 func (f *watFixture) tearDown() { 53 err := f.root.TearDown() 54 if err != nil { 55 f.t.Fatalf("Error tearing down temp dir: %v", err) 56 } 57 58 err = os.Chdir(f.origWd) 59 if err != nil { 60 f.t.Fatalf("Error resetting wd: %v", err) 61 } 62 } 63 64 // watInit adds a .wat directory in the fixture's temp dir. 65 func (f *watFixture) watInit() WatWorkspace { 66 err := os.Mkdir(filepath.Join(f.root.Path(), kWatDirName), os.FileMode(0777)) 67 if err != nil { 68 f.t.Fatalf("Error making dir: %v", err) 69 } 70 return WatWorkspace{root: f.root.Path(), a: analytics.NewMemoryAnalytics()} 71 } 72 73 func (f *watFixture) writeFile(name string) { 74 f.write(name, "hello world") 75 } 76 77 func (f *watFixture) write(name, contents string) { 78 rtPath := f.root.Path() 79 path := filepath.Join(rtPath, name) 80 81 err := os.MkdirAll(filepath.Dir(path), os.FileMode(0777)) 82 if err != nil { 83 f.t.Fatalf("Error making dir: %v", err) 84 } 85 86 err = ioutil.WriteFile(path, []byte(contents), os.FileMode(0777)) 87 if err != nil { 88 f.t.Fatalf("Error writing file: %v", err) 89 } 90 } 91 92 func (f *watFixture) assertExpectedFiles(expected map[string]bool, actual []fileInfo) { 93 expectedCount := 0 94 for _, v := range expected { 95 if v { 96 expectedCount++ 97 } 98 } 99 100 if len(actual) != expectedCount { 101 f.t.Fatalf("expected %d files returned, got %d", expectedCount, len(actual)) 102 } 103 for _, file := range actual { 104 expected, ok := expected[file.name] 105 if !ok { 106 f.t.Fatalf("Returned file that wasn't even in our map?? %s", file.name) 107 } 108 if !expected { 109 f.t.Fatalf("File %s should have been ignored", file.name) 110 } 111 } 112 } 113 114 func assertCommandLogs(t *testing.T, expected, actual []CommandLog) { 115 if len(actual) != len(expected) { 116 t.Fatalf("Expected %d logs but got %d", len(expected), len(actual)) 117 } 118 for i, l := range expected { 119 if actual[i].Command != l.Command || actual[i].Success != l.Success { 120 t.Fatalf("Expected log %+v, got %+v (NOTE: not comparing duration)", l, actual[i]) 121 } 122 } 123 } 124 125 func assertContainsStrings(t *testing.T, expectedStrs []string, output, outputName string) { 126 for _, s := range expectedStrs { 127 if !strings.Contains(output, s) { 128 t.Fatalf("%s does not contain expected string: %q", outputName, s) 129 } 130 } 131 132 } 133 134 func assertCmdLogFileContents(t *testing.T, expected CommandLogGroup) { 135 logPath := filepath.Join(kWatDirName, fnameCmdLog) 136 logContents, err := ioutil.ReadFile(logPath) 137 if err != nil { 138 t.Fatalf("[ioutil.ReadFile] %v", err) 139 } 140 actual := CommandLogGroup{} 141 err = json.Unmarshal(logContents, &actual) 142 if err != nil { 143 t.Fatalf("[ioutil.ReadFile] %v", err) 144 } 145 146 assertCommandLogGroupsEqual(t, expected, actual) 147 } 148 149 func assertCommandLogGroupsEqual(t *testing.T, expected CommandLogGroup, actual CommandLogGroup) { 150 assertLogContext(t, expected.Context, actual.Context) 151 152 if len(expected.Logs) != len(actual.Logs) { 153 t.Fatalf("CommandLogGroups not equal: expected %d logs, got %d", 154 len(expected.Logs), len(actual.Logs)) 155 } 156 for i, eLog := range expected.Logs { 157 assertCommandLog(t, eLog, actual.Logs[i]) 158 } 159 } 160 161 func assertCommandLog(t *testing.T, expected CommandLog, actual CommandLog) { 162 // zero out Duration cuz we can't reliably compare it 163 cleanActual := actual 164 cleanActual.Duration = 0 165 166 if !reflect.DeepEqual(expected, cleanActual) { 167 t.Fatalf("Logs not equal: expected %+v, got %+v (NOTE: not comparing duration)", expected, actual) 168 } 169 } 170 171 func assertLogContext(t *testing.T, expected LogContext, actual LogContext) { 172 // we don't care about testing StartTime 173 cleanExpected := actual 174 cleanExpected.StartTime = time.Unix(0, 0) 175 cleanActual := actual 176 cleanActual.StartTime = time.Unix(0, 0) 177 178 if !reflect.DeepEqual(cleanExpected, cleanActual) { 179 t.Fatalf("Log contexts not equal: expected %+v, got %+v", cleanExpected, cleanActual) 180 } 181 } 182 183 func assertCommandList(t *testing.T, exp, act CommandList) { 184 // We don't care about Timestamp, it gets wonky between json un/marshal 185 cleanExp := exp 186 cleanExp.Timestamp = time.Unix(0, 0) 187 cleanAct := act 188 cleanAct.Timestamp = time.Unix(0, 0) 189 190 if !reflect.DeepEqual(cleanExp, cleanAct) { 191 t.Fatalf("Expected CommandList %v; got %v (NOTE: not comparing timestamps)", exp, act) 192 } 193 194 }