github.com/goradd/got@v1.1.1/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/goradd/gofile/pkg/sys"
    13  	"github.com/goradd/got/internal/got"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  // This file runs the tests found in the test directory. It is set up so that code coverage can be checked as well.
    18  // Doing this is a little tricky, since got generates go code that then gets compiled and run again. Each part of the
    19  // process may generate errors. We test the process from end to end, but to do code coverage, we must directly access
    20  // the main file as part of the test.
    21  func TestGot(t *testing.T) {
    22  	// args is a global in the main package just for testing
    23  	testPath := filepath.Join(`./internal`, `testdata`)
    24  	runnerPath := filepath.Join(testPath, "runner", "runner.go")
    25  	comparePath := filepath.Join(testPath, "compare")
    26  	expectedPath := filepath.Join(testPath, "expected")
    27  	cmd := "go run " + runnerPath + " " + comparePath
    28  	curDir, _ := os.Getwd()
    29  
    30  	resetTemplates()
    31  
    32  	args = "-t got -i -o github.com/goradd/got/internal/testdata/template -I github.com/goradd/got/internal/testdata/src/inc2:github.com/goradd/got/internal/testdata/src/inc:github.com/goradd/got/internal/testdata/src/inc/testInclude4.inc -d github.com/goradd/got/internal/testdata/src"
    33  
    34  	main()
    35  
    36  	// main seems to be changing working dir
    37  	_ = os.Chdir(curDir)
    38  
    39  	if _, err := sys.ExecuteShellCommand(cmd); err != nil {
    40  		if e, ok := err.(*exec.Error); ok {
    41  			_, _ = fmt.Fprintln(os.Stderr, "error testing runner.go :"+e.Error()) // perhaps goimports is not installed?
    42  			os.Exit(1)
    43  		} else if err2, ok2 := err.(*exec.ExitError); ok2 {
    44  			// Likely a syntax error in the resulting file
    45  			_, _ = fmt.Fprintln(os.Stderr, string(err2.Stderr))
    46  			os.Exit(1)
    47  		}
    48  	}
    49  
    50  	// compare the outputs and report differences
    51  
    52  	files, _ := filepath.Glob(expectedPath + string(os.PathSeparator) + "*.out")
    53  
    54  	for _, file := range files {
    55  		compare, _ := os.ReadFile(file)
    56  		if expected, err := os.ReadFile(filepath.Join(comparePath, filepath.Base(file))); err != nil {
    57  			t.Error(err)
    58  		} else {
    59  			expected = bytes.Replace(expected, []byte("\r\n"), []byte("\n"), -1)
    60  			compare = bytes.Replace(compare, []byte("\r\n"), []byte("\n"), -1)
    61  			if bytes.Compare(expected, compare) != 0 {
    62  				t.Errorf("File %s is not a match.", filepath.Base(file))
    63  			}
    64  		}
    65  	}
    66  }
    67  
    68  // TestRecursiveGot is an alternate test for testing some of the command line options.
    69  func TestRecursiveGot(t *testing.T) {
    70  	// args is a global in the main package just for testing
    71  	outPath1 := filepath.Join(`./internal`, `testdata`, `src`, `recurse`)
    72  	outPath2 := filepath.Join(outPath1, `rdir`)
    73  	curDir, _ := os.Getwd()
    74  
    75  	resetTemplates()
    76  
    77  	var b bytes.Buffer
    78  	got.OutWriter = &b
    79  
    80  	err := got.Run("",
    81  		"got",
    82  		false,
    83  		"",
    84  		"github.com/goradd/got/internal/testdata/src/recurse",
    85  		nil,
    86  		true,
    87  		true,
    88  		false)
    89  	assert.NoError(t, err)
    90  
    91  	// main seems to be changing working dir
    92  	_ = os.Chdir(curDir)
    93  
    94  	// verify outputs were created
    95  
    96  	files1, _ := filepath.Glob(outPath1 + string(os.PathSeparator) + "*.go")
    97  	files2, _ := filepath.Glob(outPath2 + string(os.PathSeparator) + "*.go")
    98  
    99  	assert.Len(t, files1, 1)
   100  	assert.Equal(t, "r1.tpl.go", filepath.Base(files1[0]))
   101  	assert.Len(t, files2, 1)
   102  	assert.Equal(t, "r2.tpl.go", filepath.Base(files2[0]))
   103  	assert.True(t, strings.HasPrefix(b.String(), "Processing"))
   104  
   105  	// Running this again shows that files were not processed
   106  	b.Reset()
   107  	err = got.Run("",
   108  		"got",
   109  		false,
   110  		"",
   111  		"github.com/goradd/got/internal/testdata/src/recurse",
   112  		nil,
   113  		true,
   114  		true,
   115  		false)
   116  	assert.NoError(t, err)
   117  	assert.False(t, strings.HasPrefix(b.String(), "Processing"))
   118  
   119  	// Running it again with force on shows that files were processed
   120  	b.Reset()
   121  	err = got.Run("",
   122  		"got",
   123  		false,
   124  		"",
   125  		"github.com/goradd/got/internal/testdata/src/recurse",
   126  		nil,
   127  		true,
   128  		true,
   129  		true)
   130  	assert.NoError(t, err)
   131  	assert.True(t, strings.HasPrefix(b.String(), "Processing"))
   132  
   133  	resetTemplates()
   134  }
   135  
   136  func Test_badFlags1(t *testing.T) {
   137  	resetTemplates()
   138  
   139  	err := got.Run("./internal/testdata/template",
   140  		"",
   141  		false,
   142  		"",
   143  		"",
   144  		nil,
   145  		false,
   146  		true,
   147  		true)
   148  	assert.Error(t, err)
   149  }
   150  
   151  func Test_badIncludeFail(t *testing.T) {
   152  	resetTemplates()
   153  
   154  	err := got.Run("./internal/testdata/template",
   155  		"",
   156  		false,
   157  		"",
   158  		"",
   159  		[]string{"./internal/testdata/src/failureTests/badInclude.tpl.got"},
   160  		false,
   161  		false,
   162  		true)
   163  	assert.Error(t, err)
   164  }
   165  
   166  func Test_badInclude2Fail(t *testing.T) {
   167  	resetTemplates()
   168  
   169  	err := got.Run("./internal/testdata/template",
   170  		"",
   171  		true,
   172  		"",
   173  		"",
   174  		[]string{"./internal/testdata/src/failureTests/badInclude2.tpl.got"},
   175  		false,
   176  		false,
   177  		true)
   178  
   179  	assert.Error(t, err)
   180  }
   181  
   182  func Test_tooManyParams(t *testing.T) {
   183  	resetTemplates()
   184  
   185  	err := got.Run("./internal/testdata/template",
   186  		"",
   187  		false,
   188  		"",
   189  		"",
   190  		[]string{"./internal/testdata/src/failureTests/tooManyParams.tpl.got"},
   191  		false,
   192  		false,
   193  		true)
   194  
   195  	assert.Error(t, err)
   196  }
   197  
   198  func Test_badGo2(t *testing.T) {
   199  	resetTemplates()
   200  
   201  	err := got.Run("./internal/testdata/template",
   202  		"",
   203  		true,
   204  		"",
   205  		"",
   206  		[]string{"./internal/testdata/src/failureTests/badGo.tpl.got"},
   207  		false,
   208  		false,
   209  		true)
   210  
   211  	assert.Error(t, err)
   212  }
   213  
   214  func Test_badBlock(t *testing.T) {
   215  	resetTemplates()
   216  
   217  	err := got.Run("./internal/testdata/template",
   218  		"",
   219  		true,
   220  		"",
   221  		"",
   222  		[]string{"./internal/testdata/src/failureTests/badBlock.tpl.got"},
   223  		false,
   224  		false,
   225  		true)
   226  
   227  	assert.Error(t, err)
   228  }
   229  
   230  func Test_tooManyEnds(t *testing.T) {
   231  	resetTemplates()
   232  
   233  	err := got.Run("./internal/testdata/template",
   234  		"",
   235  		true,
   236  		"",
   237  		"",
   238  		[]string{"./internal/testdata/src/failureTests/tooManyEnds.tpl.got"},
   239  		false,
   240  		false,
   241  		true)
   242  
   243  	assert.Error(t, err)
   244  }
   245  
   246  func TestInfo(t *testing.T) {
   247  	resetTemplates()
   248  
   249  	// args is a global in the main package just for testing
   250  
   251  	args = "testEmpty"
   252  
   253  	main()
   254  }
   255  
   256  func resetTemplates() {
   257  	files, _ := filepath.Glob("./internal/testdata/template/*.tpl.go")
   258  	for _, f := range files {
   259  		_ = os.Remove(f)
   260  	}
   261  
   262  	files, _ = filepath.Glob("./internal/testdata/src/recurse/*.tpl.go")
   263  	for _, f := range files {
   264  		_ = os.Remove(f)
   265  	}
   266  
   267  	files, _ = filepath.Glob("./internal/testdata/src/recurse/rdir/*.tpl.go")
   268  	for _, f := range files {
   269  		_ = os.Remove(f)
   270  	}
   271  
   272  }