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