import os
import sys
import re
import json

from test_parser_const import CONST_TEST_CONTAINER_INDEX, CONST_TEST_RESULT_FIELD, CONST_TEST_RESULT_ELEMENT_INDEX
from test_parser_model import TestContainer, TestResult
from test_parser_writer import WriteToJson, WriteToHtml, WriteToHtmlDetailed


def CleanupContainerData(str):
    splited = str.split(": ")
    return splited[1].strip()

def AssignContainerData(container, lines):
    container.GroupName = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.GroupName])
    container.GroupBy = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.GroupBy])
    container.GroupFullName = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.GroupFullName])
    container.Duration = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.Duration])
    container.TestsFailed = lines[CONST_TEST_CONTAINER_INDEX.TestsFailed].strip()
    container.TestsSkipped = lines[CONST_TEST_CONTAINER_INDEX.TestsSkipped].strip()
    container.TestsPassed = lines[CONST_TEST_CONTAINER_INDEX.TestsPassed].strip()

def AssignTestResultField(resultObj: TestResult, field, text):
    cleanedText = text.strip()

    if (field == CONST_TEST_RESULT_FIELD.Name):
        resultObj.Name = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.Outcome):
        resultObj.Outcome = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.Duration):
        resultObj.Duration = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.StackTrace):
        resultObj.StackTrace = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.Message):
        resultObj.Message = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.StandardOutput):
        resultObj.StandardOutput = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.StandardError):
        resultObj.StandardError = cleanedText

argsLen = len(sys.argv)

if (argsLen < 1):
    sys.exit('Please enter input txt file path on the last argument')

filePath = sys.argv[argsLen - 1]

filename, fileExtension = os.path.splitext(filePath)

if (fileExtension != ".txt"):
    sys.exit('Please input txt file on the last argument, {} is not txt file'.format(filePath))

inputFile = open(filePath, "r")
lines = inputFile.readlines()
inputFile.close();

container = TestContainer()
AssignContainerData(container, lines)

testLines = lines[7:]

itemQuery = "(Result)(\d+) (\w+):(?:\t|$|\s)((.|\n)*)"

currentTestResultObj = TestResult()
previousField = ""
currentField = ""
currentProcessingText = ""

for line in testLines:
    matches = re.findall(itemQuery, line)
    if (len(matches) > 0):

        matches = matches[0]

        if (len(matches) > 0):
            previousField = currentField
            AssignTestResultField(currentTestResultObj, previousField, currentProcessingText)
            currentProcessingText = ""

            currentField = matches[CONST_TEST_RESULT_ELEMENT_INDEX.Field]
            currentProcessingText += matches[CONST_TEST_RESULT_ELEMENT_INDEX.Content]

            if (previousField == CONST_TEST_RESULT_FIELD.StandardError):
                container.Tests.append(currentTestResultObj)
                currentTestResultObj = TestResult()
    else:
        currentProcessingText += line;

# End of file
if (currentField == CONST_TEST_RESULT_FIELD.StandardError and previousField == CONST_TEST_RESULT_FIELD.StandardOutput):
    container.Tests.append(currentTestResultObj)

WriteToJson(container)
WriteToHtml(container)
WriteToHtmlDetailed(container)
import os
import sys
import re
import json

from test_parser_const import CONST_TEST_CONTAINER_INDEX, CONST_TEST_RESULT_FIELD, CONST_TEST_RESULT_ELEMENT_INDEX
from test_parser_model import TestContainer, TestResult
from test_parser_writer import WriteToJson, WriteToHtml, WriteToHtmlDetailed


def CleanupContainerData(str):
    splited = str.split(": ")
    return splited[1].strip()

def AssignContainerData(container, lines):
    container.GroupName = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.GroupName])
    container.GroupBy = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.GroupBy])
    container.GroupFullName = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.GroupFullName])
    container.Duration = CleanupContainerData(lines[CONST_TEST_CONTAINER_INDEX.Duration])
    container.TestsFailed = lines[CONST_TEST_CONTAINER_INDEX.TestsFailed].strip()
    container.TestsSkipped = lines[CONST_TEST_CONTAINER_INDEX.TestsSkipped].strip()
    container.TestsPassed = lines[CONST_TEST_CONTAINER_INDEX.TestsPassed].strip()

def AssignTestResultField(resultObj: TestResult, field, text):
    cleanedText = text.strip()

    if (field == CONST_TEST_RESULT_FIELD.Name):
        resultObj.Name = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.Outcome):
        resultObj.Outcome = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.Duration):
        resultObj.Duration = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.StackTrace):
        resultObj.StackTrace = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.Message):
        resultObj.Message = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.StandardOutput):
        resultObj.StandardOutput = cleanedText

    if (field == CONST_TEST_RESULT_FIELD.StandardError):
        resultObj.StandardError = cleanedText

argsLen = len(sys.argv)

if (argsLen < 1):
    sys.exit('Please enter input txt file path on the last argument')

filePath = sys.argv[argsLen - 1]

filename, fileExtension = os.path.splitext(filePath)

if (fileExtension != ".txt"):
    sys.exit('Please input txt file on the last argument, {} is not txt file'.format(filePath))

inputFile = open(filePath, "r")
lines = inputFile.readlines()
inputFile.close();

container = TestContainer()
AssignContainerData(container, lines)

testLines = lines[7:]

itemQuery = "(Result)(\d+) (\w+):(?:\t|$|\s)((.|\n)*)"

currentTestResultObj = TestResult()
previousField = ""
currentField = ""
currentProcessingText = ""

for line in testLines:
    matches = re.findall(itemQuery, line)
    if (len(matches) > 0):

        matches = matches[0]

        if (len(matches) > 0):
            previousField = currentField
            AssignTestResultField(currentTestResultObj, previousField, currentProcessingText)
            currentProcessingText = ""

            currentField = matches[CONST_TEST_RESULT_ELEMENT_INDEX.Field]
            currentProcessingText += matches[CONST_TEST_RESULT_ELEMENT_INDEX.Content]

            if (previousField == CONST_TEST_RESULT_FIELD.StandardError):
                container.Tests.append(currentTestResultObj)
                currentTestResultObj = TestResult()
    else:
        currentProcessingText += line;

# End of file
if (currentField == CONST_TEST_RESULT_FIELD.StandardError and previousField == CONST_TEST_RESULT_FIELD.StandardOutput):
    container.Tests.append(currentTestResultObj)

WriteToJson(container)
WriteToHtml(container)
WriteToHtmlDetailed(container)
# End of file