def test_css_folder_is_not_created(tmpdir, md_dummy):
    with tmpdir.as_cwd():
        md_dummy('# foo')
        args = ['testing.md', 'test.html']
        main(args)
        contents = os.listdir(tmpdir)
        assert 'github-markdown-css' not in contents
def test_convert_md_contains(content, exp, tmpdir, md_dummy):
    with tmpdir.as_cwd():
        md_dummy(content)
        args = ['testing.md', 'test.html']
        main(args)
        with open('test.html') as f:  # also checks that file exists
            contents = f.read()

    assert exp in contents
def test_single_img_with_link(tmpdir, md_dummy, link):
    with tmpdir.as_cwd():
        md_dummy(f'![alt_text]({link})')
        args = ['testing.md', 'test.html']
        main(args)
        with open('test.html') as f:  # also checks that file exists
            contents = f.read()
        assert f'<a href="{link}" rel="nofollow" target="_blank">' in contents
        assert (f'<img alt="alt_text" data-canonical-src="{link}" src="{link}"'
                ) in contents
def test_single_img_with_link_no_alt(tmpdir, md_dummy, alt):
    link = 'https://i.fluffy.cc/KT532NcD7QsGgd6zTXsbQsm6lX7sMrCD.png'
    with tmpdir.as_cwd():
        md_dummy(f'![{alt}]({link})')
        args = ['testing.md', 'test.html']
        main(args)
        with open('test.html') as f:  # also checks that file exists
            contents = f.read()
        assert f'<a href="{link}" rel="nofollow" target="_blank">' in contents
        assert (f'<img alt="{alt}" data-canonical-src="{link}" src="{link}" '
                'style="max-width:100%;"/>') in contents
def test_img_link_with_arguments_is_escaped(tmpdir, md_dummy):
    with tmpdir.as_cwd():
        md_dummy(f'![alt_text]({"https://foo-bar.baz?test=foo&bar=wat"})')
        args = ['testing.md', 'test.html']
        main(args)
        with open('test.html') as f:
            contents = f.read()
        assert ('<a href="https://foo-bar.baz?test=foo&amp;bar=wat" '
                'rel="nofollow" target="_blank">') in contents
        assert (
            '<img alt="alt_text" '
            'data-canonical-src="https://foo-bar.baz?test=foo&amp;bar=wat" '
            'src="https://foo-bar.baz?test=foo&amp;bar=wat"') in contents
def test_wrong_css_is_removed(tmpdir, md_dummy):
    test_str_xml = "<?xml version='1.0' encoding='UTF-8'?>"
    test_str_style = 'github-css.css" rel="stylesheet"/>'
    test_str_charset = '<meta charset="utf-8" content="text/html"/>'

    with tmpdir.as_cwd():
        md_dummy('# test')
        args = ['testing.md', 'test.html']
        main(args)

        with open('test.html') as f:
            contents = f.read()
        assert test_str_xml not in contents
        assert test_str_style not in contents
        assert test_str_charset not in contents
def test_local_imgs(tmpdir, md_dummy):
    with tmpdir.as_cwd():
        # mock an image file
        img_dir = 'local/img'
        os.makedirs(img_dir, exist_ok=True)
        link = f'{img_dir}/test.png'
        with open(link, 'w') as f:
            f.write('foo')

        md_dummy(f'![alt_text]({link})')
        args = ['testing.md', 'test.html']
        main(args)
        with open('test.html') as f:  # also checks that file exists
            contents = f.read()
        assert (f'<a href="{link}" rel="noopener noreferrer" target="_blank">'
                ) in contents
        assert (f'<img alt="alt_text" src="{link}" style="max-width:100%;"/>'
                ) in contents
def test_rate_limit_exceeded(tmpdir, md_dummy):
    with tmpdir.as_cwd():
        md_dummy('# test')
        mock_return = (
            '{"message":"API rate limit exceeded for 188.101.86.189. '
            '(But here\'s the good news: Authenticated requests get a '
            'higher rate limit. Check out the documentation for more '
            'details.)","documentation_url":'
            '"https://docs.github.com/rest/overview/resources-in-the-rest'
            '-api#rate-limiting"}')
        with mock.patch.object(
                gh_md_to_moodle.main,
                'BeautifulSoup',
                return_value=mock_return,
        ):
            args = ['testing.md', 'test.html']
            with pytest.raises(Exception) as exc:
                main(args)
        assert exc.value.args[0] == 'API limit exceeded. Wait a few minutes'
def test_regular_links_are_not_converted(tmpdir, md_dummy):
    with tmpdir.as_cwd():
        link_a = 'https://i.fluffy.cc/KT532NcD7QsGgd6zTXsbQsm6lX7sMrCD.png'
        link_b = 'https://i.fluffy.cc/9dgJNxvRbHXjlTvDM3cC74NTpkqqN468.png'
        md_dummy(f'[this is a link]({link_a})\n\n' f'![alt_text]({link_b})', )
        args = ['testing.md', 'test.html']
        main(args)
        with open('test.html') as f:  # also checks that file exists
            contents = f.read()

        # regular link
        assert f'<a href="{link_a}" rel="nofollow">this is a link</a>'

        # image
        assert (
            f'<a href="{link_b}" rel="nofollow" target="_blank">') in contents
        assert (
            f'<img alt="alt_text" data-canonical-src="{link_b}" src="{link_b}"'
            ' style="max-width:100%;"/>') in contents
def test_argparse_shows_help_no_args():
    with pytest.raises(SystemExit):
        main(['--help'])