Added GitHub Actions CI workflow and initial unit tests

- Added .github/workflows/go-test.yml for automated CI testing using GitHub Actions
- Implemented unit tests for:
  • utils/release.go (FetchLatestRelease)
  • routes/builds.go (buildsHandler)
  • routes/status.go (statusHandler)
- Ensured tests run with go test ./... -v -cover
- Included go vet and gofmt checks in the CI pipeline
- Improved project reliability and continuous integration setup
This commit is contained in:
Andrei Alexandru
2025-11-02 11:10:06 +02:00
parent 2bf7c84e3b
commit 20948a5d45
4 changed files with 105 additions and 0 deletions

31
.github/workflows/go-test.yml vendored Normal file
View File

@@ -0,0 +1,31 @@
name: Go CI - Tests
on:
push:
branches: [ main, master, dev ]
pull_request:
branches: [ main, master, dev ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.25'
- name: Install dependencies
run: go mod download
- name: Run unit tests
run: go test ./... -v -cover
- name: Run vet & lint checks
run: |
go vet ./...
test -z "$(gofmt -l .)"

View File

@@ -0,0 +1,24 @@
package tests
import (
"net/http"
"net/http/httptest"
"newproxy/routes"
"testing"
"github.com/gin-gonic/gin"
)
func TestBuildsHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.Default()
routes.RegisterBuildsRoute(r)
req, _ := http.NewRequest("GET", "/client/builds.json", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected 200 OK, got %d", w.Code)
}
}

View File

@@ -0,0 +1,26 @@
package tests
import (
"newproxy/utils"
"os"
"testing"
)
func TestFetchLatestRelease_ValidFile(t *testing.T) {
content := `{"release_version": "v1.0.0", "release_time": "2025-01-01T00:00:00Z"}`
_ = os.WriteFile("data/release.json", []byte(content), 0644)
utils.FetchLatestRelease()
if utils.LatestRelease.ReleaseVersion != "v1.0.0" {
t.Errorf("Expected version v1.0.0, got %s", utils.LatestRelease.ReleaseVersion)
}
}
func TestFetchLatestRelease_MissingFile(t *testing.T) {
_ = os.Remove("data/release.json")
utils.FetchLatestRelease()
if utils.LatestRelease.ReleaseVersion == "" {
t.Errorf("Expected default release info, got empty version")
}
}

View File

@@ -0,0 +1,24 @@
package tests
import (
"net/http"
"net/http/httptest"
"newproxy/routes"
"testing"
"github.com/gin-gonic/gin"
)
func TestStatusHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.Default()
routes.RegisterStatusRoute(r)
req, _ := http.NewRequest("GET", "/status", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200 OK, got %d", w.Code)
}
}