Source

atexit / atexit_test.go

package atexit

import (
	"exec"
	"testing"
	"io/ioutil"
	"time"
	"os"
)

func TestRegister(t *testing.T) {
	current := len(handlers)
	Register(func() {})
	if len(handlers) != current+1 {
		t.Fatalf("can't add handler")
	}
}

func exists(filename string) bool {
	_, err := os.Stat(filename)
	return err == nil
}

func TestHandler(t *testing.T) {
	filename := "/tmp/testprog.out"
	arg := time.UTC().String()

	os.Remove(filename)
	if exists(filename) {
		t.Fatalf("can't delete %s", filename)
	}

	if err := exec.Command("6g", "testprog.go").Run(); err != nil {
		t.Fatalf("can't compile")
	}

	if err := exec.Command("6l", "testprog.6").Run(); err != nil {
		t.Fatalf("can't link\n")
	}

	err := exec.Command("./6.out", filename, arg).Run()
	if wmsg, ok := err.(*os.Waitmsg); ok {
		if wmsg.ExitStatus() != 1 {
			t.Fatalf("bad exit status (%d), should be 1", wmsg.ExitStatus())
		}
	} else {
		t.Fatalf("bad exit status 0, should be 1")
	}

	data, err := ioutil.ReadFile(filename)
	if err != nil {
		t.Fatalf("can't read output file %s", filename)
	}

	if string(data) != arg {
		t.Fatalf("bad data")
	}
}