Start development series 0.24.2-post
[0compile.git] / tests / testcompile.py
blob9078578e82e33c00de1780753871bf8449d6e6ac
1 #!/usr/bin/env python
2 import sys, tempfile, os, shutil, tempfile, subprocess
3 from StringIO import StringIO
4 import unittest
5 from zeroinstall.injector import model, qdom
6 from zeroinstall.support import ro_rmtree, basedir
7 from zeroinstall.zerostore import Stores
9 stores = Stores()
11 my_dir = os.path.abspath(os.path.dirname(__file__))
12 sys.path.insert(0, os.path.dirname(my_dir))
13 import support
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)
29 if zeroinstall_dir:
30 launch_command = [sys.executable, os.path.join(zeroinstall_dir, '0launch')]
31 else:
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(*([sys.executable, compile_bin] + list(args)), **kwargs)
41 def run(*args, **kwargs):
42 if not isinstance(args[0], basestring):
43 args = args[0] + list(args[1:])
44 child = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
45 got, unused = child.communicate()
47 expected = kwargs.get('expect', '')
48 if expected:
49 if expected.lower() not in got.lower():
50 raise Exception("Expected '%s', got '%s'" % (expected, got))
51 elif got:
52 raise Exception("Expected nothing, got '%s'" % got)
54 # Detect accidental network access
55 os.environ['http_proxy'] = 'localhost:1111'
57 for x in ['GNUPGHOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME']:
58 if x in os.environ:
59 del os.environ[x]
60 user_cache_dir = os.environ['XDG_CACHE_DIRS'] = basedir.xdg_cache_home
62 class TestCompile(unittest.TestCase):
63 def setUp(self):
64 os.chdir('/')
65 self.tmpdir = tempfile.mkdtemp(prefix = '0compile-test-')
66 self.hello_dir = os.path.join(self.tmpdir, 'hello')
68 os.environ['HOME'] = self.tmpdir
69 reload(basedir)
71 config_dir = basedir.save_config_path('0install.net', 'injector')
72 stream = open(os.path.join(config_dir, 'implementation-dirs'), 'w')
73 for x in stores.stores:
74 stream.write(x.dir + '\n')
75 stream.close()
77 stream = open(os.path.join(config_dir, 'global'), 'w')
78 stream.write('[global]\n'
79 'freshness = -1\n'
80 'help_with_testing = True\n'
81 'network_use = off-line\n')
82 stream.close()
84 def tearDown(self):
85 ro_rmtree(self.tmpdir)
87 def testBadCommand(self):
88 compile('foo', expect = 'usage: 0compile')
89 compile('setup', hello_uri, self.tmpdir, expect = 'already exists')
90 os.chdir(self.tmpdir)
91 compile('setup', expect = 'Run 0compile from a directory containing')
92 compile('build', expect = 'Run 0compile from a directory containing')
93 compile('publish', expect = 'Run 0compile from a directory containing')
95 def testCompileNoDir(self):
96 os.chdir(self.tmpdir)
97 compile('setup', hello_uri, expect = 'Created directory')
98 os.chdir('GNU-Hello')
100 def testCompile(self):
101 compile('setup', hello_uri, self.hello_dir, expect = 'Created directory')
102 os.chdir(self.hello_dir)
104 compile('build', expect = 'Executing: "$SRCDIR/configure"')
106 target_dir = 'gnu-hello-%s' % support.get_arch_name().lower()
107 archive_stem = 'gnu-hello-%s-1.3' % support.get_arch_name().lower()
108 assert os.path.isdir(target_dir), '%s not a directory' % target_dir
110 run('%s/bin/hello' % target_dir, expect = 'Hello, world!')
111 run(launch_command, '%s/0install/GNU-Hello.xml' % target_dir, expect = 'Hello, world!')
112 compile('publish', 'http://localhost/downloads', expect = "Now upload '%s.tar.bz2'" % archive_stem)
114 def testAutocompile(self):
115 compile('autocompile', hello_uri, expect = "Registering as feed for http://0install.net/tests/GNU-Hello.xml")
116 run(launch_command, hello_uri, expect = 'Hello, world!')
118 def testLocal(self):
119 compile('setup', local_hello_path, self.hello_dir, expect = 'Created directory')
120 os.chdir(self.hello_dir)
121 compile('build', expect = 'Executing: ls -l')
122 target_dir = 'hello2-any-any'
123 assert os.path.isdir(target_dir), '%s not a directory' % target_dir
125 run(launch_command, '%s/0install/hello2.xml' % target_dir, expect = 'ROX-Lib')
127 def testBadVersion(self):
128 compile('setup', local_bad_version, self.hello_dir, expect = 'Created directory')
129 os.chdir(self.hello_dir)
130 compile('build', expect = 'hello2-0.1 requires 0compile >= 300000')
132 def testCommand(self):
133 comp_dir = os.path.join(self.tmpdir, 'cprog-command')
134 compile('setup', local_cprog_command_path, comp_dir, expect = 'Created directory')
135 os.chdir(comp_dir)
136 compile('build', expect = 'Hello from C!')
137 target_dir = 'cprog-command-%s' % support.get_arch_name().lower()
138 binary_feed = os.path.join(target_dir, '0install', 'cprog-command.xml')
139 run(launch_command, binary_feed, expect = 'Hello from C!')
140 s = open(binary_feed, 'r')
141 feed = model.ZeroInstallFeed(qdom.parse(s), binary_feed)
142 s.close()
143 impl, = feed.implementations.values()
144 assert impl.arch, "Missing arch on %s" % impl
146 def testCopySrc(self):
147 comp_dir = os.path.join(self.tmpdir, 'cprog')
148 compile('setup', local_cprog_path, comp_dir, expect = 'Created directory')
149 os.chdir(comp_dir)
150 compile('diff', expect = "No local src directory to diff against")
151 compile('diff', 'foo', expect = 'usage')
152 compile('copy-src', 'foo', expect = 'usage')
153 compile('copy-src', expect = 'Copied as')
154 compile('copy-src', expect = "Directory '")
156 # 'src' exists, but no changes
157 compile('diff')
158 compile('--verbose', 'build', expect = 'Hello from C')
159 target_dir = 'cprog-%s' % support.get_arch_name().lower()
160 patch_file = os.path.join(target_dir, '0install/from-0.1.patch')
161 assert not os.path.exists(patch_file)
163 # 'src' contains a change
164 prog = file('src/main.c').read()
165 prog = prog.replace('Hello', 'Goodbye')
166 stream = file('src/main.c', 'w')
167 stream.write(prog)
168 stream.close()
169 compile('diff', expect = 'diff')
170 shutil.rmtree('build')
171 compile('build', expect = 'Goodbye from C')
172 assert os.path.exists(patch_file)
174 # Test dup-src's unlinking while we're here
175 compile('build', expect = 'Goodbye from C')
177 # 'src' contains an error
178 stream = file('src/main.c', 'w')
179 stream.write('this is not valid C!')
180 stream.close()
181 shutil.rmtree('build')
182 compile('build', expect = 'Build failed')
183 assert os.path.exists('build/build-failure.log')
185 # 'src' does not exist
186 shutil.rmtree('src')
187 shutil.rmtree('build')
188 compile('build', expect = 'Hello from C')
189 assert not os.path.exists(patch_file)
191 # Check we fixed the .pc files...
192 pc_data = open(os.path.join(target_dir, 'pkgconfig', 'cprog.pc')).read()
193 assert pc_data == "prefix=${pcfiledir}/..\n", `pc_data`
195 # Check we removed the bad .la files...
196 assert not os.path.exists(os.path.join(target_dir, 'lib', 'bad.la')) # libtool - bad
197 assert os.path.exists(os.path.join(target_dir, 'lib', 'good.la')) # Ends in .la, but not a libtool archive
198 assert os.path.exists(os.path.join(target_dir, 'lib', 'nice.ok')) # Doesn't end in .la
200 def testInlcudeDeps(self):
201 compile('setup', hello_uri, self.hello_dir, expect = 'Created directory')
202 os.chdir(self.hello_dir)
203 os.unlink('0compile.properties')
204 compile('setup', hello_uri, '.')
205 compile('include-deps', expect = 'dependencies to')
206 compile('include-deps', expect = 'Copied 0 depend')
208 def testSetup(self):
209 compile('setup', hello_selections, self.hello_dir,
210 expect = 'Created directory')
211 compile('setup', hello_selections, self.hello_dir,
212 expect = "Directory '")
213 compile('setup', hello_selections, '.', 'foo',
214 expect = "usage")
215 os.chdir(self.hello_dir)
216 compile('setup', expect = "Selections are fixed")
218 def testReportBug(self):
219 broken_src = os.path.join(self.hello_dir, "broken.xml")
220 os.mkdir(self.hello_dir)
221 shutil.copy(local_hello_path, broken_src)
222 os.chdir(self.hello_dir)
223 compile('setup', broken_src, '.')
224 compile('build', expect = 'Build failed with exit code')
225 compile('report-bug', expect = "http://sourceforge.net")
227 env = support.BuildEnv()
228 os.unlink(os.path.join(env.metadir, "build-environment.xml"))
229 compile('report-bug', expect = "file+not+found")
231 suite = unittest.makeSuite(TestCompile)
232 if __name__ == '__main__':
233 unittest.main()