示例#1
0
    def test_is_inside_virtual_env(self):
        """
        Check `utils.is_inside_virtual_env()` against various sets of sys prefix paths.
        """
        system = mock.sentinel.system
        virtual = mock.sentinel.virtual

        # These are the sets of sys module attributes that should be present or absent
        # with various versions of Python and virtualenv / venv.
        test_cases = {
            'py2_base': dict(prefix=system),
            # Python 3 (as of 3.3 / PEP 405) adds a sys.base_prefix attribute
            'py3_base': dict(base_prefix=system, prefix=system),
            'py3_venv': dict(base_prefix=system, prefix=virtual),
            # virtualenv saves sys.real_prefix, and changes the others
            'py2_virtualenv': dict(real_prefix=system, prefix=virtual),
            'py3_virtualenv': dict(real_prefix=system, prefix=virtual, base_prefix=virtual),
        }

        for (label, sys_attrs) in test_cases.items():
            with self.subTest(label=label):
                # Note: The spec=[] is important so that absent sys_attrs raise AttributeError
                # instead of returning mocks.
                with mock.patch('django_develop.utils.sys', spec=[], **sys_attrs):
                    expected = sys_attrs['prefix'] is virtual
                    self.assertEqual(utils.is_inside_virtual_env(), expected)
示例#2
0
    def test_no_candidates(self):
        """
        Printing out no candidates.
        """
        with mock.patch('sys.path', []):
            with self._patch_stdout() as stdout:
                utils.print_candidate_settings()

        self.assertEqual(stdout.getvalue(), dedent("""\
            Looking for usable Django settings modules in Python path... None found.

            """))
示例#3
0
 def test_examples(self):
     """
     Discover the example settings modules.
     """
     # Limit the search to the test root to avoid having to mask out third-party modules,
     # such as hypothesis._settings.
     with mock.patch('sys.path', [TEST_ROOT]):
         self.assertEqual(
             list(utils.discover_candidate_settings()),
             [(TEST_ROOT, [
                 'test_examples.error_settings',
                 'test_examples.likely_settings',
                 'test_examples.no_likely_settings',
                 'test_examples.no_settings',
             ])])
示例#4
0
 def test_dummy_path(self):
     """
     Discover no candidates from an empty / dummy `sys.path`.
     """
     paths = [
         [],
         ['dummy'],
         ['foo', 'bar'],
     ]
     for path in paths:
         with self.subTest(path=path):
             with mock.patch('sys.path', path):
                 self.assertEqual(
                     list(utils.discover_candidate_settings()),
                     [])
示例#5
0
    def test_all_candidates(self):
        """
        Printing and reporting problematic candidates too.
        """
        with mock.patch('sys.path', [TEST_ROOT]):
            with self._patch_stdout() as stdout:
                utils.print_candidate_settings(include_problems=True)

        self.assertEqual(stdout.getvalue(), dedent("""\
            Looking for usable Django settings modules in Python path... Found:

                In {}:

                    test_examples.error_settings (import raised NameError)
                    test_examples.likely_settings
                    test_examples.no_likely_settings (no likely setting names)
                    test_examples.no_settings (no uppercase names)

            """.format(TEST_ROOT)))
示例#6
0
 def _patch_stdout(self):
     # Python 2 compatibility: Intercept sys.stdout with BytesIO instead of StringIO.
     return mock.patch('sys.stdout', new_callable=(
         io.BytesIO if sys.version_info < (3,) else io.StringIO))
示例#7
0
def _patch_inside_virtual_env(expected):
    return mock.patch('django_develop.utils.is_inside_virtual_env',
                      mock.Mock(return_value=expected))