<s> import hashlib <EOL> import os <EOL> import mock <EOL> import pytest <EOL> import jinja2 <EOL> from jac . compat import u , open , utf8_encode <EOL> class TestCompression : <EOL> @ pytest . fixture <EOL> def env ( self , tmpdir ) : <EOL> from jac import CompressorExtension <EOL> env = jinja2 . Environment ( extensions = [ CompressorExtension ] ) <EOL> env . compressor_output_dir = tmpdir <EOL> env . compressor_static_prefix = '<STR_LIT>' <EOL> return env <EOL> @ pytest . fixture <EOL> def html_css ( self ) : <EOL> return '''<STR_LIT>''' <EOL> @ pytest . fixture <EOL> def html_js ( self ) : <EOL> return '''<STR_LIT>''' <EOL> @ pytest . fixture <EOL> def html_template ( self ) : <EOL> return '<STR_LIT>' + self . html_css ( ) + '<STR_LIT>' <EOL> def test_render ( self , env , html_template ) : <EOL> template = env . from_string ( html_template ) <EOL> expected = '<STR_LIT>' <EOL> assert expected == template . render ( ) <EOL> def test_compile ( self , tmpdir , html_css ) : <EOL> from jac import CompressorExtension <EOL> ext = CompressorExtension ( mock . Mock ( compressor_output_dir = tmpdir , compressor_static_prefix = '<STR_LIT>' , compressor_source_dirs = [ ] ) ) <EOL> assert ext . _compress_block ( '<STR_LIT>' , mock . Mock ( return_value = html_css ) ) == '<STR_LIT>' <EOL> def test_compile_js ( self , tmpdir , html_js ) : <EOL> from jac import CompressorExtension <EOL> ext = CompressorExtension ( mock . Mock ( compressor_output_dir = tmpdir , compressor_static_prefix = '<STR_LIT>' , compressor_source_dirs = [ ] ) ) <EOL> assert ext . _compress_block ( '<STR_LIT>' , mock . Mock ( return_value = html_js ) ) == '<STR_LIT>' <EOL> def test_compile_file ( self , tmpdir ) : <EOL> from jac import CompressorExtension <EOL> ext = CompressorExtension ( mock . Mock ( compressor_output_dir = tmpdir , compressor_static_prefix = '<STR_LIT>' , compressor_source_dirs = [ str ( tmpdir ) ] ) ) <EOL> static_file = os . path . join ( str ( tmpdir ) , '<STR_LIT>' ) <EOL> with open ( static_file , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> f . write ( '''<STR_LIT>''' ) <EOL> html = '<STR_LIT>' <EOL> expected_hash = hashlib . md5 ( utf8_encode ( html ) ) <EOL> with open ( static_file ) as f : <EOL> expected_hash . update ( utf8_encode ( f . read ( ) ) ) <EOL> assert ext . _compress_block ( '<STR_LIT>' , mock . Mock ( return_value = html ) ) == '<STR_LIT>' . format ( expected_hash . hexdigest ( ) ) <EOL> def test_offline_compress ( self , env ) : <EOL> from jac import Compressor <EOL> tmpdir = str ( env . compressor_output_dir ) <EOL> env . compressor_offline_compress = True <EOL> env . compressor_source_dirs = [ os . path . join ( tmpdir , '<STR_LIT>' ) ] <EOL> env . compressor_output_dir = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> compressor = Compressor ( environment = env ) <EOL> css = '<STR_LIT>' <EOL> os . makedirs ( os . path . join ( tmpdir , '<STR_LIT>' ) ) <EOL> with open ( os . path . join ( tmpdir , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:w>' ) as fh : <EOL> fh . write ( '<STR_LIT>' + css + '<STR_LIT>' ) <EOL> os . makedirs ( os . path . join ( tmpdir , '<STR_LIT>' ) ) <EOL> with open ( os . path . join ( tmpdir , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT:w>' ) as fh : <EOL> fh . write ( '<STR_LIT>' ) <EOL> compressor . offline_compress ( env , [ os . path . join ( tmpdir , '<STR_LIT>' ) ] ) <EOL> assert os . path . exists ( env . compressor_output_dir ) == True </s>
