Use 0launch's console-based progress handler (added in 0.44)
[0test.git] / test0test.py
blob429434bce4202769db28793334a9e7e148a99a9e
1 #!/usr/bin/env python
2 import sys, tempfile, os, shutil, tempfile, subprocess
3 from StringIO import StringIO
4 import unittest
5 from zeroinstall.support import ro_rmtree, basedir
6 from zeroinstall.zerostore import Stores
8 stores = Stores()
10 my_dir = os.path.abspath(os.path.dirname(__file__))
11 #sys.path.insert(0, os.path.dirname(my_dir))
12 #import support
13 test_bin = os.path.join(my_dir, '0test')
15 publish_uri = 'http://0install.net/2006/interfaces/0publish' # The program to test
17 if 'DISPLAY' in os.environ:
18 del os.environ['DISPLAY']
20 # Ensure it's cached now, to avoid extra output during the tests
21 if subprocess.call(['0launch', '-c', '--download-only', publish_uri]):
22 raise Exception("Failed to download test program")
24 def test(*args, **kwargs):
25 run(*([test_bin] + list(args)), **kwargs)
27 def run(*args, **kwargs):
28 child = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
29 got, unused = child.communicate()
31 expected = kwargs.get('expect', '')
32 if expected:
33 if expected.lower() not in got.lower():
34 raise Exception("Expected '%s', got '%s'" % (expected, got))
35 elif got:
36 raise Exception("Expected nothing, got '%s'" % got)
38 # Detect accidental network access
39 os.environ['http_proxy'] = 'localhost:1111'
41 for x in ['GNUPGHOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME']:
42 if x in os.environ:
43 del os.environ[x]
44 user_cache_dir = os.environ['XDG_CACHE_DIRS'] = basedir.xdg_cache_home
46 class Test0Test(unittest.TestCase):
47 def setUp(self):
48 os.chdir('/')
50 self.tmpdir = tempfile.mkdtemp(prefix = '0test-test-')
52 os.environ['HOME'] = self.tmpdir
53 reload(basedir)
55 config_dir = basedir.save_config_path('0install.net', 'injector')
56 stream = open(os.path.join(config_dir, 'implementation-dirs'), 'w')
57 for x in stores.stores:
58 stream.write(x.dir + '\n')
59 stream.close()
61 stream = open(os.path.join(config_dir, 'global'), 'w')
62 stream.write('[global]\n'
63 'freshness = -1\n'
64 'help_with_testing = True\n'
65 'network_use = off-line\n')
66 stream.close()
68 def tearDown(self):
69 ro_rmtree(self.tmpdir)
71 def testVersion(self):
72 test('--version', expect = '0test (zero-install)')
73 test('--help', expect = 'Usage: 0test')
75 def test0publish(self):
76 test(publish_uri, expect = 'None failed')
78 suite = unittest.makeSuite(Test0Test)
79 if __name__ == '__main__':
80 sys.argv.append('-v')
81 unittest.main()