aboutsummaryrefslogtreecommitdiff
path: root/cmd/krino/main_test.go
blob: 6e4df444db4c127f101985832d96b2fedf8c261b (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: GPL-3.0-or-later

package main

import (
	"bytes"
	"strings"
	"testing"

	"krino/internal/config"
)

// runCLI drives run() and returns its exit code and output.
func runCLI(t *testing.T, args ...string) (int, string, string) {
	t.Helper()
	var out, errb bytes.Buffer
	code := run(args, &out, &errb)
	return code, out.String(), errb.String()
}

func TestVersion(t *testing.T) {
	code, out, _ := runCLI(t, "--version")
	if code != 0 || out != "krino dev\n" {
		t.Fatalf("got %d %q, want 0 %q", code, out, "krino dev\n")
	}
}

func TestHelp(t *testing.T) {
	for _, arg := range []string{"-h", "--help"} {
		code, out, _ := runCLI(t, arg)
		if code != 0 || !strings.HasPrefix(out, "usage: krino") {
			t.Errorf("%s: got %d %q", arg, code, out)
		}
	}
}

func TestBadFlag(t *testing.T) {
	code, _, errOut := runCLI(t, "--bogus")
	if code != 2 || !strings.Contains(errOut, "flag provided but not defined: -bogus") {
		t.Fatalf("got %d %q", code, errOut)
	}
}

// TestCommandsAreReserved is item E: every subcommand name must also be a
// reserved directory name, so a directory can never shadow a command.
func TestCommandsAreReserved(t *testing.T) {
	for name := range commands {
		if !config.Reserved[name] {
			t.Errorf("command %q is not in config.Reserved", name)
		}
	}
}

// TestSortNoConfig: apply is implemented as of Task 7, so running with no
// config at all now fails the same way every other command does — "not
// found" from engine.Load, not the old "not implemented yet" hard stop this
// test used to pin (removed as part of Task 7; see cmd/krino/sort.go).
func TestSortNoConfig(t *testing.T) {
	home(t)
	code, _, errOut := runCLI(t)
	if code != 2 || !strings.Contains(errOut, "not found; create it with: krino init") {
		t.Fatalf("got %d %q", code, errOut)
	}
}

func TestVersionStripsTheTagPrefix(t *testing.T) {
	old := version
	t.Cleanup(func() { version = old })
	for _, tc := range []struct{ stamped, want string }{
		{"v0.0.1", "krino 0.0.1\n"},
		{"v0.0.1-3-gabc1234", "krino 0.0.1-3-gabc1234\n"},
		{"v0.0.1-dirty", "krino 0.0.1-dirty\n"},
		{"0.0.1", "krino 0.0.1\n"},
		{"dev", "krino dev\n"},
	} {
		version = tc.stamped
		var out, errOut strings.Builder
		if code := run([]string{"--version"}, &out, &errOut); code != 0 {
			t.Errorf("%s: exit %d", tc.stamped, code)
		}
		if out.String() != tc.want {
			t.Errorf("stamped %q: got %q, want %q", tc.stamped, out.String(), tc.want)
		}
	}
}