Added %nonlocal version specifier
[0test.git] / test0test.py
blobba77871fc0bdf2d524d924f9be65d1a4a373350f
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
16 publish_version = '0.18'
18 if 'DISPLAY' in os.environ:
19 del os.environ['DISPLAY']
21 # Ensure it's cached now, to avoid extra output during the tests
22 if subprocess.call(['0launch', '-c', '--download-only', '--not-before=' + publish_version, '--before=' + publish_version + '-post', publish_uri]):
23 raise Exception("Failed to download test program")
25 def test(*args, **kwargs):
26 run(*([test_bin] + list(args)), **kwargs)
28 def run(*args, **kwargs):
29 child = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
30 got, unused = child.communicate()
32 expected = kwargs.get('expect', '')
33 if expected:
34 if expected.lower() not in got.lower():
35 raise Exception("Expected '%s', got '%s'" % (expected, got))
36 elif got:
37 raise Exception("Expected nothing, got '%s'" % got)
39 # Detect accidental network access
40 os.environ['http_proxy'] = 'localhost:1111'
42 for x in ['GNUPGHOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME']:
43 if x in os.environ:
44 del os.environ[x]
45 user_cache_dir = os.environ['XDG_CACHE_DIRS'] = basedir.xdg_cache_home
47 class Test0Test(unittest.TestCase):
48 def setUp(self):
49 os.chdir('/')
51 self.tmpdir = tempfile.mkdtemp(prefix = '0test-test-')
53 os.environ['HOME'] = self.tmpdir
54 reload(basedir)
56 config_dir = basedir.save_config_path('0install.net', 'injector')
57 stream = open(os.path.join(config_dir, 'implementation-dirs'), 'w')
58 for x in stores.stores:
59 stream.write(x.dir + '\n')
60 stream.close()
62 stream = open(os.path.join(config_dir, 'global'), 'w')
63 stream.write('[global]\n'
64 'freshness = -1\n'
65 'help_with_testing = True\n'
66 'network_use = off-line\n')
67 stream.close()
69 def tearDown(self):
70 ro_rmtree(self.tmpdir)
72 def testVersion(self):
73 test('--version', expect = '0test (zero-install)')
74 test('--help', expect = 'Usage: 0test')
76 def test0publish(self):
77 test(publish_uri, publish_version, expect = 'None failed')
79 def testTestCommand(self):
80 test('-t', 'echo $*', publish_uri, publish_version, expect = '/0publish')
82 def testCommand(self):
83 test('-c', 'run', publish_uri, publish_version, '--', '--version', expect = 'ABSOLUTELY NO WARRANTY')
84 test('-c', '', publish_uri, publish_version, '--', '--version', expect = 'No <command> requested and no test command either!')
85 test('-c', '', '-t', 'stat $1', publish_uri, publish_version, expect = 'directory')
87 suite = unittest.makeSuite(Test0Test)
88 if __name__ == '__main__':
89 unittest.main()