<s> from __future__ import print_function <EOL> from squadron import template <EOL> from squadron . template import FileConfig , get_config , apply_config <EOL> from squadron . exthandlers . extutils import get_filename <EOL> from squadron . log import log , setup_log <EOL> from tempfile import mkdtemp <EOL> from shutil import rmtree <EOL> import pytest <EOL> from helper import are_dir_trees_equal , get_test_path <EOL> import os <EOL> import stat <EOL> setup_log ( '<STR_LIT>' , console = True ) <EOL> test_path = os . path . join ( get_test_path ( ) , '<STR_LIT>' ) <EOL> def test_template_basic ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> test . render ( dirname , { '<STR_LIT:name>' : '<STR_LIT:user>' , '<STR_LIT>' : '<STR_LIT>' } , { } ) <EOL> assert are_dir_trees_equal ( dirname , os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> def test_template_chown_problem ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> with pytest . raises ( OSError ) as ex : <EOL> test . render ( dirname , { '<STR_LIT:name>' : '<STR_LIT:user>' } , { } , False ) <EOL> test . render ( dirname , { '<STR_LIT:name>' : '<STR_LIT:user>' } , { } , True ) <EOL> assert are_dir_trees_equal ( dirname , os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> def test_template_with_config ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> result = test . render ( dirname , { '<STR_LIT:name>' : '<STR_LIT:user>' } , { } ) <EOL> assert are_dir_trees_equal ( dirname , os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> assert len ( result ) == <NUM_LIT:3> <EOL> assert result [ '<STR_LIT>' ] == True <EOL> assert result [ '<STR_LIT>' ] == False <EOL> assert result [ '<STR_LIT>' ] == True <EOL> st_file = os . stat ( os . path . join ( dirname , '<STR_LIT:file>' ) ) <EOL> assert stat . S_IMODE ( st_file . st_mode ) == <NUM_LIT:0> <NUM_LIT> <EOL> st_test3 = os . stat ( os . path . join ( dirname , '<STR_LIT>' ) ) <EOL> assert stat . S_IMODE ( st_test3 . st_mode ) == <NUM_LIT:0> <NUM_LIT> <EOL> st_test3_file = os . stat ( os . path . join ( dirname , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> assert stat . S_IMODE ( st_test3_file . st_mode ) != <NUM_LIT:0> <NUM_LIT> <EOL> def test_template_with_config_dir_error ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> with pytest . raises ( ValueError ) as ex : <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> result = test . render ( dirname , { '<STR_LIT:name>' : '<STR_LIT:user>' } , { } ) <EOL> assert ex is not None <EOL> def test_extensions ( ) : <EOL> assert template . get_sq_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_sq_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_sq_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_sq_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_sq_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_sq_ext ( '<STR_LIT:filename>' ) == '<STR_LIT>' <EOL> assert get_filename ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert get_filename ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert get_filename ( '<STR_LIT>' ) == '<STR_LIT:filename>' <EOL> assert get_filename ( '<STR_LIT>' ) == '<STR_LIT:filename>' <EOL> assert get_filename ( '<STR_LIT>' ) == '<STR_LIT:filename>' <EOL> assert get_filename ( '<STR_LIT:filename>' ) == '<STR_LIT:filename>' <EOL> assert template . get_file_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_file_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_file_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_file_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_file_ext ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert template . get_file_ext ( '<STR_LIT:filename>' ) == '<STR_LIT>' <EOL> with pytest . raises ( ValueError ) as ex : <EOL> get_filename ( '<STR_LIT>' ) <EOL> assert ex is not None <EOL> def test_autotest ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> test . render ( dirname , { '<STR_LIT:name>' : '<STR_LIT:user>' } , { } ) <EOL> assert are_dir_trees_equal ( dirname , os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> def test_autotest_fail ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> with pytest . raises ( ValueError ) as ex : <EOL> test . render ( dirname , { '<STR_LIT:name>' : '<STR_LIT:user>' } , { } ) <EOL> assert ex is not None <EOL> def test_parse_config ( tmpdir ) : <EOL> conf_file = os . path . join ( str ( tmpdir ) , '<STR_LIT>' ) <EOL> with open ( conf_file , '<STR_LIT:w>' ) as wfile : <EOL> print ( '<STR_LIT>' , file = wfile ) <EOL> print ( '<STR_LIT>' , file = wfile ) <EOL> print ( '<STR_LIT>' , file = wfile ) <EOL> print ( '<STR_LIT>' , file = wfile ) <EOL> cfg = { '<STR_LIT>' : True , '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> render = template . DirectoryRender ( test_path ) <EOL> result = render . parse_config_sq ( conf_file , cfg ) <EOL> assert len ( result ) == <NUM_LIT:2> <EOL> assert result [ <NUM_LIT:0> ] . filepath == '<STR_LIT>' <EOL> assert result [ <NUM_LIT:0> ] . atomic == True <EOL> assert result [ <NUM_LIT:0> ] . user == None <EOL> assert result [ <NUM_LIT:0> ] . group == None <EOL> assert result [ <NUM_LIT:0> ] . mode == None <EOL> assert result [ <NUM_LIT:1> ] . filepath == '<STR_LIT>' <EOL> assert result [ <NUM_LIT:1> ] . atomic == False <EOL> assert result [ <NUM_LIT:1> ] . user == '<STR_LIT>' <EOL> assert result [ <NUM_LIT:1> ] . group == '<STR_LIT>' <EOL> assert result [ <NUM_LIT:1> ] . mode == '<STR_LIT>' <EOL> def test_parse_config_error ( tmpdir ) : <EOL> conf_file = os . path . join ( str ( tmpdir ) , '<STR_LIT>' ) <EOL> with open ( conf_file , '<STR_LIT:w>' ) as wfile : <EOL> print ( '<STR_LIT>' , file = wfile ) <EOL> render = template . DirectoryRender ( test_path ) <EOL> with pytest . raises ( ValueError ) as ex : <EOL> render . parse_config_sq ( conf_file , { } ) <EOL> assert ex is not None <EOL> conf_file = os . path . join ( str ( tmpdir ) , '<STR_LIT>' ) <EOL> with open ( conf_file , '<STR_LIT:w>' ) as wfile : <EOL> print ( '<STR_LIT>' , file = wfile ) <EOL> with pytest . raises ( ValueError ) as ex : <EOL> render . parse_config_sq ( conf_file , { } ) <EOL> assert ex is not None <EOL> conf_file = os . path . join ( str ( tmpdir ) , '<STR_LIT>' ) <EOL> with open ( conf_file , '<STR_LIT:w>' ) as wfile : <EOL> print ( '<STR_LIT>' , file = wfile ) <EOL> with pytest . raises ( ValueError ) as ex : <EOL> render . parse_config_sq ( conf_file , { } ) <EOL> assert ex is not None <EOL> def test_get_config ( ) : <EOL> config = { '<STR_LIT>' : FileConfig ( '<STR_LIT>' , True , None , None , <NUM_LIT:0> <NUM_LIT> ) , <EOL> '<STR_LIT>' : FileConfig ( '<STR_LIT>' , False , '<STR_LIT:user>' , '<STR_LIT>' , None ) } <EOL> already_configured = set ( ) <EOL> assert get_config ( '<STR_LIT>' , '<STR_LIT>' , config , already_configured ) == [ config [ '<STR_LIT>' ] ] <EOL> assert '<STR_LIT>' in already_configured <EOL> assert get_config ( '<STR_LIT>' , '<STR_LIT>' , config , already_configured ) == [ ] <EOL> assert '<STR_LIT>' in already_configured <EOL> assert get_config ( '<STR_LIT>' , '<STR_LIT>' , config , set ( ) ) == [ config [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] ] <EOL> assert get_config ( '<STR_LIT>' , '<STR_LIT>' , config , set ( ) ) == [ config [ '<STR_LIT>' ] ] <EOL> assert get_config ( '<STR_LIT>' , '<STR_LIT>' , config , set ( ) ) == [ ] <EOL> def test_apply_config ( tmpdir ) : <EOL> tmpdir = str ( tmpdir ) <EOL> filepath = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> with open ( filepath , '<STR_LIT:w>' ) as cfile : <EOL> cfile . write ( '<STR_LIT:test>' ) <EOL> apply_config ( tmpdir , [ FileConfig ( filepath , False , None , None , '<STR_LIT>' ) ] , False ) <EOL> st = os . stat ( filepath ) <EOL> assert stat . S_IMODE ( st . st_mode ) == <NUM_LIT:0> <NUM_LIT> <EOL> filepath = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> with open ( filepath , '<STR_LIT:w>' ) as cfile : <EOL> cfile . write ( '<STR_LIT>' ) <EOL> apply_config ( tmpdir , [ FileConfig ( filepath , False , None , None , '<STR_LIT>' ) ] , True ) <EOL> st = os . stat ( filepath ) <EOL> assert stat . S_IMODE ( st . st_mode ) == <NUM_LIT:0> <NUM_LIT> <EOL> def test_git_repo ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> test . render ( dirname , { } , { } ) <EOL> assert are_dir_trees_equal ( dirname , os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> def test_git_repo_chmod ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> test . render ( dirname , { } , { } ) <EOL> result_dir = os . path . join ( test_path , '<STR_LIT>' ) <EOL> assert are_dir_trees_equal ( dirname , result_dir ) <EOL> st = os . stat ( os . path . join ( dirname , '<STR_LIT:test>' , '<STR_LIT>' ) ) <EOL> assert stat . S_IMODE ( st . st_mode ) == <NUM_LIT:0> <NUM_LIT> <EOL> def test_ext_created_dir ( tmpdir ) : <EOL> dirname = str ( tmpdir ) <EOL> test = template . DirectoryRender ( os . path . join ( test_path , '<STR_LIT>' ) ) <EOL> atomic = test . render ( dirname , { } , { } ) <EOL> assert '<STR_LIT>' in atomic <EOL> assert atomic [ '<STR_LIT>' ] == True <EOL> assert '<STR_LIT>' in atomic <EOL> assert atomic [ '<STR_LIT>' ] == True <EOL> result = os . path . join ( dirname , '<STR_LIT>' ) <EOL> assert os . path . isdir ( result ) == True <EOL> assert len ( os . listdir ( result ) ) == <NUM_LIT:0> <EOL> result = os . path . join ( dirname , '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert os . path . isdir ( result ) == True <EOL> assert len ( os . listdir ( result ) ) == <NUM_LIT:0> </s>
