def test_temporary_working_directory(): with TemporaryWorkingDirectory() as directory: directory_path = Path(directory).resolve() assert directory_path.exists() assert Path.cwd().resolve() == directory_path assert not directory_path.exists() assert Path.cwd().resolve() != directory_path
def test_quoted_file_completions(self): ip = get_ipython() with TemporaryWorkingDirectory(): name = "foo'bar" open(name, "w").close() # Don't escape Windows escaped = name if sys.platform == "win32" else "foo\\'bar" # Single quote matches embedded single quote text = "open('foo" c = ip.Completer._complete( cursor_line=0, cursor_pos=len(text), full_text=text )[1] self.assertEqual(c, [escaped]) # Double quote requires no escape text = 'open("foo' c = ip.Completer._complete( cursor_line=0, cursor_pos=len(text), full_text=text )[1] self.assertEqual(c, [name]) # No quote requires an escape text = "%ls foo" c = ip.Completer._complete( cursor_line=0, cursor_pos=len(text), full_text=text )[1] self.assertEqual(c, [escaped])
def test_no_ascii_back_completion(self): ip = get_ipython() with TemporaryWorkingDirectory(): # Avoid any filename completions # single ascii letter that don't have yet completions for letter in "jJ": name, matches = ip.complete("\\" + letter) nt.assert_equal(matches, [])
def from_notebook_node(self, nb, resources=None, **kw): latex, resources = super(PDFExporter, self).from_notebook_node(nb, resources=resources, **kw) with TemporaryWorkingDirectory() as td: notebook_name = "notebook" tex_file = self.writer.write(latex, resources, notebook_name=notebook_name) self.log.info("Building PDF") rc = self.run_latex(tex_file) if not rc: rc = self.run_bib(tex_file) if not rc: rc = self.run_latex(tex_file) pdf_file = notebook_name + '.pdf' if not os.path.isfile(pdf_file): raise RuntimeError("PDF creating failed") self.log.info('PDF successfully created') with open(pdf_file, 'rb') as f: pdf_data = f.read() # convert output extension to pdf # the writer above required it to be tex resources['output_extension'] = '.pdf' return pdf_data, resources
def create_temp_cwd(self, copy_filenames=None): temp_dir = TemporaryWorkingDirectory() #Copy the files if requested. if copy_filenames is not None: self.copy_files_to(copy_filenames) #Return directory handler return temp_dir
def test_file_spaces(): """%%file with spaces in filename""" ip = get_ipython() with TemporaryWorkingDirectory() as td: fname = "file name" ip.run_cell_magic("file", '"%s"' % fname, u"\n".join(["line1", "line2"])) with open(fname) as f: s = f.read() nt.assert_in("line1\n", s) nt.assert_in("line2", s)
def test_file_spaces(): """%%file with spaces in filename""" ip = get_ipython() with TemporaryWorkingDirectory() as td: fname = "file name" ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([ 'line1', 'line2', ])) s = Path(fname).read_text() nt.assert_in('line1\n', s) nt.assert_in('line2', s)
def test_cve_2022_21699(): """ Here we test CVE-2022-21699. We create a temporary directory, cd into it. Make a profile file that should not be executed and start IPython in a subprocess, checking for the value. """ dangerous_profile_dir = Path("profile_default") dangerous_startup_dir = dangerous_profile_dir / "startup" dangerous_expected = "CVE-2022-21699-" + "".join( [random.choice(string.ascii_letters) for i in range(10)]) with TemporaryWorkingDirectory() as t: dangerous_startup_dir.mkdir(parents=True) (dangerous_startup_dir / "foo.py").write_text( f'print("{dangerous_expected}")', encoding="utf-8") # 1 sec to make sure FS is flushed. # time.sleep(1) cmd = [sys.executable, "-m", "IPython"] env = os.environ.copy() env["IPY_TEST_SIMPLE_PROMPT"] = "1" # First we fake old behavior, making sure the profile is/was actually dangerous p_dangerous = subprocess.Popen( cmd + [f"--profile-dir={dangerous_profile_dir}"], env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) out_dangerous, err_dangerouns = p_dangerous.communicate(b"exit\r") assert dangerous_expected in out_dangerous.decode() # Now that we know it _would_ have been dangerous, we test it's not loaded p = subprocess.Popen( cmd, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) out, err = p.communicate(b"exit\r") assert b"IPython" in out assert dangerous_expected not in out.decode() assert err == b""
def test_file_spaces(): """%%file with spaces in filename""" ip = get_ipython() with TemporaryWorkingDirectory() as td: fname = "file name" ip.run_cell_magic( "file", '"%s"' % fname, "\n".join([ "line1", "line2", ]), ) s = Path(fname).read_text(encoding="utf-8") assert "line1\n" in s assert "line2" in s
def test_get_connection_file(): cfg = Config() with TemporaryWorkingDirectory() as d: cfg.ProfileDir.location = d cf = 'kernel.json' app = DummyConsoleApp(config=cfg, connection_file=cf) app.initialize(argv=[]) profile_cf = os.path.join(app.profile_dir.location, 'security', cf) nt.assert_equal(profile_cf, app.connection_file) with open(profile_cf, 'w') as f: f.write("{}") nt.assert_true(os.path.exists(profile_cf)) nt.assert_equal(connect.get_connection_file(app), profile_cf) app.connection_file = cf nt.assert_equal(connect.get_connection_file(app), profile_cf)
def test_local_file_completions(self): ip = get_ipython() with TemporaryWorkingDirectory(): prefix = "./foo" suffixes = ["1", "2"] names = [prefix + s for s in suffixes] for n in names: open(n, "w").close() # Check simple completion c = ip.complete(prefix)[1] self.assertEqual(c, names) # Now check with a function call cmd = 'a = f("%s' % prefix c = ip.complete(prefix, cmd)[1] comp = {prefix + s for s in suffixes} self.assertTrue(comp.issubset(set(c)))
def test_local_file_completions(): ip = get_ipython() with TemporaryWorkingDirectory(): prefix = './foo' suffixes = ['1', '2'] names = [prefix+s for s in suffixes] for n in names: open(n, 'w').close() # Check simple completion c = ip.complete(prefix)[1] nt.assert_equal(c, names) # Now check with a function call cmd = 'a = f("%s' % prefix c = ip.complete(prefix, cmd)[1] comp = [prefix+s for s in suffixes] nt.assert_equal(c, comp)
def test_temporary_working_directory(): with TemporaryWorkingDirectory() as dir: assert os.path.exists(dir) assert os.path.realpath(os.curdir) == os.path.realpath(dir) assert not os.path.exists(dir) assert os.path.abspath(os.curdir) != dir
def test_render_unicode_cwd(self): with TemporaryWorkingDirectory(u'ünicødé'): self.pm.in_template = r'\w [\#]' p = self.pm.render('in', color=False) self.assertEqual( p, u"%s [%i]" % (py3compat.getcwd(), ip.execution_count))