示例#1
0
 def test_file(self):
     """
     ``eliot-tree`` can read and render JSON messages from a file on the
     command line.
     """
     with NamedTemporaryFile() as f:
         f.write(dump_json_bytes(message_task))
         f.flush()
         self.assertEqual(check_output(["eliot-tree", f.name]),
                          rendered_message_task)
示例#2
0
 def test_file(self):
     """
     ``eliot-tree`` can read and render JSON messages from a file on the
     command line.
     """
     f = NamedTemporaryFile()
     f.write(dump_json_bytes(message_task))
     f.flush()
     self.assertEqual(check_output(["eliot-tree", f.name]),
                      rendered_message_task)
示例#3
0
 def test_stdin(self):
     """
     ``eliot-tree`` can read and render JSON messages from stdin when no
     arguments are given.
     """
     with NamedTemporaryFile() as f:
         f.write(dump_json_bytes(message_task))
         f.flush()
         f.seek(0, 0)
         self.assertEqual(check_output(["eliot-tree"], stdin=f.read()),
                          rendered_message_task)
示例#4
0
 def test_stdin(self):
     """
     ``eliot-tree`` can read and render JSON messages from stdin when no
     arguments are given.
     """
     f = NamedTemporaryFile()
     f.write(dump_json_bytes(message_task))
     f.flush()
     f.seek(0, 0)
     self.assertEqual(check_output(["eliot-tree"], stdin=f),
                      rendered_message_task)
示例#5
0
    def test_binary(self):
        """
        Binary results are left as-is.
        """
        def dump_binary(obj):
            result = json.dumps(obj)
            if isinstance(result, text_type):
                return result.encode('utf-8')
            return result

        result = dump_json_bytes({'a': 42}, dumps=dump_binary)
        self.assertThat(result, IsInstance(binary_type))
        self.assertThat(result, Equals(b'{"a": 42}'))
示例#6
0
    def test_text(self):
        """
        Text results are encoded as UTF-8.
        """
        def dump_text(obj):
            result = json.dumps(obj)
            if isinstance(result, binary_type):
                return result.decode('utf-8')
            return result

        result = dump_json_bytes({'a': 42}, dumps=dump_text)
        self.assertThat(result, IsInstance(binary_type))
        self.assertThat(result, Equals(b'{"a": 42}'))
示例#7
0
 def test_eliot_parse_error(self):
     """
     ``eliot-tree`` displays an error containing the original file name,
     line number and offending task in the event that parsing the message
     dict fails.
     """
     with NamedTemporaryFile() as f:
         f.write(dump_json_bytes(missing_uuid_task) + b'\n')
         f.flush()
         with self.assertRaises(CalledProcessError) as m:
             check_output(['eliot-tree', f.name])
         lines = m.exception.output.stderr.splitlines()
         first_line = lines[0].decode('utf-8')
         self.assertIn('Eliot message parse error', first_line)
         self.assertIn(f.name, first_line)
         self.assertIn('line 1', first_line)
示例#8
0
 def test_json_parse_error(self):
     """
     ``eliot-tree`` displays an error containing the file name, line number
     and offending line in the event that JSON parsing fails.
     """
     with NamedTemporaryFile() as f:
         f.write(dump_json_bytes(message_task) + b'\n')
         f.write(b'totally not valid JSON {')
         f.flush()
         with self.assertRaises(CalledProcessError) as m:
             check_output(['eliot-tree', '--color=never', f.name])
         lines = m.exception.output.stderr.splitlines()
         first_line = lines[0].decode('utf-8')
         second_line = lines[1].decode('utf-8')
         self.assertIn('JSON parse error', first_line)
         self.assertIn(f.name, first_line)
         self.assertIn('line 2', first_line)
         self.assertEqual('totally not valid JSON {', second_line)
示例#9
0
    def test_binary(self):
        """
        Binary results are left as-is.
        """
        def dump_binary(obj):
            result = json.dumps(obj)
            if isinstance(result, text_type):
                return result.encode('utf-8')
            return result

        result = dump_json_bytes(
            {'a': 42}, dumps=dump_binary)
        self.assertThat(
            result,
            IsInstance(binary_type))
        self.assertThat(
            result,
            Equals(b'{"a": 42}'))
示例#10
0
    def test_text(self):
        """
        Text results are encoded as UTF-8.
        """
        def dump_text(obj):
            result = json.dumps(obj)
            if isinstance(result, binary_type):
                return result.decode('utf-8')
            return result

        result = dump_json_bytes(
            {'a': 42}, dumps=dump_text)
        self.assertThat(
            result,
            IsInstance(binary_type))
        self.assertThat(
            result,
            Equals(b'{"a": 42}'))