From 20948a5d45572b132f2658dfd9ea556e0f7628cb Mon Sep 17 00:00:00 2001 From: Andrei Alexandru Date: Sun, 2 Nov 2025 11:10:06 +0200 Subject: [PATCH] Added GitHub Actions CI workflow and initial unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .github/workflows/go-test.yml | 31 +++++++++++++++++++++++++++++++ NewProxy/tests/builds_test.go | 24 ++++++++++++++++++++++++ NewProxy/tests/release_test.go | 26 ++++++++++++++++++++++++++ NewProxy/tests/status_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 .github/workflows/go-test.yml create mode 100644 NewProxy/tests/builds_test.go create mode 100644 NewProxy/tests/release_test.go create mode 100644 NewProxy/tests/status_test.go diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml new file mode 100644 index 0000000..d4e3375 --- /dev/null +++ b/.github/workflows/go-test.yml @@ -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 .)" diff --git a/NewProxy/tests/builds_test.go b/NewProxy/tests/builds_test.go new file mode 100644 index 0000000..e330ff9 --- /dev/null +++ b/NewProxy/tests/builds_test.go @@ -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) + } +} diff --git a/NewProxy/tests/release_test.go b/NewProxy/tests/release_test.go new file mode 100644 index 0000000..8cad390 --- /dev/null +++ b/NewProxy/tests/release_test.go @@ -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") + } +} diff --git a/NewProxy/tests/status_test.go b/NewProxy/tests/status_test.go new file mode 100644 index 0000000..d758300 --- /dev/null +++ b/NewProxy/tests/status_test.go @@ -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) + } +}