Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests windows #59

Closed
wants to merge 10 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More elegant mock
  • Loading branch information
adferrand committed Jul 26, 2019
commit 55af6736a1f22c99ad587deef68a14feccaf5f10
43 changes: 28 additions & 15 deletions certbot/tests/util_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for certbot.util."""
import argparse
import contextlib
import errno
import unittest

Expand Down Expand Up @@ -54,28 +55,40 @@ def _call(cls, exe):

@mock.patch("certbot.util.filesystem.os.path.isfile")
@mock.patch("certbot.util.filesystem.os.access")
@mock.patch("certbot.util.filesystem._win_is_executable")
def test_full_path(self, mock_win_executable, mock_access, mock_isfile):
mock_win_executable.return_value = True
mock_access.return_value = True
mock_isfile.return_value = True
self.assertTrue(self._call("/path/to/exe"))
def test_full_path(self, mock_access, mock_isfile):
with _fix_window_runtime():
mock_access.return_value = True
mock_isfile.return_value = True
self.assertTrue(self._call("/path/to/exe"))

@mock.patch("certbot.util.filesystem.os.path.isfile")
@mock.patch("certbot.util.filesystem.os.access")
@mock.patch("certbot.util.filesystem._win_is_executable")
def test_rel_path(self, mock_win_executable, mock_access, mock_isfile):
mock_win_executable.return_value = True
mock_access.return_value = True
mock_isfile.return_value = True
self.assertTrue(self._call("exe"))
def test_rel_path(self, mock_access, mock_isfile):
with _fix_window_runtime():
mock_access.return_value = True
mock_isfile.return_value = True
self.assertTrue(self._call("exe"))

@mock.patch("certbot.util.filesystem.os.path.isfile")
@mock.patch("certbot.util.filesystem.os.access")
def test_not_found(self, mock_access, mock_isfile):
mock_access.return_value = True
mock_isfile.return_value = False
self.assertFalse(self._call("exe"))
with _fix_window_runtime():
mock_access.return_value = True
mock_isfile.return_value = False
self.assertFalse(self._call("exe"))


@contextlib.contextmanager
def _fix_window_runtime():
if os.name != 'nt':
yield

import ntsecuritycon # pylint: disable=import-error
with mock.patch('win32security.GetFileSecurity') as mock_get:
dacl_mock = mock_get.return_value.GetSecurityDescriptorDacl
mode_mock = dacl_mock.return_value.GetEffectiveRightsFromAcl
mode_mock.return_value = ntsecuritycon.FILE_GENERIC_EXECUTE
yield


class LockDirUntilExit(test_util.TempDirTestCase):
Expand Down