Commits

Miki Tebeka committed df97175

Initial implementation

  • Participants

Comments (0)

Files changed (4)

+include $(GOROOT)/src/Make.inc
+
+TARG=atexit
+
+GOFILES= \
+	atexit.go
+
+include $(GOROOT)/src/Make.pkg
+package atexit
+
+import (
+	"os"
+)
+
+var handlers = []func(){}
+
+func Exit(code int) {
+	for _, handler := range handlers {
+		handler()
+	}
+
+	os.Exit(code)
+}
+
+func Register(handler func()) {
+	handlers = append(handlers, handler)
+}
+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 err.String() != "exit status 1" {
+		t.Fatalf("bad exit status (%s), should be 1", err)
+	}
+
+	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")
+	}
+}
+// Test program for atexit, gets output file and data as arguments and writes
+// data to output file in atexit handler.
+package main
+
+import (
+	atexit "./_obj/atexit"
+	"flag"
+	"io/ioutil"
+)
+
+var outfile = ""
+var data = ""
+
+func handler() {
+	ioutil.WriteFile(outfile, []byte(data), 0666)
+}
+
+func main() {
+	flag.Parse()
+	outfile = flag.Arg(0)
+	data = flag.Arg(1)
+
+	atexit.Register(handler)
+	atexit.Exit(1)
+}