Пример #1
0
def test_backup_file_raises_exception_if_different_backup_exists():
    filename = join(TEST_FILES_DIR, 'config.yaml')
    backup_filename = ".".join([filename, 'back'])
    shutil.copy(join(TEST_FILES_DIR,  'nature.gif'), backup_filename)

    try:
        diet.backup_file(filename, 'back')
    except diet.DietException:
        pass

    os.remove(backup_filename)
Пример #2
0
def test_backup_file_raises_exception_if_different_backup_exists():
    filename = join(TEST_FILES_DIR, 'config.yaml')
    backup_filename = ".".join([filename, 'back'])
    shutil.copy(join(TEST_FILES_DIR, 'nature.gif'), backup_filename)

    try:
        diet.backup_file(filename, 'back')
    except diet.DietException:
        pass

    os.remove(backup_filename)
Пример #3
0
def test_backup_file_does_not_complain_if_backup_already_exists():
    filename = join(TEST_FILES_DIR, 'config.yaml')
    backup_filename = ".".join([filename, 'back'])

    result = diet.backup_file(filename, 'back')
    assert result == backup_filename

    result2 = diet.backup_file(filename, 'back')
    assert result2 == backup_filename
    assert exists(backup_filename)

    os.remove(backup_filename)
Пример #4
0
def test_backup_file_does_not_complain_if_backup_already_exists():
    filename = join(TEST_FILES_DIR, 'config.yaml')
    backup_filename = ".".join([filename, 'back'])

    result = diet.backup_file(filename, 'back')
    assert result == backup_filename

    result2 = diet.backup_file(filename, 'back')
    assert result2 == backup_filename
    assert exists(backup_filename)

    os.remove(backup_filename)
Пример #5
0
def test_diet_keeps_processed_file_if_keep_processed_is_enabled(config_copy):
    add_fake_pipeline_to_config(BALLOON_CMD, config_copy)
    config_copy['keep_processed'] = True
    orig_filename = join(TEST_FILES_DIR, 'config.yaml')
    diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])

    old_size = os.stat(filename).st_size

    diet.diet(filename, config_copy)
    assert os.stat(filename).st_size > old_size

    os.remove(filename)
Пример #6
0
def test_diet_keeps_processed_file_if_keep_processed_is_enabled(config_copy):
    add_fake_pipeline_to_config(BALLOON_CMD, config_copy)
    config_copy['keep_processed'] = True
    orig_filename = join(TEST_FILES_DIR, 'config.yaml')
    diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])

    old_size = os.stat(filename).st_size

    diet.diet(filename, config_copy)
    assert os.stat(filename).st_size > old_size

    os.remove(filename)
Пример #7
0
def test_diet_keeps_smaller_file_by_default(config_copy):
    add_fake_pipeline_to_config(BALLOON_CMD, config_copy)
    orig_filename = join(TEST_FILES_DIR, 'config.yaml')
    diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])

    old_size = os.stat(filename).st_size

    changed = diet.diet(filename, config_copy)
    assert os.stat(filename).st_size == old_size
    assert changed

    os.remove(filename)
Пример #8
0
def test_diet_keeps_smaller_file_by_default(config_copy):
    add_fake_pipeline_to_config(BALLOON_CMD, config_copy)
    orig_filename = join(TEST_FILES_DIR, 'config.yaml')
    diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])

    old_size = os.stat(filename).st_size

    changed = diet.diet(filename, config_copy)
    assert os.stat(filename).st_size == old_size
    assert changed

    os.remove(filename)
Пример #9
0
def test_diet_works_with_already_parsed_configuration(config_copy):
    add_fake_pipeline_to_config(BALLOON_CMD, config_copy)
    config = diet.parse_configuration(config_copy)

    assert config['parsed']

    orig_filename = join(TEST_FILES_DIR, 'config.yaml')
    diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])

    old_size = os.stat(filename).st_size

    changed = diet.diet(filename, config)
    assert os.stat(filename).st_size == old_size
    assert changed

    os.remove(filename)
Пример #10
0
def test_diet_works_with_already_parsed_configuration(config_copy):
    add_fake_pipeline_to_config(BALLOON_CMD, config_copy)
    config = diet.parse_configuration(config_copy)

    assert config['parsed']

    orig_filename = join(TEST_FILES_DIR, 'config.yaml')
    diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])

    old_size = os.stat(filename).st_size

    changed = diet.diet(filename, config)
    assert os.stat(filename).st_size == old_size
    assert changed

    os.remove(filename)
Пример #11
0
def test_diet_uses_default_configuration_values():
    filename = join(TEST_FILES_DIR, 'nature.png')
    backup = diet.backup_file(filename, 'back')

    pipeline_config = {'pipelines': {'png': 'optipng'}, 'parsed': True}

    changed = diet.diet(backup, pipeline_config)
    assert changed

    os.remove(backup)
Пример #12
0
def test_squeeze_shrinks_an_image():
    filename = join(TEST_FILES_DIR, 'nature.png')
    backup = diet.backup_file(filename, 'back')
    cmd = "optipng -force -o5 '{file}' "

    assert filecmp.cmp(filename, backup)

    diet.squeeze(cmd, filename, "")
    assert not filecmp.cmp(filename, backup)

    shutil.copyfile(backup, filename)
    os.remove(backup)
Пример #13
0
def test_diet_squeezes_file():
    backup = process.backup_file(TEST_FILE, 'back')

    assert filecmp.cmp(TEST_FILE, backup)

    try:
        runner = CliRunner()
        result = runner.invoke(diet.diet, ['--config', TEST_CONFIG, backup])
        assert not filecmp.cmp(TEST_FILE, backup)
        assert result.exit_code == 0
    finally:
        os.remove(backup)
Пример #14
0
def test_squeeze_shrinks_an_image():
    filename = join(TEST_FILES_DIR, 'nature.png')
    backup = diet.backup_file(filename, 'back')
    cmd = "optipng -force -o5 '{file}' "

    assert filecmp.cmp(filename, backup)

    diet.squeeze(cmd, filename, "")
    assert not filecmp.cmp(filename, backup)

    shutil.copyfile(backup, filename)
    os.remove(backup)
Пример #15
0
def test_backup_file_creates_backup_with_extension_if_it_does_not_exist_yet():
    filename = join(TEST_FILES_DIR, 'config.yaml')
    backup_filename = ".".join([filename, 'back'])

    assert not exists(backup_filename)

    result = diet.backup_file(filename, 'back')

    assert result == backup_filename
    assert exists(backup_filename)

    os.remove(backup_filename)
Пример #16
0
def test_backup_file_creates_backup_with_extension_if_it_does_not_exist_yet():
    filename = join(TEST_FILES_DIR, 'config.yaml')
    backup_filename = ".".join([filename, 'back'])

    assert not exists(backup_filename)

    result = diet.backup_file(filename, 'back')

    assert result == backup_filename
    assert exists(backup_filename)

    os.remove(backup_filename)
Пример #17
0
def test_squeeze_restores_file_if_it_fails():
    orig_filename = join(TEST_FILES_DIR, 'nature.gif')
    backup = diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])
    backup = diet.backup_file(filename, 'back')

    # Screw up file
    with open(filename, 'w') as f:
        f.write(" ")

    assert not filecmp.cmp(filename, backup)

    # This will fail because of missing command AND broken image
    try:
        diet.squeeze('no_command', filename, backup)
    except diet.CompressFileDietException as e:
        assert e.msg == ("Squeezing failed. Likely because "
                         "of missing required utilities.")

    assert filecmp.cmp(filename, backup)

    os.remove(backup)
    os.remove(filename)
Пример #18
0
def test_squeeze_restores_file_if_it_fails():
    orig_filename = join(TEST_FILES_DIR, 'nature.gif')
    backup = diet.backup_file(orig_filename, 'test')
    filename = ".".join([orig_filename, "test"])
    backup = diet.backup_file(filename, 'back')

    # Screw up file
    with open(filename, 'w') as f:
        f.write(" ")

    assert not filecmp.cmp(filename, backup)

    # This will fail because of missing command AND broken image
    try:
        diet.squeeze('no_command', filename, backup)
    except diet.CompressFileDietException as e:
        assert e.msg == ("Squeezing failed. Likely because "
                         "of missing required utilities.")

    assert filecmp.cmp(filename, backup)

    os.remove(backup)
    os.remove(filename)
Пример #19
0
def test_diet_uses_default_configuration_values():
    filename = join(TEST_FILES_DIR, 'nature.png')
    backup = diet.backup_file(filename, 'back')

    pipeline_config = {
        'pipelines': {
            'png': 'optipng'
        },
        'parsed': True
    }

    changed = diet.diet(backup, pipeline_config)
    assert changed

    os.remove(backup)
Пример #20
0
def test_backup_file_does_nothing_without_extension():
    backup = diet.backup_file('madeup_file.txt', '')
    assert backup is None
Пример #21
0
def test_backup_file_does_nothing_without_extension():
    backup = diet.backup_file('madeup_file.txt', '')
    assert backup is None