Add test with an example of a feed with a dependency
[zeroinstall/zeroinstall-mseaborn.git] / tests / testendtoend.py
blobd006bca2600818b9465a77421d058b30f0d3614d
1 #!/usr/bin/env python2.4
2 import os
3 import subprocess
4 import sys
5 import unittest
7 sys.path.insert(0, '..')
9 from basetest import BaseTest
10 from basetest import write_file
11 from zeroinstall.injector import autopolicy
12 from zeroinstall.injector import run
13 from zeroinstall.injector.iface_cache import iface_cache
14 from zeroinstall.zerostore import manifest
17 # Based on code in 0publish/archive.py
18 def manifest_for_dir(dir):
19 algorithm = manifest.get_algorithm("sha1new")
20 digest = algorithm.new_digest()
21 for line in algorithm.generate_manifest(dir):
22 digest.update(line + '\n')
23 return algorithm.getID(digest)
26 class FileTreeWriter(object):
28 def __init__(self, files):
29 self._files = files
31 def write_tree(self, dest_path):
32 for filename, data in self._files:
33 pathname = os.path.join(dest_path, filename)
34 if not os.path.exists(os.path.dirname(pathname)):
35 os.makedirs(os.path.dirname(pathname))
36 write_file(pathname, data)
37 # Making everything executable is simpler.
38 os.chmod(pathname, 0777)
41 class TestEndToEnd(BaseTest):
42 def _make_archive(self, tree):
43 tar_file = os.path.join(self.make_temp_dir(), "archive.tar.gz")
44 temp_dir = self.make_temp_dir()
45 tree.write_tree(temp_dir)
46 subprocess.check_call(["tar", "-czf", tar_file, "-C", temp_dir, "."])
47 return """
48 <implementation id="%s" version="1">
49 <archive href="%s" size="%i"/>
50 </implementation>
51 """ % (manifest_for_dir(temp_dir), tar_file, os.path.getsize(tar_file))
53 def test_module_with_no_dependencies(self):
54 tree = FileTreeWriter([("hello-test", """\
55 #!/bin/sh
56 echo Hello world!
57 """)])
58 feed_file = self.make_temp_file("""
59 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
60 <name>Test</name>
61 <summary>Placeholder</summary>
62 <group main="hello-test">
64 </group>
65 </interface>
66 """ % self._make_archive(tree))
67 policy = autopolicy.AutoPolicy(
68 feed_file, download_only=True, dry_run=False)
69 policy.download_and_execute([])
70 root_impl = policy.get_implementation(
71 iface_cache.get_interface(policy.root))
72 cmd_path = os.path.join(run._get_implementation_path(root_impl.id),
73 root_impl.main)
74 proc = subprocess.Popen([cmd_path], stdout=subprocess.PIPE)
75 stdout, stderr = proc.communicate()
76 self.assertEquals(proc.wait(), 0)
77 self.assertEquals(stdout, "Hello world!\n")
79 def test_module_with_dependency(self):
80 library_tree = FileTreeWriter([("share/useful", "useful-contents\n")])
81 main_tree = FileTreeWriter([("bin/hello-test", """\
82 #!/bin/sh
83 echo Got:
84 cat $LIBFOO/share/useful
85 """)])
86 library_feed = self.make_temp_file("""\
87 <?xml version="1.0" ?>
88 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
89 <name>Test</name>
90 <summary>Placeholder</summary>
91 <group>
93 </group>
94 </interface>
95 """ % self._make_archive(library_tree))
96 main_feed = self.make_temp_file("""
97 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
98 <name>Test</name>
99 <summary>Placeholder</summary>
100 <group main="bin/hello-test">
102 <requires interface="%s">
103 <environment name="LIBFOO" insert="."/>
104 </requires>
105 </group>
106 </interface>
107 """ % (self._make_archive(main_tree), library_feed))
108 policy = autopolicy.AutoPolicy(
109 main_feed, download_only=True, dry_run=False)
110 policy.download_and_execute([])
111 self.changes_environ()
112 run.add_env_bindings(policy)
113 root_impl = policy.get_implementation(
114 iface_cache.get_interface(policy.root))
115 cmd_path = os.path.join(run._get_implementation_path(root_impl.id),
116 root_impl.main)
117 proc = subprocess.Popen([cmd_path], stdout=subprocess.PIPE)
118 stdout, stderr = proc.communicate()
119 self.assertEquals(proc.wait(), 0)
120 self.assertEquals(stdout, "Got:\nuseful-contents\n")
123 suite = unittest.makeSuite(TestEndToEnd)
124 if __name__ == '__main__':
125 sys.argv.append('-v')
126 unittest.main()