def test_with_statement_exception(self): path = '' try: with TemporaryEnvironment() as temp: path = temp.path() raise RuntimeError() except RuntimeError: pass assert not os.path.exists(path) assert not os.path.isdir(path)
def test_with_statement(self): with TemporaryEnvironment() as temp: path = temp.path() assert path != '' assert os.path.exists(path) assert os.path.isdir(path) env = temp.env() assert env.get(temp.TEMPDIR, None) is not None assert env.get(temp.TEMPDIR, None) == path assert not os.path.exists(path) assert not os.path.isdir(path)
def _build_source_from_instructions(cls, instructions, source, logfile): """Run instructions to create source archive""" logger.info("Attempting to create source '%s' using instructions in comments", source) with TemporaryEnvironment() as tmp: script = os.path.join(tmp.path(), 'script.sh') with open(script, 'w') as f: f.write('#!/bin/sh -x\n') f.write(''.join(instructions)) f.write('cp "{}" "{}"\n'.format(source, os.getcwd())) os.chmod(script, 0o755) result = ProcessHelper.run_subprocess_cwd(script, tmp.path(), output=logfile, shell=True) if result == 0 and os.path.isfile(source): logger.info('Source creation succeeded.') else: logger.info('Source creation failed.')
def test_with_statement_callback(self): tmp_file, tmp_path = tempfile.mkstemp(text=True) os.close(tmp_file) def callback(**kwargs): path = kwargs.get(TemporaryEnvironment.TEMPDIR, '') assert path != '' with open(tmp_path, 'w') as f: f.write(path) with TemporaryEnvironment(exit_callback=callback) as temp: path = temp.path() with open(tmp_path, 'r') as f: assert f.read() == path os.unlink(tmp_path)