def test_creating_created_files_object_for_specific_module(create_temp_files): """You should be able to construct a CreatedFiles wrapper for a module.""" content, target = create_temp_files(2) created_files = CreatedFiles().wrapper_for(module='my_module') created_files.insert_creation( content=content, target=target, method=CreationMethod.SYMLINK, )
def test_creation_and_cleanup_of_directory(create_temp_files): """Created directories should be tracked.""" # my_module is going to copy one file created_files = CreatedFiles().wrapper_for(module='my_module') # Specifically content -> target content, target = create_temp_files(2) # The target directory does not exist yet, so it creates it first created_files.insert_creation( content=None, target=target.parent, method=CreationMethod.MKDIR, ) # Then copies the file over created_files.insert_creation( content=content, target=target, method=CreationMethod.COPY, ) # These two creations should now be tracked global_created_files = CreatedFiles() creations = global_created_files.by('my_module') assert len(creations) == 2 assert target.parent in global_created_files assert target in global_created_files # And a small sanity check, the directory actually exists assert target.parent.is_dir() # Now we introduce a small complication; an file **not** created by # astrality is placed within this created directory. external_file = target.parent / 'external.tmp' external_file.touch() # If we now clean up the module, the copied file can be deleted, but not # the created directory, as that would delete an external file! global_created_files.cleanup(module='my_module') assert not target.exists() assert target.parent.is_dir() # And the directory is still tracked, even after the cleanup assert target.parent in CreatedFiles() # Now we delet this external file, # such that the directory can be cleaned up. external_file.unlink() global_created_files.cleanup(module='my_module') assert not target.parent.is_dir()
def test_cleanup_of_recursive_directories(tmpdir): """Recursively created directories should be cleaned up.""" tmpdir = Path(tmpdir) # Three recursive directories are created a = tmpdir / 'a' b = a / 'b' c = b / 'c' c.mkdir(parents=True) # And these creations are persisted created_files = CreatedFiles().wrapper_for(module='my_module') for directory in (c, a, b): created_files.insert_creation( content=None, target=directory, method=CreationMethod.MKDIR, ) # And a file is copied over into the deepest of these directories content = tmpdir / 'content.tmp' target = c / 'target.tmp' content.touch() target.touch() created_files.insert_creation( content=content, target=target, method=CreationMethod.COPY, ) # All these directories should be cleaned up CreatedFiles().cleanup(module='my_module') for directory in (a, b, c): assert not directory.exists() # Also the copied file assert not target.exists()