From caee7f1885cb56f0cf3bbdb21ecac2c25f2e40ea Mon Sep 17 00:00:00 2001
From: Hampus Carlsson <carlsson889@gmail.com>
Date: Sat, 6 Apr 2024 12:47:28 +0200
Subject: [PATCH] Add go unit test workflow

Makes use of https://github.com/GoTestTools/gotestfmt to generate
readable unit test reports.
---
 .github/workflows/golangci-test.yml | 40 +++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 .github/workflows/golangci-test.yml

diff --git a/.github/workflows/golangci-test.yml b/.github/workflows/golangci-test.yml
new file mode 100644
index 00000000..de9c5ef8
--- /dev/null
+++ b/.github/workflows/golangci-test.yml
@@ -0,0 +1,40 @@
+name: golangci-test
+on:
+  push:
+    branches:
+      - main
+  pull_request:
+
+jobs:
+  unit-tests:
+    runs-on: ubuntu-20.04
+    steps:
+      - uses: actions/checkout@v4
+      - uses: actions/setup-go@v5
+        with:
+          go-version-file: 'go.mod'
+      - run: go version
+
+      - name: Install dependencies
+        run: make check-tensorflow
+
+      - name: Set environment variables for CGO
+        run: |
+          echo "CGO_ENABLED=1" >> $GITHUB_ENV
+          echo "CGO_CFLAGS=-I $HOME/src/tensorflow" >> $GITHUB_ENV
+
+      - name: Set up gotestfmt
+        run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
+
+      - name: Run unit tests
+        run: |
+          set -euo pipefail
+          go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt -ci github
+
+      - name: Upload test log
+        uses: actions/upload-artifact@v2
+        if: always()
+        with:
+          name: test-log
+          path: /tmp/gotest.log
+          if-no-files-found: error