2 import sys
, tempfile
, os
, shutil
, tempfile
, subprocess
3 from StringIO
import StringIO
5 from zeroinstall
.injector
import model
, qdom
6 from zeroinstall
.support
import ro_rmtree
, basedir
7 from zeroinstall
.zerostore
import Stores
11 my_dir
= os
.path
.abspath(os
.path
.dirname(__file__
))
12 sys
.path
.insert(0, os
.path
.dirname(my_dir
))
15 hello_uri
= 'http://0install.net/tests/GNU-Hello.xml'
16 hello_selections
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'selections.xml'))
17 local_bad_version
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'bad-version.xml'))
18 local_hello_path
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'hello2', 'hello2.xml'))
19 local_cprog_command_path
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'cprog', 'cprog-command.xml'))
20 local_cprog_path
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'cprog', 'cprog.xml'))
22 compile_bin
= os
.path
.join(my_dir
, '0compile-coverage')
23 assert os
.path
.exists(compile_bin
)
25 if 'DISPLAY' in os
.environ
:
26 del os
.environ
['DISPLAY']
28 zeroinstall_dir
= os
.environ
.get('0COMPILE_ZEROINSTALL', None)
30 launch_command
= os
.path
.join(zeroinstall_dir
, '0launch')
32 launch_command
= '0launch' # Package
34 # Ensure it's cached now, to avoid extra output during the tests
35 if subprocess
.call([launch_command
, '--source', '-vc', '--download-only', hello_uri
]):
36 raise Exception("Failed to download hello world test program")
38 def compile(*args
, **kwargs
):
39 run(*([compile_bin
] + list(args
)), **kwargs
)
41 def run(*args
, **kwargs
):
42 child
= subprocess
.Popen(args
, stdout
= subprocess
.PIPE
, stderr
= subprocess
.STDOUT
)
43 got
, unused
= child
.communicate()
45 expected
= kwargs
.get('expect', '')
47 if expected
.lower() not in got
.lower():
48 raise Exception("Expected '%s', got '%s'" % (expected
, got
))
50 raise Exception("Expected nothing, got '%s'" % got
)
52 # Detect accidental network access
53 os
.environ
['http_proxy'] = 'localhost:1111'
55 for x
in ['GNUPGHOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME']:
58 user_cache_dir
= os
.environ
['XDG_CACHE_DIRS'] = basedir
.xdg_cache_home
60 class TestCompile(unittest
.TestCase
):
63 self
.tmpdir
= tempfile
.mkdtemp(prefix
= '0compile-test-')
64 self
.hello_dir
= os
.path
.join(self
.tmpdir
, 'hello')
66 os
.environ
['HOME'] = self
.tmpdir
69 config_dir
= basedir
.save_config_path('0install.net', 'injector')
70 stream
= open(os
.path
.join(config_dir
, 'implementation-dirs'), 'w')
71 for x
in stores
.stores
:
72 stream
.write(x
.dir + '\n')
75 stream
= open(os
.path
.join(config_dir
, 'global'), 'w')
76 stream
.write('[global]\n'
78 'help_with_testing = True\n'
79 'network_use = off-line\n')
83 ro_rmtree(self
.tmpdir
)
85 def testBadCommand(self
):
86 compile('foo', expect
= 'usage: 0compile')
87 compile('setup', hello_uri
, self
.tmpdir
, expect
= 'already exists')
89 compile('setup', expect
= 'Run 0compile from a directory containing')
90 compile('build', expect
= 'Run 0compile from a directory containing')
91 compile('publish', expect
= 'Run 0compile from a directory containing')
93 def testCompileNoDir(self
):
95 compile('setup', hello_uri
, expect
= 'Created directory')
98 def testCompile(self
):
99 compile('setup', hello_uri
, self
.hello_dir
, expect
= 'Created directory')
100 os
.chdir(self
.hello_dir
)
102 compile('build', expect
= 'Executing: "$SRCDIR/configure"')
104 target_dir
= 'gnu-hello-%s' % support
.get_arch_name().lower()
105 archive_stem
= 'gnu-hello-%s-1.3' % support
.get_arch_name().lower()
106 assert os
.path
.isdir(target_dir
), '%s not a directory' % target_dir
108 run('%s/bin/hello' % target_dir
, expect
= 'Hello, world!')
109 run(launch_command
, '%s/0install/GNU-Hello.xml' % target_dir
, expect
= 'Hello, world!')
110 compile('publish', 'http://localhost/downloads', expect
= "Now upload '%s.tar.bz2'" % archive_stem
)
112 def testAutocompile(self
):
113 compile('autocompile', hello_uri
, expect
= "Registering as feed for http://0install.net/tests/GNU-Hello.xml")
114 run(launch_command
, hello_uri
, expect
= 'Hello, world!')
117 compile('setup', local_hello_path
, self
.hello_dir
, expect
= 'Created directory')
118 os
.chdir(self
.hello_dir
)
119 compile('build', expect
= 'Executing: ls -l')
120 target_dir
= 'hello2-%s' % support
.get_arch_name().lower()
121 assert os
.path
.isdir(target_dir
), '%s not a directory' % target_dir
123 run(launch_command
, '%s/0install/hello2.xml' % target_dir
, expect
= 'ROX-Lib')
125 def testBadVersion(self
):
126 compile('setup', local_bad_version
, self
.hello_dir
, expect
= 'Created directory')
127 os
.chdir(self
.hello_dir
)
128 compile('build', expect
= 'hello2-0.1 requires 0compile >= 300000')
130 def testCommand(self
):
131 comp_dir
= os
.path
.join(self
.tmpdir
, 'cprog-command')
132 compile('setup', local_cprog_command_path
, comp_dir
, expect
= 'Created directory')
134 compile('build', expect
= 'Hello from C!')
135 target_dir
= 'cprog-command-%s' % support
.get_arch_name().lower()
136 binary_feed
= os
.path
.join(target_dir
, '0install', 'cprog-command.xml')
137 run(launch_command
, binary_feed
, expect
= 'Hello from C!')
138 s
= open(binary_feed
, 'r')
139 feed
= model
.ZeroInstallFeed(qdom
.parse(s
), binary_feed
)
141 impl
, = feed
.implementations
.values()
142 assert impl
.arch
, "Missing arch on %s" % impl
144 def testCopySrc(self
):
145 comp_dir
= os
.path
.join(self
.tmpdir
, 'cprog')
146 compile('setup', local_cprog_path
, comp_dir
, expect
= 'Created directory')
148 compile('diff', expect
= "No local src directory to diff against")
149 compile('diff', 'foo', expect
= 'usage')
150 compile('copy-src', 'foo', expect
= 'usage')
151 compile('copy-src', expect
= 'Copied as')
152 compile('copy-src', expect
= "Directory '")
154 # 'src' exists, but no changes
156 compile('--verbose', 'build', expect
= 'Hello from C')
157 target_dir
= 'cprog-%s' % support
.get_arch_name().lower()
158 patch_file
= os
.path
.join(target_dir
, '0install/from-0.1.patch')
159 assert not os
.path
.exists(patch_file
)
161 # 'src' contains a change
162 prog
= file('src/main.c').read()
163 prog
= prog
.replace('Hello', 'Goodbye')
164 stream
= file('src/main.c', 'w')
167 compile('diff', expect
= 'diff')
168 shutil
.rmtree('build')
169 compile('build', expect
= 'Goodbye from C')
170 assert os
.path
.exists(patch_file
)
172 # Test dup-src's unlinking while we're here
173 compile('build', expect
= 'Goodbye from C')
175 # 'src' contains an error
176 stream
= file('src/main.c', 'w')
177 stream
.write('this is not valid C!')
179 shutil
.rmtree('build')
180 compile('build', expect
= 'Build failed')
181 assert os
.path
.exists('build/build-failure.log')
183 # 'src' does not exist
185 shutil
.rmtree('build')
186 compile('build', expect
= 'Hello from C')
187 assert not os
.path
.exists(patch_file
)
189 # Check we fixed the .pc files...
190 pc_data
= open(os
.path
.join(target_dir
, 'pkgconfig', 'cprog.pc')).read()
191 assert pc_data
== "prefix=${pcfiledir}/..\n", `pc_data`
193 # Check we removed the bad .la files...
194 assert not os
.path
.exists(os
.path
.join(target_dir
, 'lib', 'bad.la')) # libtool - bad
195 assert os
.path
.exists(os
.path
.join(target_dir
, 'lib', 'good.la')) # Ends in .la, but not a libtool archive
196 assert os
.path
.exists(os
.path
.join(target_dir
, 'lib', 'nice.ok')) # Doesn't end in .la
198 def testInlcudeDeps(self
):
199 compile('setup', hello_uri
, self
.hello_dir
, expect
= 'Created directory')
200 os
.chdir(self
.hello_dir
)
201 os
.unlink('0compile.properties')
202 compile('setup', hello_uri
, '.')
203 compile('include-deps', expect
= 'dependencies to')
204 compile('include-deps', expect
= 'Copied 0 depend')
207 compile('setup', hello_selections
, self
.hello_dir
,
208 expect
= 'Created directory')
209 compile('setup', hello_selections
, self
.hello_dir
,
210 expect
= "Directory '")
211 compile('setup', hello_selections
, '.', 'foo',
213 os
.chdir(self
.hello_dir
)
214 compile('setup', expect
= "Selections are fixed")
216 def testReportBug(self
):
217 broken_src
= os
.path
.join(self
.hello_dir
, "broken.xml")
218 os
.mkdir(self
.hello_dir
)
219 shutil
.copy(local_hello_path
, broken_src
)
220 os
.chdir(self
.hello_dir
)
221 compile('setup', broken_src
, '.')
222 compile('build', expect
= 'Build failed with exit code')
223 compile('report-bug', expect
= "http://sourceforge.net")
225 env
= support
.BuildEnv()
226 os
.unlink(os
.path
.join(env
.metadir
, "build-environment.xml"))
227 compile('report-bug', expect
= "file+not+found")
229 suite
= unittest
.makeSuite(TestCompile
)
230 if __name__
== '__main__':