Skip to content

Commit

Permalink
Break out test into subtests. (#540)
Browse files Browse the repository at this point in the history
This allow the test tools to show individuals reports.

* Breaks out TestConfigure into subtests.

* Breaks out TestPathType into subtests.

* Breaks out TestNewRequestSetsDefaultHeaders into subtests.

* Breaks out TestIsUpToDate into subtests.

* Breaks out TestUpgrade into subtests.

* Breaks out TestVersionUpdateCheck into subtests.

* Breaks out TestQuestion into subtests.

* Breaks out TestSelectionPick into subtests.

* Breaks out TestTrackIgnoreString into subtests.

* Breaks out TestNormalizeWorkspace into subtests.

* Breaks out TestSolutionString into subtests

There is currently no description for the tests: only in and expected out values,

* Breaks out TestNewTransmission into subtests.

* Breaks out TestLocateErrors into subtests.

* Breaks out TestDownload into subtests.

* Refactors TestDownload.
  • Loading branch information
FabienTregan authored and nywilken committed Jun 21, 2018
1 parent eb19ecd commit 818bc53
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 206 deletions.
21 changes: 12 additions & 9 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ func TestNewRequestSetsDefaultHeaders(t *testing.T) {

UserAgent = "BogusAgent"

tests := []struct {
testCases := []struct {
desc string
client *Client
auth string
contentType string
}{
{
// Use defaults.
desc: "User defaults",
client: &Client{},
auth: "",
contentType: "application/json",
},
{
// Override defaults.
desc: "Override defaults",
client: &Client{
UserConfig: &config.UserConfig{Token: "abc123"},
ContentType: "bogus",
Expand All @@ -40,12 +41,14 @@ func TestNewRequestSetsDefaultHeaders(t *testing.T) {
},
}

for _, test := range tests {
req, err := test.client.NewRequest("GET", ts.URL, nil)
assert.NoError(t, err)
assert.Equal(t, "BogusAgent", req.Header.Get("User-Agent"))
assert.Equal(t, test.contentType, req.Header.Get("Content-Type"))
assert.Equal(t, test.auth, req.Header.Get("Authorization"))
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
req, err := tc.client.NewRequest("GET", ts.URL, nil)
assert.NoError(t, err)
assert.Equal(t, "BogusAgent", req.Header.Get("User-Agent"))
assert.Equal(t, tc.contentType, req.Header.Get("Content-Type"))
assert.Equal(t, tc.auth, req.Header.Get("Authorization"))
})
}
}

Expand Down
29 changes: 16 additions & 13 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,49 @@ import (
)

func TestIsUpToDate(t *testing.T) {
tests := []struct {
testCases := []struct {
desc string
cliVersion string
releaseTag string
ok bool
}{
{
// It returns false for versions less than release.
desc: "It returns false for versions less than release.",
cliVersion: "1.0.0",
releaseTag: "v1.0.1",
ok: false,
},
{
// It returns false for pre-release versions of release.
desc: "It returns false for pre-release versions of release.",
cliVersion: "1.0.1-alpha.1",
releaseTag: "v1.0.1",
ok: false,
},
{
// It returns true for versions equal to release.
desc: "It returns true for versions equal to release.",
cliVersion: "2.0.1",
releaseTag: "v2.0.1",
ok: true,
},
{
// It returns true for versions greater than release.
desc: "It returns true for versions greater than release.",
cliVersion: "2.0.2",
releaseTag: "v2.0.1",
ok: true,
},
}

for _, test := range tests {
c := &CLI{
Version: test.cliVersion,
LatestRelease: &Release{TagName: test.releaseTag},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
c := &CLI{
Version: tc.cliVersion,
LatestRelease: &Release{TagName: tc.releaseTag},
}

ok, err := c.IsUpToDate()
assert.NoError(t, err)
assert.Equal(t, test.ok, ok, test.cliVersion)
ok, err := c.IsUpToDate()
assert.NoError(t, err)
assert.Equal(t, tc.ok, ok, tc.cliVersion)
})
}
}

Expand Down
59 changes: 34 additions & 25 deletions cmd/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,85 @@ import (
"github.com/stretchr/testify/assert"
)

type testCase struct {
desc string
args []string
existingUsrCfg *config.UserConfig
expectedUsrCfg *config.UserConfig
existingAPICfg *config.APIConfig
expectedAPICfg *config.APIConfig
}

func TestConfigure(t *testing.T) {
tests := []struct {
desc string
args []string
existingUsrCfg *config.UserConfig
expectedUsrCfg *config.UserConfig
existingAPICfg *config.APIConfig
expectedAPICfg *config.APIConfig
}{
{
testCases := []testCase{
testCase{
desc: "It writes the flags when there is no config file.",
args: []string{"fakeapp", "configure", "--token", "a", "--workspace", "/a", "--api", "http://example.com"},
existingUsrCfg: nil,
expectedUsrCfg: &config.UserConfig{Token: "a", Workspace: "/a"},
existingAPICfg: nil,
expectedAPICfg: &config.APIConfig{BaseURL: "http://example.com"},
},
{
testCase{
desc: "It overwrites the flags in the config file.",
args: []string{"fakeapp", "configure", "--token", "b", "--workspace", "/b", "--api", "http://example.com/v2"},
existingUsrCfg: &config.UserConfig{Token: "token-b", Workspace: "/workspace-b"},
expectedUsrCfg: &config.UserConfig{Token: "b", Workspace: "/b"},
existingAPICfg: &config.APIConfig{BaseURL: "http://example.com/v1"},
expectedAPICfg: &config.APIConfig{BaseURL: "http://example.com/v2"},
},
{
testCase{
desc: "It overwrites the flags that are passed, without losing the ones that are not.",
args: []string{"fakeapp", "configure", "--token", "c"},
existingUsrCfg: &config.UserConfig{Token: "token-c", Workspace: "/workspace-c"},
expectedUsrCfg: &config.UserConfig{Token: "c", Workspace: "/workspace-c"},
},
{
testCase{
desc: "It gets the default API base URL.",
args: []string{"fakeapp", "configure"},
existingAPICfg: &config.APIConfig{},
expectedAPICfg: &config.APIConfig{BaseURL: "https://v2.exercism.io/api/v1"},
},
}

for _, test := range tests {
for _, tc := range testCases {
t.Run(tc.desc, makeTest(tc))
}
}

func makeTest(tc testCase) func(*testing.T) {

return func(t *testing.T) {
cmdTest := &CommandTest{
Cmd: configureCmd,
InitFn: initConfigureCmd,
Args: test.args,
Args: tc.args,
}
cmdTest.Setup(t)
defer cmdTest.Teardown(t)

if test.existingUsrCfg != nil {
if tc.existingUsrCfg != nil {
// Write a fake config.
cfg := config.NewEmptyUserConfig()
cfg.Token = test.existingUsrCfg.Token
cfg.Workspace = test.existingUsrCfg.Workspace
cfg.Token = tc.existingUsrCfg.Token
cfg.Workspace = tc.existingUsrCfg.Workspace
err := cfg.Write()
assert.NoError(t, err, test.desc)
assert.NoError(t, err, tc.desc)
}

cmdTest.App.Execute()

if test.expectedUsrCfg != nil {
if tc.expectedUsrCfg != nil {
usrCfg, err := config.NewUserConfig()
assert.NoError(t, err, test.desc)
assert.Equal(t, test.expectedUsrCfg.Token, usrCfg.Token, test.desc)
assert.Equal(t, test.expectedUsrCfg.Workspace, usrCfg.Workspace, test.desc)
assert.NoError(t, err, tc.desc)
assert.Equal(t, tc.expectedUsrCfg.Token, usrCfg.Token, tc.desc)
assert.Equal(t, tc.expectedUsrCfg.Workspace, usrCfg.Workspace, tc.desc)
}

if test.expectedAPICfg != nil {
if tc.expectedAPICfg != nil {
apiCfg, err := config.NewAPIConfig()
assert.NoError(t, err, test.desc)
assert.Equal(t, test.expectedAPICfg.BaseURL, apiCfg.BaseURL, test.desc)
assert.NoError(t, err, tc.desc)
assert.Equal(t, tc.expectedAPICfg.BaseURL, apiCfg.BaseURL, tc.desc)
}
}
}
Loading

0 comments on commit 818bc53

Please sign in to comment.