Removed old Policy class
[zeroinstall/solver.git] / tests / testrun.py
blob00cfa88f15f631582c4ea4c3bc1e3f14e6d18fb9
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.support import tasks
13 from zeroinstall.injector import run, namespaces
14 from zeroinstall import SafeException
15 from zeroinstall.injector.requirements import Requirements
16 from zeroinstall.injector.driver import Driver
18 mydir = os.path.abspath(os.path.dirname(__file__))
19 local_0launch = os.path.join(os.path.dirname(mydir), '0launch')
20 runnable = os.path.join(mydir, 'runnable', 'Runnable.xml')
21 runexec = os.path.join(mydir, 'runnable', 'RunExec.xml')
22 recursive_runner = os.path.join(mydir, 'runnable', 'RecursiveRunner.xml')
23 command_feed = os.path.join(mydir, 'Command.xml')
24 package_selections = os.path.join(mydir, 'package-selection.xml')
26 class TestRun(BaseTest):
27 def testRunnable(self):
28 child = subprocess.Popen([local_0launch, '--', runnable, 'user-arg'], stdout = subprocess.PIPE)
29 stdout, _ = child.communicate()
30 assert 'Runner: script=A test script: args=command-arg -- user-arg' in stdout, stdout
32 def testCommandBindings(self):
33 if 'SELF_COMMAND' in os.environ:
34 del os.environ['SELF_COMMAND']
36 p = Driver(requirements = Requirements(command_feed), config = self.config)
37 tasks.wait_for_blocker(p.solve_with_downloads())
38 old_stdout = sys.stdout
39 try:
40 sys.stdout = StringIO()
41 run.execute_selections(p.solver.selections, [], main = 'runnable/go.sh', dry_run = True, stores = self.config.stores)
42 finally:
43 sys.stdout = old_stdout
44 assert 'local' in os.environ['LOCAL'], os.environ['LOCAL']
45 assert 'SELF_COMMAND' in os.environ
47 def testAbsMain(self):
48 p = Driver(requirements = Requirements(command_feed), config = self.config)
49 self.config.handler.wait_for_blocker(p.solve_with_downloads())
51 old_stdout = sys.stdout
52 try:
53 sys.stdout = StringIO()
54 run.execute_selections(p.solver.selections, [], main = '/runnable/runner', dry_run = True, stores = self.config.stores)
55 finally:
56 sys.stdout = old_stdout
58 try:
59 old_stdout = sys.stdout
60 try:
61 sys.stdout = StringIO()
62 run.execute_selections(p.solver.selections, [], main = '/runnable/not-there', dry_run = True, stores = self.config.stores)
63 finally:
64 sys.stdout = old_stdout
65 except SafeException as ex:
66 assert 'not-there' in unicode(ex)
68 def testArgs(self):
69 p = Driver(requirements = Requirements(runnable), config = self.config)
70 self.config.handler.wait_for_blocker(p.solve_with_downloads())
71 old_stdout = sys.stdout
72 try:
73 sys.stdout = StringIO()
74 run.execute_selections(p.solver.selections, [], dry_run = True, stores = self.config.stores)
75 out = sys.stdout.getvalue()
76 finally:
77 sys.stdout = old_stdout
78 assert 'runner-arg' in out, out
80 def testWrapper(self):
81 p = Driver(requirements = Requirements(runnable), config = self.config)
82 self.config.handler.wait_for_blocker(p.solve_with_downloads())
83 old_stdout = sys.stdout
84 try:
85 sys.stdout = StringIO()
86 run.execute_selections(p.solver.selections, [], wrapper = 'echo', dry_run = True, stores = self.config.stores)
87 out = sys.stdout.getvalue()
88 finally:
89 sys.stdout = old_stdout
90 assert '/bin/sh -c echo "$@"' in out, out
91 assert 'runner-arg' in out, out
92 assert 'script' in out, out
94 def testRecursive(self):
95 child = subprocess.Popen([local_0launch, '--', recursive_runner, 'user-arg'], stdout = subprocess.PIPE)
96 stdout, _ = child.communicate()
97 assert 'Runner: script=A test script: args=command-arg -- arg-for-runnable recursive-arg -- user-arg' in stdout, stdout
99 def testExecutable(self):
100 child = subprocess.Popen([local_0launch, '--', runexec, 'user-arg-run'], stdout = subprocess.PIPE)
101 stdout, _ = child.communicate()
102 assert 'Runner: script=A test script: args=foo-arg -- var user-arg-run' in stdout, stdout
103 assert 'Runner: script=A test script: args=command-arg -- path user-arg-run' in stdout, stdout
105 # Check runenv.py is updated correctly
106 from zeroinstall.support import basedir
107 runenv = basedir.load_first_cache(namespaces.config_site, namespaces.config_prog, 'runenv.py')
108 os.chmod(runenv, 0700)
109 with open(runenv, 'wb') as s:
110 s.write('#!/\n')
112 child = subprocess.Popen([local_0launch, '--', runexec, 'user-arg-run'], stdout = subprocess.PIPE)
113 stdout, _ = child.communicate()
114 assert 'Runner: script=A test script: args=foo-arg -- var user-arg-run' in stdout, stdout
115 assert 'Runner: script=A test script: args=command-arg -- path user-arg-run' in stdout, stdout
117 def testRunPackage(self):
118 if 'TEST' in os.environ:
119 del os.environ['TEST']
120 child = subprocess.Popen([local_0launch, '--wrapper', 'echo $TEST #', '--', package_selections], stdout = subprocess.PIPE)
121 stdout, _ = child.communicate()
122 assert stdout.strip() == 'OK', stdout
124 if __name__ == '__main__':
125 unittest.main()