files_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package cli
  3. import (
  4. "fmt"
  5. "kitty/tools/utils"
  6. "os"
  7. "path/filepath"
  8. "reflect"
  9. "sort"
  10. "strings"
  11. "testing"
  12. )
  13. var _ = fmt.Print
  14. func TestCompleteFiles(t *testing.T) {
  15. tdir := t.TempDir()
  16. cwd, _ := os.Getwd()
  17. if cwd != "" {
  18. defer os.Chdir(cwd)
  19. }
  20. os.Chdir(tdir)
  21. create := func(parts ...string) {
  22. f, _ := os.Create(filepath.Join(tdir, filepath.Join(parts...)))
  23. f.Close()
  24. }
  25. create("one.txt")
  26. create("two.txt")
  27. os.Mkdir(filepath.Join(tdir, "odir"), 0700)
  28. create("odir", "three.txt")
  29. create("odir", "four.txt")
  30. test_candidates := func(prefix string, expected ...string) {
  31. if expected == nil {
  32. expected = make([]string, 0)
  33. }
  34. sort.Strings(expected)
  35. actual := make([]string, 0, len(expected))
  36. CompleteFiles(prefix, func(entry *FileEntry) {
  37. actual = append(actual, entry.CompletionCandidate)
  38. if _, err := os.Stat(entry.Abspath); err != nil {
  39. t.Fatalf("Abspath does not exist: %#v", entry.Abspath)
  40. }
  41. }, "")
  42. sort.Strings(actual)
  43. if !reflect.DeepEqual(expected, actual) {
  44. t.Fatalf("Did not get expected completion candidates for prefix: %#v\nExpected: %#v\nActual: %#v", prefix, expected, actual)
  45. }
  46. }
  47. test_abs_candidates := func(prefix string, expected ...string) {
  48. e := make([]string, len(expected))
  49. for i, x := range expected {
  50. if filepath.IsAbs(x) {
  51. e[i] = x
  52. } else {
  53. e[i] = filepath.Join(tdir, x)
  54. if strings.HasSuffix(x, utils.Sep) {
  55. e[i] += utils.Sep
  56. }
  57. }
  58. }
  59. test_candidates(prefix, e...)
  60. }
  61. test_cwd_prefix := func(prefix string, expected ...string) {
  62. e := make([]string, len(expected))
  63. for i, x := range expected {
  64. e[i] = "./" + x
  65. }
  66. test_candidates("./"+prefix, e...)
  67. }
  68. test_cwd_prefix("", "one.txt", "two.txt", "odir/")
  69. test_cwd_prefix("t", "two.txt")
  70. test_cwd_prefix("x")
  71. test_abs_candidates(tdir+utils.Sep, "one.txt", "two.txt", "odir/")
  72. test_abs_candidates(filepath.Join(tdir, "o"), "one.txt", "odir/")
  73. test_candidates("", "one.txt", "two.txt", "odir/")
  74. test_candidates("t", "two.txt")
  75. test_candidates("o", "one.txt", "odir/")
  76. test_candidates("odir", "odir/")
  77. test_candidates("odir/", "odir/three.txt", "odir/four.txt")
  78. test_candidates("odir/f", "odir/four.txt")
  79. test_candidates("x")
  80. }
  81. func TestCompleteExecutables(t *testing.T) {
  82. tdir := t.TempDir()
  83. create := func(base string, name string, mode os.FileMode) {
  84. f, _ := os.OpenFile(filepath.Join(tdir, base, name), os.O_CREATE, mode)
  85. f.Close()
  86. }
  87. os.Mkdir(filepath.Join(tdir, "one"), 0700)
  88. os.Mkdir(filepath.Join(tdir, "two"), 0700)
  89. create("", "not-in-path", 0700)
  90. create("one", "one-exec", 0700)
  91. create("one", "one-not-exec", 0600)
  92. create("two", "two-exec", 0700)
  93. os.Symlink(filepath.Join(tdir, "two", "two-exec"), filepath.Join(tdir, "one", "s"))
  94. os.Symlink(filepath.Join(tdir, "one", "one-not-exec"), filepath.Join(tdir, "one", "n"))
  95. t.Setenv("PATH", strings.Join([]string{filepath.Join(tdir, "one"), filepath.Join(tdir, "two")}, string(os.PathListSeparator)))
  96. test_candidates := func(prefix string, expected ...string) {
  97. if expected == nil {
  98. expected = make([]string, 0)
  99. }
  100. actual := CompleteExecutablesInPath(prefix)
  101. sort.Strings(expected)
  102. sort.Strings(actual)
  103. if !reflect.DeepEqual(expected, actual) {
  104. t.Fatalf("Did not get expected completion candidates for prefix: %#v\nExpected: %#v\nActual: %#v", prefix, expected, actual)
  105. }
  106. }
  107. test_candidates("", "one-exec", "two-exec", "s")
  108. test_candidates("o", "one-exec")
  109. test_candidates("x")
  110. }