github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/process_expand_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 expandGlobals struct {
    32  	forceOverwrite bool
    33  	verbose        bool
    34  }
    35  
    36  func setupExpandGlobals(
    37  	chk *sztest.Chk, override expandGlobals,
    38  ) {
    39  	chk.T().Helper()
    40  	setupTest(chk, true, false, override.forceOverwrite, override.verbose)
    41  }
    42  
    43  func setupExpandDirs(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 getExpandFiles() (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  	gotBytes, err = os.ReadFile(targetPath) //nolint:gosec // Ok.
    75  
    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_ProcessExpand_NoTargetNoForceNoVerbose(t *testing.T) {
    91  	chk := sztest.CaptureLogAndStdout(t)
    92  	defer chk.Release()
    93  
    94  	// Clear packages from other runs.
    95  	packages = make(map[string]*packageInfo)
    96  
    97  	setupExpandGlobals(
    98  		chk, expandGlobals{forceOverwrite: false, verbose: false},
    99  	)
   100  	chk.NoErr(setupExpandDirs(false))
   101  
   102  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   103  
   104  	_, got, wnt, err := getExpandFiles()
   105  	chk.NoErr(err)
   106  	chk.StrSlice(got, wnt)
   107  
   108  	chk.Log()
   109  
   110  	chk.Stdout()
   111  }
   112  
   113  func Test_ProcessExpand_NoTargetForceNoVerbose(t *testing.T) {
   114  	chk := sztest.CaptureLogAndStdout(t)
   115  	defer chk.Release()
   116  
   117  	setupExpandGlobals(
   118  		chk, expandGlobals{forceOverwrite: true, verbose: false},
   119  	)
   120  	chk.NoErr(setupExpandDirs(false))
   121  
   122  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   123  
   124  	_, got, wnt, err := getExpandFiles()
   125  	chk.NoErr(err)
   126  	chk.StrSlice(got, wnt)
   127  
   128  	chk.Log()
   129  
   130  	chk.Stdout()
   131  }
   132  
   133  func Test_ProcessExpand_NoTargetNoForceVerbose(t *testing.T) {
   134  	chk := sztest.CaptureLogAndStdout(t)
   135  	defer chk.Release()
   136  
   137  	setupExpandGlobals(
   138  		chk, expandGlobals{forceOverwrite: false, verbose: true},
   139  	)
   140  	chk.NoErr(setupExpandDirs(false))
   141  
   142  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   143  
   144  	tFile, got, wnt, err := getExpandFiles()
   145  	chk.NoErr(err)
   146  	chk.StrSlice(got, wnt)
   147  
   148  	chk.Log(
   149  		"Expanding "+example1Path+"README_SHORT.md.gtm to: "+tFile,
   150  		"getInfo(\"package\")",
   151  	)
   152  
   153  	chk.Stdout()
   154  }
   155  
   156  func Test_ProcessExpand_NoTargetForceVerbose(t *testing.T) {
   157  	chk := sztest.CaptureLogAndStdout(t)
   158  	defer chk.Release()
   159  
   160  	setupExpandGlobals(
   161  		chk, expandGlobals{forceOverwrite: true, verbose: true},
   162  	)
   163  	chk.NoErr(setupExpandDirs(false))
   164  
   165  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   166  
   167  	tFile, got, wnt, err := getExpandFiles()
   168  	chk.NoErr(err)
   169  	chk.StrSlice(got, wnt)
   170  
   171  	chk.Log(
   172  		"Expanding "+example1Path+"README_SHORT.md.gtm to: "+tFile,
   173  		"getInfo(\"package\")",
   174  	)
   175  
   176  	chk.Stdout()
   177  }
   178  
   179  func Test_ProcessExpand_CancelOverwriteTargetForceNoVerbose(t *testing.T) {
   180  	chk := sztest.CaptureLogAndStdout(t)
   181  	defer chk.Release()
   182  
   183  	setupExpandGlobals(
   184  		chk, expandGlobals{forceOverwrite: true, verbose: false},
   185  	)
   186  	chk.NoErr(setupExpandDirs(true))
   187  
   188  	chk.SetStdinData("N\n")
   189  
   190  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   191  
   192  	_, got, wnt, err := getExpandFiles()
   193  	chk.NoErr(err)
   194  	chk.StrSlice(got, wnt)
   195  
   196  	chk.Log()
   197  
   198  	chk.Stdout()
   199  }
   200  
   201  func Test_ProcessExpand_CancelOverwriteForceVerbose(t *testing.T) {
   202  	chk := sztest.CaptureLogAndStdout(t)
   203  	defer chk.Release()
   204  
   205  	setupExpandGlobals(
   206  		chk, expandGlobals{forceOverwrite: true, verbose: true},
   207  	)
   208  	chk.NoErr(setupExpandDirs(true))
   209  
   210  	chk.SetStdinData("N\n")
   211  
   212  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   213  
   214  	tFile, got, wnt, err := getExpandFiles()
   215  	chk.NoErr(err)
   216  	chk.StrSlice(got, wnt)
   217  
   218  	chk.Log(
   219  		"Expanding "+example1Path+"README_SHORT.md.gtm to: "+tFile,
   220  		"getInfo(\"package\")",
   221  	)
   222  
   223  	chk.Stdout()
   224  }
   225  
   226  func Test_ProcessExpand_OverwriteTargetForceNoVerbose(t *testing.T) {
   227  	chk := sztest.CaptureLogAndStdout(t)
   228  	defer chk.Release()
   229  
   230  	setupExpandGlobals(
   231  		chk, expandGlobals{forceOverwrite: true, verbose: false},
   232  	)
   233  	chk.NoErr(setupExpandDirs(true))
   234  
   235  	chk.SetStdinData("Y\n")
   236  
   237  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   238  
   239  	_, got, wnt, err := getExpandFiles()
   240  	chk.NoErr(err)
   241  	chk.StrSlice(got, wnt)
   242  
   243  	chk.Log()
   244  
   245  	chk.Stdout()
   246  }
   247  
   248  func Test_ProcessExpand_OverwriteForceVerbose(t *testing.T) {
   249  	chk := sztest.CaptureLogAndStdout(t)
   250  	defer chk.Release()
   251  
   252  	setupExpandGlobals(
   253  		chk, expandGlobals{forceOverwrite: true, verbose: true},
   254  	)
   255  	chk.NoErr(setupExpandDirs(true))
   256  
   257  	chk.SetStdinData("Y\n")
   258  
   259  	chk.NoErr(expandMD(example1Path + "README_SHORT.md.gtm"))
   260  
   261  	wFile, got, wnt, err := getExpandFiles()
   262  	chk.NoErr(err)
   263  	chk.StrSlice(got, wnt)
   264  
   265  	chk.Log(
   266  		"Expanding "+example1Path+"README_SHORT.md.gtm to: "+wFile,
   267  		"getInfo(\"package\")",
   268  	)
   269  
   270  	chk.Stdout()
   271  }