def test_parse_configuration_raises_on_broken_configuration(config_copy): exception = None del config_copy['commands']['gifsicle'] try: diet.parse_configuration(config_copy) except diet.ConfigurationErrorDietException as e: exception = e assert exception is not None assert exception.msg == "Missing key(s) in configuration: gifsicle."
def test_parse_configuration_correctly_handles_output_file(config_copy): corrected = { 'gifsicle': "-O2 '{file}' > '{output_file}' && mv '{output_file}' '{file}'", 'jpegtran': "-copy none -progressive -optimize -outfile '{output_file}' '{file}' && mv '{output_file}' '{file}'", 'pngcrush': "-rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time '{file}' '{output_file}' && mv '{output_file}' '{file}'" } config = diet.parse_configuration(config_copy) for prog, params in config['parameters'].items(): if '{output_file}' in params: assert params == corrected[prog]
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_parse_configuration_correctly_builds_pipelines(config_copy): config = diet.parse_configuration(config_copy) pipelines = { 'gif': ("/usr/local/bin/gifsicle -O2 '{file}' > '{output_file}' " "&& mv '{output_file}' '{file}'"), 'jpeg': ("/usr/local/bin/jpegtran -copy none -progressive -optimize -outfile " "'{output_file}' '{file}' && mv '{output_file}' '{file}'"), 'png': ("/usr/local/bin/optipng -force -o5 '{file}' " "&& /usr/local/bin/advpng -z4 '{file}s' " "&& /usr/local/bin/pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP " "-rem sRGB -rem time '{file}' '{output_file}' && mv '{output_file}' " "'{file}'") } for img_type, pipeline in config['pipelines'].items(): assert pipeline == pipelines[img_type]
def test_parse_configuration_correctly_builds_pipelines(config_copy): config = diet.parse_configuration(config_copy) pipelines = { 'gif': ( "/usr/local/bin/gifsicle -O2 '{file}' > '{output_file}' " "&& mv '{output_file}' '{file}'"), 'jpeg': ( "/usr/local/bin/jpegtran -copy none -progressive -optimize -outfile " "'{output_file}' '{file}' && mv '{output_file}' '{file}'"), 'png': ( "/usr/local/bin/optipng -force -o5 '{file}' " "&& /usr/local/bin/advpng -z4 '{file}s' " "&& /usr/local/bin/pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP " "-rem sRGB -rem time '{file}' '{output_file}' && mv '{output_file}' " "'{file}'") } for img_type, pipeline in config['pipelines'].items(): assert pipeline == pipelines[img_type]
def squeeze(test_dir, output_dir, configuration, clean): """Simple program that copies test_dir to output_dir, compresseses all images it finds in it and outputs efficiency stats.""" total_time = total_size = total_reduced_size = no_files = 0 raw_config = process.read_yaml_configuration(configuration) config = process.parse_configuration(raw_config) click.echo('Making a copy of test files...') files = copy_dir(test_dir, output_dir) click.echo('Testing...') stats_file = os.path.join(output_dir, 'stats.csv') with open(stats_file, 'wb') as csvfile: csvwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for filename in get_files(files): stats = test_compression(filename, config) if stats: # Skip ignored files csvwriter.writerow(stats) no_files += 1 total_time += stats.time total_size += stats.orig_size total_reduced_size += stats.new_size if clean: clean_files(files) summary = """\ Files: {} Total size: {} Reduced size: {} Reduced by (%): {} Time spent (seconds): {} """ total_reduced_by = calc_effectiveness(total_size, total_reduced_size) click.echo(summary.format(no_files, total_size, total_reduced_size, total_reduced_by, total_time))
def test_parse_configuration_can_be_safely_run_multiple_times(config_copy): config = diet.parse_configuration(config_copy) config2 = diet.parse_configuration(config) assert config == config2
def test_parse_configuration_marks_configuration_as_parsed(config_copy): config = diet.parse_configuration(config_copy) assert config['parsed']
def test_parse_configuration_adds_missing_required_sections(): config = diet.parse_configuration({}) for section in ('commands', 'parameters', 'pipelines'): assert section in config