Skip to content

Commit

Permalink
Drop assert_mock_called_once compat method from tests (#2611)
Browse files Browse the repository at this point in the history
Not needed for Python >= 3.6
  • Loading branch information
mondeja committed Oct 11, 2021
1 parent 7a27572 commit b18ae29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 41 deletions.
38 changes: 12 additions & 26 deletions mkdocs/tests/build_tests.py
Expand Up @@ -24,20 +24,6 @@ def build_page(title, path, config, md_src=''):

class BuildTests(PathAssertionMixin, unittest.TestCase):

def assert_mock_called_once(self, mock):
"""assert that the mock was called only once.
The `mock.assert_called_once()` method was added in PY36.
TODO: Remove this when PY35 support is dropped.
"""
try:
mock.assert_called_once()
except AttributeError:
if not mock.call_count == 1:
mock_name = mock._mock_name or 'mock'
msg = f"Expected '{mock_name}' to have been called once. Called {self.call_count} times."
raise AssertionError(msg)

def _get_env_with_null_translations(self, config):
env = config['theme'].get_env()
env.add_extension('jinja2.ext.i18n')
Expand Down Expand Up @@ -204,8 +190,8 @@ def test_build_theme_template(self, mock_build_template, mock_write_file):
cfg = load_config()
env = cfg['theme'].get_env()
build._build_theme_template('main.html', env, mock.Mock(), cfg, mock.Mock())
self.assert_mock_called_once(mock_write_file)
self.assert_mock_called_once(mock_build_template)
mock_write_file.assert_called_once()
mock_build_template.assert_called_once()

@mock.patch('mkdocs.utils.write_file')
@mock.patch('mkdocs.commands.build._build_template', return_value='some content')
Expand All @@ -214,9 +200,9 @@ def test_build_sitemap_template(self, mock_gzip_gzipfile, mock_build_template, m
cfg = load_config()
env = cfg['theme'].get_env()
build._build_theme_template('sitemap.xml', env, mock.Mock(), cfg, mock.Mock())
self.assert_mock_called_once(mock_write_file)
self.assert_mock_called_once(mock_build_template)
self.assert_mock_called_once(mock_gzip_gzipfile)
mock_write_file.assert_called_once()
mock_build_template.assert_called_once()
mock_gzip_gzipfile.assert_called_once()

@mock.patch('mkdocs.utils.write_file')
@mock.patch('mkdocs.commands.build._build_template', return_value='')
Expand Down Expand Up @@ -244,7 +230,7 @@ def test_skip_theme_template_empty_output(self, mock_build_template, mock_write_
["INFO:mkdocs.commands.build:Template skipped: 'main.html' generated empty output."]
)
mock_write_file.assert_not_called()
self.assert_mock_called_once(mock_build_template)
mock_build_template.assert_called_once()

# Test build._build_extra_template

Expand Down Expand Up @@ -339,7 +325,7 @@ def test_populate_page_read_error(self, docs_dir, mock_open):
"ERROR:mkdocs.commands.build:Error reading page 'missing.md': Error message."
]
)
self.assert_mock_called_once(mock_open)
mock_open.assert_called_once()

@tempdir(files={'index.md': 'page content'})
@mock.patch('mkdocs.plugins.PluginCollection.run_event', side_effect=PluginError('Error message.'))
Expand All @@ -354,7 +340,7 @@ def test_populate_page_read_plugin_error(self, docs_dir, mock_open):
"ERROR:mkdocs.commands.build:Error reading page 'index.md':"
]
)
self.assert_mock_called_once(mock_open)
mock_open.assert_called_once()

# Test build._build_page

Expand Down Expand Up @@ -390,7 +376,7 @@ def test_build_page(self, site_dir):
# cm.output,
# ["INFO:mkdocs.commands.build:Page skipped: 'index.md'. Generated empty output."]
# )
# self.assert_mock_called_once(mock_template.render)
# mock_template.render.assert_called_once()
# self.assertPathNotFile(site_dir, 'index.html')

@tempdir(files={'index.md': 'page content'})
Expand Down Expand Up @@ -420,7 +406,7 @@ def test_build_page_dirty_not_modified(self, site_dir, mock_write_file):
page.markdown = 'page content'
page.content = '<p>page content</p>'
build._build_page(page, cfg, files, nav, self._get_env_with_null_translations(cfg), dirty=True)
self.assert_mock_called_once(mock_write_file)
mock_write_file.assert_called_once()

@tempdir()
def test_build_page_custom_template(self, site_dir):
Expand Down Expand Up @@ -461,7 +447,7 @@ def test_build_page_error(self, site_dir, mock_write_file):
cm.output,
["ERROR:mkdocs.commands.build:Error building page 'index.md': Error message."]
)
self.assert_mock_called_once(mock_write_file)
mock_write_file.assert_called_once()

@tempdir()
@mock.patch('mkdocs.plugins.PluginCollection.run_event', side_effect=PluginError('Error message.'))
Expand All @@ -480,7 +466,7 @@ def test_build_page_plugin_error(self, site_dir, mock_write_file):
cm.output,
["ERROR:mkdocs.commands.build:Error building page 'index.md':"]
)
self.assert_mock_called_once(mock_write_file)
mock_write_file.assert_called_once()

# Test build.build

Expand Down
16 changes: 1 addition & 15 deletions mkdocs/tests/gh_deploy_tests.py
Expand Up @@ -10,20 +10,6 @@

class TestGitHubDeploy(unittest.TestCase):

def assert_mock_called_once(self, mock):
"""assert that the mock was called only once.
The `mock.assert_called_once()` method was added in PY36.
TODO: Remove this when PY35 support is dropped.
"""
try:
mock.assert_called_once()
except AttributeError:
if not mock.call_count == 1:
mock_name = mock._mock_name or 'mock'
msg = f"Expected '{mock_name}' to have been called once. Called {self.call_count} times."
raise AssertionError(msg)

@mock.patch('subprocess.Popen')
def test_is_cwd_git_repo(self, mock_popeno):

Expand Down Expand Up @@ -128,7 +114,7 @@ def test_deploy_ignore_version_default(self, mock_import, check_version, get_rem
remote_branch='test',
)
gh_deploy.gh_deploy(config)
self.assert_mock_called_once(check_version)
check_version.assert_called_once()

@mock.patch('mkdocs.commands.gh_deploy._is_cwd_git_repo', return_value=True)
@mock.patch('mkdocs.commands.gh_deploy._get_current_sha', return_value='shashas')
Expand Down

0 comments on commit b18ae29

Please sign in to comment.