示例#1
0
def test_missing_filename(capsys):
    with pytest.raises(SystemExit) as excinfo:
        exit_code = optimage.main([])
    assert excinfo.value.code == 2
    _, err = capsys.readouterr()
    py3_error = 'error: the following arguments are required: filename'
    py2_error = 'error: too few arguments'
    assert py3_error in err or py2_error in err
示例#2
0
def test_commanderror(capsys, monkeypatch):
    def mock_check_output(args, stderr=None):
        raise subprocess.CalledProcessError(1, args, 'custom error'.encode('utf-8'))

    monkeypatch.setattr(subprocess, 'check_output', mock_check_output)
    exit_code = optimage.main([os.path.join('test_data', 'valid1.png')])
    assert exit_code == 7
    _, err = capsys.readouterr()
    assert 'Output:\ncustom error' in err
示例#3
0
def test_binary_not_found(capsys, monkeypatch):
    def mock_check_output(args, stderr=None):
        raise optimage.FileNotFoundError()

    monkeypatch.setattr(subprocess, 'check_output', mock_check_output)
    exit_code = optimage.main([os.path.join('test_data', 'valid1.png')])
    assert exit_code == 6
    _, err = capsys.readouterr()
    assert err == 'The executable "pngcrush" was not found. Please install it and re-run this command.\n'
示例#4
0
def test_output_file(filename, capsys, tmpdir):
    original_filename = os.path.join('test_data', filename)
    output_filename = os.path.join(tmpdir.dirname, filename)

    exit_code = optimage.main(['--output', output_filename, original_filename])
    assert exit_code == 0
    out, err = capsys.readouterr()
    assert 'File was losslessly compressed to' in out
    assert err == ''

    original_size = os.path.getsize(original_filename)
    output_size = os.path.getsize(output_filename)

    assert optimage._images_are_equal(original_filename, output_filename)
    assert original_size > output_size
示例#5
0
def test_replace(filename, capsys, tmpdir):
    original_filename = os.path.join('test_data', filename)
    output_filename = os.path.join(tmpdir.dirname, filename)
    shutil.copy(original_filename, output_filename)
    os.chmod(output_filename, 420)  # 420 == 0o644

    exit_code = optimage.main(['--replace', output_filename])
    assert exit_code == 0
    out, err = capsys.readouterr()
    assert 'File was losslessly compressed to' in out
    assert err == ''

    original_size = os.path.getsize(original_filename)
    output_size = os.path.getsize(output_filename)

    assert optimage._images_are_equal(original_filename, output_filename)
    assert original_size > output_size
示例#6
0
def test_check_optimized(filename, capsys):
    exit_code = optimage.main([os.path.join('test_data', filename)])
    assert exit_code == 1
    out, err = capsys.readouterr()
    assert 'File can be losslessly compressed to' in out
    assert err == ''
示例#7
0
def test_optimized_file(filename, capsys):
    exit_code = optimage.main([os.path.join('test_data', filename)])
    assert exit_code == 0
    out, err = capsys.readouterr()
    assert (out, err) == ('', '')
示例#8
0
def test_invalid_extension(filename, capsys):
    exit_code = optimage.main([os.path.join('test_data', filename)])
    assert exit_code == 5
    _, err = capsys.readouterr()
    assert filename in err
    assert 'Please correct the extension\n' in err
示例#9
0
def test_unsupported_extension(capsys):
    exit_code = optimage.main(['test_data/valid1.gif'])
    assert exit_code == 4
    _, err = capsys.readouterr()
    assert err == 'No lossless compressor defined for extension ".gif"\n'
示例#10
0
def test_input_does_not_exist(capsys):
    exit_code = optimage.main(['test_data/nonexistent.png'])
    assert exit_code == 3
    _, err = capsys.readouterr()
    assert err == 'test_data/nonexistent.png is not an image file\n'
示例#11
0
def test_input_is_directory(capsys):
    exit_code = optimage.main(['test_data'])
    assert exit_code == 3
    _, err = capsys.readouterr()
    assert err == 'test_data is not an image file\n'
示例#12
0
def test_debug(filename, compressor, caplog):
    exit_code = optimage.main([os.path.join('test_data', filename), '--debug'])
    assert '{}: best compressor for'.format(compressor) in caplog.text