Support use of <command name='test'>
[0release.git] / tests / testrelease.py
blobebb6b9e5da4769adbe3877508c2840bce959b047
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 def call_with_output_suppressed(cmd, stdin, expect_failure = False, **kwargs):
20 #cmd = [cmd[0], '-v'] + cmd[1:]
21 if stdin:
22 child = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, **kwargs)
23 else:
24 child = subprocess.Popen(cmd, stdout = subprocess.PIPE, **kwargs)
25 stdout, stderr = child.communicate(stdin)
26 if (child.returncode != 0) == expect_failure:
27 return stdout, stderr
28 #print stdout, stderr
29 raise Exception("Return code %d from %s\nstdout: %s\nstderr: %s" % (child.returncode, cmd, stdout, stderr))
31 def make_releases_dir(src_feed = '../hello/HelloWorld.xml', auto_upload = False):
32 os.chdir('releases')
33 call_with_output_suppressed(['0launch', release_feed, src_feed], None)
34 assert os.path.isfile('make-release')
36 lines = file('make-release').readlines()
37 lines[lines.index('ARCHIVE_DIR_PUBLIC_URL=\n')] = 'ARCHIVE_DIR_PUBLIC_URL=http://TESTING/releases\n'
39 if auto_upload:
40 os.mkdir('archives')
41 lines[lines.index('ARCHIVE_UPLOAD_COMMAND=\n')] = 'ARCHIVE_UPLOAD_COMMAND=\'cp "$@" ../archives/\'\n'
43 s = file('make-release', 'w')
44 s.write(''.join(lines))
45 s.close()
47 class TestRelease(unittest.TestCase):
48 def setUp(self):
49 self.tmp = tempfile.mkdtemp(prefix = '0release-')
50 os.chdir(self.tmp)
51 support.check_call(['tar', 'xzf', test_gpg])
52 os.mkdir('releases')
53 os.environ['GNUPGHOME'] = self.tmp + '/gpg'
54 os.chmod(os.environ['GNUPGHOME'], 0700)
56 def tearDown(self):
57 os.chdir(mydir)
58 shutil.rmtree(self.tmp)
60 def testSimple(self):
61 support.check_call(['tar', 'xzf', test_repo])
62 make_releases_dir()
64 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\n\n')
66 call_with_output_suppressed(['./make-release', '-k', 'Testing'], '\nP\nY\n\n')
68 assert 'Prints "Hello World"' in file('0.1/changelog-0.1').read()
69 assert 'Prints "Hello World"' not in file('0.2/changelog-0.2').read()
71 def testUncommitted(self):
72 support.check_call(['tar', 'xzf', test_repo_actions])
73 make_releases_dir()
75 unused, stderr = call_with_output_suppressed(['./make-release', '-k', 'Testing'], None,
76 expect_failure = True, stderr = subprocess.PIPE)
77 assert "Uncommitted changes!" in stderr
79 def testActions(self):
80 support.check_call(['tar', 'xzf', test_repo_actions])
81 os.chdir('hello')
82 support.check_call(['git', 'commit', '-a', '-m', 'Added release instructions'], stdout = subprocess.PIPE)
83 os.chdir('..')
84 make_releases_dir()
86 assert "version = '0.2'\n" not in file('../hello/hello.py').read()
88 child = subprocess.Popen(['./make-release', '-k', 'Testing'], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
89 unused, unused = child.communicate('\nP\n\n')
90 assert child.returncode == 0
92 assert "version = '0.2'\n" in file('../hello/hello.py').read()
94 def testBinaryRelease(self):
95 support.check_call(['tar', 'xzf', test_repo_c])
96 make_releases_dir(src_feed = '../c-prog/c-prog.xml', auto_upload = True)
98 call_with_output_suppressed(['./make-release', '-k', 'Testing', '--builders=host'], '\nP\n\n')
100 feed = model.ZeroInstallFeed(qdom.parse(file('HelloWorld-in-C.xml')))
102 assert len(feed.implementations) == 2
103 src_impl, = [x for x in feed.implementations.values() if x.arch == '*-src']
104 host_impl, = [x for x in feed.implementations.values() if x.arch != '*-src']
106 assert src_impl.main == None
107 assert host_impl.main == 'hello'
109 archives = os.listdir('archives')
110 assert os.path.basename(src_impl.download_sources[0].url) in archives
112 host_download = host_impl.download_sources[0]
113 host_archive = os.path.basename(host_download.url)
114 assert host_archive in archives
115 support.check_call(['tar', 'xjf', os.path.join('archives', host_archive)])
116 c = subprocess.Popen(['0launch', os.path.join(host_download.extract, '0install', 'helloworld-in-c-1.1.xml')], stdout = subprocess.PIPE)
117 output, _ = c.communicate()
119 self.assertEquals("Hello from C! (version 1.1)\n", output)
122 suite = unittest.makeSuite(TestRelease)
123 if __name__ == '__main__':
124 unittest.main()