github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/process_in_place_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 inPlaceGlobals struct {
    32  	forceOverwrite bool
    33  	verbose        bool
    34  }
    35  
    36  func setupInPlaceGlobals(
    37  	chk *sztest.Chk, override inPlaceGlobals,
    38  ) {
    39  	chk.T().Helper()
    40  	setupTest(chk, true, false, override.forceOverwrite, override.verbose)
    41  }
    42  
    43  func setupInPlaceDirs(makeTarget bool) error {
    44  	const fName = "README_SHORT.md"
    45  
    46  	var (
    47  		err   error
    48  		tFile string
    49  		fData []byte
    50  	)
    51  
    52  	if makeTarget {
    53  		fData, err = os.ReadFile(filepath.Join(example1Path, fName))
    54  		if err == nil {
    55  			tFile = filepath.Join(outputDir, fName)
    56  			err = os.WriteFile(tFile, fData, fs.FileMode(defaultPerm))
    57  		}
    58  	}
    59  
    60  	return err
    61  }
    62  
    63  func getInPlaceFiles() (string, []string, []string, error) {
    64  	const fName = "README_SHORT.md"
    65  
    66  	var (
    67  		targetPath string
    68  		err        error
    69  		gotBytes   []byte
    70  		wntBytes   []byte
    71  	)
    72  
    73  	targetPath = filepath.Join(outputDir, fName)
    74  
    75  	gotBytes, err = os.ReadFile(targetPath) //nolint:gosec // Ok.
    76  	if err == nil {
    77  		wntBytes, err = os.ReadFile(example1Path + fName)
    78  	}
    79  
    80  	if err != nil {
    81  		return "", nil, nil, err
    82  	}
    83  
    84  	return targetPath,
    85  		strings.Split(string(gotBytes), "\n"),
    86  		strings.Split(string(wntBytes), "\n"),
    87  		nil
    88  }
    89  
    90  func Test_ProcessInPlace_NoTargetNoForceNoVerbose(t *testing.T) {
    91  	chk := sztest.CaptureLogAndStdout(t)
    92  	defer chk.Release()
    93  
    94  	setupInPlaceGlobals(
    95  		chk, inPlaceGlobals{forceOverwrite: false, verbose: false},
    96  	)
    97  	chk.NoErr(setupInPlaceDirs(false))
    98  
    99  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   100  
   101  	_, got, wnt, err := getInPlaceFiles()
   102  	chk.NoErr(err)
   103  	chk.StrSlice(got, wnt)
   104  
   105  	chk.Log()
   106  
   107  	chk.Stdout()
   108  }
   109  
   110  func Test_ProcessInPlace_NoTargetForceNoVerbose(t *testing.T) {
   111  	chk := sztest.CaptureLogAndStdout(t)
   112  	defer chk.Release()
   113  
   114  	setupInPlaceGlobals(
   115  		chk, inPlaceGlobals{forceOverwrite: true, verbose: false},
   116  	)
   117  	chk.NoErr(setupInPlaceDirs(false))
   118  
   119  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   120  
   121  	_, got, wnt, err := getInPlaceFiles()
   122  	chk.NoErr(err)
   123  	chk.StrSlice(got, wnt)
   124  
   125  	chk.Log()
   126  
   127  	chk.Stdout()
   128  }
   129  
   130  func Test_ProcessInPlace_NoTargetNoForceVerbose(t *testing.T) {
   131  	chk := sztest.CaptureLogAndStdout(t)
   132  	defer chk.Release()
   133  
   134  	setupInPlaceGlobals(
   135  		chk, inPlaceGlobals{forceOverwrite: false, verbose: true},
   136  	)
   137  	chk.NoErr(setupInPlaceDirs(false))
   138  
   139  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   140  
   141  	tFile, got, wnt, err := getInPlaceFiles()
   142  	chk.NoErr(err)
   143  	chk.StrSlice(got, wnt)
   144  
   145  	chk.Log(
   146  		"Expanding "+example1Path+"README_SHORT.md <inPlace> to: "+tFile,
   147  		"getInfo(\"package\")",
   148  	)
   149  
   150  	chk.Stdout()
   151  }
   152  
   153  func Test_ProcessInPlace_NoTargetForceVerbose(t *testing.T) {
   154  	chk := sztest.CaptureLogAndStdout(t)
   155  	defer chk.Release()
   156  
   157  	setupInPlaceGlobals(
   158  		chk, inPlaceGlobals{forceOverwrite: true, verbose: true},
   159  	)
   160  	chk.NoErr(setupInPlaceDirs(false))
   161  
   162  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   163  
   164  	tFile, got, wnt, err := getInPlaceFiles()
   165  	chk.NoErr(err)
   166  	chk.StrSlice(got, wnt)
   167  
   168  	chk.Log(
   169  		"Expanding "+example1Path+"README_SHORT.md <inPlace> to: "+tFile,
   170  		"getInfo(\"package\")",
   171  	)
   172  
   173  	chk.Stdout()
   174  }
   175  
   176  func Test_ProcessInPlace_CancelOverwriteForceNoVerbose(t *testing.T) {
   177  	chk := sztest.CaptureLogAndStdout(t)
   178  	defer chk.Release()
   179  
   180  	setupInPlaceGlobals(
   181  		chk, inPlaceGlobals{forceOverwrite: true, verbose: false},
   182  	)
   183  	chk.NoErr(setupInPlaceDirs(true))
   184  
   185  	chk.SetStdinData("N\n")
   186  
   187  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   188  
   189  	_, got, wnt, err := getInPlaceFiles()
   190  	chk.NoErr(err)
   191  	chk.StrSlice(got, wnt)
   192  
   193  	chk.Log()
   194  
   195  	chk.Stdout()
   196  }
   197  
   198  func Test_ProcessInPlace_CancelOverwriteForceVerbose(t *testing.T) {
   199  	chk := sztest.CaptureLogAndStdout(t)
   200  	defer chk.Release()
   201  
   202  	setupInPlaceGlobals(
   203  		chk, inPlaceGlobals{forceOverwrite: true, verbose: true},
   204  	)
   205  	chk.NoErr(setupInPlaceDirs(true))
   206  
   207  	chk.SetStdinData("N\n")
   208  
   209  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   210  
   211  	tFile, got, wnt, err := getInPlaceFiles()
   212  	chk.NoErr(err)
   213  	chk.StrSlice(got, wnt)
   214  
   215  	chk.Log(
   216  		"Expanding "+example1Path+"README_SHORT.md <inPlace> to: "+tFile,
   217  		"getInfo(\"package\")",
   218  	)
   219  
   220  	chk.Stdout()
   221  }
   222  
   223  func Test_ProcessInPlace_OverwriteForceNoVerbose(t *testing.T) {
   224  	chk := sztest.CaptureLogAndStdout(t)
   225  	defer chk.Release()
   226  
   227  	setupInPlaceGlobals(
   228  		chk, inPlaceGlobals{forceOverwrite: true, verbose: false},
   229  	)
   230  	chk.NoErr(setupInPlaceDirs(true))
   231  
   232  	chk.SetStdinData("Y\n")
   233  
   234  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   235  
   236  	_, got, wnt, err := getInPlaceFiles()
   237  	chk.NoErr(err)
   238  	chk.StrSlice(got, wnt)
   239  
   240  	chk.Log()
   241  
   242  	chk.Stdout()
   243  }
   244  
   245  func Test_ProcessInPlace_OverwriteForceVerbose(t *testing.T) {
   246  	chk := sztest.CaptureLogAndStdout(t)
   247  	defer chk.Release()
   248  
   249  	setupInPlaceGlobals(
   250  		chk, inPlaceGlobals{forceOverwrite: true, verbose: true},
   251  	)
   252  	chk.NoErr(setupInPlaceDirs(true))
   253  
   254  	chk.SetStdinData("Y\n")
   255  
   256  	chk.NoErr(replaceMDInPlace(example1Path + "README_SHORT.md"))
   257  
   258  	tFile, got, wnt, err := getInPlaceFiles()
   259  	chk.NoErr(err)
   260  	chk.StrSlice(got, wnt)
   261  
   262  	chk.Log(
   263  		"Expanding "+example1Path+"README_SHORT.md <inPlace> to: "+tFile,
   264  		"getInfo(\"package\")",
   265  	)
   266  
   267  	chk.Stdout()
   268  }