Skip to content

Commit

Permalink
[audio] Test coverage for exexcution of audio adaptor
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed May 25, 2016
1 parent c111636 commit 970324c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions platforms/audio/audio_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func (a *AudioAdaptor) Sound(fileName string) []error {
return errorsList
}

cmd := exec.Command(commandName, fileName)
err = cmd.Start()
err = RunCommand(commandName, fileName)
if err != nil {
log.Println(err)
errorsList = append(errorsList, err)
Expand All @@ -75,3 +74,11 @@ func CommandName(fileName string) (commandName string, err error) {
return "", errors.New("Unknown filetype for audio file.")
}
}

var execCommand = exec.Command

func RunCommand(audioCommand string, filename string) error {
cmd := execCommand(audioCommand, filename)
err := cmd.Start()
return err
}
21 changes: 21 additions & 0 deletions platforms/audio/audio_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package audio

import (
"os"
"os/exec"
"testing"

"github.com/hybridgroup/gobot/gobottest"
Expand Down Expand Up @@ -46,3 +48,22 @@ func TestAudioAdaptorSoundWithNonexistingFilename(t *testing.T) {
errors := a.Sound("doesnotexist.mp3")
gobottest.Assert(t, errors[0].Error(), "stat doesnotexist.mp3: no such file or directory")
}

func fakeExecCommand(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
return cmd
}

func TestAudioAdaptorSoundWithValidMP3Filename(t *testing.T) {
execCommand = fakeExecCommand

a := NewAudioAdaptor("tester")
defer func() { execCommand = exec.Command }()

errors := a.Sound("../../examples/laser.mp3")

gobottest.Assert(t, len(errors), 0)
}

0 comments on commit 970324c

Please sign in to comment.