diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index da8fe9f..19bdb1c 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -1,4 +1,4 @@ -name: Go CI - Tests +name: Go Test and Coverage on: push: @@ -8,11 +8,9 @@ on: jobs: test: + name: Run Go Tests runs-on: ubuntu-latest - defaults: - run: - working-directory: ./NewProxy steps: - name: Checkout repository uses: actions/checkout@v4 @@ -20,25 +18,24 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.25' + go-version-file: ./NewProxy/go.mod - - name: Cache Go modules - uses: actions/cache@v4 - with: - path: | - ~/go/pkg/mod - ~/.cache/go-build - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - - name: Install dependencies + - name: Verify Go modules + working-directory: ./NewProxy run: go mod tidy - - name: Run unit tests - run: go test ./... -v -cover - - - name: Run vet & lint checks + - name: Run unit tests with coverage + working-directory: ./NewProxy run: | - go vet ./... - test -z "$(gofmt -l .)" + go test ./... -v -coverpkg=./... -coverprofile=coverage.out + + - name: Show coverage summary + working-directory: ./NewProxy + run: go tool cover -func=coverage.out + + - name: Upload coverage report + if: success() + uses: actions/upload-artifact@v4 + with: + name: coverage-report + path: NewProxy/coverage.out diff --git a/NewProxy/tests/builds_test.go b/NewProxy/routes/builds_test.go similarity index 85% rename from NewProxy/tests/builds_test.go rename to NewProxy/routes/builds_test.go index e330ff9..734cb00 100644 --- a/NewProxy/tests/builds_test.go +++ b/NewProxy/routes/builds_test.go @@ -1,9 +1,8 @@ -package tests +package routes import ( "net/http" "net/http/httptest" - "newproxy/routes" "testing" "github.com/gin-gonic/gin" @@ -12,7 +11,7 @@ import ( func TestBuildsHandler(t *testing.T) { gin.SetMode(gin.TestMode) r := gin.Default() - routes.RegisterBuildsRoute(r) + RegisterBuildsRoute(r) req, _ := http.NewRequest("GET", "/client/builds.json", nil) w := httptest.NewRecorder() diff --git a/NewProxy/routes/forward_test.go b/NewProxy/routes/forward_test.go new file mode 100644 index 0000000..86449b4 --- /dev/null +++ b/NewProxy/routes/forward_test.go @@ -0,0 +1,31 @@ +package routes + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" +) + +func TestForwardHandler(t *testing.T) { + gin.SetMode(gin.TestMode) + r := gin.Default() + RegisterForwardRoute(r) + + mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("OK")) + })) + defer mockServer.Close() + + req, _ := http.NewRequest("GET", "/?url="+mockServer.URL, nil) + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected 200 OK, got %d", w.Code) + } + if w.Body.String() != "OK" { + t.Errorf("Expected body 'OK', got '%s'", w.Body.String()) + } +} diff --git a/NewProxy/tests/status_test.go b/NewProxy/routes/status_test.go similarity index 72% rename from NewProxy/tests/status_test.go rename to NewProxy/routes/status_test.go index d758300..e7188f8 100644 --- a/NewProxy/tests/status_test.go +++ b/NewProxy/routes/status_test.go @@ -1,9 +1,8 @@ -package tests +package routes import ( "net/http" "net/http/httptest" - "newproxy/routes" "testing" "github.com/gin-gonic/gin" @@ -12,13 +11,13 @@ import ( func TestStatusHandler(t *testing.T) { gin.SetMode(gin.TestMode) r := gin.Default() - routes.RegisterStatusRoute(r) + 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) + t.Errorf("Expected 200 OK, got %d", w.Code) } } diff --git a/NewProxy/tests/release_test.go b/NewProxy/tests/release_test.go deleted file mode 100644 index 8cad390..0000000 --- a/NewProxy/tests/release_test.go +++ /dev/null @@ -1,26 +0,0 @@ -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/data/release.json b/NewProxy/utils/data/release.json similarity index 100% rename from NewProxy/data/release.json rename to NewProxy/utils/data/release.json