<s> import os <EOL> import re <EOL> import sys <EOL> import tempfile <EOL> import py <EOL> import pytest <EOL> import mock <EOL> from qisys import ui <EOL> import qisys . error <EOL> import qisys . sh <EOL> import qisys . script <EOL> import qisys . interact <EOL> import qisys . worktree <EOL> class TestNamespace ( object ) : <EOL> """<STR_LIT>""" <EOL> def __getattr__ ( self , name ) : <EOL> if name . startswith ( "<STR_LIT:_>" ) : <EOL> raise AttributeError ( ) <EOL> setattr ( self , name , None ) <EOL> return None <EOL> class TestWorkTree ( qisys . worktree . WorkTree ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , root = None ) : <EOL> if root is None : <EOL> self . root = os . getcwd ( ) <EOL> else : <EOL> self . root = root <EOL> super ( TestWorkTree , self ) . __init__ ( self . root ) <EOL> @ property <EOL> def tmpdir ( self ) : <EOL> return py . path . local ( self . root ) <EOL> def create_project ( self , src ) : <EOL> """<STR_LIT>""" <EOL> to_make = os . path . join ( self . root , src ) <EOL> qisys . sh . mkdir ( to_make , recursive = True ) <EOL> new_project = super ( TestWorkTree , self ) . add_project ( src ) <EOL> qiproject_xml = self . tmpdir . join ( src , "<STR_LIT>" ) <EOL> qiproject_xml . write ( """<STR_LIT>""" ) <EOL> return new_project <EOL> skip_on_win = pytest . mark . skipif ( os . name == '<STR_LIT>' , reason = "<STR_LIT>" ) <EOL> only_linux = pytest . mark . skipif ( not sys . platform . startswith ( "<STR_LIT>" ) , <EOL> reason = "<STR_LIT>" ) <EOL> @ pytest . fixture <EOL> def worktree ( cd_to_tmpdir ) : <EOL> """<STR_LIT>""" <EOL> wt = TestWorkTree ( ) <EOL> return wt <EOL> @ pytest . fixture <EOL> def interact ( request ) : <EOL> """<STR_LIT>""" <EOL> from qisys . test . fake_interact import FakeInteract <EOL> fake_interact = FakeInteract ( ) <EOL> patcher = mock . patch ( '<STR_LIT>' , fake_interact ) <EOL> request . addfinalizer ( patcher . stop ) <EOL> return patcher . start ( ) <EOL> @ pytest . fixture <EOL> def args ( request ) : <EOL> """<STR_LIT>""" <EOL> return TestNamespace ( ) <EOL> @ pytest . fixture <EOL> def record_messages ( request ) : <EOL> """<STR_LIT>""" <EOL> recorder = MessageRecorder ( ) <EOL> request . addfinalizer ( recorder . stop ) <EOL> return recorder <EOL> class MessageRecorder ( ) : <EOL> def __init__ ( self ) : <EOL> ui . CONFIG [ "<STR_LIT>" ] = True <EOL> ui . _MESSAGES = list ( ) <EOL> def stop ( self ) : <EOL> ui . CONFIG [ "<STR_LIT>" ] = False <EOL> ui . _MESSAGES = list ( ) <EOL> def reset ( self ) : <EOL> ui . _MESSAGES = list ( ) <EOL> def find ( self , pattern ) : <EOL> regexp = re . compile ( pattern ) <EOL> for message in ui . _MESSAGES : <EOL> if re . search ( regexp , message ) : <EOL> return message <EOL> @ pytest . fixture ( autouse = True ) <EOL> def tmpfiles ( request , tmpdir ) : <EOL> """<STR_LIT>""" <EOL> tmpdir = tmpdir . realpath ( ) <EOL> def fake_get_path ( * args ) : <EOL> prefix = args [ <NUM_LIT:0> ] <EOL> rest = args [ <NUM_LIT:1> : ] <EOL> full_path = os . path . join ( tmpdir . strpath , <EOL> os . path . basename ( prefix ) , <EOL> * rest ) <EOL> to_make = os . path . dirname ( full_path ) <EOL> qisys . sh . mkdir ( to_make , recursive = True ) <EOL> return full_path <EOL> patcher = mock . patch ( "<STR_LIT>" , fake_get_path ) <EOL> patcher . start ( ) <EOL> request . addfinalizer ( patcher . stop ) <EOL> @ pytest . fixture <EOL> def cd_to_tmpdir ( monkeypatch , tmpdir ) : <EOL> workdir = tmpdir . mkdir ( "<STR_LIT>" ) <EOL> monkeypatch . chdir ( workdir ) <EOL> return workdir <EOL> class TestAction ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , package ) : <EOL> self . package = package <EOL> self . worktree = TestWorkTree ( ) <EOL> def chdir ( self , directory ) : <EOL> try : <EOL> directory = directory . strpath <EOL> except AttributeError : <EOL> pass <EOL> os . chdir ( directory ) <EOL> def __call__ ( self , action , * args , ** kwargs ) : <EOL> module_name = "<STR_LIT>" % ( self . package , action . replace ( "<STR_LIT:->" , "<STR_LIT:_>" ) ) <EOL> cwd = kwargs . get ( "<STR_LIT>" ) <EOL> if cwd : <EOL> self . chdir ( cwd ) <EOL> if kwargs . get ( "<STR_LIT>" ) : <EOL> with pytest . raises ( qisys . error . Error ) as error : <EOL> qisys . script . run_action ( module_name , args ) <EOL> return str ( error . value ) <EOL> if kwargs . get ( "<STR_LIT>" ) : <EOL> try : <EOL> qisys . script . run_action ( module_name , args ) <EOL> except SystemExit as e : <EOL> return e . code <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return qisys . script . run_action ( module_name , args ) <EOL> class QiSysAction ( TestAction ) : <EOL> def __init__ ( self ) : <EOL> super ( QiSysAction , self ) . __init__ ( "<STR_LIT>" ) <EOL> @ pytest . fixture <EOL> def qisys_action ( cd_to_tmpdir ) : <EOL> return QiSysAction ( ) </s>
