예제 #1
0
def test_compress_files_success_all_algorithms(fs):
    pytest.importorskip('brotli')
    pytest.importorskip('zopfli')
    with open('/test.txt', 'wb') as file:
        file.write(b'a' * 100)
    instance = Mock()
    instance.settings = {'OUTPUT_PATH': '/'}
    pp.compress_files(instance)
    assert pathlib.Path('/test.txt.br').exists()
    assert pathlib.Path('/test.txt.br').stat().st_size != 0
    assert pathlib.Path('/test.txt.gz').exists()
    assert pathlib.Path('/test.txt.gz').stat().st_size != 0
예제 #2
0
def test_compress_files_do_nothing(fs):
    """If all compressors are disabled, no compressed files should be written."""
    fs.create_file('/test.txt')
    instance = Mock()
    instance.settings = {
        'OUTPUT_PATH': '/',
        'PRECOMPRESS_BROTLI': False,
        'PRECOMPRESS_GZIP': False,
        'PRECOMPRESS_ZOPFLI': False,
    }
    pp.compress_files(instance)
    assert not pathlib.Path('/test.txt.br').exists()
    assert not pathlib.Path('/test.txt.gz').exists()
예제 #3
0
def test_compress_files_file_size_increase(fs):
    with open('/test.txt', 'wb') as file:
        file.write(b'a' * 2)
    instance = Mock()
    instance.settings = {
        'OUTPUT_PATH': '/',
        'PRECOMPRESS_BROTLI': False,
        'PRECOMPRESS_GZIP': True,
        'PRECOMPRESS_ZOPFLI': False,
        'PRECOMPRESS_MIN_SIZE': 1,
    }
    with patch('pelican_postprocess.log', Mock()) as log:
        pp.compress_files(instance)
    log.info.assert_called_once()
    assert not pathlib.Path('/test.txt.gz').exists()
예제 #4
0
def test_compress_files_never_overwrite(fs):
    with open('/test.txt', 'wb') as file:
        file.write(b'a' * 100)
    fs.create_file('/test.txt.gz')
    instance = Mock()
    instance.settings = {
        'OUTPUT_PATH': '/',
        'PRECOMPRESS_BROTLI': False,
        'PRECOMPRESS_GZIP': True,
        'PRECOMPRESS_ZOPFLI': False,
    }
    with patch('pelican_postprocess.log', Mock()) as log:
        pp.compress_files(instance)
    log.info.assert_called_once()
    assert pathlib.Path('/test.txt.gz').exists()
    assert pathlib.Path('/test.txt.gz').stat().st_size == 0
예제 #5
0
def test_compress_files_overwrite_gz(fs):
    with open('/test.txt', 'wb') as file:
        file.write(b'a' * 100)
    with open('/test.txt.gz', 'wb') as file:
        file.write(b'a')
    instance = Mock()
    instance.settings = {
        'OUTPUT_PATH': '/',
        'PRECOMPRESS_OVERWRITE': True,
        'PRECOMPRESS_BROTLI': False,
        'PRECOMPRESS_GZIP': True,
        'PRECOMPRESS_ZOPFLI': False,
    }
    with patch('pelican_postprocess.log', Mock()) as log:
        pp.compress_files(instance)
    log.warning.assert_called_once()
    with pathlib.Path('/test.txt.gz').open('rb') as file:
        assert gzip.decompress(file.read()) == b'a' * 100
예제 #6
0
def test_compress_files_overwrite_erase_existing_file(fs):
    """Ensure existing files are erased if the file size would increase."""
    with open('/test.txt', 'wb') as file:
        file.write(b'a' * 2)
    with open('/test.txt.gz', 'wb') as file:
        file.write(b'a')
    instance = Mock()
    instance.settings = {
        'OUTPUT_PATH': '/',
        'PRECOMPRESS_BROTLI': False,
        'PRECOMPRESS_GZIP': True,
        'PRECOMPRESS_ZOPFLI': False,
        'PRECOMPRESS_OVERWRITE': True,
        'PRECOMPRESS_MIN_SIZE': 1,
    }
    with patch('pelican_postprocess.log', Mock()) as log:
        pp.compress_files(instance)
    log.info.assert_called_once()
    assert not pathlib.Path('/test.txt.gz').exists()
예제 #7
0
def test_compress_files_skip_existing_matching_files(fs):
    with open('/test.txt', 'wb') as file:
        file.write(b'abc' * 1000)
    destination = pathlib.Path('/test.txt.gz')
    with destination.open('wb') as file:
        file.write(gzip.compress(b'abc' * 1000, compresslevel=1))
    destination_size = destination.stat().st_size
    instance = Mock()
    instance.settings = {
        'OUTPUT_PATH': '/',
        'PRECOMPRESS_BROTLI': False,
        'PRECOMPRESS_GZIP': True,
        'PRECOMPRESS_ZOPFLI': False,
        'PRECOMPRESS_OVERWRITE': True,
    }
    with patch('pelican_postprocess.log', Mock()) as log:
        pp.compress_files(instance)
    log.info.assert_called_once()
    with destination.open('rb') as file:
        assert gzip.decompress(file.read()) == b'abc' * 1000
    assert destination.stat().st_size == destination_size
예제 #8
0
def test_compress_files_continue_on_small_files(fs):
    """Verify that small files do not cause an early exit.

    This was incorrect behavior was reported in issue #5.
    """

    with open('/000-too-small.txt', 'wb') as file:
        file.write(b'a')
    with open('/999-must-compress.txt', 'wb') as file:
        file.write(b'a' * 100)
    instance = Mock()
    instance.settings = {
        'OUTPUT_PATH': '/',
        'PRECOMPRESS_BROTLI': False,
        'PRECOMPRESS_GZIP': True,
        'PRECOMPRESS_ZOPFLI': False,
        'PRECOMPRESS_MIN_SIZE': 100,
    }
    with patch('pelican_postprocess.log', Mock()) as log:
        pp.compress_files(instance)
    log.info.assert_called_once()
    assert pathlib.Path('/999-must-compress.txt.gz').exists()