Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ on:
pull_request:
types: [opened, synchronize, reopened]


env:
GOLANG_VERSION: 1.21

jobs:

scans:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
with:
fetch-depth: 0

Expand All @@ -31,14 +27,14 @@ jobs:
golang:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
with:
fetch-depth: 0

- name: Setup golang ${{ env.GOLANG_VERSION }}
- name: Setup golang
uses: actions/setup-go@v5
with:
go-version: ${{ env.GOLANG_VERSION }}
go-version-file: go.mod

- name: install tools
run: make tools-get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v5
- name: Release
uses: codfish/semantic-release-action@v3.5.0
env:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work

.idea
53 changes: 49 additions & 4 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
version: "2"

linters-settings:
govet:
disable:
- printf
linters:
enable:
- cyclop
- nestif
- bodyclose
- iface
- gosec
- errcheck
- err113
- errchkjson
- errname
- errorlint
- exptostd
- fatcontext
- forbidigo
- forcetypeassert
- govet
- importas
- ireturn
- recvcheck
- sloglint
- staticcheck
- unparam
- unused
- wastedassign

settings:
staticcheck:
checks:
- "-QF1012"
exclusions:
govet:
disable:
- printf

generated: lax

rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- cyclop
- dupl
- err113
- gosec
- forbidigo
- exhaustruct
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ BINARY_PATH ?= "dist"
ci: log scan test ## Run CI checks

test: ## Run unit tests
go test -v --short -cover -failfast ./...
go test --short -cover -failfast ./...

test-watch: ## Run unit tests in watch mode
gow test -v --short -cover -failfast ./...

scan: ## run security scan
govulncheck ./...
gosec ./...
golangci-lint run ./...
go vet ./...

tools-get: ## Get project tools required
go install golang.org/x/vuln/cmd/govulncheck@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.2.2

# HELP
# This will output the help for each task
Expand Down
4 changes: 2 additions & 2 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

// Assert value is nil
// AssertNil Assert value is nil
//
// Example:
//
Expand Down Expand Up @@ -82,7 +82,7 @@ func AssertEqual(t *testing.T, expected any, actual any) {
t.Helper()

if !isEqual(expected, actual) {
logf(t, decorateDiff(expected, actual))
log(t, decorateDiff(expected, actual))
}
}

Expand Down
4 changes: 2 additions & 2 deletions assert_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package odize

import (
"fmt"
"errors"
"strings"
"testing"
)
Expand Down Expand Up @@ -128,5 +128,5 @@ func TestAssertFalse(t *testing.T) {
}

func TestAssertError(t *testing.T) {
AssertError(t, fmt.Errorf("test"))
AssertError(t, errors.New("test"))
}
18 changes: 11 additions & 7 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package odize

import "fmt"
import "errors"

var (
ErrTestAlreadyExists = errors.New("test already exists")
)

// Error - return error string
func (e *ErrorList) Error() string {
func (e *ListError) Error() string {
result := ""
for _, item := range e.errors {
result += fmt.Sprintf("%s, ", item.Error())
result += item.Error() + ", "
}

return result
}

// Unwrap - returns list of errors
func (e *ErrorList) Unwrap() []error {
func (e *ListError) Unwrap() []error {
return e.errors
}

// Append - append error to list
func (e *ErrorList) Append(err error) {
func (e *ListError) Append(err error) {
e.errors = append(e.errors, err)
}

func (e *ErrorList) Len() int {
func (e *ListError) Len() int {
return len(e.errors)
}

// Pop - remove the first error from the list and return
func (e *ErrorList) Pop() error {
func (e *ListError) Pop() error {
if len(e.errors) == 0 {
return nil
}
Expand Down
13 changes: 8 additions & 5 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestError(t *testing.T) {
list := ErrorList{}
list := ListError{}
list.Append(errors.New("expected"))

if list.Error() != "expected, " {
Expand All @@ -15,17 +15,20 @@ func TestError(t *testing.T) {
}

func TestPop(t *testing.T) {
list := ErrorList{}
list.Append(errors.New("first"))
firstErr := errors.New("first")
list := ListError{}
list.Append(firstErr)
list.Append(errors.New("second"))

expected := list.Pop()
if expected.Error() != "first" {

if !errors.Is(expected, firstErr) {
t.Errorf("expected %s, got %v ", "first", expected.Error())
}
}

func TestPop_no_errors(t *testing.T) {
list := ErrorList{}
list := ListError{}

expected := list.Pop()
if expected != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/code-gorilla-au/odize

go 1.21.5
go 1.25.0

require github.com/code-gorilla-au/env v1.1.1

Expand Down
20 changes: 10 additions & 10 deletions odize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package odize

import (
"errors"
"fmt"
"slices"
"testing"
Expand All @@ -12,12 +13,12 @@ import (
const (
// ODIZE_TAGS is the environment variable that is used to filter tests
ODIZE_TAGS = "ODIZE_TAGS"
// ENV variable declared in pipelines such as Github Actions
// ENV_CI ENV variable declared in pipelines such as Github Actions
ENV_CI = "CI"
)

var (
ErrTestOptionNotAllowedInCI = fmt.Errorf("test option 'Only' not allowed in CI environment")
ErrTestOptionNotAllowedInCI = errors.New("test option 'Only' not allowed in CI environment")
)

// NewGroup - Create a new test group.
Expand Down Expand Up @@ -98,11 +99,11 @@ func (tg *TestGroup) Run() error {

tg.beforeAll()

entries, err := filterExecutableTests(tg.t, tg.isCIEnv, tg.registry)
entries, err := filterExecutableTests(tg.isCIEnv, tg.registry)
if err != nil {
// Stop Run, suite is in an invalid state
tg.complete = true
return fmt.Errorf("Test group \"%s\" error: %w", tg.t.Name(), err)
return fmt.Errorf("test group \"%s\" error: %w", tg.t.Name(), err)
}

for _, entry := range entries {
Expand All @@ -121,7 +122,7 @@ func (tg *TestGroup) Run() error {
// registerTest registers a test to the group. Do not overwrite existing tests.
func (tg *TestGroup) registerTest(name string, testFn TestFn, options TestOpts) error {
if _, ok := tg.cache[name]; ok {
return fmt.Errorf("test already exists: %s", name)
return fmt.Errorf("%w: %s", ErrTestAlreadyExists, name)
}

tg.cache[name] = struct{}{}
Expand Down Expand Up @@ -174,7 +175,6 @@ func shouldSkipTests(groupTags []string, envTags []string) bool {

if len(groupTags) == 0 && len(envTags) == 0 {
// run all tests
fmt.Println("running test")
return false
}

Expand All @@ -189,8 +189,8 @@ func shouldSkipTests(groupTags []string, envTags []string) bool {

// filterExecutableTests filters tests that are executable within the test group
// Note that test option 'Only' is only used for debugging tests, and should not be used in a CI env.
func filterExecutableTests(t *testing.T, isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
filtered, err := filterOnlyAllowedTests(t, isCIEnv, tests)
func filterExecutableTests(isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
filtered, err := filterOnlyAllowedTests(isCIEnv, tests)
if err != nil {
return filtered, err
}
Expand Down Expand Up @@ -220,8 +220,8 @@ func filterExecutableTests(t *testing.T, isCIEnv bool, tests []TestRegistryEntry

// filterOnlyAllowedTests filters tests that are marked as only within a test group
// If the framework detects that the test is running under a CI environment and the group has tests with 'Only', then it will return an error
func filterOnlyAllowedTests(t *testing.T, isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
filtered := []TestRegistryEntry{}
func filterOnlyAllowedTests(isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
var filtered []TestRegistryEntry

for _, test := range tests {
if test.options.Only && isCIEnv {
Expand Down
7 changes: 0 additions & 7 deletions odize_log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package odize

import (
"fmt"
"testing"
)

Expand All @@ -12,9 +11,3 @@ func log(t *testing.T, args ...any) {
t.Error(args...)
t.FailNow()
}

// logf log with formatting
func logf(t *testing.T, format string, args ...any) {
t.Helper()
log(t, fmt.Sprintf(format, args...))
}
6 changes: 3 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type TestGroup struct {
complete bool
registry []TestRegistryEntry
cache map[string]struct{}
errors ErrorList
errors ListError
isCIEnv bool
}

Expand All @@ -41,7 +41,7 @@ type TestOpts struct {
Skip bool
}

// ErrorList - keep track of a number of errors
type ErrorList struct {
// ListError - keep track of a number of errors
type ListError struct {
errors []error
}
Loading