Skip to content

Commit

Permalink
feature: implement a new command to support gotestsum (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-montano-circleci authored Dec 2, 2024
1 parent a6304f1 commit e2df0e6
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 5 deletions.
44 changes: 44 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,42 @@ jobs:
- go/install:
version: 1.16.4
- run: go version && go build ./...
go-test-default:
docker:
- image: cimg/go:1.23.3
steps:
- checkout
- go/test:
project-path: tests
int-test-gotestsum-1-test:
docker:
- image: cimg/go:1.23.3
steps:
- checkout
- go/gotestsum:
project-path: tests
packages: circleci.com/go-orb/numbers
- store_test_results:
path: /home/circleci/project/tests
int-test-gotestsum-2-test:
docker:
- image: cimg/go:1.23.3
steps:
- checkout
- go/gotestsum:
project-path: tests
packages: circleci.com/go-orb/numbers circleci.com/go-orb/words
- store_test_results:
path: /home/circleci/project/tests
int-test-gotestsum-all-test:
docker:
- image: cimg/go:1.23.3
steps:
- checkout
- go/gotestsum:
project-path: tests
- store_test_results:
path: /home/circleci/project/tests
workflows:
test-deploy:
jobs:
Expand Down Expand Up @@ -164,6 +200,14 @@ workflows:
filters: *filters
- int-test-vm-arm64:
filters: *filters
- go-test-default:
filters: *filters
- int-test-gotestsum-1-test:
filters: *filters
- int-test-gotestsum-2-test:
filters: *filters
- int-test-gotestsum-all-test:
filters: *filters
- orb-tools/pack:
filters: *release-filters
- orb-tools/publish:
Expand Down
36 changes: 36 additions & 0 deletions src/commands/gotestsum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
description: |
Runs 'go test ./...' but includes extensive parameterization for finer tuning
parameters:
project-path:
description: |
The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
type: string
default: ""
packages:
description: import tests to run, by path glob.
type: string
default: "./..."
junitfile:
description: file to save junit format xml file
type: string
default: "unit-tests.xml"
covermode:
description: mode used to count coverage
type: string
default: "atomic"
coverprofile:
description: file to save coverage profile
type: string
default: "cover-source.out"
steps:
- run:
environment:
ORB_EVAL_PROJECT_PATH: <<parameters.project-path>>
ORB_VAL_PACKAGES: <<parameters.packages>>
ORB_VAL_JUNITFILE: <<parameters.junitfile>>
ORB_VAL_COVERMODE: <<parameters.covermode>>
ORB_EVAL_COVER_PROFILE: <<parameters.coverprofile>>
command: <<include(scripts/gotestsum.sh)>>
name: "gotestsum"
10 changes: 9 additions & 1 deletion src/commands/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ parameters:
(Sets -cover.)
type: string
default: "./..."
project-path:
description: |
The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
type: string
default: ""
steps:
- run:
environment:
ORB_VAL_RACE: <<parameters.race>>
ORB_VAL_COUNT: <<parameters.count>>
ORB_VAL_FAIL_FAST: <<parameters.failfast>>
ORB_VAL_FAILFAST: <<parameters.failfast>>
ORB_VAL_SHORT: <<parameters.short>>
ORB_VAL_TIMEOUT: <<parameters.timeout>>
ORB_VAL_PARALLEL: <<parameters.parallel>>
Expand All @@ -74,6 +81,7 @@ steps:
ORB_VAL_PACKAGES: <<parameters.packages>>
ORB_VAL_COVERPKG: <<parameters.coverpkg>>
ORB_EVAL_COVER_PROFILE: <<parameters.coverprofile>>
ORB_EVAL_PROJECT_PATH: <<parameters.project-path>>
command: <<include(scripts/test.sh)>>
name: "go test"
no_output_timeout: <<parameters.no_output_timeout>>
36 changes: 36 additions & 0 deletions src/scripts/gotestsum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#! /bin/bash

if [ ! "$(which gotestsum)" ]; then
echo The gotestsum command is not available in this executor.
exit 1
fi

if [ -n "${ORB_EVAL_PROJECT_PATH}" ]; then
cd "${ORB_EVAL_PROJECT_PATH}" || exit
fi

if [ -n "$ORB_VAL_PACKAGES" ];then
set -- "$@" --packages "${ORB_VAL_PACKAGES}"
fi

if [ -n "$ORB_VAL_JUNITFILE" ];then
set -- "$@" --junitfile "${ORB_VAL_JUNITFILE}"
fi

if [ -n "$ORB_VAL_COVERMODE" ];then
COVERMODE="-covermode=${ORB_VAL_COVERMODE}"
fi

if [ -n "$ORB_EVAL_COVER_PROFILE" ];then
INPUT_COVER_PROFILE=$(eval echo "$ORB_EVAL_COVER_PROFILE")
COVER_PROFILE="-coverprofile=${INPUT_COVER_PROFILE}"
fi

set -x
gotestsum \
"$@" \
-- \
"${COVERMODE}" \
"${COVER_PROFILE}" \
-coverpkg="$ORB_VAL_PACKAGES"
set +x
6 changes: 6 additions & 0 deletions src/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#!/usr/bin/env bash

if [ -n "${ORB_EVAL_PROJECT_PATH}" ]; then
cd "${ORB_EVAL_PROJECT_PATH}" || exit
fi

COVER_PROFILE=$(eval echo "$ORB_EVAL_COVER_PROFILE")

if [ -n "$ORB_VAL_RACE" ]; then
set -- "$@" -race
ORB_VAL_COVER_MODE=atomic
fi

if [ "$ORB_VAL_FAILFAST" != "false" ]; then
Expand Down
4 changes: 2 additions & 2 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module main
module circleci.com/go-orb

go 1.20
go 1.23.3
13 changes: 11 additions & 2 deletions tests/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// main.go
package main

import (
"fmt"

"circleci.com/go-orb/numbers"
"circleci.com/go-orb/words"
)

func main() {
println("Hello, World!")
}
word := words.Word()
number := numbers.Number()
fmt.Printf("%s %d\n", word, number)
}
5 changes: 5 additions & 0 deletions tests/numbers/numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package numbers

func Number() int {
return 1987
}
10 changes: 10 additions & 0 deletions tests/numbers/numbers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package numbers

import "testing"

func TestNumber(t *testing.T) {
result := Number()
if result != 1987 {
t.FailNow()
}
}
5 changes: 5 additions & 0 deletions tests/words/words.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package words

func Word() string {
return "Word"
}
10 changes: 10 additions & 0 deletions tests/words/words_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package words

import "testing"

func TestWord(t *testing.T) {
result := Word()
if result != "Word" {
t.FailNow()
}
}

0 comments on commit e2df0e6

Please sign in to comment.