File: files_test.go

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (125 lines) | stat: -rw-r--r-- 3,568 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package cli

import (
	"fmt"
	"github.com/kovidgoyal/kitty/tools/utils"
	"os"
	"path/filepath"
	"reflect"
	"sort"
	"strings"
	"testing"
)

var _ = fmt.Print

func TestCompleteFiles(t *testing.T) {
	tdir := t.TempDir()
	cwd, _ := os.Getwd()
	if cwd != "" {
		defer os.Chdir(cwd)
	}
	os.Chdir(tdir)

	create := func(parts ...string) {
		f, _ := os.Create(filepath.Join(tdir, filepath.Join(parts...)))
		f.Close()
	}
	create("one.txt")
	create("two.txt")
	os.Mkdir(filepath.Join(tdir, "odir"), 0700)
	create("odir", "three.txt")
	create("odir", "four.txt")

	test_candidates := func(prefix string, expected ...string) {
		if expected == nil {
			expected = make([]string, 0)
		}
		sort.Strings(expected)
		actual := make([]string, 0, len(expected))
		CompleteFiles(prefix, func(entry *FileEntry) {
			actual = append(actual, entry.CompletionCandidate)
			if _, err := os.Stat(entry.Abspath); err != nil {
				t.Fatalf("Abspath does not exist: %#v", entry.Abspath)
			}
		}, "")
		sort.Strings(actual)
		if !reflect.DeepEqual(expected, actual) {
			t.Fatalf("Did not get expected completion candidates for prefix: %#v\nExpected: %#v\nActual:   %#v", prefix, expected, actual)
		}
	}

	test_abs_candidates := func(prefix string, expected ...string) {
		e := make([]string, len(expected))
		for i, x := range expected {
			if filepath.IsAbs(x) {
				e[i] = x
			} else {
				e[i] = filepath.Join(tdir, x)
				if strings.HasSuffix(x, utils.Sep) {
					e[i] += utils.Sep
				}
			}
		}
		test_candidates(prefix, e...)
	}

	test_cwd_prefix := func(prefix string, expected ...string) {
		e := make([]string, len(expected))
		for i, x := range expected {
			e[i] = "./" + x
		}
		test_candidates("./"+prefix, e...)
	}

	test_cwd_prefix("", "one.txt", "two.txt", "odir/")
	test_cwd_prefix("t", "two.txt")
	test_cwd_prefix("x")

	test_abs_candidates(tdir+utils.Sep, "one.txt", "two.txt", "odir/")
	test_abs_candidates(filepath.Join(tdir, "o"), "one.txt", "odir/")

	test_candidates("", "one.txt", "two.txt", "odir/")
	test_candidates("t", "two.txt")
	test_candidates("o", "one.txt", "odir/")
	test_candidates("odir", "odir/")
	test_candidates("odir/", "odir/three.txt", "odir/four.txt")
	test_candidates("odir/f", "odir/four.txt")
	test_candidates("x")

}

func TestCompleteExecutables(t *testing.T) {
	tdir := t.TempDir()
	create := func(base string, name string, mode os.FileMode) {
		f, _ := os.OpenFile(filepath.Join(tdir, base, name), os.O_CREATE, mode)
		f.Close()
	}
	os.Mkdir(filepath.Join(tdir, "one"), 0700)
	os.Mkdir(filepath.Join(tdir, "two"), 0700)

	create("", "not-in-path", 0700)
	create("one", "one-exec", 0700)
	create("one", "one-not-exec", 0600)
	create("two", "two-exec", 0700)
	os.Symlink(filepath.Join(tdir, "two", "two-exec"), filepath.Join(tdir, "one", "s"))
	os.Symlink(filepath.Join(tdir, "one", "one-not-exec"), filepath.Join(tdir, "one", "n"))

	t.Setenv("PATH", strings.Join([]string{filepath.Join(tdir, "one"), filepath.Join(tdir, "two")}, string(os.PathListSeparator)))
	test_candidates := func(prefix string, expected ...string) {
		if expected == nil {
			expected = make([]string, 0)
		}
		actual := CompleteExecutablesInPath(prefix)
		sort.Strings(expected)
		sort.Strings(actual)
		if !reflect.DeepEqual(expected, actual) {
			t.Fatalf("Did not get expected completion candidates for prefix: %#v\nExpected: %#v\nActual:   %#v", prefix, expected, actual)
		}
	}
	test_candidates("", "one-exec", "two-exec", "s")
	test_candidates("o", "one-exec")
	test_candidates("x")
}