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
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', '')
34 if expected
.lower() not in got
.lower():
35 raise Exception("Expected '%s', got '%s'" % (expected
, 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']:
45 user_cache_dir
= os
.environ
['XDG_CACHE_DIRS'] = basedir
.xdg_cache_home
47 class Test0Test(unittest
.TestCase
):
51 self
.tmpdir
= tempfile
.mkdtemp(prefix
= '0test-test-')
53 os
.environ
['HOME'] = self
.tmpdir
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')
62 stream
= open(os
.path
.join(config_dir
, 'global'), 'w')
63 stream
.write('[global]\n'
65 'help_with_testing = True\n'
66 'network_use = off-line\n')
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__':