@ 20dc26e627d8341795ca628664e44a83b66d316a | history
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"government/ratify"
)
func runCmd(args ...string) (code int, stdout, stderr string) {
var out, errb bytes.Buffer
code = run(args, &out, &errb)
return code, out.String(), errb.String()
}
// sqSource is a small program the CLI tests write to a temp dir, so they
// do not depend on which examples exist.
const sqSource = "; sq.gov.lisp: the square of a number.\n(define sq (lambda (x) (* x x)))\n(sq 12)\n"
func writeSource(t *testing.T, name, src string) string {
t.Helper()
path := filepath.Join(t.TempDir(), name+".gov.lisp")
if err := os.WriteFile(path, []byte(src), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func TestCompileIsDeterministic(t *testing.T) {
src := writeSource(t, "sq", sqSource)
dir := t.TempDir()
a, b := filepath.Join(dir, "a.gov"), filepath.Join(dir, "b.gov")
if code, _, stderr := runCmd("compile", src, "-o", a); code != exitOK {
t.Fatalf("exit %d: %s", code, stderr)
}
if code, _, stderr := runCmd("compile", "-o", b, src); code != exitOK {
t.Fatalf("exit %d: %s", code, stderr)
}
x, _ := os.ReadFile(a)
y, _ := os.ReadFile(b)
if len(x) == 0 || !bytes.Equal(x, y) {
t.Error("two compiles of sq.gov.lisp differ")
}
if !strings.HasPrefix(string(x), "-----BEGIN CERTIFICATE-----") {
t.Errorf("text before the first block: %.60q", x)
}
}
func TestCompileSourceFlag(t *testing.T) {
code, stdout, stderr := runCmd("compile", "--source", writeSource(t, "sq", sqSource))
if code != exitOK {
t.Fatalf("exit %d: %s", code, stderr)
}
if !strings.HasPrefix(stdout, " ; sq.gov.lisp: the square of a number.\n (define sq (lambda (x) (* x x)))\n") {
t.Errorf("source missing: %.120q", stdout)
}
}
func TestCompileToStdout(t *testing.T) {
code, stdout, _ := runCmd("compile", writeSource(t, "sq", sqSource))
if code != exitOK || !strings.Contains(stdout, "-----BEGIN CERTIFICATE-----") {
t.Errorf("exit %d, output %.60q", code, stdout)
}
}
func TestCompileNamesProgramAfterFile(t *testing.T) {
code, stdout, _ := runCmd("compile", writeSource(t, "sq", sqSource))
certs, err := ratify.Load([]byte(stdout))
if code != exitOK || err != nil || certs[0].Subject.Organization[0] != "sq" {
t.Errorf("exit %d, %v; want O=sq", code, err)
}
}
func TestCompileErrors(t *testing.T) {
dir := t.TempDir()
bad := filepath.Join(dir, "bad.gov.lisp")
os.WriteFile(bad, []byte("(+ 1\n (* 2"), 0o644)
tests := []struct {
args []string
want string
}{
{[]string{"compile", bad}, "bad.gov.lisp:"},
{[]string{"compile", filepath.Join(dir, "missing.gov.lisp")}, "no such file"},
{[]string{"compile"}, "usage"},
{[]string{"compile", "a.gov.lisp", "b.gov.lisp"}, "usage"},
{[]string{"frobnicate"}, "unknown command"},
{[]string{"compile", "--frobnicate", "x.gov.lisp"}, "unknown flag: --frobnicate"},
{[]string{"compile", "-name", "x", "x.gov.lisp"}, "unknown shorthand flag: 'n' in -name"},
}
for _, tt := range tests {
code, _, stderr := runCmd(tt.args...)
if code != exitInvalid || !strings.Contains(stderr, tt.want) {
t.Errorf("%v: exit %d, stderr %q; want exit %d mentioning %q", tt.args, code, stderr, exitInvalid, tt.want)
}
}
}
// TestHelp checks that bare "government" and --help list the commands,
// and that each command has help of its own.
func TestHelp(t *testing.T) {
for _, args := range [][]string{nil, {"--help"}, {"help"}} {
code, stdout, _ := runCmd(args...)
if code != exitOK || !strings.Contains(stdout, "repeal") || !strings.Contains(stdout, "Available Commands") {
t.Errorf("%v: exit %d, %q", args, code, stdout)
}
}
for _, cmd := range []string{"compile", "run", "tree", "decompile", "verify", "repeal"} {
code, stdout, _ := runCmd(cmd, "--help")
if code != exitOK || !strings.Contains(stdout, "government "+cmd) {
t.Errorf("%s --help: exit %d, %q", cmd, code, stdout)
}
}
}