github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/main_test.go (about) 1 /* 2 Golang To Github Markdown Utility: gotomd 3 Copyright (C) 2023, 2024 Leslie Dancsecs 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19 package main 20 21 import ( 22 "os" 23 "path/filepath" 24 "strings" 25 "testing" 26 27 "github.com/dancsecs/sztest" 28 ) 29 30 const ( 31 example1 = "example1" 32 example1Path = "." + string(os.PathSeparator) + 33 example1 + string(os.PathSeparator) 34 35 example2 = "example2" 36 example2Path = "." + string(os.PathSeparator) + 37 example2 + string(os.PathSeparator) 38 ) 39 40 func Test_Example1ExpandTargetOverwriteDirVerbose(t *testing.T) { 41 chk := sztest.CaptureLogAndStdout(t) 42 defer chk.Release() 43 44 dir := chk.CreateTmpDir() 45 chk.NoErr( 46 setup( 47 dir, 48 "README.md", 49 "README.md.gtm", 50 "example1_test.go", 51 "example1.go", 52 ), 53 ) 54 55 rName := filepath.Join(dir, "README.md.gtm") 56 tName := filepath.Join(dir, "README.md") 57 58 chk.SetupArgsAndFlags([]string{ 59 "programName", 60 "-v", 61 "-z", 62 dir, 63 }) 64 65 chk.SetStdinData("Y\n") 66 67 chk.NoErr(os.Truncate(tName, 2)) 68 69 // Run command expecting the overwrite to be cancelled. 70 main() 71 72 got, wnt, err := getTestFiles(dir, "README.md") 73 chk.NoErr(err) 74 chk.StrSlice(got, wnt) 75 76 chk.Stdout( 77 "filesToProcess: "+rName, 78 "Confirm overwrite of "+tName+" (Y to overwrite)?\\s", 79 ) 80 81 chk.Log( 82 "Expanding "+rName+" to: "+tName, 83 "Loading Package info for: .", 84 "getInfo(\"package\")", 85 "getInfo(\"TimesTwo\")", 86 "getInfo(\"TimesThree\")", 87 "getInfo(\"InterfaceType\")", 88 "getInfo(\"StructureType\")", 89 "getInfo(\"TimesTwo\")", 90 "getInfo(\"TimesTwo\")", 91 "getInfo(\"TimesThree\")", 92 "getInfo(\"TimesTwo\")", 93 "getInfo(\"TimesTwo\")", 94 "getInfo(\"TimesThree\")", 95 "getInfo(\"TimesTwo\")", 96 "getInfo(\"TimesTwo\")", 97 "getInfo(\"TimesThree\")", 98 ) 99 } 100 101 //////////// 102 103 func Test_Example1ReplaceNoTarget(t *testing.T) { 104 chk := sztest.CaptureNothing(t) 105 defer chk.Release() 106 107 dir := chk.CreateTmpDir() 108 chk.NoErr(setup(dir, "example1_test.go", "example1.go")) 109 110 fName := filepath.Join(dir, "README.md") 111 112 chk.SetupArgsAndFlags([]string{ 113 "programName", 114 "-r", 115 fName, 116 }) 117 118 chk.Panic( 119 main, 120 "stat "+filepath.Join(dir, "README.md")+": no such file or directory", 121 ) 122 123 _, _, err := getTestFiles(dir, "README.md") 124 chk.Err( 125 err, 126 "open "+fName+": no such file or directory", 127 ) 128 } 129 130 func Test_Example1ReplaceTargetCancel(t *testing.T) { 131 chk := sztest.CaptureLogAndStdout(t) 132 defer chk.Release() 133 134 dir := chk.CreateTmpDir() 135 chk.NoErr( 136 setup(dir, "README.md", "example1_test.go", "example1.go"), 137 ) 138 139 fName := filepath.Join(dir, "README.md") 140 chk.SetupArgsAndFlags([]string{ 141 "programName", 142 "-v", 143 "-r", 144 fName, 145 }) 146 147 chk.SetStdinData("N\n") 148 149 // Run command expecting the overwrite to be cancelled. 150 main() 151 152 chk.Stdout( 153 "filesToProcess: "+fName, 154 "Confirm overwrite of "+fName+" (Y to overwrite)? "+ 155 "overwrite cancelled") 156 157 chk.Log( 158 "", 159 "Expanding "+fName+" <inPlace> to: "+fName, 160 "Loading Package info for: .", 161 "getInfo(\"package\")", 162 "getInfo(\"TimesTwo\")", 163 "getInfo(\"TimesThree\")", 164 "getInfo(\"InterfaceType\")", 165 "getInfo(\"StructureType\")", 166 "getInfo(\"TimesTwo\")", 167 "getInfo(\"TimesTwo\")", 168 "getInfo(\"TimesThree\")", 169 "getInfo(\"TimesTwo\")", 170 "getInfo(\"TimesTwo\")", 171 "getInfo(\"TimesThree\")", 172 "getInfo(\"TimesTwo\")", 173 "getInfo(\"TimesTwo\")", 174 "getInfo(\"TimesThree\")", 175 ) 176 } 177 178 func Test_Example1ReplaceTargetOverwrite(t *testing.T) { 179 chk := sztest.CaptureStdout(t) 180 defer chk.Release() 181 182 dir := chk.CreateTmpDir() 183 chk.NoErr( 184 setup(dir, "README.md", "example1_test.go", "example1.go"), 185 ) 186 187 fName := filepath.Join(dir, "README.md") 188 chk.SetupArgsAndFlags([]string{ 189 "programName", 190 "-r", 191 "-z", 192 fName, 193 }) 194 195 chk.SetStdinData("Y\n") 196 197 // Run command expecting the overwrite to be cancelled. 198 main() 199 200 got, wnt, err := getTestFiles(dir, "README.md") 201 chk.NoErr(err) 202 203 wnt[0] = strings.ReplaceAll(wnt[0], "**DO NOT MODIFY** ", "") 204 chk.StrSlice(got, wnt) 205 206 _, _, err = getTestFiles(dir, "README.md.gtm") 207 chk.Err( 208 err, 209 "open "+fName+".gtm: no such file or directory", 210 ) 211 212 chk.Stdout("Confirm overwrite of " + fName + " (Y to overwrite)?\\s") 213 } 214 215 func Test_Example1ReplaceTargetOverwriteDir(t *testing.T) { 216 chk := sztest.CaptureStdout(t) 217 defer chk.Release() 218 219 dir := chk.CreateTmpDir() 220 chk.NoErr( 221 setup(dir, "README.md", "example1_test.go", "example1.go"), 222 ) 223 224 chk.SetupArgsAndFlags([]string{ 225 "programName", 226 "-r", 227 "-z", 228 dir, 229 }) 230 231 chk.SetStdinData("Y\n") 232 233 // Run command expecting the overwrite to be cancelled. 234 main() 235 236 got, wnt, err := getTestFiles(dir, "README.md") 237 chk.NoErr(err) 238 239 wnt[0] = strings.ReplaceAll(wnt[0], "**DO NOT MODIFY** ", "") 240 chk.StrSlice(got, wnt) 241 242 _, _, err = getTestFiles(dir, "README.md.gtm") 243 chk.Err( 244 err, 245 "open "+dir+"/README.md.gtm: no such file or directory", 246 ) 247 248 chk.Stdout( 249 "Confirm overwrite of " + dir + "/README.md (Y to overwrite)?\\s", 250 ) 251 } 252 253 func Test_Example1ReplaceTargetOverwriteDirFromClean(t *testing.T) { 254 chk := sztest.CaptureStdout(t) 255 defer chk.Release() 256 257 dir := chk.CreateTmpDir() 258 chk.NoErr( 259 setup(dir, "README.md.gtm", "example1_test.go", "example1.go"), 260 ) 261 262 chk.NoErr( 263 os.Rename( 264 filepath.Join(dir, "README.md.gtm"), 265 filepath.Join(dir, "README.md"), 266 ), 267 ) 268 269 chk.SetupArgsAndFlags([]string{ 270 "programName", 271 "-r", 272 "-z", 273 dir, 274 }) 275 276 chk.SetStdinData("Y\n") 277 278 // Run command expecting the overwrite to be cancelled. 279 main() 280 281 got, wnt, err := getTestFiles(dir, "README.md") 282 chk.NoErr(err) 283 284 wnt[0] = strings.ReplaceAll(wnt[0], "**DO NOT MODIFY** ", "") 285 chk.StrSlice(got, wnt) 286 287 _, _, err = getTestFiles(dir, "README.md.gtm") 288 chk.Err( 289 err, 290 "open "+dir+"/README.md.gtm: no such file or directory", 291 ) 292 293 chk.Stdout( 294 "Confirm overwrite of " + dir + "/README.md (Y to overwrite)?\\s", 295 ) 296 } 297 298 func Test_Example1ReplaceTargetOverwriteDirVerbose(t *testing.T) { 299 chk := sztest.CaptureLogAndStdout(t) 300 defer chk.Release() 301 302 dir := chk.CreateTmpDir() 303 chk.NoErr( 304 setup(dir, "README.md", "example1_test.go", "example1.go"), 305 ) 306 307 chk.SetupArgsAndFlags([]string{ 308 "programName", 309 "-v", 310 "-r", 311 "-z", 312 dir, 313 }) 314 315 chk.SetStdinData("Y\n") 316 317 // Run command expecting the overwrite to be cancelled. 318 main() 319 320 got, wnt, err := getTestFiles(dir, "README.md") 321 chk.NoErr(err) 322 323 wnt[0] = strings.ReplaceAll(wnt[0], "**DO NOT MODIFY** ", "") 324 chk.StrSlice(got, wnt) 325 326 pName := filepath.Join(dir, "README.md") 327 chk.Stdout( 328 "filesToProcess: "+pName, 329 "Confirm overwrite of "+pName+" (Y to overwrite)?\\s", 330 ) 331 332 chk.Log( 333 "Expanding "+pName+" <inPlace> to: "+pName, 334 "Loading Package info for: .", 335 "getInfo(\"package\")", 336 "getInfo(\"TimesTwo\")", 337 "getInfo(\"TimesThree\")", 338 "getInfo(\"InterfaceType\")", 339 "getInfo(\"StructureType\")", 340 "getInfo(\"TimesTwo\")", 341 "getInfo(\"TimesTwo\")", 342 "getInfo(\"TimesThree\")", 343 "getInfo(\"TimesTwo\")", 344 "getInfo(\"TimesTwo\")", 345 "getInfo(\"TimesThree\")", 346 "getInfo(\"TimesTwo\")", 347 "getInfo(\"TimesTwo\")", 348 "getInfo(\"TimesThree\")", 349 ) 350 } 351 352 func setup(dir string, files ...string) error { 353 const ext = ".example" 354 355 var ( 356 err error 357 fileBytes []byte 358 ) 359 360 files = append(files, "go.mod"+ext, "go.sum"+ext) 361 for i, mi := 0, len(files); i < mi && err == nil; i++ { 362 fileBytes, err = os.ReadFile(filepath.Join("example1", files[i])) 363 if err == nil { 364 err = os.WriteFile( 365 filepath.Join(dir, strings.TrimSuffix(files[i], ext)), 366 fileBytes, 367 os.FileMode(defaultPerm), 368 ) 369 } 370 } 371 372 return err 373 } 374 375 //nolint:gosec // Ok. 376 func getTestFiles(dir, fName string) ([]string, []string, error) { 377 gotBytes, err := os.ReadFile(filepath.Join(dir, fName)) 378 if err != nil { 379 return nil, nil, err 380 } 381 382 wntBytes, err := os.ReadFile(filepath.Join("example1", fName)) 383 if err != nil { 384 return nil, nil, err 385 } 386 387 return strings.Split(string(gotBytes), "\n"), 388 strings.Split(string(wntBytes), "\n"), 389 nil 390 } 391 392 func Test_Example1CleanNoTargetAlternateOut(t *testing.T) { 393 chk := sztest.CaptureLogAndStdout(t) 394 defer chk.Release() 395 396 dir := chk.CreateTmpDir() 397 altDir := chk.CreateTmpSubDir("altDir") 398 399 chk.NoErr(setup(dir, "README.md", "example1_test.go", "example1.go")) 400 401 chk.SetupArgsAndFlags([]string{ 402 "programName", 403 "-v", 404 "-l", 405 "-c", 406 "-o", altDir, 407 filepath.Join(dir, "README.md"), 408 }) 409 410 // Nor Run the main function with no -f arg requiring confirmation 411 main() 412 413 got, wnt, err := getTestFiles(altDir, "README.md.gtm") 414 chk.NoErr(err) 415 chk.StrSlice(got, wnt) 416 417 pName := filepath.Join(dir, "README.md") 418 chk.Stdout( 419 license + 420 "filesToProcess: " + pName + "\n", 421 ) 422 423 rFile := filepath.Join(dir, "README.md") 424 wFile := filepath.Join(altDir, "README.md.gtm") 425 chk.Log("Cleaning " + rFile + " to: " + wFile) 426 }