all repos

viye @ aac48460ab9c49d76dc3bf15746ee66308cd50a7

my shot at reimplementing xiki

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

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