Skip to content

Commit de047b2

Browse files
committed
fix: resolve golangci-lint errcheck and cross-platform test failures
- Add explicit error ignoring for fmt.Fprintf and resp.Body.Close - Clear CI env vars in tests so ShouldCheck doesn't short-circuit on GitHub Actions runners - Set USERPROFILE alongside HOME for Windows test compatibility - Use unambiguously invalid YAML for corrupt state file test
1 parent 0efc8c6 commit de047b2

3 files changed

Lines changed: 38 additions & 15 deletions

File tree

internal/cli/update.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,30 @@ func newUpdateCmd() *cobra.Command {
1717
cmd := &cobra.Command{
1818
Use: "update",
1919
Short: "Update flashduty to the latest version",
20-
RunE: func(cmd *cobra.Command, args []string) error {
21-
fmt.Fprintf(cmd.OutOrStdout(), "Current version: %s\n", versionStr)
22-
fmt.Fprintf(cmd.OutOrStdout(), "Checking for updates...\n")
20+
RunE: func(cmd *cobra.Command, _ []string) error {
21+
w := cmd.OutOrStdout()
22+
_, _ = fmt.Fprintf(w, "Current version: %s\n", versionStr)
23+
_, _ = fmt.Fprintf(w, "Checking for updates...\n")
2324

2425
result, err := update.CheckForUpdate(versionStr)
2526
if err != nil {
2627
return fmt.Errorf("failed to check for updates: %w", err)
2728
}
2829

2930
if !result.UpdateAvailable {
30-
fmt.Fprintf(cmd.OutOrStdout(), "Already up to date (%s).\n", versionStr)
31+
_, _ = fmt.Fprintf(w, "Already up to date (%s).\n", versionStr)
3132
return nil
3233
}
3334

34-
fmt.Fprintf(cmd.OutOrStdout(), "A new version is available: v%s -> %s\n",
35+
_, _ = fmt.Fprintf(w, "A new version is available: v%s -> %s\n",
3536
update.StripV(versionStr), result.LatestVersion)
36-
fmt.Fprintf(cmd.OutOrStdout(), "Release: %s\n", result.LatestURL)
37+
_, _ = fmt.Fprintf(w, "Release: %s\n", result.LatestURL)
3738

3839
if flagCheck {
3940
return nil
4041
}
4142

42-
fmt.Fprintf(cmd.OutOrStdout(), "\nUpdating...\n")
43+
_, _ = fmt.Fprintf(w, "\nUpdating...\n")
4344
return runInstaller(cmd)
4445
},
4546
}
@@ -66,6 +67,6 @@ func runInstaller(cmd *cobra.Command) error {
6667
return fmt.Errorf("update failed: %w", err)
6768
}
6869

69-
fmt.Fprintf(cmd.OutOrStdout(), "\nUpdate complete. Run 'flashduty version' to verify.\n")
70+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\nUpdate complete. Run 'flashduty version' to verify.\n")
7071
return nil
7172
}

internal/update/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func fetchLatestVersion() (string, string, error) {
105105
if err != nil {
106106
return "", "", fmt.Errorf("failed to fetch latest release: %w", err)
107107
}
108-
defer resp.Body.Close()
108+
defer func() { _ = resp.Body.Close() }()
109109

110110
if resp.StatusCode != http.StatusOK {
111111
return "", "", fmt.Errorf("GitHub API returned %d", resp.StatusCode)

internal/update/check_test.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,32 @@ import (
66
"net/http/httptest"
77
"os"
88
"path/filepath"
9+
"runtime"
910
"testing"
1011
"time"
1112

1213
"gopkg.in/yaml.v3"
1314
)
1415

16+
// setTestHome overrides the home directory for testing across all platforms.
17+
func setTestHome(t *testing.T, dir string) {
18+
t.Helper()
19+
t.Setenv("HOME", dir)
20+
if runtime.GOOS == "windows" {
21+
t.Setenv("USERPROFILE", dir)
22+
}
23+
}
24+
25+
// clearCIEnv clears CI-related env vars so ShouldCheck doesn't short-circuit.
26+
func clearCIEnv(t *testing.T) {
27+
t.Helper()
28+
t.Setenv("CI", "")
29+
t.Setenv("GITHUB_ACTIONS", "")
30+
t.Setenv("JENKINS_URL", "")
31+
t.Setenv("GITLAB_CI", "")
32+
t.Setenv("FLASHDUTY_NO_UPDATE_CHECK", "")
33+
}
34+
1535
func TestStripV(t *testing.T) {
1636
tests := []struct {
1737
in, want string
@@ -101,7 +121,8 @@ func TestShouldCheck_CI(t *testing.T) {
101121

102122
func TestShouldCheck_RecentCheck(t *testing.T) {
103123
tmp := t.TempDir()
104-
t.Setenv("HOME", tmp)
124+
setTestHome(t, tmp)
125+
clearCIEnv(t)
105126

106127
dir := filepath.Join(tmp, ".flashduty")
107128
if err := os.MkdirAll(dir, 0700); err != nil {
@@ -124,7 +145,8 @@ func TestShouldCheck_RecentCheck(t *testing.T) {
124145

125146
func TestShouldCheck_StaleCheck(t *testing.T) {
126147
tmp := t.TempDir()
127-
t.Setenv("HOME", tmp)
148+
setTestHome(t, tmp)
149+
clearCIEnv(t)
128150

129151
dir := filepath.Join(tmp, ".flashduty")
130152
if err := os.MkdirAll(dir, 0700); err != nil {
@@ -147,7 +169,7 @@ func TestShouldCheck_StaleCheck(t *testing.T) {
147169

148170
func TestLoadSaveState(t *testing.T) {
149171
tmp := t.TempDir()
150-
t.Setenv("HOME", tmp)
172+
setTestHome(t, tmp)
151173

152174
now := time.Now().Truncate(time.Second)
153175
want := &State{
@@ -174,13 +196,13 @@ func TestLoadSaveState(t *testing.T) {
174196

175197
func TestLoadState_CorruptFile(t *testing.T) {
176198
tmp := t.TempDir()
177-
t.Setenv("HOME", tmp)
199+
setTestHome(t, tmp)
178200

179201
dir := filepath.Join(tmp, ".flashduty")
180202
if err := os.MkdirAll(dir, 0700); err != nil {
181203
t.Fatal(err)
182204
}
183-
if err := os.WriteFile(filepath.Join(dir, stateFileName), []byte(":::invalid yaml"), 0600); err != nil {
205+
if err := os.WriteFile(filepath.Join(dir, stateFileName), []byte("{[invalid yaml"), 0600); err != nil {
184206
t.Fatal(err)
185207
}
186208

@@ -252,7 +274,7 @@ func TestFetchLatestVersion_InvalidJSON(t *testing.T) {
252274

253275
func TestCheckForUpdate(t *testing.T) {
254276
tmp := t.TempDir()
255-
t.Setenv("HOME", tmp)
277+
setTestHome(t, tmp)
256278

257279
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
258280
rel := githubRelease{

0 commit comments

Comments
 (0)