ajhahn.de
← eeco
Go 42 lines
package main

import (
	"bytes"
	"os"
	"strings"
	"testing"
)

func TestRunAuthorize_WritesSentinel(t *testing.T) {
	root := setupInited(t)

	var out, errOut bytes.Buffer
	if code := runAuthorize([]string{"commit"}, &out, &errOut); code != 0 {
		t.Fatalf("runAuthorize commit = %d, stderr=%q", code, errOut.String())
	}
	if _, err := os.Stat(wsPath(root, "state", "git-commit-authorized")); err != nil {
		t.Errorf("commit sentinel not written: %v", err)
	}
	if !strings.Contains(out.String(), "authorized") {
		t.Errorf("output missing confirmation: %q", out.String())
	}

	if code := runAuthorize([]string{"tag"}, &out, &errOut); code != 0 {
		t.Fatalf("runAuthorize tag = %d, stderr=%q", code, errOut.String())
	}
	if _, err := os.Stat(wsPath(root, "state", "git-tag-authorized")); err != nil {
		t.Errorf("tag sentinel not written: %v", err)
	}
}

func TestRunAuthorize_RejectsBadArg(t *testing.T) {
	var out, errOut bytes.Buffer
	for _, args := range [][]string{{}, {"push"}, {"commit", "extra"}} {
		out.Reset()
		errOut.Reset()
		if code := runAuthorize(args, &out, &errOut); code != 2 {
			t.Errorf("runAuthorize(%v) = %d, want 2", args, code)
		}
	}
}