def test_custom_kernel_manager(self): from .fake_kernelmanager import FakeCustomKernelManager current_dir = os.path.dirname(__file__) filename = os.path.join(current_dir, 'files', 'HelloWorld.ipynb') with io.open(filename) as f: input_nb = nbformat.read(f, 4) preprocessor = self.build_preprocessor({ 'kernel_manager_class': FakeCustomKernelManager }) cleaned_input_nb = copy.deepcopy(input_nb) for cell in cleaned_input_nb.cells: if 'execution_count' in cell: del cell['execution_count'] cell['outputs'] = [] # Override terminal size to standardise traceback format with modified_env({'COLUMNS': '80', 'LINES': '24'}): output_nb, _ = preprocessor(cleaned_input_nb, self.build_resources()) expected = FakeCustomKernelManager.expected_methods.items() for method, call_count in expected: self.assertNotEqual(call_count, 0, '{} was called'.format(method))
def test_get_ipython_dir_7(): """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR""" home_dir = os.path.normpath(os.path.expanduser('~')) with modified_env({'IPYTHONDIR': os.path.join('~', 'somewhere')}), \ patch.object(paths, '_writable_dir', return_value=True): ipdir = paths.get_ipython_dir() nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
def test_get_ipython_dir_1(): """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions.""" env_ipdir = os.path.join("someplace", ".ipython") with patch.object(paths, '_writable_dir', return_value=True), \ modified_env({'IPYTHONDIR': env_ipdir}): ipdir = paths.get_ipython_dir() nt.assert_equal(ipdir, env_ipdir)
def test_get_ipython_dir_8(): """test_get_ipython_dir_8, test / home directory""" with patch.object(paths, '_writable_dir', lambda path: bool(path)), \ patch.object(paths, 'get_xdg_dir', return_value=None), \ modified_env({ 'IPYTHON_DIR': None, 'IPYTHONDIR': None, 'HOME': '/', }): nt.assert_equal(paths.get_ipython_dir(), '/.ipython')
def test_get_ipython_cache_dir(): with modified_env({'HOME': HOME_TEST_DIR}): if os.name == 'posix' and sys.platform != 'darwin': # test default os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) with modified_env({'XDG_CACHE_HOME': None}): ipdir = paths.get_ipython_cache_dir() nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), ipdir) assert_isdir(ipdir) # test env override with modified_env({"XDG_CACHE_HOME": XDG_CACHE_DIR}): ipdir = paths.get_ipython_cache_dir() assert_isdir(ipdir) nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) else: nt.assert_equal(paths.get_ipython_cache_dir(), paths.get_ipython_dir())
def test_get_ipython_dir_2(): """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions.""" with patch_get_home_dir('someplace'), \ patch.object(paths, 'get_xdg_dir', return_value=None), \ patch.object(paths, '_writable_dir', return_value=True), \ patch('os.name', "posix"), \ modified_env({'IPYTHON_DIR': None, 'IPYTHONDIR': None, 'XDG_CONFIG_HOME': None }): ipdir = paths.get_ipython_dir() nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
def test_ensure_path_env(self): def mock_expanduser(p): return p.replace('~', self.td) with mock.patch('os.path.expanduser', mock_expanduser), \ testpath.modified_env({'PATH': '/bin'}): self.installer.ensure_path_env() profile_file = pjoin(self.td, '.profile') testpath.assert_isfile(profile_file) with open(profile_file, 'r') as f: contents = f.read() assert self.installer.scheme['commands']+':$PATH' in contents
def test_modified_env_nosnapshot(self): os.environ['abc123'] = '4' os.environ['def567'] = '8' os.environ['ghi789'] = '10' os.environ.pop('foo951', None) with testpath.modified_env({'abc123': '9', 'def567': None}, snapshot=False): self.assertEqual(os.environ['abc123'], '9') self.assertNotIn('def567', os.environ) self.assertEqual(os.environ['ghi789'], '10') # Not affected os.environ['foo951'] = 'bar' self.assertEqual(os.environ['abc123'], '4') self.assertEqual(os.environ['def567'], '8') self.assertEqual(os.environ['ghi789'], '10') self.assertEqual(os.environ['foo951'], 'bar') # Not reset
def run(self, test, compileflags=None, out=None, clear_globs=True): # Hack: ipython needs access to the execution context of the example, # so that it can propagate user variables loaded by %run into # test.globs. We put them here into our modified %run as a function # attribute. Our new %run will then only make the namespace update # when called (rather than unconconditionally updating test.globs here # for all examples, most of which won't be calling %run anyway). #_ip._ipdoctest_test_globs = test.globs #_ip._ipdoctest_test_filename = test.filename test.globs.update(_ip.user_ns) # Override terminal size to standardise traceback format with modified_env({'COLUMNS': '80', 'LINES': '24'}): return super(IPDocTestRunner,self).run(test, compileflags,out,clear_globs)
def test_get_ipython_dir_5(): """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist.""" with patch_get_home_dir(HOME_TEST_DIR), \ patch('os.name', 'posix'): try: os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython')) except OSError as e: if e.errno != errno.ENOENT: raise with modified_env({ 'IPYTHON_DIR': None, 'IPYTHONDIR': None, 'XDG_CONFIG_HOME': XDG_TEST_DIR, }): ipdir = paths.get_ipython_dir() nt.assert_equal(ipdir, IP_TEST_DIR)
def test_get_ipython_dir_6(): """test_get_ipython_dir_6, use home over XDG if defined and neither exist.""" xdg = os.path.join(HOME_TEST_DIR, 'somexdg') os.mkdir(xdg) shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython')) print(paths._writable_dir) with patch_get_home_dir(HOME_TEST_DIR), \ patch.object(paths, 'get_xdg_dir', return_value=xdg), \ patch('os.name', 'posix'), \ modified_env({ 'IPYTHON_DIR': None, 'IPYTHONDIR': None, 'XDG_CONFIG_HOME': None, }), warnings.catch_warnings(record=True) as w: ipdir = paths.get_ipython_dir() nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, '.ipython')) nt.assert_equal(len(w), 0)
def test_get_ipython_dir_3(): """test_get_ipython_dir_3, move XDG if defined, and .ipython doesn't exist.""" tmphome = TemporaryDirectory() try: with patch_get_home_dir(tmphome.name), \ patch('os.name', 'posix'), \ modified_env({ 'IPYTHON_DIR': None, 'IPYTHONDIR': None, 'XDG_CONFIG_HOME': XDG_TEST_DIR, }), warnings.catch_warnings(record=True) as w: ipdir = paths.get_ipython_dir() nt.assert_equal(ipdir, os.path.join(tmphome.name, ".ipython")) if sys.platform != 'darwin': nt.assert_equal(len(w), 1) nt.assert_in('Moving', str(w[0])) finally: tmphome.cleanup()
def run_notebook(self, filename, opts, resources): """Loads and runs a notebook, returning both the version prior to running it and the version after running it. """ with io.open(filename) as f: input_nb = nbformat.read(f, 4) preprocessor = self.build_preprocessor(opts) cleaned_input_nb = copy.deepcopy(input_nb) for cell in cleaned_input_nb.cells: if 'execution_count' in cell: del cell['execution_count'] cell['outputs'] = [] # Override terminal size to standardise traceback format with modified_env({'COLUMNS': '80', 'LINES': '24'}): output_nb, _ = preprocessor(cleaned_input_nb, resources) return input_nb, output_nb
def test_console_example(): responses.add_callback('GET', re.compile(r'https://www.python.org/ftp/.*'), callback=respond_python_zip, content_type='application/zip', ) with TemporaryWorkingDirectory() as td: for src in example_dir.iterdir(): copy(str(src), td) with modified_env({CACHE_ENV_VAR: td}), \ MockCommand('makensis') as makensis: ec = main(['installer.cfg']) assert ec == 0 assert makensis.get_calls()[0]['argv'][1].endswith('installer.nsi') build_dir = Path(td, 'build', 'nsis') assert_isdir(build_dir) assert_isfile(build_dir / 'Python' / 'python.exe') assert_isfile(build_dir / 'pkgs' / 'guessnumber.py') assert_isfile(build_dir / 'Guess_the_Number.launch.py')
def test_get_ipython_dir_4(): """test_get_ipython_dir_4, warn if XDG and home both exist.""" with patch_get_home_dir(HOME_TEST_DIR), \ patch('os.name', 'posix'): try: os.mkdir(os.path.join(XDG_TEST_DIR, 'ipython')) except OSError as e: if e.errno != errno.EEXIST: raise with modified_env({ 'IPYTHON_DIR': None, 'IPYTHONDIR': None, 'XDG_CONFIG_HOME': XDG_TEST_DIR, }), warnings.catch_warnings(record=True) as w: ipdir = paths.get_ipython_dir() nt.assert_equal(ipdir, os.path.join(HOME_TEST_DIR, ".ipython")) if sys.platform != 'darwin': nt.assert_equal(len(w), 1) nt.assert_in('Ignoring', str(w[0]))
def test_build_sdist_unsupported(): hooks = get_hooks('pkg1') with TemporaryDirectory() as sdistdir: with modified_env({'PYTHONPATH': BUILDSYS_PKGS}): with pytest.raises(UnsupportedOperation): hooks.build_sdist(sdistdir, {'test_unsupported': True})
def test_missing_backend_gives_exception(): hooks = get_hooks('pkg1') with modified_env({'PYTHONPATH': ''}): with pytest.raises(BackendUnavailable): hooks.get_requires_for_build_wheel({})
def test_get_requires_for_build_sdist(): hooks = get_hooks('pkg2') with modified_env({'PYTHONPATH': BUILDSYS_PKGS}): res = hooks.get_requires_for_build_sdist({}) assert res == []