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_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_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_parse_front_matter_handles_no_fm(): # 'file' lines without front matter lines = ['These\n', 'are\n', '---\n', 'some\n', 'lines\n'] search_mode = SearchMode() res = search_mode._parse_frontmatter(lines) assert res == {} assert len(res) == 0
def test_parse_front_matter_handles_no_ending_dashses(): """If the front matter is not ended correctly (`---`)""" lines = [ '---\n', 'actual: fm\n', '# Heading 1\n', 'paragraph 1\n', '## Heading 2\n', ] search_mode = SearchMode() res = search_mode._parse_frontmatter(lines) assert res == {'actual': 'fm'}
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 == []
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_dir_throws_error_when_not_dir(tmpdir: py.path.local): file = tmpdir.join('file.md') search_mode = SearchMode() with pytest.raises(NotADirectoryError): search_mode._search_dir(file.strpath)
def test_parse_front_matter_parses_correct_data(lines, result): search_mode = SearchMode() res = search_mode._parse_frontmatter(lines) assert res == result
def main(argv: Optional[Sequence[str]] = None): if argv is None: argv = sys.argv[1:] # Create logger logging.basicConfig( format='[%(levelname)s:%(name)s] @ %(asctime)s: %(message)s', datefmt='%H:%M:%S', ) logger = logging.getLogger(__name__) logger.setLevel(logging.ERROR) c = Config() config = c.parse(argv) # Set the general settings if config['general']['verbose']['value']: logging.getLogger().setLevel(logging.INFO) mode: Optional[BaseMode] = None options: Optional[ModeOptions] = None if 'check' in config: # start check mode mode = CheckMode() disabled_errors = [] for disabled_error in config['check']['disabled_errors']: if disabled_error['value'] == True: disabled_errors.append(disabled_error['dest'][2:]) options = { 'visual': not config['general']['no_visual']['value'], 'args': { 'in_path': config['check']['in_path']['value'], 'fix': config['check']['fix']['value'], 'simple_errors': config['check']['simple_errors']['value'], 'disabled_errors': disabled_errors, }, } elif 'convert' in config: # start convert mode mode = ConvertMode() pandoc_options: PandocOptions = { 'arguments': config['convert']['pandoc_args']['value'], 'template': config['convert']['pandoc_template']['value'], 'output_format': ( 'pdf' if config['convert']['to_pdf']['value'] else 'html' ), 'ignore_warnings': config['convert']['ignore_warnings']['value'], } options = { 'visual': not config['general']['no_visual']['value'], 'args': { 'in_path': config['convert']['in_path']['value'], 'out_path': config['convert']['out_path']['value'], 'watch': config['convert']['watch']['value'], 'pandoc_options': pandoc_options, }, } elif 'search' in config: mode = SearchMode() search_args = { 'pattern': config['search']['pattern']['value'], 'path': config['search']['path']['value'], 'tag_str': config['search']['tags']['value'], 'tag_delimiter': config['search']['tag_delimiter']['value'], 'topic': config['search']['topic']['value'], 'case_insensitive': config['search']['case_insensitive']['value'], 'title': config['search']['title']['value'], 'full_path': config['search']['full_path']['value'], } options = { # TODO: Extract visual into variable 'visual': not config['general']['no_visual']['value'], 'args': search_args, } elif 'upload' in config: mode = UploadMode() upload_args = { 'path': config['upload']['path']['value'], 'url': config['upload']['url']['value'], 'username': config['upload']['username']['value'], 'save_credentials': config['upload']['save_credentials']['value'], } options = { 'visual': not config['general']['no_visual']['value'], 'args': upload_args, } else: raise SystemExit(1) mode.start(options)