<s> import os <EOL> import squadron <EOL> from squadron . exthandlers . makegit import ext_git <EOL> from quik import FileLoader <EOL> import pytest <EOL> import git <EOL> import mock <EOL> import json <EOL> def get_loader ( ) : <EOL> return FileLoader ( os . getcwd ( ) ) <EOL> def get_git_file ( url , refspec = None , sshkey = None , args = None ) : <EOL> obj = { '<STR_LIT:url>' : url } <EOL> if refspec : <EOL> obj [ '<STR_LIT>' ] = refspec <EOL> if sshkey : <EOL> obj [ '<STR_LIT>' ] = sshkey <EOL> if args : <EOL> obj [ '<STR_LIT:args>' ] = args <EOL> return json . dumps ( obj ) <EOL> @ pytest . mark . timeout ( <NUM_LIT:10> ) <EOL> def test_basic ( tmpdir ) : <EOL> tmpdir = str ( tmpdir ) <EOL> abs_source = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> with open ( abs_source , '<STR_LIT:w>' ) as gfile : <EOL> gfile . write ( get_git_file ( '<STR_LIT>' ) ) <EOL> dest = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> finalfile = ext_git ( abs_source , dest , { } , get_loader ( ) , { } ) <EOL> assert os . path . exists ( finalfile ) <EOL> assert os . path . exists ( os . path . join ( finalfile , '<STR_LIT>' ) ) <EOL> assert os . path . exists ( os . path . join ( finalfile , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> assert os . path . exists ( os . path . join ( finalfile , '<STR_LIT>' ) ) <EOL> @ pytest . mark . timeout ( <NUM_LIT:10> ) <EOL> def test_refspec ( tmpdir ) : <EOL> tmpdir = str ( tmpdir ) <EOL> abs_source = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> with open ( abs_source , '<STR_LIT:w>' ) as gfile : <EOL> gfile . write ( get_git_file ( '<STR_LIT>' , <EOL> '<STR_LIT>' , None , '<STR_LIT>' ) ) <EOL> dest = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> finalfile = ext_git ( abs_source , dest , { '<STR_LIT:version>' : '<STR_LIT>' } , get_loader ( ) , { } ) <EOL> assert os . path . exists ( finalfile ) <EOL> assert os . path . exists ( os . path . join ( finalfile , '<STR_LIT>' ) ) <EOL> with open ( os . path . join ( finalfile , '<STR_LIT>' , '<STR_LIT>' ) ) as cfile : <EOL> assert '<STR_LIT>' in cfile . read ( ) <EOL> install_file = os . path . join ( finalfile , '<STR_LIT>' ) <EOL> assert os . path . exists ( install_file ) <EOL> with open ( install_file ) as ifile : <EOL> assert ifile . read ( ) . strip ( ) == '<STR_LIT>' <EOL> @ pytest . mark . timeout ( <NUM_LIT:10> ) <EOL> @ pytest . mark . parametrize ( "<STR_LIT>" , [ None , "<STR_LIT>" ] ) <EOL> def test_sshkey ( tmpdir , ssh_command ) : <EOL> tmpdir = str ( tmpdir ) <EOL> test_path = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> with open ( os . path . join ( test_path , '<STR_LIT>' ) ) as k : <EOL> private_key = k . read ( ) <EOL> abs_source = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> dest = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> with mock . patch ( '<STR_LIT>' ) as submock : <EOL> with mock . patch ( '<STR_LIT>' ) as gitmock : <EOL> def check_environ ( * args ) : <EOL> if ssh_command : <EOL> with open ( os . environ [ '<STR_LIT>' ] ) as gitfile : <EOL> assert ssh_command in gitfile . read ( ) <EOL> submock . check_call . side_effect = check_environ <EOL> url = '<STR_LIT>' <EOL> version = '<STR_LIT>' <EOL> with open ( abs_source , '<STR_LIT:w>' ) as gfile : <EOL> gfile . write ( get_git_file ( url , '<STR_LIT>' , '<STR_LIT:filename>' , '<STR_LIT>' ) ) <EOL> dest = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> if ssh_command : <EOL> os . environ [ '<STR_LIT>' ] = ssh_command <EOL> finalfile = ext_git ( abs_source , dest , { '<STR_LIT:version>' : version } , get_loader ( ) , <EOL> { '<STR_LIT:filename>' : lambda : private_key } ) <EOL> expected_sub_calls = [ mock . call ( '<STR_LIT>' . format ( url , finalfile ) . split ( ) ) ] <EOL> expected_git_calls = [ mock . call ( finalfile ) , mock . call ( ) . git . checkout ( version ) ] <EOL> assert expected_sub_calls == submock . mock_calls <EOL> assert expected_git_calls == gitmock . mock_calls <EOL> if ssh_command : <EOL> assert '<STR_LIT>' in os . environ <EOL> assert os . environ [ '<STR_LIT>' ] == ssh_command <EOL> else : <EOL> assert '<STR_LIT>' not in os . environ </s>
