test_contrib.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. from __future__ import unicode_literals, absolute_import, print_function
  2. import os
  3. import shutil
  4. import tempfile
  5. from contextlib import contextmanager
  6. from six import text_type
  7. from prompt_toolkit.completion import CompleteEvent
  8. from prompt_toolkit.document import Document
  9. from prompt_toolkit.contrib.completers.filesystem import PathCompleter
  10. @contextmanager
  11. def chdir(directory):
  12. """Context manager for current working directory temporary change."""
  13. orig_dir = os.getcwd()
  14. os.chdir(directory)
  15. try:
  16. yield
  17. finally:
  18. os.chdir(orig_dir)
  19. def write_test_files(test_dir, names=None):
  20. """Write test files in test_dir using the names list."""
  21. names = names or range(10)
  22. for i in names:
  23. with open(os.path.join(test_dir, str(i)), 'wb') as out:
  24. out.write(''.encode('UTF-8'))
  25. def test_pathcompleter_completes_in_current_directory():
  26. completer = PathCompleter()
  27. doc_text = ''
  28. doc = Document(doc_text, len(doc_text))
  29. event = CompleteEvent()
  30. completions = list(completer.get_completions(doc, event))
  31. assert len(completions) > 0
  32. def test_pathcompleter_completes_files_in_current_directory():
  33. # setup: create a test dir with 10 files
  34. test_dir = tempfile.mkdtemp()
  35. write_test_files(test_dir)
  36. expected = sorted([str(i) for i in range(10)])
  37. if not test_dir.endswith(os.path.sep):
  38. test_dir += os.path.sep
  39. with chdir(test_dir):
  40. completer = PathCompleter()
  41. # this should complete on the cwd
  42. doc_text = ''
  43. doc = Document(doc_text, len(doc_text))
  44. event = CompleteEvent()
  45. completions = list(completer.get_completions(doc, event))
  46. result = sorted(c.text for c in completions)
  47. assert expected == result
  48. # cleanup
  49. shutil.rmtree(test_dir)
  50. def test_pathcompleter_completes_files_in_absolute_directory():
  51. # setup: create a test dir with 10 files
  52. test_dir = tempfile.mkdtemp()
  53. write_test_files(test_dir)
  54. expected = sorted([str(i) for i in range(10)])
  55. test_dir = os.path.abspath(test_dir)
  56. if not test_dir.endswith(os.path.sep):
  57. test_dir += os.path.sep
  58. completer = PathCompleter()
  59. # force unicode
  60. doc_text = text_type(test_dir)
  61. doc = Document(doc_text, len(doc_text))
  62. event = CompleteEvent()
  63. completions = list(completer.get_completions(doc, event))
  64. result = sorted([c.text for c in completions])
  65. assert expected == result
  66. # cleanup
  67. shutil.rmtree(test_dir)
  68. def test_pathcompleter_completes_directories_with_only_directories():
  69. # setup: create a test dir with 10 files
  70. test_dir = tempfile.mkdtemp()
  71. write_test_files(test_dir)
  72. # create a sub directory there
  73. os.mkdir(os.path.join(test_dir, 'subdir'))
  74. if not test_dir.endswith(os.path.sep):
  75. test_dir += os.path.sep
  76. with chdir(test_dir):
  77. completer = PathCompleter(only_directories=True)
  78. doc_text = ''
  79. doc = Document(doc_text, len(doc_text))
  80. event = CompleteEvent()
  81. completions = list(completer.get_completions(doc, event))
  82. result = [c.text for c in completions]
  83. assert ['subdir'] == result
  84. # check that there is no completion when passing a file
  85. with chdir(test_dir):
  86. completer = PathCompleter(only_directories=True)
  87. doc_text = '1'
  88. doc = Document(doc_text, len(doc_text))
  89. event = CompleteEvent()
  90. completions = list(completer.get_completions(doc, event))
  91. assert [] == completions
  92. # cleanup
  93. shutil.rmtree(test_dir)
  94. def test_pathcompleter_respects_completions_under_min_input_len():
  95. # setup: create a test dir with 10 files
  96. test_dir = tempfile.mkdtemp()
  97. write_test_files(test_dir)
  98. # min len:1 and no text
  99. with chdir(test_dir):
  100. completer = PathCompleter(min_input_len=1)
  101. doc_text = ''
  102. doc = Document(doc_text, len(doc_text))
  103. event = CompleteEvent()
  104. completions = list(completer.get_completions(doc, event))
  105. assert [] == completions
  106. # min len:1 and text of len 1
  107. with chdir(test_dir):
  108. completer = PathCompleter(min_input_len=1)
  109. doc_text = '1'
  110. doc = Document(doc_text, len(doc_text))
  111. event = CompleteEvent()
  112. completions = list(completer.get_completions(doc, event))
  113. result = [c.text for c in completions]
  114. assert [''] == result
  115. # min len:0 and text of len 2
  116. with chdir(test_dir):
  117. completer = PathCompleter(min_input_len=0)
  118. doc_text = '1'
  119. doc = Document(doc_text, len(doc_text))
  120. event = CompleteEvent()
  121. completions = list(completer.get_completions(doc, event))
  122. result = [c.text for c in completions]
  123. assert [''] == result
  124. # create 10 files with a 2 char long name
  125. for i in range(10):
  126. with open(os.path.join(test_dir, str(i) * 2), 'wb') as out:
  127. out.write(b'')
  128. # min len:1 and text of len 1
  129. with chdir(test_dir):
  130. completer = PathCompleter(min_input_len=1)
  131. doc_text = '2'
  132. doc = Document(doc_text, len(doc_text))
  133. event = CompleteEvent()
  134. completions = list(completer.get_completions(doc, event))
  135. result = sorted(c.text for c in completions)
  136. assert ['', '2'] == result
  137. # min len:2 and text of len 1
  138. with chdir(test_dir):
  139. completer = PathCompleter(min_input_len=2)
  140. doc_text = '2'
  141. doc = Document(doc_text, len(doc_text))
  142. event = CompleteEvent()
  143. completions = list(completer.get_completions(doc, event))
  144. assert [] == completions
  145. # cleanup
  146. shutil.rmtree(test_dir)
  147. def test_pathcompleter_does_not_expanduser_by_default():
  148. completer = PathCompleter()
  149. doc_text = '~'
  150. doc = Document(doc_text, len(doc_text))
  151. event = CompleteEvent()
  152. completions = list(completer.get_completions(doc, event))
  153. assert [] == completions
  154. def test_pathcompleter_can_expanduser(monkeypatch):
  155. monkeypatch.setenv('HOME', '/tmp')
  156. completer = PathCompleter(expanduser=True)
  157. doc_text = '~'
  158. doc = Document(doc_text, len(doc_text))
  159. event = CompleteEvent()
  160. completions = list(completer.get_completions(doc, event))
  161. assert len(completions) > 0
  162. def test_pathcompleter_can_apply_file_filter():
  163. # setup: create a test dir with 10 files
  164. test_dir = tempfile.mkdtemp()
  165. write_test_files(test_dir)
  166. # add a .csv file
  167. with open(os.path.join(test_dir, 'my.csv'), 'wb') as out:
  168. out.write(b'')
  169. file_filter = lambda f: f and f.endswith('.csv')
  170. with chdir(test_dir):
  171. completer = PathCompleter(file_filter=file_filter)
  172. doc_text = ''
  173. doc = Document(doc_text, len(doc_text))
  174. event = CompleteEvent()
  175. completions = list(completer.get_completions(doc, event))
  176. result = [c.text for c in completions]
  177. assert ['my.csv'] == result
  178. # cleanup
  179. shutil.rmtree(test_dir)
  180. def test_pathcompleter_get_paths_constrains_path():
  181. # setup: create a test dir with 10 files
  182. test_dir = tempfile.mkdtemp()
  183. write_test_files(test_dir)
  184. # add a subdir with 10 other files with different names
  185. subdir = os.path.join(test_dir, 'subdir')
  186. os.mkdir(subdir)
  187. write_test_files(subdir, 'abcdefghij')
  188. get_paths = lambda: ['subdir']
  189. with chdir(test_dir):
  190. completer = PathCompleter(get_paths=get_paths)
  191. doc_text = ''
  192. doc = Document(doc_text, len(doc_text))
  193. event = CompleteEvent()
  194. completions = list(completer.get_completions(doc, event))
  195. result = [c.text for c in completions]
  196. expected = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
  197. assert expected == result
  198. # cleanup
  199. shutil.rmtree(test_dir)