Test command

156 views
Skip to first unread message

Archos

unread,
Jul 30, 2012, 8:45:26 AM7/30/12
to golan...@googlegroups.com
I wanted to testing the output of a command and I just check that Go does very easy. I know that could testing functions of a command but not the "full command" through main().

This is really cool. To pass it the argument that you would use in the command line, insert them in os.Args, and then you already run main()

func init() {
    os.Args = []string{"def2go", "-s", "linux", "-p", "foo", "testdata/ioctls.h"}
}

func Test(t *testing.T) {
    main()
}

And it runs like if were done from command line. The only problem is that it can not be used the example functions to checking whether the ouput is like I was expecting.

Is there any technical issue for that the example functions can not be run in commands?

Patrick Mylund Nielsen

unread,
Jul 30, 2012, 8:52:13 AM7/30/12
to Archos, golan...@googlegroups.com
Since you're not using testing.T, why not just run the command?

Archos

unread,
Jul 30, 2012, 8:58:34 AM7/30/12
to golan...@googlegroups.com
Because I would want to check that the output of that command is like I'm expecting which could be done from exampla functions.

Aram Hăvărneanu

unread,
Jul 30, 2012, 9:00:46 AM7/30/12
to Archos, golan...@googlegroups.com
Use a <cmd>_test package instead of a test package to test the command
out of process.

--
Aram Hăvărneanu

Patrick Mylund Nielsen

unread,
Jul 30, 2012, 9:03:40 AM7/30/12
to Archos, golan...@googlegroups.com
If you really need to do this, run it with os/exec, store the return value, and write the appropriate message with t.Errorf().
path, err := exec.LookPath("fortune")
if err != nil {
    log.Fatal("installing fortune is in your future")
}
fmt.Printf("fortune is available at %s\n", path)
Can't you just have main() launch a function with whatever parameters in your program, and have your test launch the same functions with different parameters?

Archos

unread,
Jul 30, 2012, 9:15:23 AM7/30/12
to golan...@googlegroups.com
But it does not compare the output whith another one, and the example functions does easy to see what it is expected.

Archos

unread,
Jul 30, 2012, 9:18:13 AM7/30/12
to golan...@googlegroups.com
I already the test file in <cmd>_test.go

package main

import (
    "testing"
)
...

Aram Hăvărneanu

unread,
Jul 30, 2012, 9:43:49 AM7/30/12
to Archos, golan...@googlegroups.com
> I already the test file in <cmd>_test.go

No, what I meant is that you should use a <cmd>_test package that uses
os/exec in the Example[A-Z] functions to run your command and validate
the output against the sample output.

--
Aram Hăvărneanu

Archos

unread,
Jul 30, 2012, 10:00:03 AM7/30/12
to golan...@googlegroups.com
It looks that it's the better alternative

// TODO: use exec.LookPath()
func ExampleHello() {
    out, _ := exec.Command("def2go", "-s", "linux", "-p", "foo", "testdata/ioctls.h").Output()
    fmt.Print(string(out))
    // Output:
    // whatever...
}

Before, it should be checked if the binary to run is not in the PATH environment, to build it before of execute command.

Archos

unread,
Jul 30, 2012, 10:57:26 AM7/30/12
to golan...@googlegroups.com
For if somebody needs it:

package main

import (
    "fmt"
    "log"
    "os/exec"
)

const EXEC = [BINARY NAME]

var execPath string

func init() {
    var err error
    execPath, err = exec.LookPath(EXEC)
    if err != nil {
        if err.(*exec.Error).Err == exec.ErrNotFound {
            if err2 := exec.Command("go", "build").Run(); err2 != nil {
                log.Fatal(err2)
            }
        } else {
            log.Fatal(err)
        }
    }
}

func Example() {
    out, _ := exec.Command(execPath, [ARGS]).Output()
    fmt.Print(string(out))
    // Output:
    // ...
}

Archos

unread,
Jul 31, 2012, 8:13:19 AM7/31/12
to golan...@googlegroups.com
Link to template for testing the output of a command:

https://github.com/kless/wizard/wiki/Tester
Reply all
Reply to author
Forward
0 new messages