Refactor the code to use more modular and testable approach.

This commit is contained in:
2025-02-25 19:11:19 +00:00
parent 5964da3cef
commit 942e648d56
18 changed files with 1719 additions and 595 deletions
+46
View File
@@ -0,0 +1,46 @@
package utils
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCheckLatestRelease(t *testing.T) {
// Initialize logger
InitLogger(true)
// Save original environment variables
originalToken := os.Getenv("GITHUB_TOKEN")
defer os.Setenv("GITHUB_TOKEN", originalToken)
// Test with no token
os.Unsetenv("GITHUB_TOKEN")
release, ok := CheckLatestRelease()
assert.Equal(t, "[no GITHUB_TOKEN set]", release, "Should return no token message")
assert.False(t, ok, "Should return false when no token is set")
// We can't reliably test with a token in CI environments
// Just verify the no-token case works as expected
}
func TestUpdatePackage(t *testing.T) {
// Initialize logger
InitLogger(true)
// Save original environment variables
originalToken := os.Getenv("GITHUB_TOKEN")
defer os.Setenv("GITHUB_TOKEN", originalToken)
// Test with no token
os.Unsetenv("GITHUB_TOKEN")
result := UpdatePackage()
assert.False(t, result, "Should return false when no token is set")
// We can't fully test the update functionality as it would modify the binary
// but we can test the token check logic
}
// Note: We're not using mock transports for these tests to avoid
// adding complexity. The tests focus on the token presence logic.