Fixed code for compiling binaries
[0release.git] / tests / testrelease.py
blobc7127a61a5e8ab0ac5c325161b2167ca1cb66cc0
1 #!/usr/bin/env python
2 # Copyright (C) 2007, Thomas Leonard
3 # See the README file for details, or visit http://0install.net.
4 import sys, os, shutil, tempfile, subprocess, imp
5 import unittest
6 from zeroinstall.injector import model, qdom, writer
7 from zeroinstall.injector.config import load_config
8 from zeroinstall.support import basedir, ro_rmtree
10 sys.path.insert(0, '..')
12 import support
14 mydir = os.path.realpath(os.path.dirname(__file__))
15 release_feed = mydir + '/../0release.xml'
16 test_repo = mydir + '/test-repo.tgz'
17 test_repo_actions = mydir + '/test-repo-actions.tgz'
18 test_repo_c = mydir + '/c-prog.tgz'
19 test_gpg = mydir + '/gpg.tgz'
21 test_config = """
22 [global]
23 freshness = 0
24 auto_approve_keys = True
25 help_with_testing = True
26 network_use = full
27 """
29 def call_with_output_suppressed(cmd, stdin, expect_failure = False, **kwargs):
30 #cmd = [cmd[0], '-v'] + cmd[1:]
31 if stdin:
32 child = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, **kwargs)
33 else:
34 child = subprocess.Popen(cmd, stdout = subprocess.PIPE, **kwargs)
35 stdout, stderr = child.communicate(stdin)
36 if (child.returncode != 0) == expect_failure:
37 return stdout, stderr
38 #print stdout, stderr
39 raise Exception("Return code %d from %s\nstdout: %s\nstderr: %s" % (child.returncode, cmd, stdout, stderr))
41 def make_releases_dir(src_feed = '../hello/HelloWorld.xml', auto_upload = False):
42 os.chdir('releases')
43 call_with_output_suppressed(['0launch', '-o', release_feed, src_feed], None)
44 assert os.path.isfile('make-release')
46 lines = file('make-release').readlines()
47 lines[lines.index('ARCHIVE_DIR_PUBLIC_URL=\n')] = 'ARCHIVE_DIR_PUBLIC_URL=http://TESTING/releases\n'
49 # Force us to test against this version of 0release
50 for i, line in enumerate(lines):
51 if line.startswith('exec 0launch http://0install.net/2007/interfaces/0release.xml --release'):
52 lines[i] = '0release --release ' + line.split('--release ', 1)[1]
53 break
54 else:
55 assert 0
57 if auto_upload:
58 os.mkdir('archives')
59 lines[lines.index('ARCHIVE_UPLOAD_COMMAND=\n')] = 'ARCHIVE_UPLOAD_COMMAND=\'cp "$@" ../archives/\'\n'
61 s = file('make-release', 'w')
62 s.write(''.join(lines))
63 s.close()
65 class TestRelease(unittest.TestCase):
66 def setUp(self):
67 self.tmp = tempfile.mkdtemp(prefix = '0release-')
68 os.chdir(self.tmp)
69 support.check_call(['tar', 'xzf', test_gpg])
70 os.mkdir('releases')
71 os.environ['GNUPGHOME'] = self.tmp + '/gpg'
72 os.chmod(os.environ['GNUPGHOME'], 0700)
74 config_dir = os.path.join(self.tmp, 'config')
75 injector_config = os.path.join(config_dir, '0install.net', 'injector')
76 os.makedirs(injector_config)
77 s = open(os.path.join(injector_config, 'global'), 'w')
78 s.write(test_config)
79 s.close()
81 if 'ZEROINSTALL_PORTABLE_BASE' in os.environ:
82 del os.environ['ZEROINSTALL_PORTABLE_BASE']
83 os.environ['XDG_CONFIG_HOME'] = config_dir
84 imp.reload(basedir)
85 assert basedir.xdg_config_home == config_dir
87 # Register the local 0release as a feed so we test against that
88 self.config = load_config()
89 iface = self.config.iface_cache.get_interface("http://0install.net/2007/interfaces/0release.xml")
90 iface.extra_feeds = [model.Feed(release_feed, arch = None, user_override = True)]
91 writer.save_interface(iface)
93 def tearDown(self):
94 os.chdir(mydir)
95 ro_rmtree(self.tmp)
97 def testSimple(self):
98 support.check_call(['tar', 'xzf', test_repo])
99 make_releases_dir()
101 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\n\n')
103 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\nY\n\n')
105 assert 'Prints "Hello World"' in file('0.1/changelog-0.1').read()
106 assert 'Prints "Hello World"' not in file('0.2/changelog-0.2').read()
108 def testUncommitted(self):
109 support.check_call(['tar', 'xzf', test_repo_actions])
110 make_releases_dir()
112 unused, stderr = call_with_output_suppressed(['./make-release', '-k', 'Testing'], None,
113 expect_failure = True, stderr = subprocess.PIPE)
114 assert "Uncommitted changes!" in stderr
116 def testActions(self):
117 support.check_call(['tar', 'xzf', test_repo_actions])
118 os.chdir('hello')
119 support.check_call(['git', 'commit', '-a', '-m', 'Added release instructions'], stdout = subprocess.PIPE)
120 os.chdir('..')
121 make_releases_dir()
123 assert "version = '0.2'\n" not in file('../hello/hello.py').read()
125 child = subprocess.Popen(['./make-release', '-k', 'Testing'], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
126 unused, unused = child.communicate('\nP\n\n')
127 assert child.returncode == 0
129 assert "version = '0.2'\n" in file('../hello/hello.py').read()
131 def testBinaryRelease(self):
132 support.check_call(['tar', 'xzf', test_repo_c])
133 make_releases_dir(src_feed = '../c-prog/c-prog.xml', auto_upload = True)
135 call_with_output_suppressed(['./make-release', '-k', 'Testing', '--builders=host'], '\nP\n\n')
137 feed = model.ZeroInstallFeed(qdom.parse(file('HelloWorld-in-C.xml')))
139 assert len(feed.implementations) == 2
140 src_impl, = [x for x in feed.implementations.values() if x.arch == '*-src']
141 host_impl, = [x for x in feed.implementations.values() if x.arch != '*-src']
143 assert src_impl.main == None
144 assert host_impl.main == 'hello'
146 archives = os.listdir('archives')
147 assert os.path.basename(src_impl.download_sources[0].url) in archives
149 host_download = host_impl.download_sources[0]
150 host_archive = os.path.basename(host_download.url)
151 assert host_archive in archives
152 support.check_call(['tar', 'xjf', os.path.join('archives', host_archive)])
153 c = subprocess.Popen(['0launch', os.path.join(host_download.extract, '0install', 'feed.xml')], stdout = subprocess.PIPE)
154 output, _ = c.communicate()
156 self.assertEquals("Hello from C! (version 1.1)\n", output)
159 suite = unittest.makeSuite(TestRelease)
160 if __name__ == '__main__':
161 unittest.main()