Skip to content

Conversation

lneves12
Copy link

@lneves12 lneves12 commented Feb 6, 2020

Export the command executor, so we can mock all the executed commands for testing.

Explanation: https://npf.io/2015/06/testing-exec-command/

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

const commandMock = "OutputMock"

func TestHelperProcess(t *testing.T) {
	if os.Args[len(os.Args)-1] != "GO_TEST_PROCESS" {
		return
	}

	defer os.Exit(0)
	fmt.Fprintf(os.Stdout, "%s", dockerRunResult)
}

It("new test", func() {
	cmd.ComandExecuter = fakeExecCommand
	defer func() { cmd.ComandExecuter = exec.Command }()

        ... testing here.....
})

Issue: #45

@coveralls
Copy link

coveralls commented Feb 6, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling c6350db on lneves12:export_os_exec into a151197 on go-cmd:master.

@daniel-nichter
Copy link
Member

See #45 for explanation. Thank you, though, for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants