def test_tag_counts(fs): fs.create_file('/notes/one.md', contents='#tag1 #tag1 #tag2') fs.create_file('/notes/two.md', contents='#tag1 #tag3') fs.create_file('/notes/three.md', contents='#tag1 #tag3 #tag4') repo = DirectRepoConf(root_paths={'/notes'}).instantiate() assert repo.tag_counts(FileQuery()) == {'tag1': 3, 'tag2': 1, 'tag3': 2, 'tag4': 1} assert repo.tag_counts(FileQuery.parse('tag:tag3')) == {'tag1': 2, 'tag3': 2, 'tag4': 1}
def test_rearrange_absolute_paths(fs): docs = [ 'I am being moved but link to something that is [not](/notes/two.md)', 'I am not being moved', 'I am not being moved but link to something that [is](/notes/one.md)', 'I am being moved and so is what I link [to](/notes/one.md)' ] paths = [ '/notes/one.md', '/notes/two.md', '/notes/three.md', '/notes/four.md' ] for i in range(4): fs.create_file(paths[i], contents=docs[i]) Path('/notes/newdir').mkdir() repo = DirectRepoConf(root_paths={'/notes'}).instantiate() repo.change( edits_for_rearrange(repo, { paths[0]: '/notes/newdir/new1.md', paths[3]: '/notes/newdir/new4.md' })) assert not Path(paths[0]).exists() assert not Path(paths[3]).exists() assert Path(paths[1]).read_text() == docs[1] assert Path(paths[2]).read_text( ) == 'I am not being moved but link to something that [is](newdir/new1.md)' assert Path('/notes/newdir/new4.md').read_text( ) == 'I am being moved and so is what I link [to](new1.md)' assert Path('/notes/newdir/new1.md').read_text() == docs[0]
def test_change(fs): fs.create_file('/notes/one.md', contents='[1](old)') fs.create_file('/notes/two.md', contents='[2](foo)') edits = [SetTitleCmd('/notes/one.md', 'New Title'), ReplaceHrefCmd('/notes/one.md', 'old', 'new'), MoveCmd('/notes/one.md', '/notes/moved.md'), ReplaceHrefCmd('/notes/two.md', 'foo', 'bar')] repo = DirectRepoConf(root_paths={'/notes'}).instantiate() repo.change(edits) assert not Path('/notes/one.md').exists() assert Path('/notes/moved.md').read_text() == '---\ntitle: New Title\n...\n[1](new)' assert Path('/notes/two.md').read_text() == '[2](bar)'
def test_ignore(fs): path1 = '/notes/one.md' path2 = '/notes/.two.md' fs.create_file(path1, contents='I link to [two](.two.md)') fs.create_file(path2, contents='I link to [one](one.md)') repo = DirectRepoConf(root_paths={'/notes'}).instantiate() assert list(repo.query()) == [repo.info(path1)] assert not repo.info(path1, FileInfoReq.full()).backlinks assert repo.info(path2, FileInfoReq.full()).backlinks == [LinkInfo(path1, '.two.md')] repo.conf.ignore = lambda _1, _2: False assert list(repo.query()) == [repo.info(path1), repo.info(path2)] assert repo.info(path1, FileInfoReq.full()).backlinks == [LinkInfo(path2, 'one.md')] assert repo.info(path2, FileInfoReq.full()).backlinks == [LinkInfo(path1, '.two.md')]
def test_rearrange_swap(fs): doc1 = 'I link to [two](two.md).' doc2 = 'I link to [one](one.md).' fs.create_file('/notes/one.md', contents=doc1) fs.create_file('/notes/two.md', contents=doc2) repo = DirectRepoConf(root_paths={'/notes'}).instantiate() repo.change( edits_for_rearrange(repo, { '/notes/one.md': '/notes/two.md', '/notes/two.md': '/notes/one.md' })) assert Path('/notes/one.md').exists() assert Path('/notes/two.md').exists() assert Path('/notes/one.md').read_text() == 'I link to [one](two.md).' assert Path('/notes/two.md').read_text() == 'I link to [two](one.md).'
def test_backlinks(fs): fs.cwd = '/notes/foo' fs.create_file('/notes/foo/subject.md') fs.create_file('/notes/bar/baz/r1.md', contents='[1](no) [2](../../foo/subject.md)') fs.create_file('/notes/bar/baz/no.md', contents='[3](../../foo/bogus') fs.create_file('/notes/r2.md', contents='[4](foo/subject.md) [5](foo/bogus)') fs.create_file('/notes/foo/r3.md', contents='[6](subject.md)') repo = DirectRepoConf(root_paths={'/notes'}).instantiate() info = repo.info('subject.md', 'backlinks') assert info.backlinks == [ LinkInfo('/notes/bar/baz/r1.md', '../../foo/subject.md'), LinkInfo('/notes/foo/r3.md', 'subject.md'), LinkInfo('/notes/r2.md', 'foo/subject.md'), ]
def test_change_directories(fs): paths1 = ['/notes/dir1/subdir1/one.md', '/notes/dir2/subdir2/two.md', '/notes/dir2/subdir3/three.md'] nonmovingpath = '/notes/dir2/subdir3/four.md' for path in paths1: fs.create_file(path) fs.create_file(nonmovingpath) paths2 = [p.replace('/notes/', '/notes/newdir/') for p in paths1] repo = DirectRepoConf(root_paths={'/notes'}).instantiate() edits = [MoveCmd(paths1[i], paths2[i], create_parents=True, delete_empty_parents=True) for i in range(3)] repo.change(edits) assert [p for p in paths1 if os.path.exists(p)] == [] assert [p for p in paths2 if os.path.exists(p)] == paths2 assert not os.path.exists('/notes/dir1') assert not os.path.exists('/notes/dir2/subdir2') assert os.path.exists(nonmovingpath)
def test_rearrange_special_characters(fs): doc1 = 'I link to [two](second%20doc%21.md).' doc2 = 'I link to [one](first%20doc%21.md).' fs.create_file('/notes/first doc!.md', contents=doc1) fs.create_file('/notes/second doc!.md', contents=doc2) Path('/notes/subdir').mkdir() repo = DirectRepoConf(root_paths={'/notes'}).instantiate() repo.change( edits_for_rearrange( repo, {'/notes/first doc!.md': '/notes/subdir/new loc!.md'})) assert not Path('/notes/first doc!.md').exists() assert Path('/notes/second doc!.md').exists() assert Path('/notes/subdir/new loc!.md').exists() assert Path('/notes/second doc!.md').read_text( ) == 'I link to [one](subdir/new%20loc%21.md).' assert (Path('/notes/subdir/new loc!.md').read_text() == 'I link to [two](../second%20doc%21.md).')
def test_rearrange_folder(fs): doc1 = 'I link to [two](dir/two.md).' doc2 = 'I link to [three](subdir/three.md).' doc3 = 'I link to [one](../../one.md) and [the web](https://example.com).' fs.create_file('/notes/one.md', contents=doc1) fs.create_file('/notes/dir/two.md', contents=doc2) fs.create_file('/notes/dir/subdir/three.md', contents=doc3) Path('/notes/wrapper').mkdir() repo = DirectRepoConf(root_paths={'/notes'}).instantiate() repo.change( edits_for_rearrange(repo, {'/notes/dir': '/notes/wrapper/newdir'})) assert not Path('/notes/dir').exists() assert Path('/notes/one.md').read_text( ) == 'I link to [two](wrapper/newdir/two.md).' assert Path('/notes/wrapper/newdir/two.md').read_text( ) == 'I link to [three](subdir/three.md).' assert ( Path('/notes/wrapper/newdir/subdir/three.md').read_text() == 'I link to [one](../../../one.md) and [the web](https://example.com).')
def test_rearrange_mutual_subdirs(fs): doc1 = 'I link to [two](../two.md).' doc2 = 'I link to [one](subdir1/one.md).' fs.create_file('/notes/subdir1/one.md', contents=doc1) fs.create_file('/notes/two.md', contents=doc2) Path('/notes/subdir2').mkdir() repo = DirectRepoConf(root_paths={'/notes'}).instantiate() repo.change( edits_for_rearrange( repo, { '/notes/subdir1/one.md': '/notes/one.md', '/notes/two.md': '/notes/subdir2/two.md' })) assert not Path('/notes/subdir1/one.md').exists() assert not Path('/notes/two.md').exists() assert Path('/notes/one.md').exists() assert Path('/notes/subdir2/two.md').exists() assert Path( '/notes/one.md').read_text() == 'I link to [two](subdir2/two.md).' assert Path( '/notes/subdir2/two.md').read_text() == 'I link to [one](../one.md).'
def test_skip_parse(fs): path1 = '/notes/one.md' path2 = '/notes/one.md.resources/two.md' path3 = '/notes/skip.md' path4 = '/notes/unskip.md' fs.create_file(path1, contents='---\ntitle: Note One\n---\n') fs.create_file(path2, contents='---\ntitle: Note Two\n---\n') fs.create_file(path3, contents='---\ntitle: Note Skip\n---\n') fs.create_file(path4, contents='---\ntitle: Note No Skip\n---\n') def fn(parentpath, filename): return filename.endswith('.resources') or filename == 'skip.md' repo = DirectRepoConf(root_paths={'/notes'}, skip_parse=fn).instantiate() assert list(repo.query('sort:path')) == [ FileInfo(path1, title='Note One'), FileInfo(path2), FileInfo(path3), FileInfo(path4, title='Note No Skip') ] assert repo.info(path1) == FileInfo(path1, title='Note One') assert repo.info(path2) == FileInfo(path2) assert repo.info(path3) == FileInfo(path3) assert repo.info(path4) == FileInfo(path4, title='Note No Skip')
def test_rearrange_selfreference(fs): doc = 'I link to [myself](one.md).' fs.create_file('/notes/one.md', contents=doc) repo = DirectRepoConf(root_paths={'/notes'}).instantiate() repo.change(edits_for_rearrange(repo, {'/notes/one.md': '/notes/two.md'})) assert not Path('/notes/one.md').exists() assert Path('/notes/two.md').exists() assert Path('/notes/two.md').read_text() == 'I link to [myself](two.md).' doc3 = 'I link to [a section of myself](#foo)' fs.create_file('/notes/three.md', contents=doc3) repo.change( edits_for_rearrange(repo, {'/notes/three.md': '/notes/four.md'})) assert Path('/notes/four.md').read_text( ) == 'I link to [a section of myself](#foo)'
def test_info_directory(fs): path = Path('/notes/foo/bar') path.mkdir(parents=True, exist_ok=True) repo = DirectRepoConf(root_paths={'/notes'}).instantiate() assert repo.info(str(path)) == FileInfo(str(path)) assert repo.info(str(path.parent)) == FileInfo(str(path.parent))
def test_referrers_self(fs): fs.create_file('/notes/subject.md', contents='[1](subject.md)') repo = DirectRepoConf(root_paths={'/notes'}).instantiate() info = repo.info('/notes/subject.md', 'backlinks') assert info.backlinks == [LinkInfo('/notes/subject.md', 'subject.md')]
def test_info_nonexistent(fs): path = '/notes/foo' repo = DirectRepoConf(root_paths={'/notes'}).instantiate() assert repo.info(path) == FileInfo(path)
def test_query(fs): fs.create_file('/notes/one.md', contents='#tag1 #tag1 #tag2 #tag4') fs.create_file('/notes/two.md', contents='#tag1 #tag3') fs.create_file('/notes/three.md', contents='#tag1 #tag3 #tag4') repo = DirectRepoConf(root_paths={'/notes'}).instantiate() paths = {i.path for i in repo.query(FileQuery())} assert paths == {'/notes/one.md', '/notes/two.md', '/notes/three.md'} paths = {i.path for i in repo.query(FileQuery.parse('tag:tag3'))} assert paths == {'/notes/two.md', '/notes/three.md'} paths = {i.path for i in repo.query(FileQuery.parse('tag:tag1,tag4'))} assert paths == {'/notes/one.md', '/notes/three.md'} assert not list(repo.query(FileQuery.parse('tag:bogus'))) paths = {i.path for i in repo.query(FileQuery.parse('-tag:tag2'))} assert paths == {'/notes/two.md', '/notes/three.md'} paths = {i.path for i in repo.query(FileQuery.parse('-tag:tag2,tag4'))} assert paths == {'/notes/two.md'} assert not list(repo.query(FileQuery.parse('-tag:tag1'))) paths = {i.path for i in repo.query(FileQuery.parse('tag:tag3 -tag:tag4'))} assert paths == {'/notes/two.md'} assert [os.path.basename(i.path) for i in repo.query('sort:filename')] == ['one.md', 'three.md', 'two.md']
def config(): return NotesdirConf(repo_conf=DirectRepoConf(root_paths={'/notes'}))