Skip to content

Commit 3c39494

Browse files
committed
all: added basic tests
Use testscript to test go-chromecast command helptext and the version. Added _maybe_ working github action to test.
1 parent 5db2340 commit 3c39494

File tree

11 files changed

+126
-13
lines changed

11 files changed

+126
-13
lines changed

.github/workflows/goreleaser.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ jobs:
1111
steps:
1212
- name: Checkout
1313
uses: actions/checkout@master
14+
- name: Set up Go
15+
uses: actions/setup-go@v1
16+
with:
17+
go-version: '1.12'
1418
- name: Run GoReleaser
1519
uses: goreleaser/goreleaser-action@master
1620
with:

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
strategy:
8+
matrix:
9+
go-version: [1.12.x]
10+
platform: [ubuntu-latest, macos-latest]
11+
runs-on: ${{ matrix.platform }}
12+
steps:
13+
- name: Install Go
14+
uses: actions/setup-go@v1
15+
with:
16+
go-version: ${{ matrix.go-version }}
17+
- name: Checkout code
18+
uses: actions/checkout@v1
19+
- name: Test
20+
run: go test ./...

cmd/root.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"os"
2019
"time"
2120

2221
"github.com/spf13/cobra"
@@ -50,7 +49,7 @@ command line.`,
5049

5150
// Execute adds all child commands to the root command and sets flags appropriately.
5251
// This is called by main.main(). It only needs to happen once to the rootCmd.
53-
func Execute(version, commit, date string) {
52+
func Execute(version, commit, date string) int {
5453
Version = version
5554
Commit = commit
5655
if date != "" {
@@ -59,9 +58,9 @@ func Execute(version, commit, date string) {
5958
Date = time.Now().UTC().Format(time.RFC3339)
6059
}
6160
if err := rootCmd.Execute(); err != nil {
62-
fmt.Println(err)
63-
os.Exit(1)
61+
return 1
6462
}
63+
return 0
6564
}
6665

6766
func init() {

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ require (
1313
github.com/mitchellh/go-homedir v1.1.0
1414
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d // indirect
1515
github.com/pkg/errors v0.8.1
16+
github.com/rogpeppe/go-internal v1.5.0
1617
github.com/sirupsen/logrus v1.4.0
1718
github.com/spf13/cobra v0.0.3
1819
github.com/spf13/pflag v1.0.3 // indirect
1920
google.golang.org/api v0.3.0
2021
google.golang.org/genproto v0.0.0-20190321212433-e79c0c59cdb5
21-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
22-
gopkg.in/yaml.v2 v2.2.2 // indirect
2322
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
7878
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
7979
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
8080
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
81+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
82+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
83+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
8184
github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
85+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
86+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
8287
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
8388
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
8489
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
@@ -113,6 +118,8 @@ github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R
113118
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
114119
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
115120
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
121+
github.com/rogpeppe/go-internal v1.5.0 h1:Usqs0/lDK/NqTkvrmKSwA/3XkZAs7ZAW/eLeQ2MVBTw=
122+
github.com/rogpeppe/go-internal v1.5.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
116123
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
117124
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
118125
github.com/sirupsen/logrus v1.4.0 h1:yKenngtzGh+cUSSh6GWbxW2abRqhYUSR/t/6+2QqNvE=
@@ -213,7 +220,10 @@ google.golang.org/grpc v1.19.0 h1:cfg4PD8YEdSFnm7qLV4++93WcmhH2nIUhMjhdCvl3j8=
213220
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
214221
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
215222
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
223+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
216224
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
225+
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
226+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
217227
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
218228
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
219229
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=

main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
package main
1616

17-
import "github.com/vishen/go-chromecast/cmd"
17+
import (
18+
"os"
19+
20+
"github.com/vishen/go-chromecast/cmd"
21+
)
1822

1923
var (
2024
// These are build-time variables that get set by goreleaser.
@@ -24,5 +28,9 @@ var (
2428
)
2529

2630
func main() {
27-
cmd.Execute(version, commit, date)
31+
os.Exit(main1())
32+
}
33+
34+
func main1() int {
35+
return cmd.Execute(version, commit, date)
2836
}

main_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/rogpeppe/go-internal/testscript"
8+
)
9+
10+
func TestMain(m *testing.M) {
11+
os.Exit(testscript.RunMain(m, map[string]func() int{
12+
"go-chromecast": main1,
13+
}))
14+
}
15+
16+
func TestCommands(t *testing.T) {
17+
testscript.Run(t, testscript.Params{
18+
Dir: "testdata",
19+
})
20+
}

testdata/hello.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# hello world
2+
exec cat hello.text
3+
stdout 'hello world\n'
4+
! stderr .
5+
6+
-- hello.text --
7+
hello world

testdata/helptext.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Check Helptext
2+
go-chromecast
3+
cmp stdout expected-helptext.txt
4+
! stderr .
5+
6+
7+
-- expected-helptext.txt --
8+
Control your Google Chromecast or Google Home Mini from the
9+
command line.
10+
11+
Usage:
12+
go-chromecast [flags]
13+
go-chromecast [command]
14+
15+
Available Commands:
16+
help Help about any command
17+
load Load and play media on the chromecast
18+
ls List devices
19+
next Play the next available media
20+
pause Pause the currently playing media on the chromecast
21+
playlist Load and play media on the chromecast
22+
previous Play the previous available media
23+
restart Restart the currently playing media
24+
rewind Rewind by seconds the currently playing media
25+
seek Seek by seconds into the currently playing media
26+
status Current chromecast status
27+
stop Stop casting
28+
tts text-to-speech
29+
ui Run the UI
30+
unpause Unpause the currently playing media on the chromecast
31+
volume Get or set volume
32+
watch Watch all events sent from a chromecast device
33+
34+
Flags:
35+
-a, --addr string Address of the chromecast device
36+
-v, --debug debug logging
37+
-d, --device string chromecast device, ie: 'Chromecast' or 'Google Home Mini'
38+
-n, --device-name string chromecast device name
39+
--disable-cache disable the cache
40+
-h, --help help for go-chromecast
41+
-i, --iface string Network interface to use when looking for a local address to use for the http server
42+
-p, --port string Port of the chromecast device if 'addr' is specified (default "8009")
43+
-u, --uuid string chromecast device uuid
44+
--version display command version
45+
--with-ui run with a UI
46+
47+
Use "go-chromecast [command] --help" for more information about a command.

testdata/version.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Version
2+
go-chromecast --version
3+
stdout 'go-chromecast dev \(master\)'
4+
! stderr .

0 commit comments

Comments
 (0)