From 950f63cea46d9ae4703f1d5d4f68cce1207e4f4a Mon Sep 17 00:00:00 2001 From: Mark Seaborn Date: Sun, 2 Nov 2008 17:05:26 +0000 Subject: [PATCH] Add test with an example of a feed with a dependency * Refactor run.execute(): split out environment-setting into a separate function --- tests/basetest.py | 13 +++++++ tests/testendtoend.py | 89 +++++++++++++++++++++++++++++++++++---------- zeroinstall/injector/run.py | 25 +++++++------ 3 files changed, 95 insertions(+), 32 deletions(-) diff --git a/tests/basetest.py b/tests/basetest.py index fe58028..f09cbd5 100755 --- a/tests/basetest.py +++ b/tests/basetest.py @@ -25,6 +25,14 @@ empty_feed = qdom.parse(StringIO.StringIO(""" @@ -46,10 +50,12 @@ echo Hello World """ % (manifest_for_dir(temp_dir), tar_file, os.path.getsize(tar_file)) - def test(self): - tempdir = self.make_temp_dir() - feed_file = os.path.join(tempdir, "foo.xml") - write_file(feed_file, """ + def test_module_with_no_dependencies(self): + tree = FileTreeWriter([("hello-test", """\ +#!/bin/sh +echo Hello world! +""")]) + feed_file = self.make_temp_file(""" Test Placeholder @@ -57,7 +63,7 @@ echo Hello World %s -""" % self._make_hello()) +""" % self._make_archive(tree)) policy = autopolicy.AutoPolicy( feed_file, download_only=True, dry_run=False) policy.download_and_execute([]) @@ -68,7 +74,50 @@ echo Hello World proc = subprocess.Popen([cmd_path], stdout=subprocess.PIPE) stdout, stderr = proc.communicate() self.assertEquals(proc.wait(), 0) - self.assertEquals(stdout, "Hello World\n") + self.assertEquals(stdout, "Hello world!\n") + + def test_module_with_dependency(self): + library_tree = FileTreeWriter([("share/useful", "useful-contents\n")]) + main_tree = FileTreeWriter([("bin/hello-test", """\ +#!/bin/sh +echo Got: +cat $LIBFOO/share/useful +""")]) + library_feed = self.make_temp_file("""\ + + + Test + Placeholder + + %s + + +""" % self._make_archive(library_tree)) + main_feed = self.make_temp_file(""" + + Test + Placeholder + + %s + + + + + +""" % (self._make_archive(main_tree), library_feed)) + policy = autopolicy.AutoPolicy( + main_feed, download_only=True, dry_run=False) + policy.download_and_execute([]) + self.changes_environ() + run.add_env_bindings(policy) + root_impl = policy.get_implementation( + iface_cache.get_interface(policy.root)) + cmd_path = os.path.join(run._get_implementation_path(root_impl.id), + root_impl.main) + proc = subprocess.Popen([cmd_path], stdout=subprocess.PIPE) + stdout, stderr = proc.communicate() + self.assertEquals(proc.wait(), 0) + self.assertEquals(stdout, "Got:\nuseful-contents\n") suite = unittest.makeSuite(TestEndToEnd) diff --git a/zeroinstall/injector/run.py b/zeroinstall/injector/run.py index d3c0971..b81b515 100644 --- a/zeroinstall/injector/run.py +++ b/zeroinstall/injector/run.py @@ -21,6 +21,18 @@ def do_env_binding(binding, path): os.environ.get(binding.name, None)) info("%s=%s", binding.name, os.environ[binding.name]) +def add_env_bindings(policy): + for impl in policy.implementation.values(): + assert impl + _do_bindings(impl, impl.bindings) + for dep in impl.requires: + dep_iface = iface_cache.get_interface(dep.interface) + dep_impl = policy.get_implementation(dep_iface) + if isinstance(dep_impl, ZeroInstallImplementation): + _do_bindings(dep_impl, dep.bindings) + else: + debug("Implementation %s is native; no bindings needed", dep_impl) + def execute(policy, prog_args, dry_run = False, main = None, wrapper = None): """Execute program. On success, doesn't return. On failure, raises an Exception. Returns normally only for a successful dry run. @@ -37,18 +49,7 @@ def execute(policy, prog_args, dry_run = False, main = None, wrapper = None): @precondition: C{policy.ready and policy.get_uncached_implementations() == []} """ iface = iface_cache.get_interface(policy.root) - - for impl in policy.implementation.values(): - assert impl - _do_bindings(impl, impl.bindings) - for dep in impl.requires: - dep_iface = iface_cache.get_interface(dep.interface) - dep_impl = policy.get_implementation(dep_iface) - if isinstance(dep_impl, ZeroInstallImplementation): - _do_bindings(dep_impl, dep.bindings) - else: - debug("Implementation %s is native; no bindings needed", dep_impl) - + add_env_bindings(policy) root_impl = policy.get_implementation(iface) _execute(root_impl, prog_args, dry_run, main, wrapper) -- 2.11.4.GIT