Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posting test results as an image #48

Merged
merged 8 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/multi-platform-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ jobs:
shell: bash
run: |
pip install defusedxml
python "${{ github.workspace }}/scripts/OutPutResultsToJsons.py" "${{ github.workspace }}/scripts/junitout.xml" "Real Test" --json_output "${{ github.workspace }}/scripts/finalresult.json" --discord_json_output "${{ github.workspace }}/scripts/discordJson_output.json" --os "${{ runner.os }}" --compiler "${{ matrix.c_compiler }}" --event "${{ github.event_name }}" --author "${{ github.actor }}" --branch "${{ github.ref_name }}"
pip install pillow
python "${{ github.workspace }}/scripts/OutPutResultsToJsons.py" "${{ github.workspace }}/scripts/junitout.xml" "Real Test" --json_output "${{ github.workspace }}/scripts/finalresult.json" --discord_json_output "${{ github.workspace }}/scripts/discordJson_output.json" --image_out "${{ github.workspace }}/scripts/discrod_image.png" --os "${{ runner.os }}" --compiler "${{ matrix.c_compiler }}" --event "${{ github.event_name }}" --author "${{ github.actor }}" --branch "${{ github.ref_name }}"

- name: Post results
if: success()
Expand All @@ -124,4 +125,4 @@ jobs:
uses: tsickert/[email protected]
with:
webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }}
raw-data: ${{ github.workspace }}/scripts/discordJson_output.json
filename: "${{ github.workspace }}/scripts/discrod_image.png"
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@ CMakeCache.txt
bin/
obj/
*.spv
_deps/
_deps/
scripts/ctestOutput.xml

scripts/discord.json

scripts/output.json

scripts/testimage.png

Testing/Temporary/
103 changes: 103 additions & 0 deletions scripts/OutPutResultsToJsons.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import time
import argparse
import os
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime, timezone
import re

def xml_to_json(xml_file, tool_name):
# Check if the file exists
Expand Down Expand Up @@ -158,14 +160,108 @@ def json_to_discord_json(json_data, os_name, compiler, event, author, branch):

return discord_json

def parse_test_results_from_file(file_path):
# Read the XML data from the file
with open(file_path, 'r', encoding='utf-8') as file:
xml_data = file.read()

# Parse the XML data
root = ET.fromstring(xml_data)

# Extract test suite information
test_suite_name = root.attrib.get('name', 'Test Suite')
tests = root.attrib.get('tests', '0')
failures = root.attrib.get('failures', '0')
skipped = root.attrib.get('skipped', '0')

# Initialize the total time
total_time = 0.0

# Extract test case information
test_cases = []
for testcase in root.findall('testcase'):
name = testcase.attrib.get('name')
time = float(testcase.attrib.get('time', '0'))
status = 'passed' if testcase.find('failure') is None else 'failed'
test_cases.append((name, status, time))

# Accumulate total time
total_time += time

return test_suite_name, tests, failures, skipped, total_time, test_cases


def create_dark_theme_test_results_image(file_path, os, compiler):
# Parse the XML data from the file
test_suite_name, tests, failures, skipped, total_time, test_cases = parse_test_results_from_file(file_path)

# Set up image
width, height = 500, 350
background_color = (45, 47, 49) # Dark gray
image = Image.new('RGB', (width, height), color=background_color)
draw = ImageDraw.Draw(image)

# Load fonts
try:
if os == "Windows":
title_font = ImageFont.truetype("arial.ttf", 24)
header_font = ImageFont.truetype("arial.ttf", 18)
body_font = ImageFont.truetype("arial.ttf", 14)
else:
# Use DejaVuSans as it is commonly available across platforms
title_font = ImageFont.truetype("DejaVuSans-Bold.ttf", 24)
header_font = ImageFont.truetype("DejaVuSans.ttf", 18)
body_font = ImageFont.truetype("DejaVuSans.ttf", 14)
except IOError:
# Fallback to default font if not available
title_font = ImageFont.load_default()
header_font = ImageFont.load_default()
body_font = ImageFont.load_default()

# Colors
white = (255, 255, 255)
light_gray = (200, 200, 200)
green = (0, 255, 0)
red = (255, 0, 0)

# Draw title
draw.text((20, 20), "Test Results", font=title_font, fill=white)

# Hard set the suite name
test_suite_name = f"{os}-{compiler}"

# Draw test suite information
y = 60
draw.text((20, y), f"Test Suite: {test_suite_name}", font=body_font, fill=white)
y += 25
draw.text((20, y), f"Total Tests: {tests}", font=body_font, fill=white)
y += 25
draw.text((20, y), f"Failures: {failures}", font=body_font, fill=red if int(failures) > 0 else green)
y += 25
draw.text((20, y), f"Skipped: {skipped}", font=body_font, fill=light_gray)
y += 25
draw.text((20, y), f"Duration: {total_time:.6f}", font=body_font, fill=light_gray)
y += 40

# Draw test cases
draw.text((20, y), "Detailed Test Results", font=header_font, fill=white)
y += 30
for name, status, duration in test_cases:
draw.text((20, y), name, font=body_font, fill=white)
status_color = green if status == 'passed' else red
draw.text((200, y), status, font=body_font, fill=status_color)
draw.text((280, y), f"{duration:.6f}", font=body_font, fill=light_gray) # Corrected here
y += 25

return image

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Convert XML test results to JSON and Discord markdown.')
parser.add_argument('xml_file', help='Path to the XML file.')
parser.add_argument('tool_name', help='Name of the testing tool.')
parser.add_argument('--json_output', help='Path to the output JSON file.')
parser.add_argument('--discord_json_output', help='Path to the output Discord JSON file.')
parser.add_argument('--image_out', help="Image to post to discord")
parser.add_argument('--os', help="Runners operating system")
parser.add_argument('--compiler', help="Runners compiler")
parser.add_argument('--event', help="The event triggering the action")
Expand Down Expand Up @@ -199,6 +295,13 @@ def json_to_discord_json(json_data, os_name, compiler, event, author, branch):
json.dump(discord_json, discord_json_file, indent=2)
print(f"Discord JSON output has been written to {discord_json_output_file}")

# Generate data
image = create_dark_theme_test_results_image(args.xml_file, args.os, args.compiler)

# Create the image
os.makedirs(os.path.dirname(args.image_out), exist_ok=True)
image.save(args.image_out)

except FileNotFoundError as e:
print(e)
except Exception as e:
Expand Down