...

Source file src/github.com/pelletier/go-toml/v2/internal/cli/cli_test.go

Documentation: github.com/pelletier/go-toml/v2/internal/cli

     1  package cli
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/pelletier/go-toml/v2"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func processMain(args []string, input io.Reader, stdout, stderr io.Writer, f ConvertFn) int {
    19  	p := Program{Fn: f}
    20  	return p.main(args, input, stdout, stderr)
    21  }
    22  
    23  func TestProcessMainStdin(t *testing.T) {
    24  	stdout := new(bytes.Buffer)
    25  	stderr := new(bytes.Buffer)
    26  	input := strings.NewReader("this is the input")
    27  
    28  	exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error {
    29  		return nil
    30  	})
    31  
    32  	assert.Equal(t, 0, exit)
    33  	assert.Empty(t, stdout.String())
    34  	assert.Empty(t, stderr.String())
    35  }
    36  
    37  func TestProcessMainStdinErr(t *testing.T) {
    38  	stdout := new(bytes.Buffer)
    39  	stderr := new(bytes.Buffer)
    40  	input := strings.NewReader("this is the input")
    41  
    42  	exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error {
    43  		return fmt.Errorf("something bad")
    44  	})
    45  
    46  	assert.Equal(t, -1, exit)
    47  	assert.Empty(t, stdout.String())
    48  	assert.NotEmpty(t, stderr.String())
    49  }
    50  
    51  func TestProcessMainStdinDecodeErr(t *testing.T) {
    52  	stdout := new(bytes.Buffer)
    53  	stderr := new(bytes.Buffer)
    54  	input := strings.NewReader("this is the input")
    55  
    56  	exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error {
    57  		var v interface{}
    58  		return toml.Unmarshal([]byte(`qwe = 001`), &v)
    59  	})
    60  
    61  	assert.Equal(t, -1, exit)
    62  	assert.Empty(t, stdout.String())
    63  	assert.Contains(t, stderr.String(), "error occurred at")
    64  }
    65  
    66  func TestProcessMainFileExists(t *testing.T) {
    67  	tmpfile, err := ioutil.TempFile("", "example")
    68  	require.NoError(t, err)
    69  	defer os.Remove(tmpfile.Name())
    70  	_, err = tmpfile.Write([]byte(`some data`))
    71  	require.NoError(t, err)
    72  
    73  	stdout := new(bytes.Buffer)
    74  	stderr := new(bytes.Buffer)
    75  
    76  	exit := processMain([]string{tmpfile.Name()}, nil, stdout, stderr, func(r io.Reader, w io.Writer) error {
    77  		return nil
    78  	})
    79  
    80  	assert.Equal(t, 0, exit)
    81  	assert.Empty(t, stdout.String())
    82  	assert.Empty(t, stderr.String())
    83  }
    84  
    85  func TestProcessMainFileDoesNotExist(t *testing.T) {
    86  	stdout := new(bytes.Buffer)
    87  	stderr := new(bytes.Buffer)
    88  
    89  	exit := processMain([]string{"/lets/hope/this/does/not/exist"}, nil, stdout, stderr, func(r io.Reader, w io.Writer) error {
    90  		return nil
    91  	})
    92  
    93  	assert.Equal(t, -1, exit)
    94  	assert.Empty(t, stdout.String())
    95  	assert.NotEmpty(t, stderr.String())
    96  }
    97  
    98  func TestProcessMainFilesInPlace(t *testing.T) {
    99  	dir, err := ioutil.TempDir("", "")
   100  	require.NoError(t, err)
   101  	defer os.RemoveAll(dir)
   102  
   103  	path1 := path.Join(dir, "file1")
   104  	path2 := path.Join(dir, "file2")
   105  
   106  	err = ioutil.WriteFile(path1, []byte("content 1"), 0600)
   107  	require.NoError(t, err)
   108  	err = ioutil.WriteFile(path2, []byte("content 2"), 0600)
   109  	require.NoError(t, err)
   110  
   111  	p := Program{
   112  		Fn:      dummyFileFn,
   113  		Inplace: true,
   114  	}
   115  
   116  	exit := p.main([]string{path1, path2}, os.Stdin, os.Stdout, os.Stderr)
   117  
   118  	require.Equal(t, 0, exit)
   119  
   120  	v1, err := ioutil.ReadFile(path1)
   121  	require.NoError(t, err)
   122  	require.Equal(t, "1", string(v1))
   123  
   124  	v2, err := ioutil.ReadFile(path2)
   125  	require.NoError(t, err)
   126  	require.Equal(t, "2", string(v2))
   127  }
   128  
   129  func TestProcessMainFilesInPlaceErrRead(t *testing.T) {
   130  	p := Program{
   131  		Fn:      dummyFileFn,
   132  		Inplace: true,
   133  	}
   134  
   135  	exit := p.main([]string{"/this/path/is/invalid"}, os.Stdin, os.Stdout, os.Stderr)
   136  
   137  	require.Equal(t, -1, exit)
   138  }
   139  
   140  func TestProcessMainFilesInPlaceFailFn(t *testing.T) {
   141  	dir, err := ioutil.TempDir("", "")
   142  	require.NoError(t, err)
   143  	defer os.RemoveAll(dir)
   144  
   145  	path1 := path.Join(dir, "file1")
   146  
   147  	err = ioutil.WriteFile(path1, []byte("content 1"), 0600)
   148  	require.NoError(t, err)
   149  
   150  	p := Program{
   151  		Fn:      func(io.Reader, io.Writer) error { return fmt.Errorf("oh no") },
   152  		Inplace: true,
   153  	}
   154  
   155  	exit := p.main([]string{path1}, os.Stdin, os.Stdout, os.Stderr)
   156  
   157  	require.Equal(t, -1, exit)
   158  
   159  	v1, err := ioutil.ReadFile(path1)
   160  	require.NoError(t, err)
   161  	require.Equal(t, "content 1", string(v1))
   162  }
   163  
   164  func dummyFileFn(r io.Reader, w io.Writer) error {
   165  	b, err := ioutil.ReadAll(r)
   166  	if err != nil {
   167  		return err
   168  	}
   169  	v := strings.SplitN(string(b), " ", 2)[1]
   170  	_, err = w.Write([]byte(v))
   171  	return err
   172  }
   173  

View as plain text