Skip to content

Commit fd5d7d0

Browse files
authored
Add tests for the command line tool (#7)
* Add basic tests for command line tool * Add tests to the workflow
1 parent 5931981 commit fd5d7d0

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

.github/workflows/main.yml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ jobs:
77
steps:
88
- uses: actions/checkout@v3
99

10+
- uses: actions/setup-go@v3
11+
with:
12+
go-version: '^1.19'
13+
1014
- uses: actions/setup-node@v3
1115
with:
1216
node-version: '14'
1317

1418
- name: Install Danger JS
1519
run: npm install -g danger
1620

17-
- uses: actions/setup-go@v3
18-
with:
19-
go-version: '^1.19'
20-
2121
- name: Install danger-go
2222
run: go install github.com/moolmanruan/danger-go/cmd/[email protected] # match version used in build/ci
2323

@@ -31,4 +31,17 @@ jobs:
3131
run: danger-go ci
3232
working-directory: ./build/ci
3333
env:
34-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
36+
test:
37+
name: Tests
38+
runs-on: ubuntu-latest
39+
steps:
40+
- uses: actions/checkout@v3
41+
42+
- uses: actions/setup-go@v3
43+
with:
44+
go-version: '^1.19'
45+
46+
- name: Go tests
47+
run: go test -v ./...

cmd/danger-go/main.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,8 @@ const version = "v0.0.3"
1313

1414
// main entrypoint of the danger-go command
1515
func main() {
16-
// require process (always included) and command/flag
17-
if len(os.Args) < 2 {
18-
fmt.Println(usage)
19-
}
20-
21-
if argsContain("-V", "--version") {
22-
fmt.Printf("danger-go %s\n", version)
23-
return
24-
}
25-
if argsContain("-h", "--help") {
26-
fmt.Println(usage)
16+
if len(os.Args) <= 1 || argsContain("-h", "--help") {
17+
fmt.Print(usage)
2718
return
2819
}
2920

@@ -40,6 +31,8 @@ func main() {
4031
}
4132
case "runner":
4233
runner.Run()
34+
case "version":
35+
fmt.Printf("danger-go %s\n", version)
4336
default:
4437
log.Fatalf("invalid sub-command `%s`\n\n%s", command, usage)
4538
}
@@ -61,12 +54,12 @@ func argsContain(args ...string) bool {
6154
const usage = `Usage: danger-go [options] [command]
6255
6356
Options:
64-
-V, --version Output the version number
6557
-h, --help Output usage information
6658
6759
Commands:
68-
init Helps you get started with DSL
6960
ci Runs DSL on CI
61+
local Runs danger standalone on a repo, useful for git hooks
7062
pr Runs your local Dangerfile against an existing GitHub DSL. Will not post on the DSL
7163
runner Runs a dangerfile against a DSL passed in via STDIN [You probably don't need this]
72-
local Runs danger standalone on a repo, useful for git hooks`
64+
version Show the version of the application
65+
`

cmd/danger-go/main_internal_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
const cliPkg = "github.com/moolmanruan/danger-go/cmd/danger-go"
12+
13+
func execute(args ...string) (string, error) {
14+
cmdArgs := append([]string{"run", cliPkg}, args...)
15+
cmd := exec.Command("go", cmdArgs...)
16+
res, err := cmd.CombinedOutput()
17+
return string(res), err
18+
}
19+
20+
func TestShowsUsage(t *testing.T) {
21+
cc := []struct {
22+
name string
23+
args []string
24+
}{
25+
{name: "no args", args: nil},
26+
{name: "-h arg", args: []string{"-h"}},
27+
{name: "--help arg", args: []string{"--help"}},
28+
}
29+
30+
for _, c := range cc {
31+
t.Run(c.name, func(t *testing.T) {
32+
res, err := execute(c.args...)
33+
require.Nil(t, err)
34+
require.Equal(t, usage, res)
35+
})
36+
}
37+
}
38+
39+
func TestShowsVersion(t *testing.T) {
40+
res, err := execute("version")
41+
require.Nil(t, err)
42+
require.Equal(t, fmt.Sprintf("danger-go %s\n", version), res)
43+
}

0 commit comments

Comments
 (0)