def test_right_extension_function(): # creates same extension as input file create_extension = lambda filein: filein.split('.')[-1] PutsHelloExtension = carpet.create_context_class(puts_hello_into_file, output_extension=create_extension) dummy = create_dummy_file(extension=".pepe") with PutsHelloExtension(dummy) as temp_file: assert temp_file.endswith(".pepe")
"""Appends hello to a file content and writes it to another file.""" with open(file_in) as fi: content = fi.read() content += "hello" with open(file_out, "w") as fo: fo.write(content) def create_dummy_file(extension=""): tmp = tempfile.mktemp() + extension with open(tmp, "w") as tf: tf.write("hello!") return tmp ############### #### Tests #### ############### PutsHello = carpet.create_context_class(puts_hello_into_file) ### Tests again TempFileContext ### def test_base_class_right_extension_string(): extension = ".nii" with carpet.TempFileContext(file_extension=extension, remove_at_exit=False)\ as tmp_file: assert tmp_file.endswith(extension) def test_conserves_docstring(): assert puts_hello_into_file.__doc__ in PutsHello.__doc__
def test_right_extension_string(): PutsHelloExtension = carpet.create_context_class(puts_hello_into_file, output_extension=".hello") dummy = create_dummy_file() with PutsHelloExtension(dummy) as temp_file: assert temp_file.endswith(".hello")