Golang test main function

Golang test main function

This code shows how you can test the main function in Golang, using the official testing package.

It compiles to an executable file, runs it, and makes sure there is no errors, its also used for other tests in the same main package.

func TestMain(m *testing.M) {
    fmt.Println("-> Building...")
    // checking if Microsoft Windows OS
    // if so, we append .exe to make it excutable by Go
    if runtime.GOOS == "windows" {
        appName += ".exe"
    }
    // We run a command to build the code
    // we are using appName string as tthe name of the excutable
    build := exec.Command("go", "build", "-o", appName)
    if err := build.Run(); err != nil {
        fmt.Fprintf(os.Stderr, "Error building %s: %s", appName, err)
        os.Exit(1)
    }
    fmt.Println("-> Running...")
    // Running the
    result := m.Run()
    fmt.Println("-> Getting done...")
    os.Remove(appName)
    os.Remove(fileName)
    os.Exit(result)
}