OLD | NEW |
1 # -*- encoding: utf-8 -*- | 1 # -*- encoding: utf-8 -*- |
2 | 2 |
3 import json | 3 import json |
4 import logging | 4 import logging |
5 import os | 5 import os |
6 import StringIO | 6 import StringIO |
7 from argparse import ArgumentTypeError | 7 from argparse import ArgumentTypeError |
8 from contextlib import closing | 8 from contextlib import closing |
9 | 9 |
10 from twisted.internet.defer import inlineCallbacks, returnValue | 10 from twisted.internet.defer import inlineCallbacks, returnValue |
11 import yaml | |
12 | 11 |
13 from juju.hooks.cli import ( | 12 from juju.hooks.cli import ( |
14 CommandLineClient, parse_log_level, parse_port_protocol) | 13 CommandLineClient, parse_log_level, parse_port_protocol) |
15 from juju.lib.testing import TestCase | 14 from juju.lib.testing import TestCase |
16 | 15 |
17 | 16 |
18 class NoopCli(CommandLineClient): | 17 class NoopCli(CommandLineClient): |
19 """ | 18 """ |
20 do nothing client used to test options | 19 do nothing client used to test options |
21 """ | 20 """ |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 def test_format_smart_v1(self): | 415 def test_format_smart_v1(self): |
417 """Verify legacy format v1 works""" | 416 """Verify legacy format v1 works""" |
418 self.change_environment(_JUJU_CHARM_FORMAT="1") | 417 self.change_environment(_JUJU_CHARM_FORMAT="1") |
419 self.assert_format_smart_v1() | 418 self.assert_format_smart_v1() |
420 | 419 |
421 def assert_smart_output(self, sample, formatted): | 420 def assert_smart_output(self, sample, formatted): |
422 cli = CommandLineClient() | 421 cli = CommandLineClient() |
423 with closing(StringIO.StringIO()) as output: | 422 with closing(StringIO.StringIO()) as output: |
424 cli.format_smart(sample, output) | 423 cli.format_smart(sample, output) |
425 self.assertEqual(output.getvalue(), formatted) | 424 self.assertEqual(output.getvalue(), formatted) |
426 self.assertEqual(sample, yaml.safe_load(output.getvalue())) | |
427 | 425 |
428 def test_format_smart_v2(self): | 426 def test_format_smart_v2(self): |
429 """Verifies smart format v2 writes correct YAML""" | 427 """Verifies smart format v2 writes raw strings properly""" |
430 self.change_environment(_JUJU_CHARM_FORMAT="2") | 428 self.change_environment(_JUJU_CHARM_FORMAT="2") |
431 | 429 |
432 # For each case, verify actual output serialization along with | 430 # For each case, verify actual output serialization along with |
433 # roundtripping through YAML | 431 # roundtripping through YAML |
434 self.assert_smart_output(None, "") # No newline in output for None | 432 self.assert_smart_output(None, "") # No newline in output for None |
435 self.assert_smart_output("", "''\n") | 433 self.assert_smart_output("", "") |
436 self.assert_smart_output("A string", "A string\n") | 434 self.assert_smart_output("A string", "A string") |
437 # Note: YAML uses b64 encoding for byte strings tagged by !!binary | |
438 self.assert_smart_output( | 435 self.assert_smart_output( |
439 "High bytes: \xCA\xFE", | 436 "High bytes: \xCA\xFE", "High bytes: \xca\xfe") |
440 "!!binary |\n SGlnaCBieXRlczogyv4=\n") | 437 self.assert_smart_output("中文", "\xe4\xb8\xad\xe6\x96\x87") |
441 self.assert_smart_output(u"", "''\n") | |
442 self.assert_smart_output( | |
443 u"A unicode string (but really ascii)", | |
444 "A unicode string (but really ascii)\n") | |
445 # Any non-ascii Unicode will use UTF-8 encoding | |
446 self.assert_smart_output(u"中文", "\xe4\xb8\xad\xe6\x96\x87\n") | |
447 self.assert_smart_output({}, "{}\n") | |
448 self.assert_smart_output( | 438 self.assert_smart_output( |
449 {u"public-address": u"ec2-1-2-3-4.compute-1.amazonaws.com", | 439 {u"public-address": u"ec2-1-2-3-4.compute-1.amazonaws.com", |
450 u"foo": u"bar", | 440 u"foo": u"bar", |
451 u"configured": True}, | 441 u"configured": True}, |
452 ("configured: true\n" | 442 ("configured: true\n" |
453 "foo: bar\n" | 443 "foo: bar\n" |
454 "public-address: ec2-1-2-3-4.compute-1.amazonaws.com\n")) | 444 "public-address: ec2-1-2-3-4.compute-1.amazonaws.com")) |
455 self.assert_smart_output(False, "false\n") | |
456 self.assert_smart_output(True, "true\n") | |
457 self.assert_smart_output(0.0, "0.0\n") | |
458 self.assert_smart_output(3.14159, "3.14159\n") | |
459 self.assert_smart_output(6.02214178e23, "6.02214178e+23\n") | |
460 self.assert_smart_output(0, "0\n") | |
461 self.assert_smart_output(42, "42\n") | |
OLD | NEW |