542f091258ef7464d4f885f13b5c14336aaec494
[0release.git] / tests / testrelease.py
blob542f091258ef7464d4f885f13b5c14336aaec494
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, '..')
11 os.environ['http_proxy'] = 'localhost:1111' # Prevent accidental network access
13 import support
15 mydir = os.path.realpath(os.path.dirname(__file__))
16 release_feed = mydir + '/../0release.xml'
17 test_repo = mydir + '/test-repo.tgz'
18 test_repo_actions = mydir + '/test-repo-actions.tgz'
19 test_repo_c = mydir + '/c-prog.tgz'
20 test_gpg = mydir + '/gpg.tgz'
22 test_config = """
23 [global]
24 freshness = 0
25 auto_approve_keys = True
26 help_with_testing = True
27 network_use = full
28 """
30 def call_with_output_suppressed(cmd, stdin, expect_failure = False, **kwargs):
31 #cmd = [cmd[0], '-v'] + cmd[1:]
32 if stdin:
33 child = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, **kwargs)
34 else:
35 child = subprocess.Popen(cmd, stdout = subprocess.PIPE, **kwargs)
36 stdout, stderr = child.communicate(stdin)
37 if (child.returncode != 0) == expect_failure:
38 return stdout, stderr
39 #print stdout, stderr
40 raise Exception("Return code %d from %s\nstdout: %s\nstderr: %s" % (child.returncode, cmd, stdout, stderr))
42 def make_releases_dir(src_feed = '../hello/HelloWorld.xml', auto_upload = False):
43 os.chdir('releases')
44 call_with_output_suppressed(['0release', src_feed], None)
45 assert os.path.isfile('make-release')
47 lines = file('make-release').readlines()
48 lines[lines.index('ARCHIVE_DIR_PUBLIC_URL=\n')] = 'ARCHIVE_DIR_PUBLIC_URL=http://TESTING/releases/\\$RELEASE_VERSION\n'
50 # Force us to test against this version of 0release
51 for i, line in enumerate(lines):
52 if line.startswith('exec 0launch http://0install.net/2007/interfaces/0release.xml --release'):
53 lines[i] = '0release --release ' + line.split('--release ', 1)[1]
54 break
55 else:
56 assert 0
58 if auto_upload:
59 os.mkdir('archives')
60 lines[lines.index('ARCHIVE_UPLOAD_COMMAND=\n')] = 'ARCHIVE_UPLOAD_COMMAND=\'cp "$@" ../archives/\'\n'
62 s = file('make-release', 'w')
63 s.write(''.join(lines))
64 s.close()
66 class TestRelease(unittest.TestCase):
67 def setUp(self):
68 self.tmp = tempfile.mkdtemp(prefix = '0release-')
69 os.chdir(self.tmp)
70 support.check_call(['tar', 'xzf', test_gpg])
71 os.mkdir('releases')
72 os.environ['GNUPGHOME'] = self.tmp + '/gpg'
73 os.chmod(os.environ['GNUPGHOME'], 0700)
75 config_dir = os.path.join(self.tmp, 'config')
76 injector_config = os.path.join(config_dir, '0install.net', 'injector')
77 os.makedirs(injector_config)
78 s = open(os.path.join(injector_config, 'global'), 'w')
79 s.write(test_config)
80 s.close()
82 if 'ZEROINSTALL_PORTABLE_BASE' in os.environ:
83 del os.environ['ZEROINSTALL_PORTABLE_BASE']
84 os.environ['XDG_CONFIG_HOME'] = config_dir
85 imp.reload(basedir)
86 assert basedir.xdg_config_home == config_dir
88 def tearDown(self):
89 os.chdir(mydir)
90 ro_rmtree(self.tmp)
92 def testSimple(self):
93 support.check_call(['tar', 'xzf', test_repo])
94 make_releases_dir()
96 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\n\n')
98 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\nY\n\n')
100 assert 'Prints "Hello World"' in file('0.1/changelog-0.1').read()
101 assert 'Prints "Hello World"' not in file('0.2/changelog-0.2').read()
103 def testUncommitted(self):
104 support.check_call(['tar', 'xzf', test_repo_actions])
105 make_releases_dir()
107 unused, stderr = call_with_output_suppressed(['./make-release', '-k', 'Testing'], None,
108 expect_failure = True, stderr = subprocess.PIPE)
109 assert "Uncommitted changes!" in stderr
111 def testActions(self):
112 support.check_call(['tar', 'xzf', test_repo_actions])
113 os.chdir('hello')
114 support.check_call(['git', 'commit', '-a', '-m', 'Added release instructions'], stdout = subprocess.PIPE)
115 os.chdir('..')
116 make_releases_dir()
118 assert "version = '0.2'\n" not in file('../hello/hello.py').read()
120 child = subprocess.Popen(['./make-release', '-k', 'Testing'], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
121 unused, unused = child.communicate('\nP\n\n')
122 assert child.returncode == 0
124 assert "version = '0.2'\n" in file('../hello/hello.py').read()
126 def testBinaryRelease(self):
127 support.check_call(['tar', 'xzf', test_repo_c])
128 make_releases_dir(src_feed = '../c-prog/c-prog.xml', auto_upload = True)
130 call_with_output_suppressed(['./make-release', '-k', 'Testing', '--builders=host'], '\nP\n\n')
132 feed = model.ZeroInstallFeed(qdom.parse(file('HelloWorld-in-C.xml')))
134 assert len(feed.implementations) == 2
135 src_impl, = [x for x in feed.implementations.values() if x.arch == '*-src']
136 host_impl, = [x for x in feed.implementations.values() if x.arch != '*-src']
138 assert src_impl.main == None
139 assert host_impl.main == 'hello'
141 archives = os.listdir('archives')
142 assert os.path.basename(src_impl.download_sources[0].url) in archives
144 host_download = host_impl.download_sources[0]
145 self.assertEqual('http://TESTING/releases/1.1/helloworld-in-c-linux-x86_64-1.1.tar.bz2',
146 host_download.url)
147 host_archive = os.path.basename(host_download.url)
148 assert host_archive in archives
149 support.check_call(['tar', 'xjf', os.path.join('archives', host_archive)])
150 c = subprocess.Popen(['0launch', os.path.join(host_download.extract, '0install', 'feed.xml')], stdout = subprocess.PIPE)
151 output, _ = c.communicate()
153 self.assertEquals("Hello from C! (version 1.1)\n", output)
156 suite = unittest.makeSuite(TestRelease)
157 if __name__ == '__main__':
158 unittest.main()