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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
def test_backup_file_does_nothing_without_extension(): backup = diet.backup_file('madeup_file.txt', '') assert backup is None