github.com/coveo/gotemplate@v2.7.7+incompatible/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCli(t *testing.T) {
    14  	variableTempDir := must(ioutil.TempDir("", "gotemplate-test-variable")).(string)
    15  	defer os.RemoveAll(variableTempDir)
    16  	variableFile := path.Join(variableTempDir, "test.variables")
    17  	must(ioutil.WriteFile(variableFile, []byte("testInt = 5\ntestString = \"hello\"\ntestBool = true"), 0644))
    18  
    19  	tests := []struct {
    20  		name           string
    21  		pipe           string
    22  		args           []string
    23  		template       string
    24  		expectedCode   int
    25  		expectedResult string
    26  	}{
    27  		{
    28  			name:           "No arguments",
    29  			args:           []string{},
    30  			template:       "{{ add 3 2 }}",
    31  			expectedCode:   0,
    32  			expectedResult: "5",
    33  		},
    34  		{
    35  			name:           "Integers",
    36  			args:           []string{"--var", "test1=3", "--var", "test2=7"},
    37  			template:       "{{ $var := add .test1 .test2 }}{{ $var }}",
    38  			expectedCode:   0,
    39  			expectedResult: "10",
    40  		},
    41  		{
    42  			name:           "Import variables",
    43  			args:           []string{"--import", variableFile, "--var", "test1=3"},
    44  			template:       "{{ $var := add .test1 .testInt }}{{ $var }}",
    45  			expectedCode:   0,
    46  			expectedResult: "8",
    47  		},
    48  		{
    49  			name:           "STDIN variables",
    50  			pipe:           "testInt=3",
    51  			args:           []string{"-V-", "--var", "test2=4", "test.template"},
    52  			template:       "{{ $var := add .testInt .test2 }}{{ $var }}",
    53  			expectedCode:   0,
    54  			expectedResult: "7",
    55  		},
    56  
    57  		// Ambiguous variables
    58  		{
    59  			name:           "Variables that may look like filenames",
    60  			args:           []string{"--var", "test1=2.3.4", "--var", "test2=/var/path/literal"},
    61  			template:       "{{ print .test1 \"-\" .test2 }}",
    62  			expectedCode:   0,
    63  			expectedResult: "2.3.4-/var/path/literal",
    64  		},
    65  		{
    66  			name:           "Variable file as var",
    67  			args:           []string{"--var", "testFile=" + variableFile},
    68  			template:       "{{ print .testFile.testInt \"-\" .testFile.testString }}",
    69  			expectedCode:   0,
    70  			expectedResult: "5-hello",
    71  		},
    72  		{
    73  			name:           "Literal existing file name",
    74  			args:           []string{"--var", fmt.Sprintf("testFile='%s'", variableFile)},
    75  			template:       "{{ print .testFile }}",
    76  			expectedCode:   0,
    77  			expectedResult: variableFile,
    78  		},
    79  
    80  		// Missing source
    81  		{
    82  			name:         "Non-existing source",
    83  			args:         []string{"--source", "/path/that/does/not/exist"},
    84  			expectedCode: 1,
    85  		},
    86  		{
    87  			name:         "Non-existing source (Ignored)",
    88  			args:         []string{"--source", "/path/that/does/not/exist", "--ignore-missing-source"},
    89  			expectedCode: 0,
    90  		},
    91  		{
    92  			name:         "Non-existing source (Ignored2)",
    93  			args:         []string{"--source", "/path/that/does/not/exist", "--ignore-missing-paths"},
    94  			expectedCode: 0,
    95  		},
    96  
    97  		// Missing import
    98  		{
    99  			name:         "Non-existing import",
   100  			args:         []string{"--import", "/path/that/does/not/exist"},
   101  			expectedCode: 1,
   102  		},
   103  		{
   104  			name:         "Non-existing import (Ignored)",
   105  			args:         []string{"--import", "/path/that/does/not/exist", "--ignore-missing-import"},
   106  			expectedCode: 0,
   107  		},
   108  		{
   109  			name:         "Non-existing import (Ignored2)",
   110  			args:         []string{"--import", "/path/that/does/not/exist", "--ignore-missing-paths"},
   111  			expectedCode: 0,
   112  		},
   113  
   114  		// Errors
   115  		{
   116  			name:         "Error recovery",
   117  			args:         []string{},
   118  			template:     "{{ panic `test string` }}",
   119  			expectedCode: 1,
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			var tempDir string
   125  
   126  			// Change and revert (defer) os values
   127  			oldArgs := os.Args
   128  			defer func() { os.Args = oldArgs }()
   129  			oldCw := must(os.Getwd()).(string)
   130  			defer os.Chdir(oldCw)
   131  
   132  			if tt.pipe != "" {
   133  				content := []byte(tt.pipe)
   134  				tmpStdin := must(ioutil.TempFile("", "example")).(*os.File)
   135  				defer os.Remove(tmpStdin.Name())
   136  				must(tmpStdin.Write(content))
   137  				must(tmpStdin.Seek(0, 0))
   138  
   139  				oldStdin := os.Stdin
   140  				defer func() {
   141  					tmpStdin.Close()
   142  					os.Stdin = oldStdin
   143  				}()
   144  
   145  				os.Stdin = tmpStdin
   146  			}
   147  
   148  			if tt.template != "" {
   149  				// Create template
   150  				tempDir = must(ioutil.TempDir("", "gotemplate-test")).(string)
   151  				os.Chdir(tempDir)
   152  				defer os.RemoveAll(tempDir)
   153  				templateFile := path.Join(tempDir, "test.template")
   154  				must(ioutil.WriteFile(templateFile, []byte(tt.template), 0644))
   155  				os.Args = append([]string{"gotemplate", "--source", tempDir}, tt.args...)
   156  			} else {
   157  				os.Args = append([]string{"gotemplate"}, tt.args...)
   158  			}
   159  
   160  			// Run gotemplate
   161  			exitCode := runGotemplate()
   162  
   163  			assert.Equal(t, tt.expectedCode, exitCode, "Bad exit code")
   164  
   165  			if tt.expectedResult != "" {
   166  				generatedFile := path.Join(tempDir, "test.generated")
   167  				readGeneratedFileContent := must(ioutil.ReadFile(generatedFile)).([]byte)
   168  				assert.Equal(t, []byte(tt.expectedResult), readGeneratedFileContent, "Bad generated content")
   169  			}
   170  		})
   171  	}
   172  }