Include a copy of os.path.relpath from Python 2.6, for Python 2.5 users
[0compile.git] / tests / testcompile.py
blob08009f28df52a609f6f2cee2e0836ce724f7ded6
1 #!/usr/bin/env python
2 import sys, tempfile, os, shutil, tempfile, subprocess
3 from StringIO import StringIO
4 import unittest
5 from zeroinstall.support import ro_rmtree, basedir
6 from zeroinstall.zerostore import Stores
8 stores = Stores()
10 sys.path.insert(0, '..')
11 import support
13 hello_uri = 'http://0install.net/tests/GNU-Hello.xml'
14 hello_selections = os.path.realpath(os.path.join(os.path.dirname(__file__), 'selections.xml'))
15 local_bad_version = os.path.realpath(os.path.join(os.path.dirname(__file__), 'bad-version.xml'))
16 local_hello_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'hello2', 'hello2.xml'))
17 local_cprog_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'cprog', 'cprog.xml'))
19 compile_bin = os.path.abspath('0compile-coverage')
20 assert os.path.exists(compile_bin)
22 if 'DISPLAY' in os.environ:
23 del os.environ['DISPLAY']
25 # Ensure it's cached now, to avoid extra output during the tests
26 if subprocess.call(['0launch', '--source', '-vc', '--download-only', hello_uri]):
27 raise Exception("Failed to download hello world test program")
29 def compile(*args, **kwargs):
30 run(*([compile_bin] + list(args)), **kwargs)
32 def run(*args, **kwargs):
33 child = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
34 got, unused = child.communicate()
36 expected = kwargs.get('expect', '')
37 if expected:
38 if expected.lower() not in got.lower():
39 raise Exception("Expected '%s', got '%s'" % (expected, got))
40 elif got:
41 raise Exception("Expected nothing, got '%s'" % got)
43 # Detect accidental network access
44 os.environ['http_proxy'] = 'localhost:1111'
46 for x in ['GNUPGHOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME']:
47 if x in os.environ:
48 del os.environ[x]
49 user_cache_dir = os.environ['XDG_CACHE_DIRS'] = basedir.xdg_cache_home
51 class TestCompile(unittest.TestCase):
52 def setUp(self):
53 os.chdir('/')
54 self.tmpdir = tempfile.mkdtemp(prefix = '0compile-test-')
55 self.hello_dir = os.path.join(self.tmpdir, 'hello')
57 os.environ['HOME'] = self.tmpdir
58 reload(basedir)
60 config_dir = basedir.save_config_path('0install.net', 'injector')
61 stream = open(os.path.join(config_dir, 'implementation-dirs'), 'w')
62 for x in stores.stores:
63 stream.write(x.dir + '\n')
64 stream.close()
66 stream = open(os.path.join(config_dir, 'global'), 'w')
67 stream.write('[global]\n'
68 'freshness = -1\n'
69 'help_with_testing = True\n'
70 'network_use = off-line\n')
71 stream.close()
73 def tearDown(self):
74 ro_rmtree(self.tmpdir)
76 def testBadCommand(self):
77 compile('foo', expect = 'usage: 0compile')
78 compile('setup', hello_uri, self.tmpdir, expect = 'already exists')
79 os.chdir(self.tmpdir)
80 compile('setup', expect = 'Run 0compile from a directory containing')
81 compile('build', expect = 'Run 0compile from a directory containing')
82 compile('publish', expect = 'Run 0compile from a directory containing')
84 def testCompileNoDir(self):
85 os.chdir(self.tmpdir)
86 compile('setup', hello_uri, expect = 'Created directory')
87 os.chdir('GNU-Hello')
89 def testCompile(self):
90 compile('setup', hello_uri, self.hello_dir, expect = 'Created directory')
91 os.chdir(self.hello_dir)
93 compile('build', expect = 'Executing: "$SRCDIR/configure"')
95 target_dir = 'gnu-hello-%s' % support.get_arch_name().lower()
96 archive_stem = 'gnu-hello-%s-1.3' % support.get_arch_name().lower()
97 assert os.path.isdir(target_dir), '%s not a directory' % target_dir
99 run('%s/bin/hello' % target_dir, expect = 'Hello, world!')
100 run('0launch', '%s/0install/GNU-Hello.xml' % target_dir, expect = 'Hello, world!')
101 compile('publish', 'http://localhost/downloads', expect = "Now upload '%s.tar.bz2'" % archive_stem)
103 def testAutocompile(self):
104 compile('autocompile', hello_uri, expect = "Registering feed...")
105 run('0launch', hello_uri, expect = 'Hello, world!')
107 def testLocal(self):
108 compile('setup', local_hello_path, self.hello_dir, expect = 'Created directory')
109 os.chdir(self.hello_dir)
110 compile('build', expect = 'Executing: ls -l')
111 target_dir = 'hello2-%s' % support.get_arch_name().lower()
112 assert os.path.isdir(target_dir), '%s not a directory' % target_dir
114 run('0launch', '%s/0install/hello2.xml' % target_dir, expect = 'ROX-Lib')
116 def testBadVersion(self):
117 compile('setup', local_bad_version, self.hello_dir, expect = 'Created directory')
118 os.chdir(self.hello_dir)
119 compile('build', expect = 'hello2-0.1 requires 0compile >= 300000')
121 def testCopySrc(self):
122 comp_dir = os.path.join(self.tmpdir, 'cprog')
123 compile('setup', local_cprog_path, comp_dir, expect = 'Created directory')
124 os.chdir(comp_dir)
125 compile('diff', expect = "No local src directory to diff against")
126 compile('diff', 'foo', expect = 'usage')
127 compile('copy-src', 'foo', expect = 'usage')
128 compile('copy-src', expect = 'Copied as')
129 compile('copy-src', expect = "Directory '")
131 # 'src' exists, but no changes
132 compile('diff')
133 compile('--verbose', 'build', expect = 'Hello from C')
134 target_dir = 'cprog-%s' % support.get_arch_name().lower()
135 patch_file = os.path.join(target_dir, '0install/from-0.1.patch')
136 assert not os.path.exists(patch_file)
138 # 'src' contains a change
139 prog = file('src/main.c').read()
140 prog = prog.replace('Hello', 'Goodbye')
141 stream = file('src/main.c', 'w')
142 stream.write(prog)
143 stream.close()
144 compile('diff', expect = 'diff')
145 shutil.rmtree('build')
146 compile('build', expect = 'Goodbye from C')
147 assert os.path.exists(patch_file)
149 # Test dup-src's unlinking while we're here
150 compile('build', expect = 'Goodbye from C')
152 # 'src' contains an error
153 stream = file('src/main.c', 'w')
154 stream.write('this is not valid C!')
155 stream.close()
156 shutil.rmtree('build')
157 compile('build', expect = 'Build failed')
158 assert os.path.exists('build/build-failure.log')
160 # 'src' does not exist
161 shutil.rmtree('src')
162 shutil.rmtree('build')
163 compile('build', expect = 'Hello from C')
164 assert not os.path.exists(patch_file)
166 pc_data = open(os.path.join(target_dir, 'pkgconfig', 'cprog.pc')).read()
167 assert pc_data == "prefix=${pcfiledir}/..\n", `pc_data`
169 def testInlcudeDeps(self):
170 compile('setup', hello_uri, self.hello_dir, expect = 'Created directory')
171 os.chdir(self.hello_dir)
172 os.unlink('0compile.properties')
173 compile('setup', hello_uri, '.')
174 compile('include-deps', expect = 'Copied 1 depend')
175 compile('include-deps', expect = 'Copied 0 depend')
177 def testSetup(self):
178 compile('setup', hello_selections, self.hello_dir,
179 expect = 'Created directory')
180 compile('setup', hello_selections, self.hello_dir,
181 expect = "Directory '")
182 compile('setup', hello_selections, '.', 'foo',
183 expect = "usage")
184 os.chdir(self.hello_dir)
185 compile('setup', expect = "Selections are fixed")
187 suite = unittest.makeSuite(TestCompile)
188 if __name__ == '__main__':
189 sys.argv.append('-v')
190 unittest.main()