-
Notifications
You must be signed in to change notification settings - Fork 15
Support junit xml output for pkl test #93
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5f119d8
Support junit xml output for pkl test
gordonbondon a50883a
Add missing license header
gordonbondon 867b582
Replace with dots
gordonbondon 1b79e1b
remove prefix dogts
gordonbondon 2c65f92
Fix header
gordonbondon 7fd3254
Suite name in starlark
gordonbondon 65c1963
Switch from integration bazel test to pytest
gordonbondon 142c3b9
Update pkl/private/pkl.bzl
gordonbondon 12a34fe
fix python toolchain resulution order issue
gordonbondon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@rules_pkl//pkl:defs.bzl", "pkl_test") | ||
load("@rules_python//python:defs.bzl", "py_test") | ||
|
||
# Sample pkl test for XML generation | ||
pkl_test( | ||
name = "sample_xml_generator", | ||
srcs = ["sample_xml_test.pkl"], | ||
) | ||
|
||
# Python unittest that runs pkl_test with XML output and validates the XML content | ||
py_test( | ||
name = "junit_xml_validation_test", | ||
srcs = ["junit_xml_validation_test.py"], | ||
data = [":sample_xml_generator"], | ||
env = { | ||
"SAMPLE_XML_GENERATOR_PATH": "$(location :sample_xml_generator)", | ||
}, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Test that validates the JUnit XML output from pkl_test.""" | ||
|
||
import os | ||
import subprocess | ||
import sys | ||
import tempfile | ||
import unittest | ||
import xml.etree.ElementTree as ET | ||
from functools import cached_property | ||
from pathlib import Path | ||
|
||
|
||
class JUnitXMLValidationTest(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
with tempfile.NamedTemporaryFile(suffix=".xml", delete=False) as xml_file: | ||
cls.xml_file = Path(xml_file.name) | ||
|
||
# Set environment and get script path | ||
env = os.environ | {"XML_OUTPUT_FILE": str(cls.xml_file)} | ||
script_path = os.environ.get("SAMPLE_XML_GENERATOR_PATH") | ||
|
||
if not script_path: | ||
raise RuntimeError("SAMPLE_XML_GENERATOR_PATH environment variable not set") | ||
|
||
# Run the pkl_test with XML output enabled | ||
try: | ||
result = subprocess.run( | ||
[script_path], env=env, capture_output=True, text=True, check=True | ||
) | ||
except subprocess.CalledProcessError as e: | ||
raise RuntimeError( | ||
f"""Failed to run pkl_test: {e} | ||
STDOUT: | ||
{result.stdout} | ||
STDERR: | ||
{result.stderr} | ||
""" | ||
) from e | ||
|
||
if not cls.xml_file.exists(): | ||
raise RuntimeError(f"XML output file was not created at {cls.xml_file}") | ||
|
||
@cached_property | ||
def xml_root(self): | ||
return ET.parse(self.xml_file).getroot() | ||
|
||
def test_xml_file_exists_and_parseable(self): | ||
self.assertTrue( | ||
self.xml_file.exists(), f"XML file {self.xml_file} does not exist" | ||
) | ||
|
||
try: | ||
self.assertIsNotNone(self.xml_root) | ||
except ET.ParseError as e: | ||
self.fail(f"Failed to parse XML: {e}") | ||
|
||
def test_root_element_structure(self): | ||
root = self.xml_root | ||
|
||
self.assertEqual( | ||
root.tag, | ||
"testsuites", | ||
f"Root element should be 'testsuites', got '{root.tag}'", | ||
) | ||
|
||
required_attrs = ["name", "tests", "failures"] | ||
missing_attrs = [attr for attr in required_attrs if attr not in root.attrib] | ||
self.assertFalse( | ||
missing_attrs, f"testsuites missing attributes: {missing_attrs}" | ||
) | ||
|
||
self.assertEqual(root.attrib["name"], "tests.junit_xml.sample_xml_generator") | ||
|
||
def test_testsuite_structure(self): | ||
testsuites = self.xml_root.findall("testsuite") | ||
self.assertGreater(len(testsuites), 0, "No testsuite elements found") | ||
|
||
required_attrs = ["name", "tests", "failures"] | ||
for i, testsuite in enumerate(testsuites): | ||
missing_attrs = [ | ||
attr for attr in required_attrs if attr not in testsuite.attrib | ||
] | ||
self.assertFalse( | ||
missing_attrs, f"testsuite {i} missing attributes: {missing_attrs}" | ||
) | ||
|
||
def test_testcase_structure(self): | ||
testcases = self.xml_root.findall(".//testcase") | ||
self.assertGreater(len(testcases), 0, "No testcase elements found") | ||
|
||
required_attrs = ["name", "classname"] | ||
for i, testcase in enumerate(testcases): | ||
missing_attrs = [ | ||
attr for attr in required_attrs if attr not in testcase.attrib | ||
] | ||
self.assertFalse( | ||
missing_attrs, f"testcase {i} missing attributes: {missing_attrs}" | ||
) | ||
|
||
def test_expected_test_cases(self): | ||
testcases = self.xml_root.findall(".//testcase") | ||
testcase_names = {tc.attrib["name"] for tc in testcases} | ||
expected_names = {"dummy test line item 1", "dummy test line item 2"} | ||
missing_names = expected_names - testcase_names | ||
self.assertFalse( | ||
missing_names, f"Expected test cases not found: {missing_names}" | ||
) | ||
|
||
def test_xml_declaration(self): | ||
content = self.xml_file.read_text() | ||
self.assertIn("<?xml version", content, "XML declaration not found") | ||
|
||
def test_suite_name_matches_target(self): | ||
expected_name = "tests.junit_xml.sample_xml_generator" | ||
self.assertEqual( | ||
self.xml_root.attrib["name"], | ||
expected_name, | ||
"Root testsuites name should match target path", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===----------------------------------------------------------------------===// | ||
// Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
amends "pkl:test" | ||
|
||
facts { | ||
["dummy test line item 1"] { | ||
true | ||
} | ||
["dummy test line item 2"] { | ||
true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.