Force unit-tests to test with the same version of 0release
[0release.git] / tests / testrelease.py
blob5c89651c566a5eaf78c2926a2f95e0ec5e9816fe
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
5 import unittest
6 from zeroinstall.injector import model, qdom
8 sys.path.insert(0, '..')
10 import support
12 mydir = os.path.realpath(os.path.dirname(__file__))
13 release_feed = mydir + '/../0release.xml'
14 test_repo = mydir + '/test-repo.tgz'
15 test_repo_actions = mydir + '/test-repo-actions.tgz'
16 test_repo_c = mydir + '/c-prog.tgz'
17 test_gpg = mydir + '/gpg.tgz'
19 test_config = """
20 [global]
21 freshness = 0
22 auto_approve_keys = True
23 help_with_testing = True
24 network_use = full
25 """
27 def call_with_output_suppressed(cmd, stdin, expect_failure = False, **kwargs):
28 #cmd = [cmd[0], '-v'] + cmd[1:]
29 if stdin:
30 child = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, **kwargs)
31 else:
32 child = subprocess.Popen(cmd, stdout = subprocess.PIPE, **kwargs)
33 stdout, stderr = child.communicate(stdin)
34 if (child.returncode != 0) == expect_failure:
35 return stdout, stderr
36 #print stdout, stderr
37 raise Exception("Return code %d from %s\nstdout: %s\nstderr: %s" % (child.returncode, cmd, stdout, stderr))
39 def make_releases_dir(src_feed = '../hello/HelloWorld.xml', auto_upload = False):
40 os.chdir('releases')
41 call_with_output_suppressed(['0launch', release_feed, src_feed], None)
42 assert os.path.isfile('make-release')
44 lines = file('make-release').readlines()
45 lines[lines.index('ARCHIVE_DIR_PUBLIC_URL=\n')] = 'ARCHIVE_DIR_PUBLIC_URL=http://TESTING/releases\n'
47 # Force us to test against this version of 0release
48 for i, line in enumerate(lines):
49 if line.startswith('exec 0launch http://0install.net/2007/interfaces/0release.xml --release'):
50 lines[i] = '0release --release ' + line.split('--release ', 1)[1]
51 break
52 else:
53 assert 0
55 if auto_upload:
56 os.mkdir('archives')
57 lines[lines.index('ARCHIVE_UPLOAD_COMMAND=\n')] = 'ARCHIVE_UPLOAD_COMMAND=\'cp "$@" ../archives/\'\n'
59 s = file('make-release', 'w')
60 s.write(''.join(lines))
61 s.close()
63 class TestRelease(unittest.TestCase):
64 def setUp(self):
65 self.tmp = tempfile.mkdtemp(prefix = '0release-')
66 os.chdir(self.tmp)
67 support.check_call(['tar', 'xzf', test_gpg])
68 os.mkdir('releases')
69 os.environ['GNUPGHOME'] = self.tmp + '/gpg'
70 os.chmod(os.environ['GNUPGHOME'], 0700)
72 config_dir = os.path.join(self.tmp, 'config')
73 injector_config = os.path.join(config_dir, '0install.net', 'injector')
74 os.makedirs(injector_config)
75 s = open(os.path.join(injector_config, 'global'), 'w')
76 s.write(test_config)
77 s.close()
78 os.environ['XDG_CONFIG_HOME'] = config_dir
80 def tearDown(self):
81 os.chdir(mydir)
82 shutil.rmtree(self.tmp)
84 def testSimple(self):
85 support.check_call(['tar', 'xzf', test_repo])
86 make_releases_dir()
88 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\n\n')
90 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\nY\n\n')
92 assert 'Prints "Hello World"' in file('0.1/changelog-0.1').read()
93 assert 'Prints "Hello World"' not in file('0.2/changelog-0.2').read()
95 def testUncommitted(self):
96 support.check_call(['tar', 'xzf', test_repo_actions])
97 make_releases_dir()
99 unused, stderr = call_with_output_suppressed(['./make-release', '-k', 'Testing'], None,
100 expect_failure = True, stderr = subprocess.PIPE)
101 assert "Uncommitted changes!" in stderr
103 def testActions(self):
104 support.check_call(['tar', 'xzf', test_repo_actions])
105 os.chdir('hello')
106 support.check_call(['git', 'commit', '-a', '-m', 'Added release instructions'], stdout = subprocess.PIPE)
107 os.chdir('..')
108 make_releases_dir()
110 assert "version = '0.2'\n" not in file('../hello/hello.py').read()
112 child = subprocess.Popen(['./make-release', '-k', 'Testing'], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
113 unused, unused = child.communicate('\nP\n\n')
114 assert child.returncode == 0
116 assert "version = '0.2'\n" in file('../hello/hello.py').read()
118 def testBinaryRelease(self):
119 support.check_call(['tar', 'xzf', test_repo_c])
120 make_releases_dir(src_feed = '../c-prog/c-prog.xml', auto_upload = True)
122 call_with_output_suppressed(['./make-release', '-k', 'Testing', '--builders=host'], '\nP\n\n')
124 feed = model.ZeroInstallFeed(qdom.parse(file('HelloWorld-in-C.xml')))
126 assert len(feed.implementations) == 2
127 src_impl, = [x for x in feed.implementations.values() if x.arch == '*-src']
128 host_impl, = [x for x in feed.implementations.values() if x.arch != '*-src']
130 assert src_impl.main == None
131 assert host_impl.main == 'hello'
133 archives = os.listdir('archives')
134 assert os.path.basename(src_impl.download_sources[0].url) in archives
136 host_download = host_impl.download_sources[0]
137 host_archive = os.path.basename(host_download.url)
138 assert host_archive in archives
139 support.check_call(['tar', 'xjf', os.path.join('archives', host_archive)])
140 c = subprocess.Popen(['0launch', os.path.join(host_download.extract, '0install', 'feed.xml')], stdout = subprocess.PIPE)
141 output, _ = c.communicate()
143 self.assertEquals("Hello from C! (version 1.1)\n", output)
146 suite = unittest.makeSuite(TestRelease)
147 if __name__ == '__main__':
148 unittest.main()