Commits

Miki Tebeka committed 13d826d

first import

Comments (0)

Files changed (2)

+// Emulate Perl's "die" function
+package die
+
+import (
+	"fmt"
+	"os"
+)
+
+// Die prints error message and aborts the program
+func Die(format string, args ...interface{}) {
+	msg := fmt.Sprintf(format, args...)
+	fmt.Fprintf(os.Stderr, "error: %s\n", msg)
+	os.Exit(1)
+}
+package die
+
+import (
+	"bytes"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"os/exec"
+	"strings"
+	"testing"
+	"time"
+)
+
+func TestDie(t *testing.T) {
+	gofile := "/tmp/testprog.go"
+	if err := ioutil.WriteFile(gofile, testprog, 0666); err != nil {
+		t.Fatalf("can't create go file")
+	}
+
+	if err := os.Setenv("GOPATH", "../.."); err != nil {
+		t.Fatalf("can't set GOPATH environment variable")
+	}
+
+	arg := time.Now().UTC().String()
+	cmd := exec.Command("go", "run", gofile, arg)
+	var stderr bytes.Buffer
+	cmd.Stderr = &stderr
+	err := cmd.Run()
+	if err == nil {
+		t.Fatalf("completed normally, should have failed")
+	}
+
+	expected := fmt.Sprintf("error: %s", arg)
+	lines := strings.Split(stderr.String(), "\n")
+	out := lines[0]  // go run add it's own junk at the end
+	if out != expected {
+		t.Fatalf("bad data, got:\n%s\nwanted:\n%s", out, expected)
+	}
+}
+
+var testprog = []byte(`
+// Test program for die, gets output file and data as arguments and writes
+// data to output file in atexit handler.
+package main
+
+import (
+	"die"
+	"flag"
+)
+
+var data = ""
+
+func main() {
+	flag.Parse()
+	data = flag.Arg(0)
+
+	die.Die("%s", data)
+}
+`)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.