Don't set a default filename for ConfigFile

With a default filename tests will leave a file in the working directory
that is never cleaned up.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2018-03-05 19:59:53 -05:00
parent a0b19f0ec2
commit 7c8b5708eb
3 changed files with 35 additions and 29 deletions

View File

@ -5,14 +5,14 @@ import (
"fmt" "fmt"
"testing" "testing"
"golang.org/x/net/context"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
registrytypes "github.com/docker/docker/api/types/registry" registrytypes "github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"
"github.com/gotestyourself/gotestyourself/fs"
"golang.org/x/net/context"
) )
const userErr = "userunknownError" const userErr = "userunknownError"
@ -131,22 +131,27 @@ func TestRunLogin(t *testing.T) {
expectedErr: testAuthErrMsg, expectedErr: testAuthErrMsg,
}, },
} }
for _, tc := range testCases { for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
tmpFile := fs.NewFile(t, "test-run-login")
defer tmpFile.Remove()
cli := test.NewFakeCli(&fakeClient{}) cli := test.NewFakeCli(&fakeClient{})
errBuf := new(bytes.Buffer) configfile := cli.ConfigFile()
cli.SetErr(errBuf) configfile.Filename = tmpFile.Path()
if tc.inputStoredCred != nil { if tc.inputStoredCred != nil {
cred := *tc.inputStoredCred cred := *tc.inputStoredCred
cli.ConfigFile().GetCredentialsStore(cred.ServerAddress).Store(cred) configfile.GetCredentialsStore(cred.ServerAddress).Store(cred)
} }
loginErr := runLogin(cli, tc.inputLoginOption) loginErr := runLogin(cli, tc.inputLoginOption)
if tc.expectedErr != "" { if tc.expectedErr != "" {
assert.Check(t, is.Equal(tc.expectedErr, loginErr.Error())) assert.Error(t, loginErr, tc.expectedErr)
} else { return
assert.Check(t, loginErr) }
savedCred, credStoreErr := cli.ConfigFile().GetCredentialsStore(tc.inputStoredCred.ServerAddress).Get(tc.inputStoredCred.ServerAddress) assert.NilError(t, loginErr)
savedCred, credStoreErr := configfile.GetCredentialsStore(tc.inputStoredCred.ServerAddress).Get(tc.inputStoredCred.ServerAddress)
assert.Check(t, credStoreErr) assert.Check(t, credStoreErr)
assert.Check(t, is.DeepEqual(tc.expectedSavedCred, savedCred)) assert.DeepEqual(t, tc.expectedSavedCred, savedCred)
} })
} }
} }

View File

@ -10,7 +10,6 @@ import (
"github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/credentials" "github.com/docker/cli/cli/config/credentials"
"github.com/docker/cli/internal/test/testutil"
"github.com/docker/docker/pkg/homedir" "github.com/docker/docker/pkg/homedir"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"
@ -78,7 +77,7 @@ func TestEmptyFile(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
_, err = Load(tmpHome) _, err = Load(tmpHome)
testutil.ErrorContains(t, err, "EOF") assert.ErrorContains(t, err, "EOF")
} }
func TestEmptyJSON(t *testing.T) { func TestEmptyJSON(t *testing.T) {
@ -122,7 +121,7 @@ email`: "Invalid auth configuration file",
assert.NilError(t, err) assert.NilError(t, err)
_, err = Load(tmpHome) _, err = Load(tmpHome)
testutil.ErrorContains(t, err, expectedError) assert.ErrorContains(t, err, expectedError)
} }
} }
@ -469,7 +468,7 @@ func TestJSONSaveWithNoFile(t *testing.T) {
config, err := LoadFromReader(strings.NewReader(js)) config, err := LoadFromReader(strings.NewReader(js))
assert.NilError(t, err) assert.NilError(t, err)
err = config.Save() err = config.Save()
testutil.ErrorContains(t, err, "with empty filename") assert.ErrorContains(t, err, "with empty filename")
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome, err := ioutil.TempDir("", "config-test")
assert.NilError(t, err) assert.NilError(t, err)
@ -500,7 +499,7 @@ func TestLegacyJSONSaveWithNoFile(t *testing.T) {
config, err := LegacyLoadFromReader(strings.NewReader(js)) config, err := LegacyLoadFromReader(strings.NewReader(js))
assert.NilError(t, err) assert.NilError(t, err)
err = config.Save() err = config.Save()
testutil.ErrorContains(t, err, "with empty filename") assert.ErrorContains(t, err, "with empty filename")
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome, err := ioutil.TempDir("", "config-test")
assert.NilError(t, err) assert.NilError(t, err)

View File

@ -45,7 +45,9 @@ func NewFakeCli(client client.APIClient) *FakeCli {
outBuffer: outBuffer, outBuffer: outBuffer,
err: errBuffer, err: errBuffer,
in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))), in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))),
configfile: configfile.New("configfile"), // Use an empty string for filename so that tests don't create configfiles
// Set cli.ConfigFile().Filename to a tempfile to support Save.
configfile: configfile.New(""),
} }
} }