示例#1
0
def test_that_cleanup_method_removes_files(tmpdir, create_temp_files):
    """The cleanup method should remove all files created by module."""
    # Create some files which are made by modules
    (
        content1,
        content2,
        content3,
        content4,
        compilation1,
        compilation2,
        compilation4,
    ) = create_temp_files(7)
    symlink3 = Path(tmpdir, 'symlink3.tmp')
    symlink3.symlink_to(content3)

    # Tell CreatedFiles object that these files have been created by two
    # different modules
    created_files = CreatedFiles()
    created_files.insert(
        module='name',
        creation_method=CreationMethod.COMPILE,
        contents=[content1, content2],
        targets=[compilation1, compilation2],
    )
    created_files.insert(
        module='name',
        creation_method=CreationMethod.SYMLINK,
        contents=[content3],
        targets=[symlink3],
    )
    created_files.insert(
        module='other_module',
        creation_method=CreationMethod.COMPILE,
        contents=[content4],
        targets=[compilation4],
    )

    # Cleanup files only made by name
    created_files.cleanup(module='name')

    # Content files should be left alone
    for content in (content1, content2, content3, content4):
        assert content.exists()

    # But target files should be removed
    for cleaned_file in (compilation1, compilation2, symlink3):
        assert not cleaned_file.exists()

    # The other module should be left alone
    assert compilation4.exists()

    # No files should be persisted as created by the module afterwards
    assert created_files.by(module='name') == []

    # This should be the case between object lifetimes too
    del created_files
    created_files = CreatedFiles()
    assert created_files.by(module='name') == []
示例#2
0
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()
示例#3
0
def test_that_created_files_are_properly_persisted(create_temp_files):
    """Inserted files should be persisted properly."""
    created_files = CreatedFiles()
    # We should be able to query created files even when none have been created
    assert created_files.by(module='name') == []

    a, b, c, d = create_temp_files(4)
    # Let us start by inserting one file and see if it is returned
    created_files.insert(
        module='name',
        creation_method=CreationMethod.COPY,
        contents=[a],
        targets=[b],
    )
    assert created_files.by(module='name') == [b]

    # Insertion should be idempotent
    created_files.insert(
        module='name',
        creation_method=CreationMethod.COPY,
        contents=[a],
        targets=[b],
    )
    assert created_files.by(module='name') == [b]

    # Now insert new files
    created_files.insert(
        module='name',
        creation_method=CreationMethod.COPY,
        contents=[c],
        targets=[d],
    )
    assert created_files.by(module='name') == [b, d]

    # The content should be persisted across object lifetimes
    del created_files
    created_files = CreatedFiles()
    assert created_files.by(module='name') == [b, d]
示例#4
0
def test_that_inserting_non_existent_file_is_skipped(create_temp_files, ):
    """When creations have been deleted they should be skipped."""
    content, target = create_temp_files(2)
    target.unlink()

    created_files = CreatedFiles()
    created_files.insert(
        module='name',
        creation_method=CreationMethod.COPY,
        contents=[content],
        targets=[target],
    )

    # No file has been created!
    assert created_files.by(module='name') == []
示例#5
0
def test_mkdir_method_of_created_files(tmpdir, with_wrapper):
    """CreatedFiles should be able to create and persist directories."""
    tmpdir = Path(tmpdir)

    # Directory "a" already exists as a directory
    a = tmpdir / 'a'
    a.mkdir(parents=True)

    # But directory b and c are supposed to be created
    b = a / 'b'
    c = b / 'c'

    # We now want to create directory c and all parents
    created_files = CreatedFiles()

    if with_wrapper:
        created_files.wrapper_for(module='my_module').mkdir(path=c)
    else:
        created_files.mkdir(module='my_module', path=c)

    # The directories have been created
    assert b.is_dir()
    assert c.is_dir()

    # And persisted
    assert len(created_files.by('my_module')) == 2
    assert b in created_files
    assert c in created_files

    # But a should not be counted as a created directory
    assert a not in created_files

    # Cleanup should only clean b and c, but not a
    created_files.cleanup(module='my_module')
    assert not b.is_dir()
    assert not c.is_dir()
    assert a.is_dir()
示例#6
0
def test_that_dry_run_is_respected(create_temp_files, caplog):
    """When dry_run is True, no files should be deleted."""
    content, target = create_temp_files(2)

    created_files = CreatedFiles()
    created_files.insert(
        module='name',
        creation_method=CreationMethod.COPY,
        contents=[content],
        targets=[target],
    )

    caplog.clear()
    created_files.cleanup(module='name', dry_run=True)

    # None of the files should be affected
    assert content.exists()
    assert target.exists()

    # Skipping the deletion should be explicitly logged
    assert 'SKIPPED: ' in caplog.record_tuples[0][2]

    # And the files should still be considered created
    assert created_files.by(module='name') == [target]
示例#7
0
def test_that_created_files_are_persisted(method, create_temp_files):
    """When modules create files, they should be persisted."""
    (
        template1,
        template2,
        template3,
        target1,
        target2,
        target3,
    ) = create_temp_files(6)

    # Delete targets to prevent backups from being restored
    for target in (target1, target2, target3):
        target.unlink()

    modules = {
        'A': {
            method: [
                {
                    'content': str(template1),
                    'target': str(target1),
                },
                {
                    'content': str(template2),
                    'target': str(target2),
                },
            ],
        },
        'B': {
            method: {
                'content': str(template3),
                'target': str(target3),
            },
        },
    }
    module_manager = ModuleManager(modules=modules)
    module_manager.finish_tasks()

    created_files = CreatedFiles()
    assert created_files.by(module='A') == [target1, target2]
    assert created_files.by(module='B') == [target3]

    # Now we should be able to cleanup the created files
    assert target1.exists()
    assert target2.exists()
    assert target3.exists()

    # First let's see if dry run is respected
    created_files.cleanup(module='A', dry_run=True)
    assert target1.exists()
    assert target2.exists()
    assert target3.exists()
    assert created_files.by(module='A') == [target1, target2]
    assert created_files.by(module='B') == [target3]

    # Now see if we can cleanup module A and let B stay intact
    created_files.cleanup(module='A')
    assert not target1.exists()
    assert not target2.exists()
    assert target3.exists()
    assert created_files.by(module='A') == []
    assert created_files.by(module='B') == [target3]

    # Now all files should be cleaned
    created_files.cleanup(module='B')
    assert not target3.exists()
    assert created_files.by(module='A') == []
    assert created_files.by(module='B') == []

    # Let's see if it has been properly persisted too
    del created_files
    created_files = CreatedFiles()
    assert created_files.by(module='A') == []
    assert created_files.by(module='B') == []