lunarmodules/luasocket

Unit tests should not os.exit()

Opened this issue · 1 comments

In my attempts to run unit tests from my own Go lang based runner, I find myself having to rewrite all os.exit() calls to become error() instead, so that test failures can be detected and reported.

In addressing this, it might also be the ideal time to adopt the Busted unit test framework. I would be able to support such an effort.

Other test frameworks use a non-zero exit status, which is useful for even this simple bash test harness:

lua test/hello.lua || { printf "test failed\n" >&2 ; exit 1; }

In fact, the LuaSocket makefile's make test target runs exactly this way.

A Go program can invoke tests in a subprocess and inspect exit status in a manner similar to the following:

package main

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

func main() {
    var out bytes.Buffer
    cmd := exec.Command("lua", "test/hello.lua")
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Printf("%q\n", out.String() )
}

Which in the success case does this:

$ go run test.go
"Hello from LuaSocket 3.0-rc1 and MIME 1.0.3!\n"

And in the failure case does this:

$ go run test.go
2019/02/18 02:58:35 exit status 1
exit status 1

Let me know if that approach doesn't work for you for some reason and we can try to figure out why.

We're reviewing open items in preparation for a release. If action is needed here, please add a comment. Otherwise, this issue will be closed on or after 24-Feb-2019.

Thanks!