def test_symlink_file_fail_with_existing_destination_file(tmpdir):
    # Setup
    original_file = tmpdir.join("origin.somefile")
    destination_file = tmpdir.join("destination.somefile")
    destination_file.write("some content")

    with pytest.raises(RuntimeError):
        # System under test
        symlink_file(original_file, destination_file)
def test_copy_check_fail_symlink(tmpdir):
    # Setup
    original_file = tmpdir.join("origin.somefile")
    destination_file = tmpdir.join("destination.somefile")
    original_file_content = "some content"
    original_file.write(original_file_content)
    symlink_file(original_file, destination_file)

    # System under test
    is_files_copied = is_copied(original_file, destination_file)

    # Verification
    assert is_files_copied is False
def test_symlink_check_success(tmpdir):
    # Setup
    original_file = tmpdir.join("origin.somefile")
    destination_file = tmpdir.join("destination.somefile")
    original_file_content = "some content"
    original_file.write(original_file_content)
    symlink_file(original_file, destination_file)

    # System under test
    is_files_linked = is_linked(original_file, destination_file)

    # Verification
    assert is_files_linked is True
def test_broken_link_check_fail_normal_symlink(tmpdir):
    # Setup
    original_file = tmpdir.join("origin.somefile")
    destination_file = tmpdir.join("destination.somefile")
    original_file_content = "some content"
    original_file.write(original_file_content)
    symlink_file(original_file, destination_file)

    # System under test
    is_link_broken = is_broken_link(destination_file)

    # Verification
    assert is_link_broken is False
def test_symlink_file_success(tmpdir):
    # Setup
    original_file = tmpdir.join("origin.somefile")
    destination_file = tmpdir.join("destination.somefile")
    original_file_content = "some content"
    original_file.write(original_file_content)

    # System under test
    symlink_file(original_file, destination_file)

    # Verification
    assert os.path.islink(destination_file)
    assert os.path.realpath(destination_file) == original_file
def test_unsymlink_file_success(tmpdir):
    # Setup
    original_file = tmpdir.join("origin.somefile")
    destination_file = tmpdir.join("destination.somefile")
    original_file_content = "some content"
    original_file.write(original_file_content)
    symlink_file(original_file, destination_file)

    # Verification of setup
    assert os.path.islink(destination_file)
    assert os.path.realpath(destination_file) == original_file

    # System under test
    unsymlink_file(destination_file)

    # Verification
    assert not os.path.islink(destination_file)
    assert os.path.isfile(
        original_file
    )  # Makes sure the original file wasn't removed in the unlink
    def do(self):
        logger.debug(
            f"File Action: Starting action [action={self.action}, origin={self.origin}, destination={self.destination}, use_sudo={self.run_as_sudo}]"
        )
        if self.action == FileActionType.COPY:
            copy_file(self.origin, self.destination, self.run_as_sudo,
                      self.sudo_password)
        elif self.action == FileActionType.SYMLINK:
            symlink_file(self.origin, self.destination, self.run_as_sudo,
                         self.sudo_password)
        elif self.action == FileActionType.SCRIPT:
            run_file(self.origin, self.run_as_sudo, self.sudo_password)
        else:
            raise NotImplementedError(
                f"File Action: The action `{self.action}` is not supported yet (feel free to open a ticket on github!)"
            )

        logger.debug(
            f"File Action: Successful action [action={self.action}, origin={self.origin}, destination={self.destination}, use_sudo={self.run_as_sudo}]"
        )
def test_symlink_file_fail_none():
    with pytest.raises(RuntimeError):
        # System under test
        symlink_file(None, None)