From 6544e13ffbe892c0e25e59f00f0b9d4e0ed915e7 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Sat, 3 Aug 2019 17:43:02 +0545 Subject: [PATCH] Generate valid json if multiple log entries are generated on same line --- tests/test_json_generator.py | 56 +++++++++++++++++++++++++++++++++++ zippy/utils/json_generator.py | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/test_json_generator.py b/tests/test_json_generator.py index 20561e0..dcc6a28 100644 --- a/tests/test_json_generator.py +++ b/tests/test_json_generator.py @@ -55,6 +55,24 @@ def single_log_file(): return file +@pytest.fixture +def log_on_same_line(): + """Create a log file with multiple entries on same line.""" + file = tempfile.NamedTemporaryFile(mode="w") + file.write('{"message": 1}{"message": 2}{"message": 3}') + file.seek(0) + return file + + +@pytest.fixture +def log_comma_separation_same_line(): + """Create a log file with multiple entries on same line, comma separated.""" + file = tempfile.NamedTemporaryFile(mode="w") + file.write('{"message": 1},{"message": 2},{"message": 3}') + file.seek(0) + return file + + @pytest.fixture def empty_log_file(): """Create a log file without any entries.""" @@ -110,3 +128,41 @@ def test_single_logfile(single_log_file): else: assert len(json_log["logs"]) == 1 assert json_log["logs"][0]["from"] == "test0@localhost.org" + + +def test_logfile_log_entries_on_same_line(log_on_same_line): + """Test with single log.""" + output = "" + with open(log_on_same_line.name) as file: + for line in wrap_json_output(file): + output += line + + try: + json_log = json.loads(output) + except ValueError: + assert False, "Invalid json generated." + else: + assert len(json_log["logs"]) == 3 + assert json_log["logs"][0]["message"] == 1 + assert json_log["logs"][1]["message"] == 2 + assert json_log["logs"][2]["message"] == 3 + + +def test_logfile_log_entries_on_same_line_with_comma_separation( + log_comma_separation_same_line +): + """Test with single log.""" + output = "" + with open(log_comma_separation_same_line.name) as file: + for line in wrap_json_output(file): + output += line + + try: + json_log = json.loads(output) + except ValueError: + assert False, "Invalid json generated." + else: + assert len(json_log["logs"]) == 3 + assert json_log["logs"][0]["message"] == 1 + assert json_log["logs"][1]["message"] == 2 + assert json_log["logs"][2]["message"] == 3 diff --git a/zippy/utils/json_generator.py b/zippy/utils/json_generator.py index 4ed6f23..0ca2a0a 100644 --- a/zippy/utils/json_generator.py +++ b/zippy/utils/json_generator.py @@ -7,5 +7,5 @@ def wrap_json_output(input_file): json_ending = "]}" yield json_starting - yield ",".join(input_file.readlines()) + yield ",".join(input_file.readlines()).replace("}{", "},{") yield json_ending