Allow passing an app name as the first argument
[0test.git] / test0test.py
blob5a5f746d51d775485c49307c7a34641f96ef41fb
1 #!/usr/bin/env python
2 import tempfile, os, subprocess, sys
3 import unittest
4 from zeroinstall.support import ro_rmtree, basedir
5 from zeroinstall.zerostore import Stores
7 stores = Stores()
9 my_dir = os.path.abspath(os.path.dirname(__file__))
10 test_bin = os.path.join(my_dir, '0test')
12 # Python 2.6 doesn't have this
13 def check_output(cmd):
14 process = subprocess.Popen(cmd, stdout = subprocess.PIPE)
15 stdout, stderr = process.communicate()
16 assert not stderr, stderr
17 status = process.poll()
18 assert status == 0, status
19 return stdout
21 # Get the version of 0publish to be tested...
22 publish_version_line = check_output(['0publish', '--version']).decode('utf-8').split('\n', 1)[0]
23 publish_version = publish_version_line.rsplit(' ', 1)[1]
24 publish_uri = 'http://0install.net/2006/interfaces/0publish'
26 if 'DISPLAY' in os.environ:
27 del os.environ['DISPLAY']
29 def test(*args, **kwargs):
30 run(*([sys.executable, test_bin] + list(args)), **kwargs)
32 def run(*args, **kwargs):
33 child = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
34 got, unused = child.communicate()
36 expected = kwargs.get('expect', '')
37 if expected:
38 if expected.lower() not in got.lower():
39 raise Exception("Expected '%s', got '%s'" % (expected, got))
40 elif got:
41 raise Exception("Expected nothing, got '%s'" % got)
43 # Detect accidental network access
44 os.environ['http_proxy'] = 'localhost:1111'
46 for x in ['GNUPGHOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME']:
47 if x in os.environ:
48 del os.environ[x]
49 os.environ['XDG_CACHE_DIRS'] = basedir.xdg_cache_home
51 class Test0Test(unittest.TestCase):
52 def setUp(self):
53 os.chdir('/')
55 self.tmpdir = tempfile.mkdtemp(prefix = '0test-test-')
57 os.environ['HOME'] = self.tmpdir
58 reload(basedir)
60 config_dir = basedir.save_config_path('0install.net', 'injector')
61 stream = open(os.path.join(config_dir, 'implementation-dirs'), 'w')
62 for x in stores.stores:
63 stream.write(x.dir + '\n')
64 stream.close()
66 stream = open(os.path.join(config_dir, 'global'), 'w')
67 stream.write('[global]\n'
68 'freshness = -1\n'
69 'help_with_testing = True\n'
70 'network_use = off-line\n')
71 stream.close()
73 def tearDown(self):
74 ro_rmtree(self.tmpdir)
76 def testVersion(self):
77 test('--version', expect = '0test (zero-install)')
78 test('--help', expect = 'Usage: 0test')
80 def test0publish(self):
81 test(publish_uri, publish_version, expect = 'None failed')
83 def testTestCommand(self):
84 test('-t', 'echo $*', publish_uri, publish_version, expect = '/0publish')
86 def testCommand(self):
87 test('-c', 'run', publish_uri, publish_version, '--', '--version', expect = 'ABSOLUTELY NO WARRANTY')
88 test('-c', '', publish_uri, publish_version, '--', '--version', expect = 'No <command> requested and no test command either!')
89 test('-c', '', '-t', 'stat $1', publish_uri, publish_version, expect = 'directory')
91 suite = unittest.makeSuite(Test0Test)
92 if __name__ == '__main__':
93 unittest.main()