2 import sys
, tempfile
, os
, shutil
, tempfile
, subprocess
3 from StringIO
import StringIO
5 from zeroinstall
.support
import ro_rmtree
, basedir
6 from zeroinstall
.zerostore
import Stores
10 my_dir
= os
.path
.abspath(os
.path
.dirname(__file__
))
11 #sys.path.insert(0, os.path.dirname(my_dir))
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', '')
33 if expected
.lower() not in got
.lower():
34 raise Exception("Expected '%s', got '%s'" % (expected
, 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']:
44 user_cache_dir
= os
.environ
['XDG_CACHE_DIRS'] = basedir
.xdg_cache_home
46 class Test0Test(unittest
.TestCase
):
50 self
.tmpdir
= tempfile
.mkdtemp(prefix
= '0test-test-')
52 os
.environ
['HOME'] = self
.tmpdir
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')
61 stream
= open(os
.path
.join(config_dir
, 'global'), 'w')
62 stream
.write('[global]\n'
64 'help_with_testing = True\n'
65 'network_use = off-line\n')
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__':