Skip to content

Commit 84f1a12

Browse files
committed
fix some unit tests
1 parent 8983ef9 commit 84f1a12

File tree

5 files changed

+73
-22
lines changed

5 files changed

+73
-22
lines changed

cli/autostart_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestAutostart(t *testing.T) {
104104
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
105105
)
106106

107-
cmd, root := clitest.New(t, "autostart", "set", "doesnotexist")
107+
cmd, root := clitest.New(t, "autostart", "set", "doesnotexist", "09:30AM")
108108
clitest.SetupConfig(t, client, root)
109109

110110
err := cmd.Execute()

coderd/util/tz/tz_darwin.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
const etcLocaltime = "/etc/localtime"
13-
const zoneInfoPath = "/var/db/timezone/zoneinfo"
13+
const zoneInfoPath = "/var/db/timezone/zoneinfo/"
1414

1515
// TimezoneIANA attempts to determine the local timezone in IANA format.
1616
// If the TZ environment variable is set, this is used.
@@ -25,20 +25,21 @@ func TimezoneIANA() (*time.Location, error) {
2525
return time.UTC, nil
2626
}
2727
loc, err := time.LoadLocation(tzEnv)
28-
if err == nil {
29-
return loc, nil
28+
if err != nil {
29+
return nil, xerrors.Errorf("load location from TZ env: %w", err)
3030
}
31+
return loc, nil
3132
}
3233

3334
lp, err := filepath.EvalSymlinks(etcLocaltime)
3435
if err != nil {
35-
return nil, err
36+
return nil, xerrors.Errorf("read location of %s: %w", etcLocaltime, err)
3637
}
3738

3839
stripped := strings.Replace(lp, etcLocaltime, "", -1)
3940
loc, err := time.LoadLocation(stripped)
4041
if err != nil {
41-
return nil, err
42+
return nil, xerrors.Errorf("invalid location %q guessed from %s: %w", stripped, lp, err)
4243
}
4344
return loc
4445
}

coderd/util/tz/tz_linux.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"path/filepath"
88
"strings"
99
"time"
10+
11+
"golang.org/x/xerrors"
1012
)
1113

1214
const etcLocaltime = "/etc/localtime"
13-
const zoneInfoPath = "/usr/share/zoneinfo"
15+
const zoneInfoPath = "/usr/share/zoneinfo/"
1416

1517
// TimezoneIANA attempts to determine the local timezone in IANA format.
1618
// If the TZ environment variable is set, this is used.
@@ -26,20 +28,21 @@ func TimezoneIANA() (*time.Location, error) {
2628
return time.UTC, nil
2729
}
2830
loc, err := time.LoadLocation(tzEnv)
29-
if err == nil {
30-
return loc, nil
31+
if err != nil {
32+
return nil, xerrors.Errorf("load location from TZ env: %w", err)
3133
}
34+
return loc, nil
3235
}
3336

3437
lp, err := filepath.EvalSymlinks(etcLocaltime)
3538
if err != nil {
36-
return nil, err
39+
return nil, xerrors.Errorf("read location of %s: %w", etcLocaltime, err)
3740
}
3841

39-
stripped := strings.Replace(lp, etcLocaltime, "", -1)
42+
stripped := strings.Replace(lp, zoneInfoPath, "", -1)
4043
loc, err := time.LoadLocation(stripped)
4144
if err != nil {
42-
return nil, err
45+
return nil, xerrors.Errorf("invalid location %q guessed from %s: %w", stripped, lp, err)
4346
}
4447
return loc, nil
4548
}

coderd/util/tz/tz_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tz_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/coderd/util/tz"
11+
)
12+
13+
//nolint:paralleltest // Environment variables
14+
func Test_TimezoneIANA(t *testing.T) {
15+
//nolint:paralleltest // t.Setenv
16+
t.Run("Env", func(t *testing.T) {
17+
t.Setenv("TZ", "Europe/Dublin")
18+
19+
zone, err := tz.TimezoneIANA()
20+
assert.NoError(t, err)
21+
if assert.NotNil(t, zone) {
22+
assert.Equal(t, "Europe/Dublin", zone.String())
23+
}
24+
})
25+
26+
//nolint:paralleltest // UnsetEnv
27+
t.Run("NoEnv", func(t *testing.T) {
28+
oldEnv, found := os.LookupEnv("TZ")
29+
if found {
30+
require.NoError(t, os.Unsetenv("TZ"))
31+
t.Cleanup(func() {
32+
_ = os.Setenv("TZ", oldEnv)
33+
})
34+
}
35+
36+
zone, err := tz.TimezoneIANA()
37+
assert.NoError(t, err)
38+
assert.NotNil(t, zone)
39+
})
40+
}

coderd/util/tz/tz_windows.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
package tz
44

55
import (
6-
"exec"
6+
"bytes"
7+
"fmt"
8+
"os"
9+
"os/exec"
710
"time"
11+
12+
"golang.org/x/xerrors"
813
)
914

1015
const cmdTimezone = "[Windows.Globalization.Calendar,Windows.Globalization,ContentType=WindowsRuntime]::New().GetTimeZone()"
@@ -22,35 +27,37 @@ func TimezoneIANA() (*time.Location, error) {
2227
return time.UTC, nil
2328
}
2429
loc, err := time.LoadLocation(tzEnv)
25-
if err == nil {
26-
return loc, nil
30+
if err != nil {
31+
return nil, xerrors.Errorf("load location from TZ env: %w", err)
2732
}
33+
return loc, nil
2834
}
2935

3036
// https://superuser.com/a/1584968
31-
cmd := exec.Command("powershell", "-nologo", "-noprofile")
37+
cmd := exec.Command("powershell.exe", "-nologo", "-noprofile")
3238
stdin, err := cmd.StdinPipe()
3339
if err != nil {
34-
return nil, err
40+
return nil, xerrors.Errorf("run powershell: %w", err)
3541
}
3642

3743
done := make(chan struct{})
3844
go func() {
3945
defer stdin.Close()
4046
defer close(done)
4147
_, _ = fmt.Fprintln(stdin, cmdTimezone)
42-
}
48+
}()
4349

44-
<- done
50+
<-done
4551

4652
out, err := cmd.CombinedOutput()
4753
if err != nil {
48-
return nil, err
54+
return nil, xerrors.Errorf("execute powershell command %q: %w", cmdTimezone, err)
4955
}
5056

51-
loc, err := time.LoadLocation(out)
57+
locStr := string(bytes.TrimSpace(out))
58+
loc, err := time.LoadLocation(locStr)
5259
if err != nil {
53-
return nil, err
60+
return nil, xerrors.Errorf("invalid location %q from powershell: %w", locStr, err)
5461
}
5562

5663
return loc, nil

0 commit comments

Comments
 (0)