Skip to content

Commit

Permalink
Merge pull request #7 from CubicrootXYZ/golang-test-workflow
Browse files Browse the repository at this point in the history
add workflow for golang unittesting
  • Loading branch information
CubicrootXYZ authored Aug 11, 2024
2 parents 3d01347 + 9bfd394 commit 9f2e1c2
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golang_quality_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jobs:
golang_quality:
uses: ./.github/workflows/golang_quality.yaml
with:
workdir: "tests/golang_quality"
workdir: "tests/golang_quality/"
32 changes: 32 additions & 0 deletions .github/workflows/golang_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
workflow_call:
inputs:
workdir:
required: false
default: "./"
type: string
services:
required: false
type: string
default: '{}'
env:
required: false
type: string
default: '{}'

jobs:
test:
runs-on: ubuntu-latest
if: "!startsWith(github.ref, 'refs/tags/')"
container:
image: cubicrootxyz/golang-ci:v1.22.5-3
options: --user root
services: ${{ fromJSON(inputs.services) }}
env: ${{ fromJSON(inputs.env) }}
steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Run tests
run: (cd ${{ inputs.workdir }} && CGO_ENABLED=1 && go test -cover -race ./...)
9 changes: 9 additions & 0 deletions .github/workflows/golang_test_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
on: push

jobs:
golang_test:
uses: ./.github/workflows/golang_test.yaml
with:
workdir: "tests/golang_test/"
services: '{"database": {"image": "mysql:8.0", "ports": ["3306:3306"]}}'
env: '{"TESTENV": "value"}'
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,28 @@ jobs:
To ensure code quality use the provided `golang_quality` workflow. It runs multiple analysis tools.

```yaml
permissions:
# Required by golangci job to write annotations to the merge request.
contents: read
checks: write
jobs:
golang_quality:
uses: CubicrootXYZ/Workflows/.github/workflows/[email protected]
with:
workdir: "golang/application/subfolder"
workdir: "golang/application/subfolder/"
```

### Execute Tests

To execute golang tests use the provided `golang_test` workflow.

```yaml
jobs:
golang_test:
uses: ./.github/workflows/golang_test.yaml
with:
workdir: "tests/golang_test/"
services: '{"database": {"image": "mysql:8.0", "ports": ["3306:3306"]}}'
env: '{"TESTENV": "value"}'
```
2 changes: 1 addition & 1 deletion tests/golang_quality/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module golang_quality

go 1.22.1
go 1.22.5
3 changes: 3 additions & 0 deletions tests/golang_test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module golangtest

go 1.22.5
7 changes: 7 additions & 0 deletions tests/golang_test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package golangtest

import "fmt"

func DoSomething(input string) {
fmt.Println(input)
}
19 changes: 19 additions & 0 deletions tests/golang_test/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golangtest_test

import (
"golangtest"
"os"
"testing"
)

func TestSomething(t *testing.T) {
golangtest.DoSomething("a test")
}

func TestEnvVarsAreSet(t *testing.T) {
v := os.Getenv("TESTENV")
t.Logf("env var is: %s", v)
if v != "value" {
t.Fail()
}
}

0 comments on commit 9f2e1c2

Please sign in to comment.