Skip to content

Commit 142cd5b

Browse files
authored
Fix failing UserId test (#636)
* Fix error check. * Isolate test. Withouth this the test will pollute the user's XDG_CONFIG_HOME, potentially overwriting valid data. * Minor testify usage cleanup. * Use testify.NoError instead of testify.Nil. * Fix error check in test. * Fix directory permission.
1 parent 71a10b0 commit 142cd5b

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

iterative/utils/analytics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func GroupId() (string, error) {
161161

162162
func readId(path string) (string, error) {
163163
file, err := os.Open(path)
164-
if file != nil {
164+
if err != nil {
165165
return "", err
166166
}
167167
defer file.Close()
@@ -190,7 +190,7 @@ func readId(path string) (string, error) {
190190
}
191191

192192
func writeId(path string, id string) error {
193-
if err := os.MkdirAll(filepath.Dir(path), 0644); err != nil {
193+
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
194194
return err
195195
}
196196

iterative/utils/analytics_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
)
1414

1515
func TestVersion(t *testing.T) {
16-
assert.Equal(t, Version, "0.0.0")
16+
assert.Equal(t, "0.0.0", Version)
1717
}
1818

1919
func TestTerraformVersion(t *testing.T) {
2020
ver, _ := TerraformVersion()
21-
assert.Equal(t, strings.HasPrefix(ver, "v"), true)
21+
assert.True(t, strings.HasPrefix(ver, "v"))
2222
}
2323

2424
func TestSystemInfo(t *testing.T) {
@@ -28,6 +28,8 @@ func TestSystemInfo(t *testing.T) {
2828
}
2929

3030
func TestUserId(t *testing.T) {
31+
tempHome := t.TempDir()
32+
os.Setenv("XDG_CONFIG_HOME", tempHome)
3133
old := appdirs.UserConfigDir("dvc/user_id", "iterative", "", false)
3234
new := appdirs.UserConfigDir("iterative/telemetry", "", "", false)
3335

@@ -36,18 +38,19 @@ func TestUserId(t *testing.T) {
3638
"user_id": userId,
3739
}
3840
json, _ := json.MarshalIndent(data, "", " ")
41+
err := os.MkdirAll(filepath.Dir(old), 0755)
42+
assert.NoError(t, err)
43+
err = ioutil.WriteFile(old, json, 0644)
44+
assert.NoError(t, err)
3945

40-
_ = os.MkdirAll(filepath.Dir(old), 0644)
41-
_ = ioutil.WriteFile(old, json, 0644)
42-
43-
id, _ := UserId()
44-
assert.Equal(t, len(id) == 36, true)
46+
id, err := UserId()
47+
assert.NoError(t, err)
48+
assert.Len(t, id, 36)
4549

4650
if !IsCI() {
47-
assert.Equal(t, userId == id, true)
48-
51+
assert.Equal(t, userId, id)
4952
_, err := os.Stat(new)
50-
assert.Equal(t, !os.IsNotExist(err), true)
53+
assert.False(t, os.IsNotExist(err))
5154
}
5255
}
5356

0 commit comments

Comments
 (0)