Don't force use of Python 2.4 for unit-tests
[0compile.git] / tests / testcompile.py
blob17dd645de1e73e14c55105575a6c6082a6751949
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
7 sys.path.insert(0, '..')
8 import support
10 hello_uri = 'http://0install.net/tests/GNU-Hello.xml'
11 hello_selections = os.path.realpath(os.path.join(os.path.dirname(__file__), 'selections.xml'))
12 local_bad_version = os.path.realpath(os.path.join(os.path.dirname(__file__), 'bad-version.xml'))
13 local_hello_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'hello2', 'hello2.xml'))
14 local_cprog_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'cprog', 'cprog.xml'))
16 compile_bin = os.path.abspath('0compile-coverage')
17 assert os.path.exists(compile_bin)
19 if 'DISPLAY' in os.environ:
20 del os.environ['DISPLAY']
22 # Ensure it's cached now, to avoid extra output during the tests
23 if subprocess.call(['0launch', '--source', '-vc', '--download-only', hello_uri]):
24 raise Exception("Failed to download hello world test program")
26 def compile(*args, **kwargs):
27 run(*([compile_bin] + list(args)), **kwargs)
29 def run(*args, **kwargs):
30 child = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
31 got, unused = child.communicate()
33 expected = kwargs.get('expect', '')
34 if expected:
35 if expected.lower() not in got.lower():
36 raise Exception("Expected '%s', got '%s'" % (expected, got))
37 elif got:
38 raise Exception("Expected nothing, got '%s'" % got)
40 class TestCompile(unittest.TestCase):
41 def setUp(self):
42 os.chdir('/')
43 self.tmpdir = tempfile.mkdtemp(prefix = '0compile-test-')
44 self.hello_dir = os.path.join(self.tmpdir, 'hello')
46 def tearDown(self):
47 ro_rmtree(self.tmpdir)
49 def testBadCommand(self):
50 compile('foo', expect = 'usage: 0compile')
51 compile('setup', hello_uri, self.tmpdir, expect = 'already exists')
52 os.chdir(self.tmpdir)
53 compile('setup', expect = 'Run 0compile from a directory containing')
54 compile('build', expect = 'Run 0compile from a directory containing')
55 compile('publish', expect = 'Run 0compile from a directory containing')
57 def testCompileNoDir(self):
58 os.chdir(self.tmpdir)
59 compile('setup', hello_uri, expect = 'Created directory')
60 os.chdir('GNU-Hello')
62 def testCompile(self):
63 compile('setup', hello_uri, self.hello_dir, expect = 'Created directory')
64 os.chdir(self.hello_dir)
66 compile('build', expect = 'Executing: "$SRCDIR/configure"')
68 target_dir = 'gnu-hello-%s' % support.get_arch_name().lower()
69 archive_stem = 'gnu-hello-%s-1.3' % support.get_arch_name().lower()
70 assert os.path.isdir(target_dir), '%s not a directory' % target_dir
72 run('%s/bin/hello' % target_dir, expect = 'Hello, world!')
73 run('0launch', '%s/0install/GNU-Hello.xml' % target_dir, expect = 'Hello, world!')
74 compile('publish', 'http://localhost/downloads', expect = "Now upload '%s.tar.bz2'" % archive_stem)
76 def testLocal(self):
77 compile('setup', local_hello_path, self.hello_dir, expect = 'Created directory')
78 os.chdir(self.hello_dir)
79 compile('build', expect = 'Executing: ls -l')
80 target_dir = 'hello2-%s' % support.get_arch_name().lower()
81 assert os.path.isdir(target_dir), '%s not a directory' % target_dir
83 run('0launch', '%s/0install/hello2.xml' % target_dir, expect = 'ROX-Lib')
85 def testBadVersion(self):
86 compile('setup', local_bad_version, self.hello_dir, expect = 'Created directory')
87 os.chdir(self.hello_dir)
88 compile('build', expect = 'hello2-0.1 requires 0compile >= 300000')
90 def testCopySrc(self):
91 comp_dir = os.path.join(self.tmpdir, 'cprog')
92 compile('setup', local_cprog_path, comp_dir, expect = 'Created directory')
93 os.chdir(comp_dir)
94 compile('diff', expect = "No local src directory to diff against")
95 compile('diff', 'foo', expect = 'usage')
96 compile('copy-src', 'foo', expect = 'usage')
97 compile('copy-src', expect = 'Copied as')
98 compile('copy-src', expect = "Directory '")
100 # 'src' exists, but no changes
101 compile('diff')
102 compile('--verbose', 'build', expect = 'Hello from C')
103 target_dir = 'cprog-%s' % support.get_arch_name().lower()
104 patch_file = os.path.join(target_dir, '0install/from-0.1.patch')
105 assert not os.path.exists(patch_file)
107 # 'src' contains a change
108 prog = file('src/main.c').read()
109 prog = prog.replace('Hello', 'Goodbye')
110 stream = file('src/main.c', 'w')
111 stream.write(prog)
112 stream.close()
113 compile('diff', expect = 'diff')
114 shutil.rmtree('build')
115 compile('build', expect = 'Goodbye from C')
116 assert os.path.exists(patch_file)
118 # Test dup-src's unlinking while we're here
119 compile('build', expect = 'Goodbye from C')
121 # 'src' contains an error
122 stream = file('src/main.c', 'w')
123 stream.write('this is not valid C!')
124 stream.close()
125 shutil.rmtree('build')
126 compile('build', expect = 'Build failed')
127 assert os.path.exists('build/build-failure.log')
129 # 'src' does not exist
130 shutil.rmtree('src')
131 shutil.rmtree('build')
132 compile('build', expect = 'Hello from C')
133 assert not os.path.exists(patch_file)
135 def testInlcudeDeps(self):
136 compile('setup', hello_uri, self.hello_dir, expect = 'Created directory')
137 os.chdir(self.hello_dir)
138 os.unlink('0compile.properties')
139 compile('setup', hello_uri, '.')
140 compile('include-deps', expect = 'Copied 1 depend')
141 compile('include-deps', expect = 'Copied 0 depend')
143 def testSetup(self):
144 compile('setup', hello_selections, self.hello_dir,
145 expect = 'Created directory')
146 compile('setup', hello_selections, self.hello_dir,
147 expect = "Directory '")
148 compile('setup', hello_selections, '.', 'foo',
149 expect = "usage")
150 os.chdir(self.hello_dir)
151 compile('setup', expect = "Selections are fixed")
153 suite = unittest.makeSuite(TestCompile)
154 if __name__ == '__main__':
155 sys.argv.append('-v')
156 unittest.main()