Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for the test to run on windows #71

Merged
merged 2 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ func TestUpdateFeatures(t *testing.T) {
}`)

cfg := config.DefaultConfig()
cfg.Git.RepoPath = "/tmp"
dir, err := ioutil.TempDir("", "example")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May we generalize these into a shared function, something like this:

func TempDir(t *testing.T) string {
	dir, err := ioutil.TempDir(os.TempDir(), "dcdr")
	if err != nil {
		t.Fatalf("ioutil.TempDir error: %s", err.Error())
	}
	return dir
}

We can also use t.Fatalf to allow the test-suite to handle the error gracefully. Also we should prefix the directory with a related to this project: dcdr or similar.

Then from each test case we can call the parent function and not need to handle the errors:

dir := TempDir(t)
defer os.RemoveAll(dir)
cfg.Git.RepoPath = dir

if err != nil {
log.Fatal(err)
}
cfg.Git.RepoPath = dir
c, _ := New(cfg)
c.UpdateFeatures(raw)

Expand All @@ -235,14 +239,19 @@ func TestClient_UpdateFeatures_Failure(t *testing.T) {
},`)

cfg := config.DefaultConfig()
cfg.Git.RepoPath = "/tmp"
dir, err := ioutil.TempDir("", "example")
if err != nil {
log.Fatal(err)
}
cfg.Git.RepoPath = dir
c, _ := New(cfg)
c.UpdateFeatures(badUpdate)
assert.EqualValues(t, models.EmptyFeatureMap(), c.FeatureMap(), "Assert bad payload returns empty feature map")
}

func TestWatch(t *testing.T) {
p := "/tmp/decider.json"
tmpfile, err := ioutil.TempFile("", "example")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name this decider.json to be in line with what we are expecting. I know that the library assumes "" to be the default temporary directory, but maybe we can be more explicit with: os.TempDir().

It seems like it's up to us to clean up this file as well.

p := tmpfile.Name()
fm, err := models.NewFeatureMap(JSONBytes)
assert.NoError(t, err)
err = ioutil.WriteFile(p, JSONBytes, 0644)
Expand Down
9 changes: 8 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os/user"
"testing"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May we lint these imports? I know this was an error before you change but since you're here.

Copy link
Contributor Author

@fruch fruch May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the other issues, but I'm not sure what exactly you mean like this imports... care to elabrate ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it's a nitpick but the imports, the declarations for dependencies for this file, are not ordered properly:

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/vsco/dcdr/config"
	"github.com/vsco/dcdr/models"
)

Unfortunately goimports does not catch this lint error due to the spaces between the groups yet, but work is being done here: golang/go#20818

This is not necessary to merge this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ordering of imports, got it (i'll try to noticed it next time around)

"io/ioutil"

"os"

"fmt"
Expand Down Expand Up @@ -41,7 +43,12 @@ func TestDefaultConfig(t *testing.T) {
}

func TestEnvOverride(t *testing.T) {
os.Setenv(envConfigDirOverride, "/tmp/dcdr")
dir, err := ioutil.TempDir("", "example")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, as above let's use a more relatable name.

assert.NoError(t, err)

defer os.RemoveAll(dir)

os.Setenv(envConfigDirOverride, dir)
cfg := LoadConfig()

assert.Equal(t, Path(), fmt.Sprintf("%s/%s", os.Getenv(envConfigDirOverride), configFileName))
Expand Down