def create_temp_dir(files): with tempman.create_temp_dir() as directory: for path, contents in files.items(): with open(os.path.join(directory.path, path), "w") as fileobj: fileobj.write(contents) yield directory
def using_root_to_create_temporary_directories_creates_directories_under_root_path( ): with tempman.create_temp_dir() as parent_directory: root = tempman.root(parent_directory.path) with root.create_temp_dir() as directory: assert_equal([os.path.basename(directory.path)], os.listdir(parent_directory.path))
def _files_and_directories_older_than_timeout_are_deleted_when_cleaning_up( one_minute): with tempman.create_temp_dir() as parent_directory: root = tempman.root(parent_directory.path, timeout=one_minute) now = time.time() old_dir = os.path.join(parent_directory.path, "one") os.mkdir(old_dir) os.utime(old_dir, (now - 70, now - 70)) newer_dir = os.path.join(parent_directory.path, "two") os.mkdir(newer_dir) os.utime(newer_dir, (now - 10, now - 10)) old_file = os.path.join(parent_directory.path, 'file_one') with open(old_file, 'a'): os.utime(old_file, (now - 70, now - 70)) new_file = os.path.join(parent_directory.path, 'file_two') with open(new_file, 'a'): os.utime(new_file, (now - 10, now - 10)) temp_file = root.create_temp_file(prefix='prefix') root.cleanup() assert_equal( sorted([temp_file.path.split('/')[-1:][0], "file_two", "two"]), sorted(os.listdir(parent_directory.path)))
def _check_program_string(program): with tempman.create_temp_dir() as temp_dir: path = os.path.join(temp_dir.path, "main.py") with open(path, "w") as main_file: main_file.write("#!/usr/bin/env python\n") main_file.write(program) return nope.check(path=path)
def html_is_written_to_file_if_output_file_is_set(): with tempman.create_temp_dir() as temp_dir: output_path = os.path.join(temp_dir.path, "output.html") docx_path = test_path("single-paragraph.docx") result = _local.run(["mammoth", docx_path, output_path]) assert_equal(b"", result.stderr_output) assert_equal(b"", result.output) with open(output_path) as output_file: assert_equal("<p>Walking on imported air</p>", output_file.read())
def html_is_written_to_file_if_output_file_is_set(): with tempman.create_temp_dir() as temp_dir: output_path = os.path.join(temp_dir.path, "output.html") docx_path = test_path("single-paragraph.docx") result = _local.run(["mammoth", docx_path, output_path]) assert_equal(b"", result.stderr_output) assert_equal(b"", result.output) with open(output_path) as output_file: assert_equal("<p>Walking on imported air</p>", output_file.read())
def style_map_is_used_if_set(): with tempman.create_temp_dir() as temp_dir: docx_path = test_path("single-paragraph.docx") style_map_path = os.path.join(temp_dir.path, "style-map") with open(style_map_path, "w") as style_map_file: style_map_file.write("p => span:fresh") result = _local.run(["mammoth", docx_path, "--style-map", style_map_path]) assert_equal(b"", result.stderr_output) assert_equal(b"<span>Walking on imported air</span>", result.output)
def create_project(travis_yml): temp_dir = tempman.create_temp_dir() try: with open(os.path.join(temp_dir.path, ".travis.yml"), "w") as travis_yml_file: yaml.dump(travis_yml, travis_yml_file) except Exception: temp_dir.close() raise return Project(temp_dir)
def style_map_is_used_if_set(): with tempman.create_temp_dir() as temp_dir: docx_path = test_path("single-paragraph.docx") style_map_path = os.path.join(temp_dir.path, "style-map") with open(style_map_path, "w") as style_map_file: style_map_file.write("p => span:fresh") result = _local.run( ["mammoth", docx_path, "--style-map", style_map_path]) assert_equal(b"", result.stderr_output) assert_equal(b"<span>Walking on imported air</span>", result.output)
def cleanup_occurs_when_creating_directory_under_root(): with tempman.create_temp_dir() as parent_directory: root = tempman.root(parent_directory.path, timeout=60) now = time.time() old_dir = os.path.join(parent_directory.path, "one") os.mkdir(old_dir) os.utime(old_dir, (now - 70, now - 70)) with root.create_temp_dir() as directory: pass assert_equal([], os.listdir(parent_directory.path))
def cleanup_occurs_when_creating_directory_under_root(): with tempman.create_temp_dir() as parent_directory: root = tempman.root(parent_directory.path, timeout=60) now = time.time() old_dir = os.path.join(parent_directory.path, "one") os.mkdir(old_dir) os.utime(old_dir, (now - 70, now - 70)) with root.create_temp_dir() as directory: pass assert_equal([], os.listdir(parent_directory.path))
def warn_if_images_stored_outside_of_document_are_not_found(): with tempman.create_temp_dir() as temp_dir: document_path = os.path.join(temp_dir.path, "document.docx") with open(document_path, "wb") as fileobj: with open(test_path("external-picture.docx"), "rb") as source_fileobj: shutil.copyfileobj(source_fileobj, fileobj) with open(document_path, "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("", result.value) expected_warning = "could not open external image: 'tiny-picture.png'" assert_equal("warning", result.messages[0].type) assert result.messages[0].message.startswith(expected_warning), "message was: " + result.messages[0].message assert_equal(1, len(result.messages))
def images_are_written_to_separate_files_if_output_dir_is_set(): with tempman.create_temp_dir() as temp_dir: output_path = os.path.join(temp_dir.path, "tiny-picture.html") image_path = os.path.join(temp_dir.path, "1.png") docx_path = test_path("tiny-picture.docx") result = _local.run(["mammoth", docx_path, "--output-dir", temp_dir.path]) assert_equal(b"", result.stderr_output) assert_equal(b"", result.output) with open(output_path) as output_file: assert_equal("""<p><img src="1.png" /></p>""", output_file.read()) with open(image_path, "rb") as image_file: assert_equal(_image_base_64, base64.b64encode(image_file.read()))
def images_are_written_to_separate_files_if_output_dir_is_set(): with tempman.create_temp_dir() as temp_dir: output_path = os.path.join(temp_dir.path, "tiny-picture.html") image_path = os.path.join(temp_dir.path, "1.png") docx_path = test_path("tiny-picture.docx") result = _local.run( ["mammoth", docx_path, "--output-dir", temp_dir.path]) assert_equal(b"", result.stderr_output) assert_equal(b"", result.output) with open(output_path) as output_file: assert_equal("""<p><img src="1.png" /></p>""", output_file.read()) with open(image_path, "rb") as image_file: assert_equal(_image_base_64, base64.b64encode(image_file.read()))
def _directories_older_than_timeout_are_deleted_when_cleaning_up(one_minute): with tempman.create_temp_dir() as parent_directory: root = tempman.root(parent_directory.path, timeout=one_minute) now = time.time() old_dir = os.path.join(parent_directory.path, "one") os.mkdir(old_dir) os.utime(old_dir, (now - 70, now - 70)) newer_dir = os.path.join(parent_directory.path, "two") os.mkdir(newer_dir) os.utime(newer_dir, (now - 10, now - 10)) root.cleanup() assert_equal(["two"], os.listdir(parent_directory.path))
def warn_if_images_stored_outside_of_document_are_not_found(): with tempman.create_temp_dir() as temp_dir: document_path = os.path.join(temp_dir.path, "document.docx") with open(document_path, "wb") as fileobj: with open(test_path("external-picture.docx"), "rb") as source_fileobj: shutil.copyfileobj(source_fileobj, fileobj) with open(document_path, "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("", result.value) expected_warning = "could not open external image: 'tiny-picture.png'" assert_equal("warning", result.messages[0].type) assert result.messages[0].message.startswith( expected_warning), "message was: " + result.messages[0].message assert_equal(1, len(result.messages))
def _directories_older_than_timeout_are_deleted_when_cleaning_up(one_minute): with tempman.create_temp_dir() as parent_directory: root = tempman.root(parent_directory.path, timeout=one_minute) now = time.time() old_dir = os.path.join(parent_directory.path, "one") os.mkdir(old_dir) os.utime(old_dir, (now - 70, now - 70)) newer_dir = os.path.join(parent_directory.path, "two") os.mkdir(newer_dir) os.utime(newer_dir, (now - 10, now - 10)) root.cleanup() assert_equal(["two"], os.listdir(parent_directory.path))
def cli_can_compile_programs_to_node_js(): with tempman.create_temp_dir() as temp_dir: output_dir = os.path.join(temp_dir.path, "output") os.mkdir(output_dir) path = os.path.join(temp_dir.path, "main.py") with open(path, "w") as main_file: main_file.write("#!/usr/bin/env python\n") main_file.write("print('Hello')") _local.run([ "nope", "compile", path, "--backend=node", "--output-dir", output_dir ]) result = _local.run(["node", os.path.join(output_dir, "main.js")]) assert_equal(b"Hello\n", result.output)
def temporary_directory_is_deleted_on_close(): directory = tempman.create_temp_dir() directory.close() assert not os.path.exists(directory.path)
def _run_program_string(self, program, allow_error=False): with tempman.create_temp_dir() as temp_dir: with open(os.path.join(temp_dir.path, "main.py"), "w") as main_file: main_file.write("#!/usr/bin/env python\n") main_file.write(program) return self._run_program(path=temp_dir.path, program="main", allow_error=allow_error)
def dir_argument_can_be_used_to_set_parent_directory(): with tempman.create_temp_dir() as parent_directory: with tempman.create_temp_dir(dir=parent_directory.path) as directory: assert_equal([os.path.basename(directory.path)], os.listdir(parent_directory.path))
def using_root_to_create_temporary_directories_creates_directories_under_root_path(): with tempman.create_temp_dir() as parent_directory: root = tempman.root(parent_directory.path) with root.create_temp_dir() as directory: assert_equal([os.path.basename(directory.path)], os.listdir(parent_directory.path))
def compiled(platform, path, program): with tempman.create_temp_dir() as temp_dir: output_dir = temp_dir.path nope.compile(path, output_dir, platform) filename = "{}.{}".format(program, platform.extension) yield CompiledCode(output_dir, filename)
def setup(self): self._temp_dir = tempman.create_temp_dir()
def temporary_directory_is_deleted_on_close(): directory = tempman.create_temp_dir() directory.close() assert not os.path.exists(directory.path)
def __init__(self): self._dir = tempman.create_temp_dir() self.run = _local.run
def temporary_directory_is_deleted_on_exit(): with tempman.create_temp_dir() as directory: pass assert not os.path.exists(directory.path)
def temporary_directory_is_initially_empty(): with tempman.create_temp_dir() as directory: assert_equal([], os.listdir(directory.path))
def temporary_directory_is_initially_empty(): with tempman.create_temp_dir() as directory: assert_equal([], os.listdir(directory.path))
def dir_argument_can_be_used_to_set_parent_directory(): with tempman.create_temp_dir() as parent_directory: with tempman.create_temp_dir(dir=parent_directory.path) as directory: assert_equal([os.path.basename(directory.path)], os.listdir(parent_directory.path))
def temporary_directory_is_deleted_on_exit(): with tempman.create_temp_dir() as directory: pass assert not os.path.exists(directory.path)