# Path: scripnix/pybin/backup_file.py
# COMMAND_NAME = "backup-file"
# BACKUP_DATE_FORMAT = "%Y%m%d"
# def assemble_dry_run_message(backups):
# def collect_backups(file_paths):
# def execute_backups(backups):
# def main(file, dry_run):
# 
# Path: tests/command.py
# def common_help_option(command_entry, command_name):
#     """ Test help message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = HELP_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
# 
#     version_option_switch("-h")
#     version_option_switch("--help")
# 
# def common_version_option(command_entry, command_name):
#     """ Test version message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = VERSION_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
#         assert match.group(2) == __version__
# 
#     version_option_switch("-V")
#     version_option_switch("--version")


import datetime
import os
import re
import time
import pytest
from click import ClickException
from click.testing import CliRunner
from scripnix.pybin.backup_file import assemble_dry_run_message, Backup, collect_backups, COMMAND_NAME, execute_backups, main
from .command import common_help_option, common_version_option
        monkeyed_backups = [Backup(b.from_path + '.bad', b.to_path, b.is_exec_or_suid) for b in backups]
        monkeyed_backups_copy = monkeyed_backups[:]

        with pytest.raises(ClickException) as excinfo:
            execute_backups(monkeyed_backups)

        assert monkeyed_backups_copy == monkeyed_backups
        message = str(excinfo.value)
        assert len(message.split("\n")) == len(monkeyed_backups)
        assert message.startswith("Unable to copy")

        # Make sure unexpected files weren't somehow created.
        assert len(os.listdir(test_path)) == len(expected)

        for file_name, mode in expected:
            assert os.path.isfile(file_name)
            assert os.stat(file_name).st_mode == mode


@pytest.mark.parametrize('file_name_permissions,expected', [
    ([], []),
    ([("test.tst", 0o0644)], [("test.tst", 0o100644), ("test.tst.20120616", 0o100644)]),
    ([("test.exe", 0o0755)], [("test.exe", 0o100755), ("test.exe.20120616", 0o100644)]),
    ([("test.suid", 0o4644)], [("test.suid", 0o104644), ("test.suid.20120616", 0o100644)]),
    ([("test.suid", 0o4750)], [("test.suid", 0o104750), ("test.suid.20120616", 0o100640)]),
])
def test_execute_backups_success(file_name_permissions, expected):
    with CliRunner().isolated_filesystem():
        backups, test_path = _create_files(file_name_permissions, datetime.datetime(2012, 6, 16, 0, 0).timestamp())
        backups_copy = backups[:]

# Path: scripnix/pybin/backup_file.py
# COMMAND_NAME = "backup-file"
# BACKUP_DATE_FORMAT = "%Y%m%d"
# def assemble_dry_run_message(backups):
# def collect_backups(file_paths):
# def execute_backups(backups):
# def main(file, dry_run):
# 
# Path: tests/command.py
# def common_help_option(command_entry, command_name):
#     """ Test help message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = HELP_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
# 
#     version_option_switch("-h")
#     version_option_switch("--help")
# 
# def common_version_option(command_entry, command_name):
#     """ Test version message for both standard option switches."""
#     def version_option_switch(option_switch):
#         runner = CliRunner()
#         result = runner.invoke(command_entry, [option_switch])
#         assert result.exit_code == 0
#         match = VERSION_OPTION_REGEX.match(result.output.replace("\n", ""))
#         assert match.group(1) == command_name
#         assert match.group(2) == __version__
# 
#     version_option_switch("-V")
#     version_option_switch("--version")


# Path: tests/test_backup_file.py
import datetime
import os
import re
import time
import pytest
from click import ClickException
from click.testing import CliRunner
from scripnix.pybin.backup_file import assemble_dry_run_message, Backup, collect_backups, COMMAND_NAME, execute_backups, main
from .command import common_help_option, common_version_option

        monkeyed_backups = [Backup(b.from_path + '.bad', b.to_path, b.is_exec_or_suid) for b in backups]
        monkeyed_backups_copy = monkeyed_backups[:]

        with pytest.raises(ClickException) as excinfo:
            execute_backups(monkeyed_backups)

        assert monkeyed_backups_copy == monkeyed_backups
        message = str(excinfo.value)
        assert len(message.split("\n")) == len(monkeyed_backups)
        assert message.startswith("Unable to copy")

        # Make sure unexpected files weren't somehow created.
        assert len(os.listdir(test_path)) == len(expected)

        for file_name, mode in expected:
            assert os.path.isfile(file_name)
            assert os.stat(file_name).st_mode == mode


@pytest.mark.parametrize('file_name_permissions,expected', [
    ([], []),
    ([("test.tst", 0o0644)], [("test.tst", 0o100644), ("test.tst.20120616", 0o100644)]),
    ([("test.exe", 0o0755)], [("test.exe", 0o100755), ("test.exe.20120616", 0o100644)]),
    ([("test.suid", 0o4644)], [("test.suid", 0o104644), ("test.suid.20120616", 0o100644)]),
    ([("test.suid", 0o4750)], [("test.suid", 0o104750), ("test.suid.20120616", 0o100640)]),
])
def test_execute_backups_success(file_name_permissions, expected):
    with CliRunner().isolated_filesystem():
        backups, test_path = _create_files(file_name_permissions, datetime.datetime(2012, 6, 16, 0, 0).timestamp())
        backups_copy = backups[:]

        execute_backups(backups)