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 def command_for_policy(policy
):
27 root_impl
= policy
.get_implementation(
28 iface_cache
.get_interface(policy
.root
))
29 return os
.path
.join(run
._get
_implementation
_path
(root_impl
.id),
33 class FileTreeWriter(object):
35 def __init__(self
, files
):
38 def write_tree(self
, dest_path
):
39 for filename
, data
in self
._files
:
40 pathname
= os
.path
.join(dest_path
, filename
)
41 if not os
.path
.exists(os
.path
.dirname(pathname
)):
42 os
.makedirs(os
.path
.dirname(pathname
))
43 write_file(pathname
, data
)
44 # Making everything executable is simpler.
45 os
.chmod(pathname
, 0777)
48 class TestEndToEnd(BaseTest
):
49 def _make_archive(self
, tree
):
50 tar_file
= os
.path
.join(self
.make_temp_dir(), "archive.tar.gz")
51 temp_dir
= self
.make_temp_dir()
52 tree
.write_tree(temp_dir
)
53 subprocess
.check_call(["tar", "-czf", tar_file
, "-C", temp_dir
, "."])
55 <implementation id="%s" version="1">
56 <archive href="%s" size="%i"/>
58 """ % (manifest_for_dir(temp_dir
), tar_file
, os
.path
.getsize(tar_file
))
60 def test_module_with_no_dependencies(self
):
61 tree
= FileTreeWriter([("hello-test", """\
65 feed_file
= self
.make_temp_file("""
66 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
68 <summary>Placeholder</summary>
69 <group main="hello-test">
73 """ % self
._make
_archive
(tree
))
74 policy
= autopolicy
.AutoPolicy(
75 feed_file
, download_only
=True, dry_run
=False)
76 policy
.download_and_execute([])
77 cmd_path
= command_for_policy(policy
)
78 proc
= subprocess
.Popen([cmd_path
], stdout
=subprocess
.PIPE
)
79 stdout
, stderr
= proc
.communicate()
80 self
.assertEquals(proc
.wait(), 0)
81 self
.assertEquals(stdout
, "Hello world!\n")
83 def test_module_with_dependency(self
):
84 library_tree
= FileTreeWriter([("share/useful", "useful-contents\n")])
85 main_tree
= FileTreeWriter([("bin/hello-test", """\
88 cat $LIBFOO/share/useful
90 library_feed
= self
.make_temp_file("""\
91 <?xml version="1.0" ?>
92 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
94 <summary>Placeholder</summary>
99 """ % self
._make
_archive
(library_tree
))
100 main_feed
= self
.make_temp_file("""
101 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
103 <summary>Placeholder</summary>
104 <group main="bin/hello-test">
106 <requires interface="%s">
107 <environment name="LIBFOO" insert="."/>
111 """ % (self
._make
_archive
(main_tree
), library_feed
))
112 policy
= autopolicy
.AutoPolicy(
113 main_feed
, download_only
=True, dry_run
=False)
114 policy
.download_and_execute([])
116 run
.add_env_bindings(env
, policy
)
117 self
.assertEquals(env
.keys(), ["LIBFOO"])
118 cmd_path
= command_for_policy(policy
)
119 proc
= subprocess
.Popen([cmd_path
], stdout
=subprocess
.PIPE
, env
=env
)
120 stdout
, stderr
= proc
.communicate()
121 self
.assertEquals(proc
.wait(), 0)
122 self
.assertEquals(stdout
, "Got:\nuseful-contents\n")
124 def test_self_environment_binding(self
):
125 tree
= FileTreeWriter([("share/useful", "useful-contents\n"),
126 ("bin/hello-test", """\
129 cat $MY_DATA_DIR/share/useful
131 feed_file
= self
.make_temp_file("""
132 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
134 <summary>Placeholder</summary>
135 <group main="bin/hello-test">
137 <environment name="MY_DATA_DIR" insert="."/>
140 """ % (self
._make
_archive
(tree
)))
141 policy
= autopolicy
.AutoPolicy(
142 feed_file
, download_only
=True, dry_run
=False)
143 policy
.download_and_execute([])
145 run
.add_env_bindings(env
, policy
)
146 self
.assertEquals(env
.keys(), ["MY_DATA_DIR"])
147 cmd_path
= command_for_policy(policy
)
148 proc
= subprocess
.Popen([cmd_path
], stdout
=subprocess
.PIPE
, env
=env
)
149 stdout
, stderr
= proc
.communicate()
150 self
.assertEquals(proc
.wait(), 0)
151 self
.assertEquals(stdout
, "Got:\nuseful-contents\n")
154 suite
= unittest
.makeSuite(TestEndToEnd
)
155 if __name__
== '__main__':
156 sys
.argv
.append('-v')