github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/config_test.go (about)

     1  package plural_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/urfave/cli"
    11  
    12  	"github.com/pluralsh/plural-cli/cmd/plural"
    13  	"github.com/pluralsh/plural-cli/pkg/config"
    14  	pluraltest "github.com/pluralsh/plural-cli/pkg/test"
    15  )
    16  
    17  func TestPluralConfigCommand(t *testing.T) {
    18  	tests := []struct {
    19  		name             string
    20  		args             []string
    21  		createConfig     bool
    22  		expectedResponse string
    23  	}{
    24  		{
    25  			name:             `test "config read" command when config file doesn't exists'`,
    26  			args:             []string{plural.ApplicationName, "config", "read"},
    27  			expectedResponse: "apiVersion: platform.plural.sh/v1alpha1\nkind: Config\nmetadata: null\nspec:\n  email: \"\"\n  token: \"\"\n  consoleToken: \"\"\n  namespacePrefix: \"\"\n  endpoint: \"\"\n  lockProfile: \"\"\n  reportErrors: false\n",
    28  		},
    29  		{
    30  			name:             `test "config read" command with default test config'`,
    31  			args:             []string{plural.ApplicationName, "config", "read"},
    32  			createConfig:     true,
    33  			expectedResponse: "apiVersion: platform.plural.sh/v1alpha1\nkind: Config\nmetadata: null\nspec:\n  email: test@plural.sh\n  token: abc\n  consoleToken: \"\"\n  namespacePrefix: test\n  endpoint: http://example.com\n  lockProfile: abc\n  reportErrors: false\n",
    34  		},
    35  	}
    36  	for _, test := range tests {
    37  		t.Run(test.name, func(t *testing.T) {
    38  			// create temp environment
    39  			currentDir, err := os.Getwd()
    40  			assert.NoError(t, err)
    41  			dir, err := os.MkdirTemp("", "config")
    42  			assert.NoError(t, err)
    43  			defer func(path, currentDir string) {
    44  				_ = os.RemoveAll(path)
    45  				_ = os.Chdir(currentDir)
    46  			}(dir, currentDir)
    47  
    48  			os.Setenv("HOME", dir)
    49  			defer os.Unsetenv("HOME")
    50  
    51  			if test.createConfig {
    52  				defaultConfig := pluraltest.GenDefaultConfig()
    53  				err := defaultConfig.Save(config.ConfigName)
    54  				assert.NoError(t, err)
    55  			}
    56  
    57  			app := plural.CreateNewApp(nil)
    58  			app.HelpName = plural.ApplicationName
    59  			os.Args = test.args
    60  			res, err := captureStdout(app, os.Args)
    61  			assert.NoError(t, err)
    62  
    63  			assert.Equal(t, test.expectedResponse, res)
    64  		})
    65  	}
    66  }
    67  
    68  func captureStdout(app *cli.App, arg []string) (string, error) {
    69  	old := os.Stdout
    70  	r, w, _ := os.Pipe()
    71  	os.Stdout = w
    72  
    73  	err := app.Run(arg)
    74  	if err != nil {
    75  		return "", err
    76  	}
    77  
    78  	w.Close()
    79  	os.Stdout = old
    80  
    81  	var buf bytes.Buffer
    82  	if _, err := io.Copy(&buf, r); err != nil {
    83  		return "", err
    84  	}
    85  	return buf.String(), nil
    86  }