Don't print a stack trace if the user exits with CTRL-C.
[0publish.git] / tests / testvalidator.py
blob5799143ef940f92e7e3e3b0e3b24bccc6e6c4216
1 #!/usr/bin/env python2.4
2 import sys, logging
3 from StringIO import StringIO
4 from zeroinstall.injector.reader import InvalidInterface
5 import unittest
7 sys.path.insert(0, '..')
9 import validator
11 header = """<?xml version="1.0" ?>
12 <?xml-stylesheet type='text/xsl' href='http://0install.net/2006/stylesheets/interface.xsl'?>
13 <interface xmlns="http://zero-install.sourceforge.net/2004/injector/interface">
14 <name>test</name>
15 <summary>for testing</summary>
16 <description>
17 This is for testing.
18 </description>
19 <homepage>http://0install.net</homepage>
20 <feed-for interface='http://0install.net/2007/tests/just-testing.xml'/>"""
21 footer = """
22 </interface>
23 """
25 root_logger = logging.getLogger()
27 def check(xml, expectWarnings = ""):
28 my_log_stream = StringIO()
29 my_handler = logging.StreamHandler(my_log_stream)
31 root_logger.addHandler(my_handler)
32 old_stderr = sys.stderr
33 try:
34 sys.stderr = my_log_stream
35 validator.check(xml)
36 warnings = my_log_stream.getvalue()
37 finally:
38 sys.stderr = old_stderr
39 root_logger.removeHandler(my_handler)
40 if expectWarnings:
41 assert expectWarnings in warnings, "Expected warning '%s' not in '%s'" % (expectWarnings, warnings)
42 elif warnings:
43 raise Exception("Unexpected warnings '%s'" % warnings)
45 class TestValidator(unittest.TestCase):
46 def testSimple(self):
47 check(header + footer)
49 def testWarnings(self):
50 check(header + "<foo/>" + footer, expectWarnings = "Unknown Zero Install element <foo>")
51 check(header + "<foo:bar xmlns:foo='http://foo'/>" + footer)
52 check(header + "<group foo='bar'/>" + footer, expectWarnings = "Non Zero-Install attributes should be namespaced")
54 def testInvalid(self):
55 try:
56 check(header)
57 except InvalidInterface, ex:
58 assert "Invalid XML" in str(ex), ex
60 try:
61 check(header + "<implementation/>" + footer)
62 except InvalidInterface, ex:
63 assert "Missing 'id' attribute" in str(ex), ex
65 suite = unittest.makeSuite(TestValidator)
66 if __name__ == '__main__':
67 sys.argv.append('-v')
68 unittest.main()