1 #!/usr/bin/env python2.4
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
):
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
, "."])
48 <implementation id="%s" version="1">
49 <archive href="%s" size="%i"/>
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", """\
58 feed_file
= self
.make_temp_file("""
59 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
61 <summary>Placeholder</summary>
62 <group main="hello-test">
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),
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", """\
84 cat $LIBFOO/share/useful
86 library_feed
= self
.make_temp_file("""\
87 <?xml version="1.0" ?>
88 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
90 <summary>Placeholder</summary>
95 """ % self
._make
_archive
(library_tree
))
96 main_feed
= self
.make_temp_file("""
97 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
99 <summary>Placeholder</summary>
100 <group main="bin/hello-test">
102 <requires interface="%s">
103 <environment name="LIBFOO" insert="."/>
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),
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')