2 import sys
, tempfile
, os
, shutil
, tempfile
, subprocess
3 from StringIO
import StringIO
5 from zeroinstall
.support
import ro_rmtree
, basedir
6 from zeroinstall
.zerostore
import Stores
10 my_dir
= os
.path
.abspath(os
.path
.dirname(__file__
))
11 sys
.path
.insert(0, os
.path
.dirname(my_dir
))
14 hello_uri
= 'http://0install.net/tests/GNU-Hello.xml'
15 hello_selections
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'selections.xml'))
16 local_bad_version
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'bad-version.xml'))
17 local_hello_path
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'hello2', 'hello2.xml'))
18 local_cprog_path
= os
.path
.realpath(os
.path
.join(os
.path
.dirname(__file__
), 'cprog', 'cprog.xml'))
20 compile_bin
= os
.path
.join(my_dir
, '0compile-coverage')
21 assert os
.path
.exists(compile_bin
)
23 if 'DISPLAY' in os
.environ
:
24 del os
.environ
['DISPLAY']
26 # Ensure it's cached now, to avoid extra output during the tests
27 if subprocess
.call(['0launch', '--source', '-vc', '--download-only', hello_uri
]):
28 raise Exception("Failed to download hello world test program")
30 def compile(*args
, **kwargs
):
31 run(*([compile_bin
] + list(args
)), **kwargs
)
33 def run(*args
, **kwargs
):
34 child
= subprocess
.Popen(args
, stdout
= subprocess
.PIPE
, stderr
= subprocess
.STDOUT
)
35 got
, unused
= child
.communicate()
37 expected
= kwargs
.get('expect', '')
39 if expected
.lower() not in got
.lower():
40 raise Exception("Expected '%s', got '%s'" % (expected
, got
))
42 raise Exception("Expected nothing, got '%s'" % got
)
44 # Detect accidental network access
45 os
.environ
['http_proxy'] = 'localhost:1111'
47 for x
in ['GNUPGHOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME']:
50 user_cache_dir
= os
.environ
['XDG_CACHE_DIRS'] = basedir
.xdg_cache_home
52 class TestCompile(unittest
.TestCase
):
55 self
.tmpdir
= tempfile
.mkdtemp(prefix
= '0compile-test-')
56 self
.hello_dir
= os
.path
.join(self
.tmpdir
, 'hello')
58 os
.environ
['HOME'] = self
.tmpdir
61 config_dir
= basedir
.save_config_path('0install.net', 'injector')
62 stream
= open(os
.path
.join(config_dir
, 'implementation-dirs'), 'w')
63 for x
in stores
.stores
:
64 stream
.write(x
.dir + '\n')
67 stream
= open(os
.path
.join(config_dir
, 'global'), 'w')
68 stream
.write('[global]\n'
70 'help_with_testing = True\n'
71 'network_use = off-line\n')
75 ro_rmtree(self
.tmpdir
)
77 def testBadCommand(self
):
78 compile('foo', expect
= 'usage: 0compile')
79 compile('setup', hello_uri
, self
.tmpdir
, expect
= 'already exists')
81 compile('setup', expect
= 'Run 0compile from a directory containing')
82 compile('build', expect
= 'Run 0compile from a directory containing')
83 compile('publish', expect
= 'Run 0compile from a directory containing')
85 def testCompileNoDir(self
):
87 compile('setup', hello_uri
, expect
= 'Created directory')
90 def testCompile(self
):
91 compile('setup', hello_uri
, self
.hello_dir
, expect
= 'Created directory')
92 os
.chdir(self
.hello_dir
)
94 compile('build', expect
= 'Executing: "$SRCDIR/configure"')
96 target_dir
= 'gnu-hello-%s' % support
.get_arch_name().lower()
97 archive_stem
= 'gnu-hello-%s-1.3' % support
.get_arch_name().lower()
98 assert os
.path
.isdir(target_dir
), '%s not a directory' % target_dir
100 run('%s/bin/hello' % target_dir
, expect
= 'Hello, world!')
101 run('0launch', '%s/0install/GNU-Hello.xml' % target_dir
, expect
= 'Hello, world!')
102 compile('publish', 'http://localhost/downloads', expect
= "Now upload '%s.tar.bz2'" % archive_stem
)
104 def testAutocompile(self
):
105 compile('autocompile', hello_uri
, expect
= "Registering feed...")
106 run('0launch', hello_uri
, expect
= 'Hello, world!')
109 compile('setup', local_hello_path
, self
.hello_dir
, expect
= 'Created directory')
110 os
.chdir(self
.hello_dir
)
111 compile('build', expect
= 'Executing: ls -l')
112 target_dir
= 'hello2-%s' % support
.get_arch_name().lower()
113 assert os
.path
.isdir(target_dir
), '%s not a directory' % target_dir
115 run('0launch', '%s/0install/hello2.xml' % target_dir
, expect
= 'ROX-Lib')
117 def testBadVersion(self
):
118 compile('setup', local_bad_version
, self
.hello_dir
, expect
= 'Created directory')
119 os
.chdir(self
.hello_dir
)
120 compile('build', expect
= 'hello2-0.1 requires 0compile >= 300000')
122 def testCopySrc(self
):
123 comp_dir
= os
.path
.join(self
.tmpdir
, 'cprog')
124 compile('setup', local_cprog_path
, comp_dir
, expect
= 'Created directory')
126 compile('diff', expect
= "No local src directory to diff against")
127 compile('diff', 'foo', expect
= 'usage')
128 compile('copy-src', 'foo', expect
= 'usage')
129 compile('copy-src', expect
= 'Copied as')
130 compile('copy-src', expect
= "Directory '")
132 # 'src' exists, but no changes
134 compile('--verbose', 'build', expect
= 'Hello from C')
135 target_dir
= 'cprog-%s' % support
.get_arch_name().lower()
136 patch_file
= os
.path
.join(target_dir
, '0install/from-0.1.patch')
137 assert not os
.path
.exists(patch_file
)
139 # 'src' contains a change
140 prog
= file('src/main.c').read()
141 prog
= prog
.replace('Hello', 'Goodbye')
142 stream
= file('src/main.c', 'w')
145 compile('diff', expect
= 'diff')
146 shutil
.rmtree('build')
147 compile('build', expect
= 'Goodbye from C')
148 assert os
.path
.exists(patch_file
)
150 # Test dup-src's unlinking while we're here
151 compile('build', expect
= 'Goodbye from C')
153 # 'src' contains an error
154 stream
= file('src/main.c', 'w')
155 stream
.write('this is not valid C!')
157 shutil
.rmtree('build')
158 compile('build', expect
= 'Build failed')
159 assert os
.path
.exists('build/build-failure.log')
161 # 'src' does not exist
163 shutil
.rmtree('build')
164 compile('build', expect
= 'Hello from C')
165 assert not os
.path
.exists(patch_file
)
167 # Check we fixed the .pc files...
168 pc_data
= open(os
.path
.join(target_dir
, 'pkgconfig', 'cprog.pc')).read()
169 assert pc_data
== "prefix=${pcfiledir}/..\n", `pc_data`
171 # Check we removed the bad .la files...
172 assert not os
.path
.exists(os
.path
.join(target_dir
, 'lib', 'bad.la')) # libtool - bad
173 assert os
.path
.exists(os
.path
.join(target_dir
, 'lib', 'good.la')) # Ends in .la, but not a libtool archive
174 assert os
.path
.exists(os
.path
.join(target_dir
, 'lib', 'nice.ok')) # Doesn't end in .la
176 def testInlcudeDeps(self
):
177 compile('setup', hello_uri
, self
.hello_dir
, expect
= 'Created directory')
178 os
.chdir(self
.hello_dir
)
179 os
.unlink('0compile.properties')
180 compile('setup', hello_uri
, '.')
181 compile('include-deps', expect
= 'dependencies to')
182 compile('include-deps', expect
= 'Copied 0 depend')
185 compile('setup', hello_selections
, self
.hello_dir
,
186 expect
= 'Created directory')
187 compile('setup', hello_selections
, self
.hello_dir
,
188 expect
= "Directory '")
189 compile('setup', hello_selections
, '.', 'foo',
191 os
.chdir(self
.hello_dir
)
192 compile('setup', expect
= "Selections are fixed")
194 def testReportBug(self
):
195 broken_src
= os
.path
.join(self
.hello_dir
, "broken.xml")
196 os
.mkdir(self
.hello_dir
)
197 shutil
.copy(local_hello_path
, broken_src
)
198 os
.chdir(self
.hello_dir
)
199 compile('setup', broken_src
, '.')
200 compile('build', expect
= 'Build failed with exit code')
201 compile('report-bug', expect
= "http://sourceforge.net")
203 env
= support
.BuildEnv()
204 os
.unlink(os
.path
.join(env
.metadir
, "build-environment.xml"))
205 compile('report-bug', expect
= "file+not+found")
207 suite
= unittest
.makeSuite(TestCompile
)
208 if __name__
== '__main__':
209 sys
.argv
.append('-v')