def test_search_file_pattern_and_title_is_found(tmpdir: py.path.local): file = tmpdir.join('test.md') file.write('''--- title: Essay --- # Heading 1 search term ''') search_mode = SearchMode() args = { 'pattern': 'search term', 'path': file.strpath, 'tag_str': None, 'topic': None, 'case_insensitive': False, 'title': 'essay', # Note: is lowercase but should still be found 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) assert len(search_mode.matches) == 1
def test_search_file_subject_is_found_as_topic(tmpdir: py.path.local): file = tmpdir.join('test.md') file.write('''--- subject: test topic --- # Heading 1 search term ''') search_mode = SearchMode() args = { 'pattern': 'search term', 'path': file.strpath, 'tag_str': None, 'topic': 'test topic', 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) assert len(search_mode.matches) == 1
def test_search_file_finds_pattern_but_not_title(tmpdir: py.path.local): file = tmpdir.join('test.md') file.write('''--- title: Essay --- # Heading 1 search term ''') search_mode = SearchMode() args = { 'pattern': 'search term', 'path': file.strpath, 'tag_str': None, 'topic': None, 'case_insensitive': False, 'title': 'not in the doc', 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) assert len(search_mode.matches) == 0
def test_split_tag_str_correctly(): search_mode = SearchMode() tags = ['t1', 't2', 't3', 'ajj'] args = { 'pattern': 'pattern', 'path': 'location', 'tag_str': ' '.join(tags), 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } try: search_mode.start(options) # It will throw FileNotFoundError because location does not exist # but that does not matter for this test except FileNotFoundError: pass assert search_mode.tags == tags
def test_pattern_is_found_correctly( tmpdir: py.path.local, file_contents: str, pattern: str, n_matches: int, ): file = tmpdir.join('test.md') file.write(file_contents) search_mode = SearchMode() args = { 'pattern': pattern, 'path': file.strpath, 'tag_str': None, 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) c = 0 for match in search_mode.matches: for _ in match['matched_lines']: c += 1 assert c == n_matches
def test_search_file_case_insisitive_works_correctly(tmpdir: py.path.local): # Case sensitive (not found) file = tmpdir.join('test.md') file.write('''--- title: Essay --- # Heading 1 search TERM ''') search_mode = SearchMode() args = { 'pattern': 'search term', 'path': file.strpath, 'tag_str': None, 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) assert len(search_mode.matches) == 0 search_mode_i = SearchMode() args_i = { 'pattern': 'search term', 'path': file.strpath, 'tag_str': None, 'topic': None, 'case_insensitive': True, 'title': None, 'full_path': False, } options_i: ModeOptions = { 'visual': True, 'args': args_i, } search_mode_i.start(options_i) assert len(search_mode_i.matches) == 1
def test_search_file_handles_empty_file(tmpdir: py.path.local): file = tmpdir.join('test.md') file.write('') search_mode = SearchMode() args = { 'pattern': 'search term', 'path': file.strpath, 'tag_str': None, 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) assert len(search_mode.matches) == 0
def test_star_pattern(tmpdir: py.path.local): file1 = tmpdir.join('test1.md') file1.write("""--- tags: findme --- Random content """) file2 = tmpdir.join('test2.md') file2.write("""--- title: Random title tags: findme --- Random content 2 """) file3 = tmpdir.join('test3.md') file3.write("""--- tags: other --- Random content """) search_mode = SearchMode() args = { 'pattern': '*', 'path': tmpdir.strpath, 'tag_str': 'findme', 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) assert len(search_mode.matches) == 2
def test_search_dir_searches_all_md_files(mock: Mock, tmpdir: py.path.local): n_files = 5 for i in range(5): file = tmpdir.join(f'test_file{i}.md') file.write('# Heading 1') search_mode = SearchMode() args = { 'pattern': 'search term', 'path': tmpdir.strpath, 'tag_str': None, 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } search_mode.start(options) assert mock.call_count == n_files
def test_split_tags_with_custom_delimiter_correctly(): """This tests that the tag_str is sepereted correctly""" tags = ['t', 't2', 't4'] search_mode = SearchMode() args = { 'pattern': 'pattern', 'path': 'location', 'tag_str': ','.join(tags), 'tag_delimiter': ',', 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } try: search_mode.start(options) except FileNotFoundError: pass assert search_mode.tags == tags
def test_allow_no_tag(): search_mode = SearchMode() args = { 'pattern': 'pattern', 'path': 'location', 'tag_str': None, 'topic': None, 'case_insensitive': False, 'title': None, 'full_path': False, } options: ModeOptions = { 'visual': True, 'args': args, } try: search_mode.start(options) # It will throw FileNotFoundError because location does not exist # but that does not matter for this test except FileNotFoundError: pass assert search_mode.tags == []