all repos

viye @ fa602cd

my shot at reimplementing xiki

viye/internal/plumbing/files/files_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
fix files tests, 2 months ago
1
package files
2
3
import (
4
	"os"
5
	"path/filepath"
6
	"strings"
7
	"testing"
8
9
	"github.com/olexsmir/viye/internal/viye"
10
)
11
12
func TestMatch(t *testing.T) {
13
	dir := t.TempDir()
14
	os.Mkdir(filepath.Join(dir, "sub"), 0o755)
15
	os.WriteFile(filepath.Join(dir, "f"), nil, 0o644)
16
17
	tests := []struct {
18
		p, dir string
19
		m      bool
20
	}{
21
		{"~", ".", true},
22
		{"/tmp", ".", true},
23
		{dir, ".", true},
24
		{"sub", dir, true},
25
		{"f", dir, true},
26
		{"nope", dir, false},
27
		{"$ ls", dir, false},
28
		{".", dir, true},
29
		{"..", dir, true},
30
	}
31
	for _, tt := range tests {
32
		got := (&Tool{}).Match(&viye.Context{Path: []string{tt.p}, Dir: tt.dir})
33
		if got != tt.m {
34
			t.Errorf("Match(%q, dir=%q) = %v; want %v", tt.p, tt.dir, got, tt.m)
35
		}
36
	}
37
}
38
39
func TestExecute(t *testing.T) {
40
	t.Run("list dir", func(t *testing.T) {
41
		dir := t.TempDir()
42
		os.WriteFile(filepath.Join(dir, "x"), nil, 0o644)
43
		ctx := &viye.Context{Path: []string{dir}, Dir: "."}
44
		got, err := (&Tool{}).Execute(ctx)
45
		if err != nil {
46
			t.Fatal(err)
47
		}
48
		want := "| x\n"
49
		if got != want {
50
			t.Errorf("Execute = %q; want %q", got, want)
51
		}
52
	})
53
54
	t.Run("navigate into subdir", func(t *testing.T) {
55
		dir := t.TempDir()
56
		sub := filepath.Join(dir, "sub")
57
		os.Mkdir(sub, 0o755)
58
		os.WriteFile(filepath.Join(sub, "y"), nil, 0o644)
59
60
		v := viye.New()
61
		v.Register(&Tool{})
62
		var out strings.Builder
63
		if err := v.Run(&out, []string{"viye", dir, "sub"}); err != nil {
64
			t.Fatal(err)
65
		}
66
		want := "| y\n"
67
		if out.String() != want {
68
			t.Errorf("Run = %q; want %q", out.String(), want)
69
		}
70
	})
71
72
	t.Run("non existent path", func(t *testing.T) {
73
		ctx := &viye.Context{Path: []string{"/nonexistent_foobar"}, Dir: "."}
74
		if _, err := (&Tool{}).Execute(ctx); err == nil {
75
			t.Error("expected error")
76
		}
77
	})
78
}
79
80
func TestResolve(t *testing.T) {
81
	home, _ := os.UserHomeDir()
82
	tests := []struct {
83
		p, dir string
84
		want   string
85
	}{
86
		{"~", ".", home},
87
		{"~/foo", ".", filepath.Join(home, "foo")},
88
		{"/abs/path", ".", "/abs/path"},
89
		{"rel", "/base", "/base/rel"},
90
		{".", "/base", "/base"},
91
		{"..", "/base/sub", "/base"},
92
	}
93
	for _, tt := range tests {
94
		got := resolve(tt.p, tt.dir)
95
		if got != tt.want {
96
			t.Errorf("resolve(%q, %q) = %q; want %q", tt.p, tt.dir, got, tt.want)
97
		}
98
	}
99
}
100
101
func TestListDir(t *testing.T) {
102
	dir := t.TempDir()
103
	os.Mkdir(filepath.Join(dir, "sub"), 0o755)
104
	os.WriteFile(filepath.Join(dir, "a.txt"), nil, 0o644)
105
106
	got, err := listDir(dir)
107
	if err != nil {
108
		t.Fatal(err)
109
	}
110
	if got != "a.txt\nsub/\n" {
111
		t.Errorf("listDir = %q; want %q", got, "a.txt\\nsub/\\n")
112
	}
113
}