From 15a71692a276376fa31def9175706775bea691b3 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 6 Apr 2026 17:17:11 +0800 Subject: [PATCH] fix(tests): improve error handling in unit tests and Makefile - Enhanced the Makefile to include additional checks for test failures, specifically for panic messages and general failure indicators. - Updated service tests to dynamically retrieve the server port, ensuring accurate request routing. - Added a clear function call in the dashboard process test setup to ensure a clean state before tests run. --- Makefile | 18 ++++++++++++++++++ service/service_test.go | 11 +++++++---- widgets/dashboard/process_test.go | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index ffd4ed97..c17ace96 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,12 @@ unit-test: if grep -q "^--- FAIL" tmp.out; then \ rm tmp.out; \ exit 1; \ + elif grep -q "^FAIL" tmp.out; then \ + rm tmp.out; \ + exit 1; \ + elif grep -q "^panic:" tmp.out; then \ + rm tmp.out; \ + exit 1; \ elif grep -q "build failed" tmp.out; then \ rm tmp.out; \ exit 1; \ @@ -67,6 +73,12 @@ unit-test-core: if grep -q "^--- FAIL" tmp.out; then \ rm tmp.out; \ exit 1; \ + elif grep -q "^FAIL" tmp.out; then \ + rm tmp.out; \ + exit 1; \ + elif grep -q "^panic:" tmp.out; then \ + rm tmp.out; \ + exit 1; \ elif grep -q "build failed" tmp.out; then \ rm tmp.out; \ exit 1; \ @@ -359,6 +371,12 @@ unit-test-grpc: if grep -q "^--- FAIL" tmp.out; then \ rm tmp.out; \ exit 1; \ + elif grep -q "^FAIL" tmp.out; then \ + rm tmp.out; \ + exit 1; \ + elif grep -q "^panic:" tmp.out; then \ + rm tmp.out; \ + exit 1; \ elif grep -q "build failed" tmp.out; then \ rm tmp.out; \ exit 1; \ diff --git a/service/service_test.go b/service/service_test.go index d3795959..8b2c1bd9 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -33,10 +33,13 @@ func TestStartStop(t *testing.T) { } defer srv.Stop() - <-srv.Event() + port, err := srv.http.Port() + if err != nil { + t.Fatal(err) + } // API Server - req := test.NewRequest(cfg.Port).Route("/api/__yao/app/setting") + req := test.NewRequest(port).Route("/api/__yao/app/setting") res, err := req.Get() if err != nil { t.Fatal(err) @@ -49,7 +52,7 @@ func TestStartStop(t *testing.T) { assert.True(t, len(data["name"].(string)) > 0) // Public - req = test.NewRequest(cfg.Port).Route("/") + req = test.NewRequest(port).Route("/") res, err = req.Get() if err != nil { t.Fatal(err) @@ -58,7 +61,7 @@ func TestStartStop(t *testing.T) { assert.Equal(t, "Hello World\n", res.Body()) // XGEN - req = test.NewRequest(cfg.Port).Route("/admin/") + req = test.NewRequest(port).Route("/admin/") res, err = req.Get() if err != nil { t.Fatal(err) diff --git a/widgets/dashboard/process_test.go b/widgets/dashboard/process_test.go index cd597ce5..a84c36f8 100644 --- a/widgets/dashboard/process_test.go +++ b/widgets/dashboard/process_test.go @@ -32,6 +32,7 @@ func TestProcessComponent(t *testing.T) { test.Prepare(t, config.Conf) defer test.Clean() prepare(t) + clear(t) testData(t) args := []interface{}{