Set generated feed's arch to host architecture by default
[0compile.git] / tests / testcompile.py
bloba7f6a44ec1d7a0436ff29f3207d732dd0f42bc90
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 = 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(*([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', '')
46 if expected:
47 if expected.lower() not in got.lower():
48 raise Exception("Expected '%s', got '%s'" % (expected, got))
49 elif 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']:
56 if x in os.environ:
57 del os.environ[x]
58 user_cache_dir = os.environ['XDG_CACHE_DIRS'] = basedir.xdg_cache_home
60 class TestCompile(unittest.TestCase):
61 def setUp(self):
62 os.chdir('/')
63 self.tmpdir = tempfile.mkdtemp(prefix = '0compile-test-')
64 self.hello_dir = os.path.join(self.tmpdir, 'hello')
66 os.environ['HOME'] = self.tmpdir
67 reload(basedir)
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')
73 stream.close()
75 stream = open(os.path.join(config_dir, 'global'), 'w')
76 stream.write('[global]\n'
77 'freshness = -1\n'
78 'help_with_testing = True\n'
79 'network_use = off-line\n')
80 stream.close()
82 def tearDown(self):
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')
88 os.chdir(self.tmpdir)
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):
94 os.chdir(self.tmpdir)
95 compile('setup', hello_uri, expect = 'Created directory')
96 os.chdir('GNU-Hello')
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!')
116 def testLocal(self):
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')
133 os.chdir(comp_dir)
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)
140 s.close()
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')
147 os.chdir(comp_dir)
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
155 compile('diff')
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')
165 stream.write(prog)
166 stream.close()
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!')
178 stream.close()
179 shutil.rmtree('build')
180 compile('build', expect = 'Build failed')
181 assert os.path.exists('build/build-failure.log')
183 # 'src' does not exist
184 shutil.rmtree('src')
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')
206 def testSetup(self):
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',
212 expect = "usage")
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__':
231 unittest.main()