Skip to content

Commit 3224772

Browse files
committed
style: fix linters errors
1 parent 5f63f61 commit 3224772

File tree

22 files changed

+203
-60
lines changed

22 files changed

+203
-60
lines changed

cmd/tempo/componentcmd/new_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,10 @@ func TestComponentCommand_NewSubCmd_MissingActionsFile(t *testing.T) {
360360
}
361361

362362
// Ensure component.json is missing.
363-
os.Remove(filepath.Join(actionsDir, "component.json"))
363+
err := os.Remove(filepath.Join(actionsDir, "component.json"))
364+
if err != nil && !os.IsNotExist(err) {
365+
t.Errorf("Unexpected error from os.Remove: %v", err)
366+
}
364367

365368
cliCtx := &app.AppContext{
366369
Logger: logger.NewDefaultLogger(),
@@ -378,7 +381,7 @@ func TestComponentCommand_NewSubCmd_MissingActionsFile(t *testing.T) {
378381
"tempo", "component", "new",
379382
"--name", "missingActions",
380383
}
381-
err := cmd.Run(context.Background(), args)
384+
err = cmd.Run(context.Background(), args)
382385
if err == nil {
383386
t.Fatalf("Expected error due to missing actions file, but got nil")
384387
}
@@ -691,10 +694,13 @@ func TestComponentCommand_NewSubCmd_Func_validateComponentNewPrerequisites(t *te
691694
t.Run("Missing component templates directory", func(t *testing.T) {
692695
// Ensure the templates directory does not exist
693696
componentTemplateDir := filepath.Join(cfg.Paths.TemplatesDir, "component")
694-
os.RemoveAll(componentTemplateDir)
697+
err := os.RemoveAll(componentTemplateDir)
698+
if err != nil {
699+
t.Errorf("Unexpected error message from os.RemoveAll: %v", err)
700+
}
695701

696702
validate := validateComponentNewPrerequisites(cfg)
697-
_, err := validate(context.Background(), &cli.Command{})
703+
_, err = validate(context.Background(), &cli.Command{})
698704

699705
if err == nil {
700706
t.Fatal("Expected an error due to missing component templates directory, but got none")

cmd/tempo/initcmd/init.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ func validateInitPrerequisites(workingDir, configFilePath string) error {
115115
if err != nil {
116116
return errors.Wrap("Failed to write the configuration file", err)
117117
}
118-
file.Close()
118+
if err := file.Close(); err != nil {
119+
return errors.Wrap("Failed to close the configuration file", err)
120+
}
119121
return errors.Wrap("Configuration file already exists", configFilePath)
120122
}
121123
return nil
@@ -232,12 +234,12 @@ func formatUserData(sb *strings.Builder, userData map[string]any) {
232234
for key, value := range userData {
233235
switch v := value.(type) {
234236
case map[string]any:
235-
sb.WriteString(fmt.Sprintf(" %s:\n", key))
237+
fmt.Fprintf(sb, " %s:\n", key)
236238
for subKey, subValue := range v {
237-
sb.WriteString(fmt.Sprintf(" %s: %v\n", subKey, subValue))
239+
fmt.Fprintf(sb, " %s: %v\n", subKey, subValue)
238240
}
239241
default:
240-
sb.WriteString(fmt.Sprintf(" %s: %v\n", key, value))
242+
fmt.Fprintf(sb, " %s: %v\n", key, value)
241243
}
242244
}
243245
}
@@ -260,9 +262,9 @@ func formatFunctionProviders(sb *strings.Builder, providers []config.TemplateFun
260262
// Append configured function providers
261263
if len(providers) > 0 {
262264
for _, provider := range providers {
263-
sb.WriteString(fmt.Sprintf(" - name: %s\n", provider.Name))
264-
sb.WriteString(fmt.Sprintf(" type: %s\n", provider.Type))
265-
sb.WriteString(fmt.Sprintf(" value: %s\n", provider.Value))
265+
fmt.Fprintf(sb, " - name: %s\n", provider.Name)
266+
fmt.Fprintf(sb, " type: %s\n", provider.Type)
267+
fmt.Fprintf(sb, " value: %s\n", provider.Value)
266268
}
267269
}
268270
}

cmd/tempo/initcmd/init_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ func TestInitCommand_FailsOnUnwritableConfigFile(t *testing.T) {
156156
if err != nil {
157157
t.Fatalf("Failed to create config file: %v", err)
158158
}
159-
file.Close()
159+
if err := file.Close(); err != nil {
160+
t.Fatalf("Failed to close the configuration file: %v", err)
161+
}
160162

161163
// Make the file unwritable
162164
if err := os.Chmod(configFile, 0444); err != nil {

cmd/tempo/main_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ func TestRunApp_InitAutoGen(t *testing.T) {
155155
defer restoreWorkingDir(t, origDir)
156156

157157
configPath := filepath.Join(tempDir, "tempo.yaml")
158-
os.Remove(configPath) // Ensure it's missing.
158+
err := os.Remove(configPath) // Ensure it's missing.
159+
if err != nil && !os.IsNotExist(err) {
160+
t.Errorf("Unexpected error from os.Remove: %v", err)
161+
}
159162

160163
// Run the command and capture output
161164
output, err := testhelpers.CaptureStdout(func() {

cmd/tempo/variantcmd/new_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ func TestVariantCommand_NewSubCmd_MissingActionsFile(t *testing.T) {
538538
}
539539

540540
// Ensure variant.json is missing.
541-
os.Remove(filepath.Join(actionsDir, "variant.json"))
541+
err := os.Remove(filepath.Join(actionsDir, "variant.json"))
542+
if err != nil && !os.IsNotExist(err) {
543+
t.Fatalf("Failed to remove variant.json: %v", err)
544+
}
542545

543546
cliCtx := &app.AppContext{
544547
Logger: logger.NewDefaultLogger(),
@@ -559,7 +562,7 @@ func TestVariantCommand_NewSubCmd_MissingActionsFile(t *testing.T) {
559562
"--name", "missingVariantActions",
560563
"--component", "someComponent",
561564
}
562-
err := app.Run(context.Background(), args)
565+
err = app.Run(context.Background(), args)
563566
if err == nil {
564567
t.Fatalf("Expected error due to missing variant actions file, but got nil")
565568
}
@@ -693,7 +696,9 @@ func TestVariantCommand_NewSubCmd_MissingFolders(t *testing.T) {
693696
cfg := testutils.SetupConfig(tempDir, nil)
694697
// Ensure one of the required folders (e.g. component-variant) is missing.
695698
variantDir := filepath.Join(cfg.Paths.TemplatesDir, "component-variant")
696-
os.RemoveAll(variantDir)
699+
if err := os.RemoveAll(variantDir); err != nil {
700+
t.Fatalf("Failed to remove variant directory %q: %v", variantDir, err)
701+
}
697702

698703
validate := validateVariantNewPrerequisites(cfg)
699704
_, err := validate(context.Background(), &cli.Command{})

internal/cmdrunner/runner_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmdrunner
22

33
import (
44
"context"
5+
"log"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -76,7 +77,11 @@ func TestRunCommand_InsideDir(t *testing.T) {
7677
tempDir := os.TempDir()
7778
testDir := filepath.Join(tempDir, "test-cmd-runner")
7879
_ = os.MkdirAll(testDir, os.ModePerm)
79-
defer os.RemoveAll(testDir)
80+
defer func() {
81+
if err := os.RemoveAll(testDir); err != nil {
82+
log.Printf("Failed to remove test directory %s: %v", testDir, err)
83+
}
84+
}()
8085

8186
err := RunCommand(testDir, "touch", "testfile.txt")
8287
if err != nil {

internal/config/config_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"log"
45
"os"
56
"path/filepath"
67
"reflect"
@@ -71,7 +72,12 @@ func TestLoadConfig_ReadError(t *testing.T) {
7172
if err := os.WriteFile(filePath, []byte("app:\n go_module: something"), 0000); err != nil {
7273
t.Fatalf("Failed to write unreadable config file: %v", err)
7374
}
74-
defer os.Remove(filePath)
75+
76+
defer func() {
77+
if err := os.Remove(filePath); err != nil {
78+
log.Printf("Failed to remove %s: %v", filePath, err)
79+
}
80+
}()
7581

7682
_, err := LoadConfig()
7783
if err == nil || !utils.ContainsSubstring(err.Error(), "failed to read config file:") {
@@ -94,7 +100,11 @@ func TestLoadConfig_ParseError(t *testing.T) {
94100
if err := os.WriteFile(filePath, badYaml, 0644); err != nil {
95101
t.Fatalf("Failed to write broken config file: %v", err)
96102
}
97-
defer os.Remove(filePath)
103+
defer func() {
104+
if err := os.Remove(filePath); err != nil {
105+
log.Printf("Failed to remove %s: %v", filePath, err)
106+
}
107+
}()
98108

99109
_, err := LoadConfig()
100110
if err == nil || !utils.ContainsSubstring(err.Error(), "failed to parse config file:") {

internal/errors/errors.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io"
78
"sort"
89
"strings"
910

@@ -138,6 +139,10 @@ func Wrap(msg string, args ...any) error {
138139
/* LOGGING FUNCTIONS */
139140
/* ------------------------------------------------------------------------- */
140141

142+
func mustWrite(w io.Writer, format string, args ...any) {
143+
_, _ = fmt.Fprintf(w, format, args...)
144+
}
145+
141146
// LogErrorChain logs an error chain to the console.
142147
func LogErrorChain(err error) {
143148
if err == nil {
@@ -147,9 +152,9 @@ func LogErrorChain(err error) {
147152
output := color.Output
148153
errorColor := color.New(color.FgRed, color.Bold).SprintFunc()
149154

150-
fmt.Fprintf(output, "%s\n", errorColor("✘ Something went wrong:"))
155+
mustWrite(output, "%s\n", errorColor("✘ Something went wrong:"))
151156
for err != nil {
152-
fmt.Fprintf(output, " %s %v\n", errorColor("→"), err)
157+
mustWrite(output, " %s %v\n", errorColor("→"), err)
153158
err = errors.Unwrap(err)
154159
}
155160
}
@@ -160,21 +165,21 @@ func LogErrorChainWithAttrs(err error) {
160165
errorColor := color.New(color.FgRed, color.Bold).SprintFunc()
161166
argColor := color.New(color.Faint).SprintFunc()
162167

163-
fmt.Fprintf(output, "%s\n", errorColor("✘ Something went wrong:"))
168+
mustWrite(output, "%s\n", errorColor("✘ Something went wrong:"))
164169
for err != nil {
165170
if tempoErr, ok := err.(*TempoError); ok {
166-
fmt.Fprintf(output, " - Code: %d, Message: %s\n", tempoErr.Code, tempoErr.Message)
171+
mustWrite(output, " - Code: %d, Message: %s\n", tempoErr.Code, tempoErr.Message)
167172
if len(tempoErr.Attrs) > 0 {
168-
fmt.Fprintf(output, " Attrs:\n")
173+
mustWrite(output, " Attrs:\n")
169174

170175
// Sort the metadata keys for consistent output
171176
keys := sortedKeys(tempoErr.Attrs)
172177
for _, key := range keys {
173-
fmt.Fprintf(output, " %s: %v\n", argColor(key), tempoErr.Attrs[key])
178+
mustWrite(output, " %s: %v\n", argColor(key), tempoErr.Attrs[key])
174179
}
175180
}
176181
} else {
177-
fmt.Fprintf(output, " - %v\n", err)
182+
mustWrite(output, " - %v\n", err)
178183
}
179184
err = errors.Unwrap(err)
180185
}

internal/generator/action_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"log"
78
"os"
89
"path"
910
"path/filepath"
@@ -25,7 +26,12 @@ func TestGenerateActionJSONFile(t *testing.T) {
2526
}
2627

2728
tempFile := "test_output.json"
28-
defer os.Remove(tempFile)
29+
30+
defer func() {
31+
if err := os.Remove(tempFile); err != nil {
32+
log.Printf("Failed to remove test directory %s: %v", tempFile, err)
33+
}
34+
}()
2935

3036
err := GenerateActionJSONFile(tempFile, actions)
3137
if err != nil {

internal/git/git_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"errors"
5+
"log"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -60,7 +61,11 @@ func TestDefaultCloneOrUpdate(t *testing.T) {
6061

6162
func TestIsValidGitRepo_ValidRepo(t *testing.T) {
6263
tempDir := setupTestRepo(t)
63-
defer os.RemoveAll(tempDir)
64+
defer func() {
65+
if err := os.RemoveAll(tempDir); err != nil {
66+
log.Printf("Failed to remove test directory %s: %v", tempDir, err)
67+
}
68+
}()
6469

6570
if !IsValidGitRepo(tempDir) {
6671
t.Errorf("Expected IsValidGitRepo to return true for a valid repo, but got false")
@@ -74,7 +79,11 @@ func TestIsValidGitRepo_InvalidRepo(t *testing.T) {
7479
if err := os.MkdirAll(tempDir, 0755); err != nil {
7580
t.Fatalf("Failed to create temp directory: %v", err)
7681
}
77-
defer os.RemoveAll(tempDir)
82+
defer func() {
83+
if err := os.RemoveAll(tempDir); err != nil {
84+
log.Printf("Failed to remove test directory %s: %v", tempDir, err)
85+
}
86+
}()
7887

7988
if IsValidGitRepo(tempDir) {
8089
t.Errorf("Expected IsValidGitRepo to return false for a non-Git directory, but got true")

0 commit comments

Comments
 (0)