fix: resolve golangci-lint errcheck and staticcheck warnings

Use t.Setenv in tests, add error return handling, and replace
WriteString(fmt.Sprintf(...)) with fmt.Fprintf.
This commit is contained in:
juanatsap
2026-03-15 20:20:20 +00:00
parent 585949b709
commit cafd117437
3 changed files with 38 additions and 59 deletions
+28 -49
View File
@@ -7,9 +7,9 @@ import (
func TestLoad(t *testing.T) {
// Clear environment variables for clean test
os.Unsetenv("PORT")
os.Unsetenv("HOST")
os.Unsetenv("GO_ENV")
_ = os.Unsetenv("PORT")
_ = os.Unsetenv("HOST")
_ = os.Unsetenv("GO_ENV")
cfg := Load()
@@ -41,16 +41,10 @@ func TestLoad(t *testing.T) {
func TestLoadWithEnvVars(t *testing.T) {
// Set custom environment variables
os.Setenv("PORT", "8080")
os.Setenv("HOST", "0.0.0.0")
os.Setenv("READ_TIMEOUT", "30")
os.Setenv("WRITE_TIMEOUT", "45")
defer func() {
os.Unsetenv("PORT")
os.Unsetenv("HOST")
os.Unsetenv("READ_TIMEOUT")
os.Unsetenv("WRITE_TIMEOUT")
}()
t.Setenv("PORT", "8080")
t.Setenv("HOST", "0.0.0.0")
t.Setenv("READ_TIMEOUT", "30")
t.Setenv("WRITE_TIMEOUT", "45")
cfg := Load()
@@ -72,8 +66,8 @@ func TestLoadWithEnvVars(t *testing.T) {
}
func TestAddress(t *testing.T) {
os.Unsetenv("PORT")
os.Unsetenv("HOST")
_ = os.Unsetenv("PORT")
_ = os.Unsetenv("HOST")
cfg := Load()
addr := cfg.Address()
@@ -83,12 +77,8 @@ func TestAddress(t *testing.T) {
}
// Test with custom values
os.Setenv("PORT", "3000")
os.Setenv("HOST", "127.0.0.1")
defer func() {
os.Unsetenv("PORT")
os.Unsetenv("HOST")
}()
t.Setenv("PORT", "3000")
t.Setenv("HOST", "127.0.0.1")
cfg = Load()
addr = cfg.Address()
@@ -100,8 +90,7 @@ func TestAddress(t *testing.T) {
func TestGetEnv(t *testing.T) {
// Test with existing var
os.Setenv("TEST_VAR", "test_value")
defer os.Unsetenv("TEST_VAR")
t.Setenv("TEST_VAR", "test_value")
result := getEnv("TEST_VAR", "default")
if result != "test_value" {
@@ -117,8 +106,7 @@ func TestGetEnv(t *testing.T) {
func TestGetEnvAsInt(t *testing.T) {
// Test with valid int
os.Setenv("INT_VAR", "42")
defer os.Unsetenv("INT_VAR")
t.Setenv("INT_VAR", "42")
result := getEnvAsInt("INT_VAR", 10)
if result != 42 {
@@ -126,8 +114,7 @@ func TestGetEnvAsInt(t *testing.T) {
}
// Test with invalid int
os.Setenv("INVALID_INT", "not_a_number")
defer os.Unsetenv("INVALID_INT")
t.Setenv("INVALID_INT", "not_a_number")
result = getEnvAsInt("INVALID_INT", 10)
if result != 10 {
@@ -158,8 +145,7 @@ func TestGetEnvAsBool(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("BOOL_VAR", tt.envValue)
defer os.Unsetenv("BOOL_VAR")
t.Setenv("BOOL_VAR", tt.envValue)
result := getEnvAsBool("BOOL_VAR", tt.defaultValue)
if result != tt.expected {
@@ -192,11 +178,10 @@ func TestIsDevelopment(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue == "" {
os.Unsetenv("GO_ENV")
_ = os.Unsetenv("GO_ENV")
} else {
os.Setenv("GO_ENV", tt.envValue)
t.Setenv("GO_ENV", tt.envValue)
}
defer os.Unsetenv("GO_ENV")
result := isDevelopment()
if result != tt.expected {
@@ -208,9 +193,8 @@ func TestIsDevelopment(t *testing.T) {
func TestTemplateHotReload(t *testing.T) {
// In development, hot reload should be true by default
os.Setenv("GO_ENV", "development")
os.Unsetenv("TEMPLATE_HOT_RELOAD")
defer os.Unsetenv("GO_ENV")
t.Setenv("GO_ENV", "development")
_ = os.Unsetenv("TEMPLATE_HOT_RELOAD")
cfg := Load()
if !cfg.Template.HotReload {
@@ -218,8 +202,7 @@ func TestTemplateHotReload(t *testing.T) {
}
// Explicit false should override
os.Setenv("TEMPLATE_HOT_RELOAD", "false")
defer os.Unsetenv("TEMPLATE_HOT_RELOAD")
t.Setenv("TEMPLATE_HOT_RELOAD", "false")
cfg = Load()
if cfg.Template.HotReload {
@@ -227,8 +210,8 @@ func TestTemplateHotReload(t *testing.T) {
}
// In production, hot reload should be false by default
os.Setenv("GO_ENV", "production")
os.Unsetenv("TEMPLATE_HOT_RELOAD")
t.Setenv("GO_ENV", "production")
_ = os.Unsetenv("TEMPLATE_HOT_RELOAD")
cfg = Load()
if cfg.Template.HotReload {
@@ -237,10 +220,10 @@ func TestTemplateHotReload(t *testing.T) {
}
func TestEmailConfig(t *testing.T) {
os.Unsetenv("SMTP_HOST")
os.Unsetenv("SMTP_PORT")
os.Unsetenv("SMTP_USER")
os.Unsetenv("SMTP_PASSWORD")
_ = os.Unsetenv("SMTP_HOST")
_ = os.Unsetenv("SMTP_PORT")
_ = os.Unsetenv("SMTP_USER")
_ = os.Unsetenv("SMTP_PASSWORD")
cfg := Load()
@@ -254,12 +237,8 @@ func TestEmailConfig(t *testing.T) {
}
// Test custom values
os.Setenv("SMTP_HOST", "mail.example.com")
os.Setenv("SMTP_PORT", "465")
defer func() {
os.Unsetenv("SMTP_HOST")
os.Unsetenv("SMTP_PORT")
}()
t.Setenv("SMTP_HOST", "mail.example.com")
t.Setenv("SMTP_PORT", "465")
cfg = Load()
if cfg.Email.SMTPHost != "mail.example.com" {
+9 -9
View File
@@ -303,19 +303,19 @@ func (e *Service) formatMultipartMessage(from, to, replyTo, subject, htmlBody, t
var message strings.Builder
// Headers
message.WriteString(fmt.Sprintf("From: %s\r\n", from))
message.WriteString(fmt.Sprintf("To: %s\r\n", to))
fmt.Fprintf(&message, "From: %s\r\n", from)
fmt.Fprintf(&message, "To: %s\r\n", to)
if replyTo != "" {
message.WriteString(fmt.Sprintf("Reply-To: %s\r\n", replyTo))
fmt.Fprintf(&message, "Reply-To: %s\r\n", replyTo)
}
message.WriteString(fmt.Sprintf("Subject: %s\r\n", subject))
fmt.Fprintf(&message, "Subject: %s\r\n", subject)
message.WriteString("MIME-Version: 1.0\r\n")
message.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=\"%s\"\r\n", boundary))
message.WriteString(fmt.Sprintf("Date: %s\r\n", time.Now().Format(time.RFC1123Z)))
fmt.Fprintf(&message, "Content-Type: multipart/alternative; boundary=\"%s\"\r\n", boundary)
fmt.Fprintf(&message, "Date: %s\r\n", time.Now().Format(time.RFC1123Z))
message.WriteString("\r\n")
// Plain text part
message.WriteString(fmt.Sprintf("--%s\r\n", boundary))
fmt.Fprintf(&message, "--%s\r\n", boundary)
message.WriteString("Content-Type: text/plain; charset=\"utf-8\"\r\n")
message.WriteString("Content-Transfer-Encoding: quoted-printable\r\n")
message.WriteString("\r\n")
@@ -323,7 +323,7 @@ func (e *Service) formatMultipartMessage(from, to, replyTo, subject, htmlBody, t
message.WriteString("\r\n")
// HTML part
message.WriteString(fmt.Sprintf("--%s\r\n", boundary))
fmt.Fprintf(&message, "--%s\r\n", boundary)
message.WriteString("Content-Type: text/html; charset=\"utf-8\"\r\n")
message.WriteString("Content-Transfer-Encoding: base64\r\n")
message.WriteString("\r\n")
@@ -340,7 +340,7 @@ func (e *Service) formatMultipartMessage(from, to, replyTo, subject, htmlBody, t
}
// End boundary
message.WriteString(fmt.Sprintf("--%s--\r\n", boundary))
fmt.Fprintf(&message, "--%s--\r\n", boundary)
return message.String()
}
+1 -1
View File
@@ -128,7 +128,7 @@ func TestDefaultCVShortcut(t *testing.T) {
if err != nil || resp.StatusCode != http.StatusOK {
t.Skip("Skipping PDF generation test - server not running on localhost:1999")
}
resp.Body.Close()
_ = resp.Body.Close()
handler := newTestCVHandler(t, "localhost:1999", nil)