github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/process_clean_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 "io/fs" 23 "os" 24 "path/filepath" 25 "strings" 26 "testing" 27 28 "github.com/dancsecs/sztest" 29 ) 30 31 type cleanGlobals struct { 32 forceOverwrite bool 33 verbose bool 34 } 35 36 func getCleanedFiles() (string, []string, []string, error) { 37 const fName = "README.md.gtm" 38 39 var ( 40 targetPath string 41 err error 42 gotBytes []byte 43 wntBytes []byte 44 ) 45 46 targetPath = filepath.Join(outputDir, fName) 47 48 gotBytes, err = os.ReadFile(targetPath) //nolint:gosec // Ok. 49 if err == nil { 50 wntBytes, err = os.ReadFile(example1Path + fName) 51 } 52 53 if err != nil { 54 return "", nil, nil, err 55 } 56 57 return targetPath, 58 strings.Split(string(gotBytes), "\n"), 59 strings.Split(string(wntBytes), "\n"), 60 nil 61 } 62 63 func setupCleanDirs(makeTarget bool) error { 64 const fName = "README.md.gtm" 65 66 var err error 67 68 if makeTarget { 69 tFile := filepath.Join(outputDir, fName) 70 err = os.WriteFile(tFile, nil, fs.FileMode(defaultPerm)) 71 } 72 73 return err 74 } 75 76 func setupCleanGlobals( 77 chk *sztest.Chk, override cleanGlobals, 78 ) { 79 chk.T().Helper() 80 setupTest(chk, true, false, override.forceOverwrite, override.verbose) 81 } 82 83 // +-------------------------------------------------------+ 84 // | Flag possibilities for type of test. | 85 // +------------+-----------+------------------+-----------+ 86 // | cleanOnly | replace | forceOverwrite | verbose | 87 // +------------+-----------+------------------+-----------+ 88 // | false | false | false | false | 89 // | false | true | false | false | 90 // | true | false | false | false | 91 // +------------+-----------+------------------+-----------+ 92 // | false | false | false | true | 93 // | false | true | false | true | 94 // | true | false | false | true | 95 // +------------+-----------+------------------+-----------+ 96 // | false | false | true | false | 97 // | false | true | true | false | 98 // | true | false | true | false | 99 // +------------+-----------+------------------+-----------+ 100 // | false | false | true | true | 101 // | false | true | true | true | 102 // | true | false | true | true | 103 // +------------+-----------+------------------+-----------+. 104 105 func Test_ProcessClean_NoTargetNoForceNoVerbose(t *testing.T) { 106 chk := sztest.CaptureLog(t) 107 defer chk.Release() 108 109 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: false, verbose: false}) 110 chk.NoErr(setupCleanDirs(false)) 111 112 chk.NoErr(cleanMD(example1Path + "README.md")) 113 114 _, got, wnt, err := getCleanedFiles() 115 chk.NoErr(err) 116 chk.StrSlice(got, wnt) 117 118 chk.Log() 119 } 120 121 func Test_ProcessClean_NoTargetForceNoVerbose(t *testing.T) { 122 chk := sztest.CaptureLog(t) 123 defer chk.Release() 124 125 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: true, verbose: false}) 126 chk.NoErr(setupCleanDirs(false)) 127 128 chk.NoErr(cleanMD(example1Path + "README.md")) 129 130 _, got, wnt, err := getCleanedFiles() 131 chk.NoErr(err) 132 chk.StrSlice(got, wnt) 133 134 chk.Log() 135 } 136 137 func Test_ProcessClean_NoTargetNoForceVerbose(t *testing.T) { 138 chk := sztest.CaptureLog(t) 139 defer chk.Release() 140 141 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: false, verbose: true}) 142 chk.NoErr(setupCleanDirs(false)) 143 144 chk.NoErr(cleanMD(example1Path + "README.md")) 145 146 tPath, got, wnt, err := getCleanedFiles() 147 chk.NoErr(err) 148 chk.StrSlice(got, wnt) 149 150 chk.Log("Cleaning " + example1Path + "README.md to: " + tPath) 151 } 152 153 func Test_ProcessClean_NoTargetForceVerbose(t *testing.T) { 154 chk := sztest.CaptureLog(t) 155 defer chk.Release() 156 157 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: true, verbose: true}) 158 chk.NoErr(setupCleanDirs(false)) 159 160 chk.NoErr(cleanMD(example1Path + "README.md")) 161 162 tPath, got, wnt, err := getCleanedFiles() 163 chk.NoErr(err) 164 chk.StrSlice(got, wnt) 165 166 chk.Log("Cleaning " + example1Path + "README.md to: " + tPath) 167 } 168 169 func Test_ProcessClean_CancelOverwriteNoForceNoVerbose(t *testing.T) { 170 chk := sztest.CaptureLogAndStdout(t) 171 defer chk.Release() 172 173 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: false, verbose: false}) 174 chk.NoErr(setupCleanDirs(true)) 175 176 chk.SetStdinData("N\n") 177 178 // Run command expecting the overwrite to be cancelled. 179 chk.NoErr(cleanMD(example1Path + "README.md")) 180 181 tFile := filepath.Join(outputDir, "README.md.gtm") 182 chk.Stdout("Confirm overwrite of " + tFile + " (Y to overwrite)? " + 183 "overwrite cancelled", 184 ) 185 186 chk.Log() 187 } 188 189 func Test_ProcessClean_CancelOverwriteForceNoVerbose(t *testing.T) { 190 chk := sztest.CaptureLogAndStdout(t) 191 defer chk.Release() 192 193 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: true, verbose: false}) 194 chk.NoErr(setupCleanDirs(true)) 195 196 // Run command expecting the overwrite to be cancelled. 197 chk.NoErr(cleanMD(example1Path + "README.md")) 198 199 chk.Stdout() 200 201 chk.Log() 202 } 203 204 func Test_ProcessClean_CancelOverwriteNoForceVerbose(t *testing.T) { 205 chk := sztest.CaptureLogAndStdout(t) 206 defer chk.Release() 207 208 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: false, verbose: true}) 209 chk.NoErr(setupCleanDirs(true)) 210 211 chk.SetStdinData("N\n") 212 213 // Run command expecting the overwrite to be cancelled. 214 chk.NoErr(cleanMD(example1Path + "README.md")) 215 216 tFile := filepath.Join(outputDir, "README.md.gtm") 217 chk.Stdout("Confirm overwrite of " + tFile + " (Y to overwrite)? " + 218 "overwrite cancelled", 219 ) 220 221 chk.Log("Cleaning " + example1Path + "README.md to: " + tFile) 222 } 223 224 func Test_ProcessClean_CancelOverwriteForceVerbose(t *testing.T) { 225 chk := sztest.CaptureLogAndStdout(t) 226 defer chk.Release() 227 228 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: true, verbose: true}) 229 chk.NoErr(setupCleanDirs(true)) 230 231 // Run command expecting the overwrite to be cancelled. 232 chk.NoErr(cleanMD(example1Path + "README.md")) 233 234 chk.Stdout() 235 236 tFile := filepath.Join(outputDir, "README.md.gtm") 237 chk.Log("Cleaning " + example1Path + "README.md to: " + tFile) 238 } 239 240 func Test_ProcessClean_OverwriteNoForceNoVerbose(t *testing.T) { 241 chk := sztest.CaptureLogAndStdout(t) 242 defer chk.Release() 243 244 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: false, verbose: false}) 245 chk.NoErr(setupCleanDirs(true)) 246 247 chk.SetStdinData("Y\n") 248 249 // Run command expecting the overwrite to be cancelled. 250 chk.NoErr(cleanMD(example1Path + "README.md")) 251 252 _, got, wnt, err := getCleanedFiles() 253 chk.NoErr(err) 254 chk.StrSlice(got, wnt) 255 256 tFile := filepath.Join(outputDir, "README.md.gtm") 257 chk.Stdout("Confirm overwrite of " + tFile + " (Y to overwrite)?\\s") 258 259 chk.Log() 260 } 261 262 func Test_ProcessClean_OverwriteForceNoVerbose(t *testing.T) { 263 chk := sztest.CaptureLogAndStdout(t) 264 defer chk.Release() 265 266 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: true, verbose: false}) 267 chk.NoErr(setupCleanDirs(true)) 268 269 // Run command expecting the overwrite to be cancelled. 270 chk.NoErr(cleanMD(example1Path + "README.md")) 271 272 _, got, wnt, err := getCleanedFiles() 273 chk.NoErr(err) 274 chk.StrSlice(got, wnt) 275 276 chk.Stdout() 277 278 chk.Log() 279 } 280 281 func Test_ProcessClean_OverwriteNoForceVerbose(t *testing.T) { 282 chk := sztest.CaptureLogAndStdout(t) 283 defer chk.Release() 284 285 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: false, verbose: true}) 286 chk.NoErr(setupCleanDirs(true)) 287 288 chk.SetStdinData("Y\n") 289 290 // Run command expecting the overwrite to be cancelled. 291 chk.NoErr(cleanMD(example1Path + "README.md")) 292 293 _, got, wnt, err := getCleanedFiles() 294 chk.NoErr(err) 295 chk.StrSlice(got, wnt) 296 297 tFile := filepath.Join(outputDir, "README.md.gtm") 298 chk.Stdout("Confirm overwrite of " + tFile + " (Y to overwrite)?\\s") 299 300 chk.Log("Cleaning " + example1Path + "README.md to: " + tFile) 301 } 302 303 func Test_ProcessClean_OverwriteForceVerbose(t *testing.T) { 304 chk := sztest.CaptureLogAndStdout(t) 305 defer chk.Release() 306 307 setupCleanGlobals(chk, cleanGlobals{forceOverwrite: true, verbose: true}) 308 chk.NoErr(setupCleanDirs(true)) 309 310 // Run command expecting the overwrite to be cancelled. 311 chk.NoErr(cleanMD(example1Path + "README.md")) 312 313 _, got, wnt, err := getCleanedFiles() 314 chk.NoErr(err) 315 chk.StrSlice(got, wnt) 316 317 chk.Stdout() 318 319 tFile := filepath.Join(outputDir, "README.md.gtm") 320 chk.Log("Cleaning " + example1Path + "README.md to: " + tFile) 321 }