aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/callayer_test.go
blob: 4803fdf3915eb0c637417b3896b0dc55d1da415b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cli

import (
	"bytes"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestRunCalNewAndCheck(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("LECTIO_CONFIG", filepath.Join(dir, "config.ini"))

	var buf bytes.Buffer
	if code := runCalNew("krakow", &buf, &buf); code != 0 {
		t.Fatalf("cal-new exit %d: %s", code, buf.String())
	}
	path := filepath.Join(dir, "calendars", "krakow.ini")
	if _, err := os.Stat(path); err != nil {
		t.Fatal("scaffold not written")
	}

	// the scaffold must validate clean
	buf.Reset()
	if code := runCalCheck("krakow", &buf, &buf); code != 0 {
		t.Fatalf("cal-check on scaffold exit %d: %s", code, buf.String())
	}
	if !strings.Contains(buf.String(), "ok:") {
		t.Errorf("expected ok, got: %s", buf.String())
	}

	// refuse to overwrite
	buf.Reset()
	if code := runCalNew("krakow", &buf, &buf); code == 0 {
		t.Error("cal-new should refuse to overwrite an existing file")
	}
}

func TestRunCalCheckReportsBadFields(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("LECTIO_CONFIG", filepath.Join(dir, "config.ini"))
	os.MkdirAll(filepath.Join(dir, "calendars"), 0o755)
	os.WriteFile(filepath.Join(dir, "calendars", "bad.ini"),
		[]byte("[layer]\nid = bad\n\n[x]\ndate = 99-99\nrank = archfeast\ncolour = teal\n"), 0o644)

	var buf bytes.Buffer
	if code := runCalCheck("bad", &buf, &buf); code == 0 {
		t.Fatalf("cal-check should fail on bad fields:\n%s", buf.String())
	}
	out := buf.String()
	for _, want := range []string{"date", "rank", "colour"} {
		if !strings.Contains(out, want) {
			t.Errorf("expected a %s problem reported:\n%s", want, out)
		}
	}
}