def test_convert_markup_without_html2text(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = u'''Look at [[this page|Some Page]] More info at: [[MoreInfo]] [[Even More Info]] Our website is [[http://sf.net]]. '[[Escaped Tag]] [External link to the wiki page](https://github.com/a/b/wiki/Page) [External link](https://github.com/a/b/issues/1)''' result = u'''<p>Look at [[this page|Some Page]]</p> <p>More info at: [[MoreInfo]] [[Even More Info]]</p> <p>Our website is [[http://sf.net]].</p> <p>'[[Escaped Tag]]</p> <p>[External link to the wiki page](https://github.com/a/b/wiki/Page)</p> <p>[External link](https://github.com/a/b/issues/1)</p>''' assert_equal(f(source, 'test.textile').strip(), result)
def test_convert_markup_with_mediawiki2markdown(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = u''' ''Al'fredas 235 BC'' == See also == * [https://github.com/a/b/wiki/AgentSpring-running-instructions-for-d13n-model Test1] * [https://github.com/a/b/wiki/AgentSpring-conventions Test2] * [https://github.com/a/b/wiki/AgentSpring-Q&A Test3] * [https://github.com/a/b/wiki/Extensions Test4]''' result = u'''_Al'fredas 235 BC_ ## See also * [Test1](/p/test/wiki/AgentSpring running instructions for d13n model) * [Test2](/p/test/wiki/AgentSpring conventions) * [Test3](/p/test/wiki/AgentSpring Q&A) * [Test4](/p/test/wiki/Extensions) ''' assert_equal(f(source, 'test.mediawiki'), result)
def test_convert_markup_with_mediawiki2markdown(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = ''' ''Al'fredas 235 BC'' == See also == * [https://github.com/a/b/wiki/AgentSpring-running-instructions-for-d13n-model Test1] * [https://github.com/a/b/wiki/AgentSpring-conventions Test2] * [https://github.com/a/b/wiki/AgentSpring-Q&A Test3] * [https://github.com/a/b/wiki/Extensions Test4]''' result = '''_Al'fredas 235 BC_ ## See also * [Test1](/p/test/wiki/AgentSpring running instructions for d13n model) * [Test2](/p/test/wiki/AgentSpring conventions) * [Test3](/p/test/wiki/AgentSpring Q&A) * [Test4](/p/test/wiki/Extensions) ''' assert_equal(f(source, 'test.mediawiki'), result)
def test_convert_markup_without_html2text(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = '''Look at [[this page|Some Page]] More info at: [[MoreInfo]] [[Even More Info]] Our website is [[http://domain.net]]. '[[Escaped Tag]] [External link to the wiki page](https://github.com/a/b/wiki/Page) [External link](https://github.com/a/b/issues/1)''' result = '''<p>Look at [[this page|Some Page]]</p> <p>More info at: [[MoreInfo]] [[Even More Info]]</p> <p>Our website is [[http://domain.net]].</p> <p>\u2018[[Escaped Tag]]</p> <p>[External link to the wiki page](https://github.com/a/b/wiki/Page)</p> <p>[External link](https://github.com/a/b/issues/1)</p>''' assert_equal(f(source, 'test.textile').strip(), result)
def test_convert_textile_special_tag(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = u'*[[this checklist|Troubleshooting]]*' assert_equal(f(source, 't.textile').strip(), u'**[this checklist](Troubleshooting)**')
def test_convert_textile_special_tag_without_html2text(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = u'*[[this checklist|Troubleshooting]]*' result = u'<p><strong>[[this checklist|Troubleshooting]]</strong></p>' assert_equal(f(source, 't.textile').strip(), result)
def test_convert_textile_special_tag(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = '*[[this checklist|Troubleshooting]]*' assert_equal(f(source, 't.textile').strip(), '**[this checklist](Troubleshooting)**')
def test_convert_textile_special_tag_without_html2text(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = '*[[this checklist|Troubleshooting]]*' result = '<p><strong>[[this checklist|Troubleshooting]]</strong></p>' assert_equal(f(source, 't.textile').strip(), result)
def test_convert_markup_with_amp_in_links(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = '[[Ticks & Leeches]]' result = '[Ticks & Leeches]' # markdown should be untouched assert_equal(f(source, 'test.rst').strip(), result)
def test_set_available_pages(self): importer = GitHubWikiImporter() commit = Mock() blobs = [Mock() for i in range(3)] blobs[0].name = u'Home-42.md' blobs[1].name = u'image.png' blobs[2].name = u'code & fun.textile' commit.tree.traverse.return_value = blobs importer._set_available_pages(commit) assert_equal(importer.available_pages, [u'Home 42', u'code & fun'])
def test_set_available_pages(self): importer = GitHubWikiImporter() commit = Mock() blobs = [Mock() for i in range(3)] blobs[0].name = 'Home-42.md' blobs[1].name = 'image.png' blobs[2].name = 'code & fun.textile' commit.tree.traverse.return_value = blobs importer._set_available_pages(commit) assert_equal(importer.available_pages, ['Home 42', 'code & fun'])
def test_convert_markup_with_amp_in_links(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = u'[[Ticks & Leeches]]' result = u'[Ticks & Leeches]' # markdown should be untouched assert_equal(f(source, 'test.rst').strip(), result)
def test_has_wiki_repo(self, repo, rmtree, mkdtemp): mkdtemp.return_value = 'fake path' i = GitHubWikiImporter() assert_equal(i.has_wiki_repo('fake url'), True) repo.clone_from.assert_called_once_with( 'fake url', to_path='fake path', bare=True) rmtree.assert_called_once_with('fake path') def raise_error(*args, **kw): raise git.GitCommandError('bam', 'bam', 'bam') repo.clone_from.side_effect = raise_error assert_equal(i.has_wiki_repo('fake url'), False)
def test_clone_from(self, path, repo): with patch('forgeimporters.github.wiki.rmtree'): path.return_value = 'temp_path' GitHubWikiImporter().import_pages('wiki_url') repo.clone_from.assert_called_with('wiki_url', to_path='temp_path', bare=True)
def test_import_tool_happy_path(self, ghpe, g, tlorms, M): with patch('forgeimporters.github.wiki.GitHubWikiImporter.import_pages'),\ patch('forgeimporters.github.wiki.GitHubWikiImporter.has_wiki_repo', return_value=True),\ patch('forgeimporters.github.wiki.c'): ghpe.return_value.has_wiki.return_value = True p = self._make_project(gh_proj_name='myproject') u = Mock(name='c.user') app = p.install_app.return_value app.config.options.mount_point = 'wiki' app.url = 'foo' GitHubWikiImporter().import_tool( p, u, project_name='project_name', user_name='testuser') p.install_app.assert_called_once_with( 'Wiki', mount_point='wiki', mount_label='Wiki', import_id={ 'source': 'GitHub', 'project_name': 'testuser/project_name', } ) M.AuditLog.log.assert_called_once_with( 'import tool wiki from testuser/project_name on GitHub', project=p, user=u, url='foo') g.post_event.assert_called_once_with('project_updated')
def test_rewrite_links(self): f = GitHubWikiImporter().rewrite_links prefix = 'https://github/a/b/wiki' new = '/p/test/wiki/' assert_equal( f('<a href="https://github/a/b/wiki/Test Page">Test Page</a>', prefix, new), '<a href="/p/test/wiki/Test Page">Test Page</a>') assert_equal( f('<a href="https://github/a/b/wiki/Test-Page">Test-Page</a>', prefix, new), '<a href="/p/test/wiki/Test Page">Test Page</a>') assert_equal( f('<a href="https://github/a/b/issues/1" class="1"></a>', prefix, new), '<a class="1" href="https://github/a/b/issues/1"></a>') assert_equal( f('<a href="https://github/a/b/wiki/Test Page">https://github/a/b/wiki/Test Page</a>', prefix, new), '<a href="/p/test/wiki/Test Page">/p/test/wiki/Test Page</a>') assert_equal( f('<a href="https://github/a/b/wiki/Test Page">Test blah blah</a>', prefix, new), '<a href="/p/test/wiki/Test Page">Test blah blah</a>') assert_equal( f('<a href="https://github/a/b/wiki/Test Page">Test <b>Page</b></a>', prefix, new), '<a href="/p/test/wiki/Test Page">Test <b>Page</b></a>')
def test_import_id(self, upsert): page = Mock() upsert.return_value = page importer = GitHubWikiImporter() importer.app = Mock() importer.app.config.options = { 'import_id': { 'source': 'GitHub', 'project_name': 'me/project', } } importer._make_page('text', 'Page.md', self.commit2) import_id = { 'source': 'GitHub', 'project_name': 'me/project', 'source_id': 'Page', } assert_equal(page.import_id, import_id)
def test_convert_markup(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' importer.github_markdown_converter = GitHubMarkdownConverter( 'user', 'proj') f = importer.convert_markup source = '''Look at [[this page|Some Page]] More info at: [[MoreInfo]] [[Even More Info]] Our website is [[http://domain.net]]. '[[Escaped Tag]] ```python codeblock ``` ticket #1 #1 header sha aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa''' result = '''Look at [this page](Some Page) More info at: [MoreInfo] [Even More Info] Our website is <http://domain.net>. [[Escaped Tag]] :::python codeblock ticket [#1] [#1] header sha [aaaaaa]''' assert_equal(f(source, 'test.md').strip(), result) assert_equal(f('h1. Hello', 't.textile').strip(), '# Hello')
def test_convert_markup(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' importer.github_markdown_converter = GitHubMarkdownConverter( 'user', 'proj') f = importer.convert_markup source = u'''Look at [[this page|Some Page]] More info at: [[MoreInfo]] [[Even More Info]] Our website is [[http://domain.net]]. '[[Escaped Tag]] ```python codeblock ``` ticket #1 #1 header sha aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa''' result = u'''Look at [this page](Some Page) More info at: [MoreInfo] [Even More Info] Our website is <http://domain.net>. [[Escaped Tag]] :::python codeblock ticket [#1] [#1] header sha [aaaaaa]''' assert_equal(f(source, 'test.md').strip(), result) assert_equal(f(u'h1. Hello', 't.textile').strip(), u'# Hello')
def test_convert_markup_textile(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup # check if lists converting works properly source = '''There are good reasons for this: # Duplicate libraries regularly break builds # Subtle bugs emerge with duplicate libraries, and to a lesser extent, duplicate tools # We want you to try harder to make your formula work with what OS X comes with ''' result = '''There are good reasons for this: 1. Duplicate libraries regularly break builds 2. Subtle bugs emerge with duplicate libraries, and to a lesser extent, duplicate tools 3. We want you to try harder to make your formula work with what OS X comes with ''' assert_equal(f(source, 'test.textile'), result) # textile-style links converts normal source = '*"Textile":Troubleshooting*' result = '**[Textile](Troubleshooting)**\n\n' assert_equal(f(source, 'test2.textile'), result) # links with formatting converts normal in textile now source = '''*[[this checklist|Troubleshooting]]* some text and *[[Tips n' Tricks]]* *[[link|http://otherlink.com]]* ''' result = '''**[this checklist](Troubleshooting)** some text and **[Tips n\u2019 Tricks]** **[link](http://otherlink.com)** ''' assert_equal(f(source, 'test3.textile'), result)
def test_with_history_mediawiki(self, md2mkm, upsert): self.commit2.stats.files = {"Home.mediawiki": self.blob1} self.commit2.tree = {"Home.mediawiki": self.blob1} md2mkm.return_value = '# test message' importer = GitHubWikiImporter() importer._set_available_pages = Mock() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.config.options = {} importer.app.url = '/p/test/wiki/' importer.rewrite_links = Mock(return_value='') importer.convert_gollum_tags = Mock(return_value='# test message') importer._with_history(self.commit2) assert_equal(upsert.call_args_list, [call('Home')]) assert_equal(md2mkm.call_args_list, [call('# test message')])
def test_convert_markup_textile(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup # check if lists converting works properly source = u'''There are good reasons for this: # Duplicate libraries regularly break builds # Subtle bugs emerge with duplicate libraries, and to a lesser extent, duplicate tools # We want you to try harder to make your formula work with what OS X comes with ''' result = u'''There are good reasons for this: 1. Duplicate libraries regularly break builds 2. Subtle bugs emerge with duplicate libraries, and to a lesser extent, duplicate tools 3. We want you to try harder to make your formula work with what OS X comes with ''' assert_equal(f(source, 'test.textile'), result) # textile-style links converts normal source = '*"Textile":Troubleshooting*' result = '**[Textile](Troubleshooting)**\n' assert_equal(f(source, 'test2.textile'), result) # links with formatting converts normal in textile now source = u'''*[[this checklist|Troubleshooting]]* some text and *[[Tips n' Tricks]]* *[[link|http://otherlink.com]]* ''' result = u'''**[this checklist](Troubleshooting)** some text and **[Tips n' Tricks]** **[link](http://otherlink.com)** ''' assert_equal(f(source, 'test3.textile'), result)
def test_convert_gollum_page_links_escaped(self): f = GitHubWikiImporter().convert_gollum_tags assert_equal(f("'[[Page]]"), '[[Page]]') assert_equal(f("'[[Page Title|Page]]"), '[[Page Title|Page]]') assert_equal(f("'[[Page With Spaces]]"), '[[Page With Spaces]]') assert_equal(f("'[[Page-With-Spaces]]"), '[[Page-With-Spaces]]') assert_equal(f("'[[Page / 1]]"), '[[Page / 1]]') assert_equal(f("'[[Title|Page With Spaces]]"), '[[Title|Page With Spaces]]') assert_equal(f("'[[Title|Page-With-Spaces]]"), '[[Title|Page-With-Spaces]]') assert_equal(f("'[[go here|Page / 1]]"), '[[go here|Page / 1]]')
def test_convert_markup(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = u'''Look at [[this page|Some Page]] More info at: [[MoreInfo]] [[Even More Info]] Our website is [[http://sf.net]]. '[[Escaped Tag]] [External link to the wiki page](https://github.com/a/b/wiki/Page) [External link](https://github.com/a/b/issues/1)''' # markdown should be untouched assert_equal(f(source, 'test.md'), source) assert_equal(f(u'h1. Hello', 't.textile').strip(), u'# Hello')
def test_convert_textile_no_leading_tabs(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = u'''h1. Header 1 Some text 1. h2. Header 2 See [[Page]]''' result = u'''# Header 1 Some text 1. ## Header 2 See [Page]''' assert_equal(f(source, 'test.textile').strip(), result)
def test_convert_textile_no_leading_tabs(self): importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.url = '/p/test/wiki/' f = importer.convert_markup source = '''h1. Header 1 Some text 1. h2. Header 2 See [[Page]]''' result = '''# Header 1 Some text 1. ## Header 2 See [Page]''' assert_equal(f(source, 'test.textile').strip(), result)
def test_with_history_mediawiki(self, md2mkm, upsert): self.commit2.stats.files = {"Home.mediawiki": self.blob1} self.commit2.tree = {"Home.mediawiki": self.blob1} md2mkm.return_value = u'# test message' importer = GitHubWikiImporter() importer._set_available_pages = Mock() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.config.options = {} importer.app.url = '/p/test/wiki/' importer.rewrite_links = Mock(return_value='') importer.convert_gollum_tags = Mock(return_value=u'# test message') importer._with_history(self.commit2) assert_equal(upsert.call_args_list, [call('Home')]) assert_equal(md2mkm.call_args_list, [call(u'# test message')])
def test_convert_gollum_page_links(self): f = GitHubWikiImporter().convert_gollum_tags assert_equal(f(u'[[Page]]'), u'[Page]') assert_equal(f(u'[[Page Title|Page]]'), u'[Page Title](Page)') assert_equal(f(u'[[Pagê Nâme]]'), u'[Pagê Nâme]') # Github always converts spaces and slashes in links to hyphens, # to lookup page in the filesystem. During import we're converting # all hyphens in page name to spaces, but still supporting both link formats. assert_equal(f(u'[[Page With Spaces]]'), u'[Page With Spaces]') assert_equal(f(u'[[Page-With-Spaces]]'), u'[Page With Spaces]') assert_equal(f(u'[[Page / 1]]'), u'[Page 1]') assert_equal(f(u'[[Title|Page With Spaces]]'), u'[Title](Page With Spaces)') assert_equal(f(u'[[Title|Page-With-Spaces]]'), u'[Title](Page With Spaces)') assert_equal(f(u'[[go here|Page / 1]]'), u'[go here](Page 1)')
def test_without_history(self, render, upsert): self.commit2.tree.blobs = [self.blob2, self.blob3] upsert.text = Mock() importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.config.options = {} importer.app.url = '/p/test/wiki/' importer.rewrite_links = Mock(return_value='') importer._without_history(self.commit2) assert_equal(upsert.call_args_list, [call('Home2'), call('Home3')]) assert_equal(render.call_args_list, [ call('Home2.creole', '**test message**'), call('Home3.rest', 'test message')])
def test_convert_gollum_tags(self): f = GitHubWikiImporter().convert_gollum_tags source = '''Look at [[this page|Some Page]] More info at: [[MoreInfo]] [[Even More Info]] Our website is [[http://domain.net]]. '[[Escaped Tag]]''' result = '''Look at [this page](Some Page) More info at: [MoreInfo] [Even More Info] Our website is <http://domain.net>. [[Escaped Tag]]''' assert_equal(f(source), result)
def test_without_history(self, render, upsert): self.commit2.tree.blobs = [self.blob2, self.blob3] upsert.text = Mock() importer = GitHubWikiImporter() importer.github_wiki_url = 'https://github.com/a/b/wiki' importer.app = Mock() importer.app.config.options = {} importer.app.url = '/p/test/wiki/' importer.rewrite_links = Mock(return_value='') importer._without_history(self.commit2) assert_equal(upsert.call_args_list, [call('Home2'), call('Home3')]) assert_equal(render.call_args_list, [ call('Home2.creole', u'**test message**'), call('Home3.rest', u'test message')])
def test_import_with_history(self, without_history, with_history, clone): repo = clone.return_value repo.iter_commits.return_value = [self.commit1, self.commit2] GitHubWikiImporter().import_pages('wiki_url', history=True) assert_equal(with_history.call_count, 2) assert_equal(without_history.call_count, 0)
def test_convert_gollum_external_links(self): f = GitHubWikiImporter().convert_gollum_tags assert_equal(f(u'[[http://sf.net]]'), u'<http://sf.net>') assert_equal(f(u'[[https://sf.net]]'), u'<https://sf.net>') assert_equal(f(u'[[SourceForge|http://sf.net]]'), u'[SourceForge](http://sf.net)')
def test_convert_gollum_external_links_escaped(self): f = GitHubWikiImporter().convert_gollum_tags assert_equal(f(u"'[[http://sf.net]]"), u'[[http://sf.net]]') assert_equal(f(u"'[[https://sf.net]]"), u'[[https://sf.net]]') assert_equal(f(u"'[[SourceForge|http://sf.net]]"), u'[[SourceForge|http://sf.net]]')
def test_gollum_page_links_case_insensitive(self): i = GitHubWikiImporter() i.available_pages = [u'Home 42', u'code & fun'] assert_equal(i.convert_gollum_tags(u'[[Code & Fun]]'), u'[code & fun]') assert_equal(i.convert_gollum_tags(u'[[home-42]]'), u'[Home 42]') assert_equal(i.convert_gollum_tags(u'[[Unknown]]'), u'[Unknown]')
def test_get_commits_without_history(self, without_history, with_history): with patch('forgeimporters.github.wiki.git.Repo._clone'): GitHubWikiImporter().import_pages('wiki_url') assert_equal(with_history.call_count, 0) assert_equal(without_history.call_count, 1)
def test_gollum_page_links_case_insensitive(self): i = GitHubWikiImporter() i.available_pages = ['Home 42', 'code & fun'] assert_equal(i.convert_gollum_tags('[[Code & Fun]]'), '[code & fun]') assert_equal(i.convert_gollum_tags('[[home-42]]'), '[Home 42]') assert_equal(i.convert_gollum_tags('[[Unknown]]'), '[Unknown]')
def test_convert_gollum_toc(self): f = GitHubWikiImporter().convert_gollum_tags assert_equal(f('[[_TOC_]]'), '[TOC]') assert_equal(f("'[[_TOC_]]"), '[[_TOC_]]')
def test_convert_gollum_external_links_escaped(self): f = GitHubWikiImporter().convert_gollum_tags assert_equal(f("'[[http://domain.net]]"), '[[http://domain.net]]') assert_equal(f("'[[https://domain.net]]"), '[[https://domain.net]]') assert_equal(f("'[[Site|http://domain.net]]"), '[[Site|http://domain.net]]')
def test_convert_gollum_external_links(self): f = GitHubWikiImporter().convert_gollum_tags assert_equal(f('[[http://domain.net]]'), '<http://domain.net>') assert_equal(f('[[https://domain.net]]'), '<https://domain.net>') assert_equal(f('[[Site|http://domain.net]]'), '[Site](http://domain.net)')
def test_convert_page_name(self): f = GitHubWikiImporter()._convert_page_name assert_equal(f('Page Name'), 'Page Name') assert_equal(f('Page-Name'), 'Page Name') assert_equal(f('Page / Name'), 'Page Name')