From 4e683f1a57bb7c4c6ade9b606a1e7b7de3fed658 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Mon, 17 Jun 2024 15:19:11 +0800 Subject: [PATCH] test: enable wasm test in CI Signed-off-by: Zxilly --- .github/workflows/tests.yml | 6 +++-- .gitignore | 3 ++- internal/printer/console.go | 2 ++ internal/printer/html.go | 2 ++ internal/printer/svg.go | 2 ++ internal/result/resultutils_test.go | 1 + internal/test/test.go | 10 +++++--- internal/tui/tui_test.go | 3 ++- scripts/tests.py | 38 +++++++++++++++++++++++++++++ 9 files changed, 59 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e168d36e2a..a2adfbaaf5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -50,7 +50,9 @@ jobs: check-latest: true - name: Setup Go global dependencies - run: go install github.com/jstemmer/go-junit-report/v2@85bf4716ac1f025f2925510a9f5e9f5bb347c009 + run: | + go install github.com/jstemmer/go-junit-report/v2@85bf4716ac1f025f2925510a9f5e9f5bb347c009 + go install github.com/Zxilly/go_js_wasm_exec@17f8c465d89b47d00637caf8ca7b38d20de31f5b - uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 with: @@ -101,7 +103,7 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} name: ${{ matrix.os }} unit tests flags: ${{ matrix.os }}-unit - files: unit.xml,unit_embed.xml + files: unit.xml,unit_embed.xml,unit_wasm.xml verbose: true - name: Upload results diff --git a/.gitignore b/.gitignore index 7c643d6127..8870665a92 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ data.json *.sarif unit.xml -unit_embed.xml \ No newline at end of file +unit_embed.xml +unit_wasm.xml \ No newline at end of file diff --git a/internal/printer/console.go b/internal/printer/console.go index ce037375b3..715390b99f 100644 --- a/internal/printer/console.go +++ b/internal/printer/console.go @@ -1,3 +1,5 @@ +//go:build !js && !wasm + package printer import ( diff --git a/internal/printer/html.go b/internal/printer/html.go index cb8cfa973d..ca9ca32a0e 100644 --- a/internal/printer/html.go +++ b/internal/printer/html.go @@ -1,3 +1,5 @@ +//go:build !js && !wasm + package printer import ( diff --git a/internal/printer/svg.go b/internal/printer/svg.go index d2337ee209..8930e88a2c 100644 --- a/internal/printer/svg.go +++ b/internal/printer/svg.go @@ -1,3 +1,5 @@ +//go:build !js && !wasm + package printer import ( diff --git a/internal/result/resultutils_test.go b/internal/result/resultutils_test.go index 7ccb7f6b2d..c4a480702a 100644 --- a/internal/result/resultutils_test.go +++ b/internal/result/resultutils_test.go @@ -2,6 +2,7 @@ package result_test import ( "encoding/gob" + "github.com/Zxilly/go-size-analyzer/internal/entity" ) diff --git a/internal/test/test.go b/internal/test/test.go index 0517b238f3..4c09ec6353 100644 --- a/internal/test/test.go +++ b/internal/test/test.go @@ -1,14 +1,16 @@ package test import ( - "github.com/Zxilly/go-size-analyzer/internal" - "github.com/Zxilly/go-size-analyzer/internal/result" - "github.com/stretchr/testify/require" - "golang.org/x/exp/mmap" "os" "path/filepath" "runtime" "testing" + + "github.com/stretchr/testify/require" + "golang.org/x/exp/mmap" + + "github.com/Zxilly/go-size-analyzer/internal" + "github.com/Zxilly/go-size-analyzer/internal/result" ) func GetProjectRoot() string { diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go index 65b4c537a3..c5fabac828 100644 --- a/internal/tui/tui_test.go +++ b/internal/tui/tui_test.go @@ -2,7 +2,6 @@ package tui import ( "bytes" - "github.com/Zxilly/go-size-analyzer/internal/test" "testing" "time" @@ -10,6 +9,8 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/exp/teatest" "github.com/muesli/termenv" + + "github.com/Zxilly/go-size-analyzer/internal/test" ) func init() { diff --git a/scripts/tests.py b/scripts/tests.py index 0186237859..7b3d777d70 100644 --- a/scripts/tests.py +++ b/scripts/tests.py @@ -23,6 +23,7 @@ def run_unit_tests(): ensure_dir(unit_output_dir) try: + log("Running full unit tests...") embed_result = subprocess.run( [ "go", @@ -41,6 +42,7 @@ def run_unit_tests(): timeout=600 ) embed_result.check_returncode() + with open(os.path.join(unit_output_dir, "unit_embed.txt"), "w", encoding="utf-8") as f: f.write(embed_result.stdout) @@ -51,6 +53,7 @@ def run_unit_tests(): exit(1) try: + log("Running normal unit tests for webui...") normal_result = subprocess.run( [ "go", @@ -68,6 +71,7 @@ def run_unit_tests(): timeout=600 ) normal_result.check_returncode() + with open(os.path.join(unit_output_dir, "unit.txt"), "w", encoding="utf-8") as f: f.write(normal_result.stdout) @@ -77,6 +81,40 @@ def run_unit_tests(): log(f"stdout: {e.stdout}") exit(1) + try: + log("Running WebAssembly unit tests...") + env = os.environ.copy() + env["GOOS"] = "js" + env["GOARCH"] = "wasm" + + wasm_result = subprocess.run( + [ + "go", + "test", + "-v", + "-covermode=atomic", + "-cover", + "./internal/result", + f"-test.gocoverdir={unit_path}" + ], + text=True, + cwd=get_project_root(), + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + timeout=600, + env=env + ) + wasm_result.check_returncode() + + with open(os.path.join(unit_output_dir, "unit_wasm.txt"), "w", encoding="utf-8") as f: + f.write(wasm_result.stdout) + + generate_junit(wasm_result.stdout, os.path.join(get_project_root(), "unit_wasm.xml")) + except subprocess.CalledProcessError as e: + log("Error running wasm unit tests:") + log(f"stdout: {e.stdout}") + exit(1) + log("Unit tests passed.")