2 from basetest
import BaseTest
3 import os
, sys
, subprocess
5 from StringIO
import StringIO
7 sys
.path
.insert(0, '..')
9 from zeroinstall
.injector
import policy
, run
, namespaces
10 from zeroinstall
import SafeException
12 mydir
= os
.path
.abspath(os
.path
.dirname(__file__
))
13 local_0launch
= os
.path
.join(os
.path
.dirname(mydir
), '0launch')
14 runnable
= os
.path
.join(mydir
, 'runnable', 'Runnable.xml')
15 runexec
= os
.path
.join(mydir
, 'runnable', 'RunExec.xml')
16 recursive_runner
= os
.path
.join(mydir
, 'runnable', 'RecursiveRunner.xml')
17 command_feed
= os
.path
.join(mydir
, 'Command.xml')
19 class TestRun(BaseTest
):
20 def testRunnable(self
):
21 child
= subprocess
.Popen([local_0launch
, '--', runnable
, 'user-arg'], stdout
= subprocess
.PIPE
)
22 stdout
, _
= child
.communicate()
23 assert 'Runner: script=A test script: args=command-arg -- user-arg' in stdout
, stdout
25 def testCommandBindings(self
):
26 if 'SELF_COMMAND' in os
.environ
:
27 del os
.environ
['SELF_COMMAND']
29 p
= policy
.Policy(command_feed
, config
= self
.config
)
30 self
.config
.handler
.wait_for_blocker(p
.solve_with_downloads())
31 old_stdout
= sys
.stdout
33 sys
.stdout
= StringIO()
34 run
.execute_selections(p
.solver
.selections
, [], main
= 'runnable/go.sh', dry_run
= True, stores
= self
.config
.stores
)
36 sys
.stdout
= old_stdout
37 assert 'local' in os
.environ
['LOCAL'], os
.environ
['LOCAL']
38 assert 'SELF_COMMAND' in os
.environ
40 def testAbsMain(self
):
41 p
= policy
.Policy(command_feed
, config
= self
.config
)
42 self
.config
.handler
.wait_for_blocker(p
.solve_with_downloads())
44 old_stdout
= sys
.stdout
46 sys
.stdout
= StringIO()
47 run
.execute_selections(p
.solver
.selections
, [], main
= '/runnable/runner', dry_run
= True, stores
= self
.config
.stores
)
49 sys
.stdout
= old_stdout
52 old_stdout
= sys
.stdout
54 sys
.stdout
= StringIO()
55 run
.execute_selections(p
.solver
.selections
, [], main
= '/runnable/not-there', dry_run
= True, stores
= self
.config
.stores
)
57 sys
.stdout
= old_stdout
58 except SafeException
as ex
:
59 assert 'not-there' in unicode(ex
)
62 p
= policy
.Policy(runnable
, config
= self
.config
)
63 self
.config
.handler
.wait_for_blocker(p
.solve_with_downloads())
64 old_stdout
= sys
.stdout
66 sys
.stdout
= StringIO()
67 run
.execute_selections(p
.solver
.selections
, [], dry_run
= True, stores
= self
.config
.stores
)
68 out
= sys
.stdout
.getvalue()
70 sys
.stdout
= old_stdout
71 assert 'runner-arg' in out
, out
73 def testWrapper(self
):
74 p
= policy
.Policy(runnable
, config
= self
.config
)
75 self
.config
.handler
.wait_for_blocker(p
.solve_with_downloads())
76 old_stdout
= sys
.stdout
78 sys
.stdout
= StringIO()
79 run
.execute_selections(p
.solver
.selections
, [], wrapper
= 'echo', dry_run
= True, stores
= self
.config
.stores
)
80 out
= sys
.stdout
.getvalue()
82 sys
.stdout
= old_stdout
83 assert '/bin/sh -c echo "$@"' in out
, out
84 assert 'runner-arg' in out
, out
85 assert 'script' in out
, out
87 def testRecursive(self
):
88 child
= subprocess
.Popen([local_0launch
, '--', recursive_runner
, 'user-arg'], stdout
= subprocess
.PIPE
)
89 stdout
, _
= child
.communicate()
90 assert 'Runner: script=A test script: args=command-arg -- arg-for-runnable recursive-arg -- user-arg' in stdout
, stdout
92 def testExecutable(self
):
93 child
= subprocess
.Popen([local_0launch
, '--', runexec
, 'user-arg-run'], stdout
= subprocess
.PIPE
)
94 stdout
, _
= child
.communicate()
95 assert 'Runner: script=A test script: args=foo-arg -- var user-arg-run' in stdout
, stdout
96 assert 'Runner: script=A test script: args=command-arg -- path user-arg-run' in stdout
, stdout
98 # Check runenv.py is updated correctly
99 from zeroinstall
.support
import basedir
100 runenv
= basedir
.load_first_cache(namespaces
.config_site
, namespaces
.config_prog
, 'runenv.py')
101 os
.chmod(runenv
, 0700)
102 with
open(runenv
, 'wb') as s
:
105 child
= subprocess
.Popen([local_0launch
, '--', runexec
, 'user-arg-run'], stdout
= subprocess
.PIPE
)
106 stdout
, _
= child
.communicate()
107 assert 'Runner: script=A test script: args=foo-arg -- var user-arg-run' in stdout
, stdout
108 assert 'Runner: script=A test script: args=command-arg -- path user-arg-run' in stdout
, stdout
110 if __name__
== '__main__':