Windows: use support.portable_rename in various places
[zeroinstall/solver.git] / tests / testrun.py
blob7c05b95137dbf49f85f6cec57654de9a9f242fe5
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 import os, sys, subprocess
4 import unittest
5 from StringIO import StringIO
7 sys.path.insert(0, '..')
9 # (testing command support imports zeroinstall.injector._runenv in a sub-process)
10 os.environ['PYTHONPATH'] = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
12 from zeroinstall.injector import policy, run, namespaces
13 from zeroinstall import SafeException
15 mydir = os.path.abspath(os.path.dirname(__file__))
16 local_0launch = os.path.join(os.path.dirname(mydir), '0launch')
17 runnable = os.path.join(mydir, 'runnable', 'Runnable.xml')
18 runexec = os.path.join(mydir, 'runnable', 'RunExec.xml')
19 recursive_runner = os.path.join(mydir, 'runnable', 'RecursiveRunner.xml')
20 command_feed = os.path.join(mydir, 'Command.xml')
21 package_selections = os.path.join(mydir, 'package-selection.xml')
23 class TestRun(BaseTest):
24 def testRunnable(self):
25 child = subprocess.Popen([local_0launch, '--', runnable, 'user-arg'], stdout = subprocess.PIPE)
26 stdout, _ = child.communicate()
27 assert 'Runner: script=A test script: args=command-arg -- user-arg' in stdout, stdout
29 def testCommandBindings(self):
30 if 'SELF_COMMAND' in os.environ:
31 del os.environ['SELF_COMMAND']
33 p = policy.Policy(command_feed, config = self.config)
34 self.config.handler.wait_for_blocker(p.solve_with_downloads())
35 old_stdout = sys.stdout
36 try:
37 sys.stdout = StringIO()
38 run.execute_selections(p.solver.selections, [], main = 'runnable/go.sh', dry_run = True, stores = self.config.stores)
39 finally:
40 sys.stdout = old_stdout
41 assert 'local' in os.environ['LOCAL'], os.environ['LOCAL']
42 assert 'SELF_COMMAND' in os.environ
44 def testAbsMain(self):
45 p = policy.Policy(command_feed, config = self.config)
46 self.config.handler.wait_for_blocker(p.solve_with_downloads())
48 old_stdout = sys.stdout
49 try:
50 sys.stdout = StringIO()
51 run.execute_selections(p.solver.selections, [], main = '/runnable/runner', dry_run = True, stores = self.config.stores)
52 finally:
53 sys.stdout = old_stdout
55 try:
56 old_stdout = sys.stdout
57 try:
58 sys.stdout = StringIO()
59 run.execute_selections(p.solver.selections, [], main = '/runnable/not-there', dry_run = True, stores = self.config.stores)
60 finally:
61 sys.stdout = old_stdout
62 except SafeException as ex:
63 assert 'not-there' in unicode(ex)
65 def testArgs(self):
66 p = policy.Policy(runnable, config = self.config)
67 self.config.handler.wait_for_blocker(p.solve_with_downloads())
68 old_stdout = sys.stdout
69 try:
70 sys.stdout = StringIO()
71 run.execute_selections(p.solver.selections, [], dry_run = True, stores = self.config.stores)
72 out = sys.stdout.getvalue()
73 finally:
74 sys.stdout = old_stdout
75 assert 'runner-arg' in out, out
77 def testWrapper(self):
78 p = policy.Policy(runnable, config = self.config)
79 self.config.handler.wait_for_blocker(p.solve_with_downloads())
80 old_stdout = sys.stdout
81 try:
82 sys.stdout = StringIO()
83 run.execute_selections(p.solver.selections, [], wrapper = 'echo', dry_run = True, stores = self.config.stores)
84 out = sys.stdout.getvalue()
85 finally:
86 sys.stdout = old_stdout
87 assert '/bin/sh -c echo "$@"' in out, out
88 assert 'runner-arg' in out, out
89 assert 'script' in out, out
91 def testRecursive(self):
92 child = subprocess.Popen([local_0launch, '--', recursive_runner, 'user-arg'], stdout = subprocess.PIPE)
93 stdout, _ = child.communicate()
94 assert 'Runner: script=A test script: args=command-arg -- arg-for-runnable recursive-arg -- user-arg' in stdout, stdout
96 def testExecutable(self):
97 child = subprocess.Popen([local_0launch, '--', runexec, 'user-arg-run'], stdout = subprocess.PIPE)
98 stdout, _ = child.communicate()
99 assert 'Runner: script=A test script: args=foo-arg -- var user-arg-run' in stdout, stdout
100 assert 'Runner: script=A test script: args=command-arg -- path user-arg-run' in stdout, stdout
102 # Check runenv.py is updated correctly
103 from zeroinstall.support import basedir
104 runenv = basedir.load_first_cache(namespaces.config_site, namespaces.config_prog, 'runenv.py')
105 os.chmod(runenv, 0700)
106 with open(runenv, 'wb') as s:
107 s.write('#!/\n')
109 child = subprocess.Popen([local_0launch, '--', runexec, 'user-arg-run'], stdout = subprocess.PIPE)
110 stdout, _ = child.communicate()
111 assert 'Runner: script=A test script: args=foo-arg -- var user-arg-run' in stdout, stdout
112 assert 'Runner: script=A test script: args=command-arg -- path user-arg-run' in stdout, stdout
114 def testRunPackage(self):
115 if 'TEST' in os.environ:
116 del os.environ['TEST']
117 child = subprocess.Popen([local_0launch, '--wrapper', 'echo $TEST #', '--', package_selections], stdout = subprocess.PIPE)
118 stdout, _ = child.communicate()
119 assert stdout.strip() == 'OK', stdout
121 if __name__ == '__main__':
122 unittest.main()