github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/process_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  	"testing"
    25  
    26  	"github.com/dancsecs/sztest"
    27  )
    28  
    29  func setupTest(
    30  	chk *sztest.Chk, tCleanOnly, tReplace, tForceOverwrite, tVerbose bool,
    31  ) {
    32  	chk.T().Helper()
    33  
    34  	origOutputDir := outputDir
    35  	origCWD, err := os.Getwd()
    36  	origCleanOnly := cleanOnly
    37  	origReplace := replace
    38  	origForceOverwrite := forceOverwrite
    39  	origVerbose := verbose
    40  
    41  	cleanOnly = tCleanOnly
    42  	replace = tReplace
    43  	forceOverwrite = tForceOverwrite
    44  	verbose = tVerbose
    45  
    46  	if chk.NoErr(err) {
    47  		outputDir = chk.CreateTmpDir()
    48  		chk.PushPostReleaseFunc(func() error {
    49  			outputDir = origOutputDir
    50  			cleanOnly = origCleanOnly
    51  			replace = origReplace
    52  			forceOverwrite = origForceOverwrite
    53  			verbose = origVerbose
    54  
    55  			return os.Chdir(origCWD)
    56  		})
    57  	}
    58  }
    59  
    60  // +-------------------------------------------------------+
    61  // | Flag possibilities for type of test.                  |
    62  // +------------+-----------+------------------+-----------+
    63  // | cleanOnly  |  replace  |  forceOverwrite  |  verbose  |
    64  // +------------+-----------+------------------+-----------+
    65  // |  false     |   false   |     false        |   false   |
    66  // |  false     |   true    |     false        |   false   |
    67  // |  true      |   false   |     false        |   false   |
    68  // +------------+-----------+------------------+-----------+
    69  // |  false     |   false   |     false        |   true    |
    70  // |  false     |   true    |     false        |   true    |
    71  // |  true      |   false   |     false        |   true    |
    72  // +------------+-----------+------------------+-----------+
    73  // |  false     |   false   |     true         |   false   |
    74  // |  false     |   true    |     true         |   false   |
    75  // |  true      |   false   |     true         |   false   |
    76  // +------------+-----------+------------------+-----------+
    77  // |  false     |   false   |     true         |   true    |
    78  // |  false     |   true    |     true         |   true    |
    79  // |  true      |   false   |     true         |   true    |
    80  // +------------+-----------+------------------+-----------+.
    81  
    82  //  func Test_Process_Stop(t *testing.T) {
    83  //  	t.Fatal("STOPPING")
    84  //  }
    85  
    86  func Test_Process_ConfirmOverwrite(t *testing.T) {
    87  	chk := sztest.CaptureStdout(t)
    88  	defer chk.Release()
    89  
    90  	data := "The data."
    91  
    92  	dir := chk.CreateTmpDir()
    93  
    94  	ok, err := confirmOverwrite(filepath.Join(dir, "noFile.dat"), data)
    95  	chk.NoErr(err)
    96  	chk.True(ok)
    97  
    98  	fPath := chk.CreateTmpFile([]byte(data))
    99  	ok, err = confirmOverwrite(fPath, data)
   100  	chk.NoErr(err)
   101  	chk.False(ok)
   102  
   103  	chk.Stdout(
   104  		"No change: " + fPath,
   105  	)
   106  }