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

feat: port pytest_test macro #401

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

feat: port pytest_test macro #401

wants to merge 5 commits into from

Conversation

alexeagle
Copy link
Member

Comes from caseyduquettesc/rules_python_pytest#13 It's much easier to properly gazelle-generate a BUILD file in this shape rather than the awkward way you're forced to hold our py_pytest_main.

Comes from caseyduquettesc/rules_python_pytest#13
It's much easier to properly gazelle-generate a BUILD file in this shape
rather than the awkward way you're forced to hold our py_pytest_main.
@@ -0,0 +1,71 @@
"""A shim for executing pytest that supports test filtering, sharding, and more.

Copied from https://github.com/caseyduquettesc/rules_python_pytest/blob/331e0e511130cf4859b7589a479db6c553974abf/python_pytest/pytest_shim.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @caseyduquettesc - thank you for writing this!

if os.environ.get("XML_OUTPUT_FILE"):
pytest_args.append("--junitxml={xml_output_file}".format(xml_output_file=os.environ.get("XML_OUTPUT_FILE")))

# Handle test sharding - requires pytest-shard plugin.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the presence of the plugin be detected, and an error printed if it's missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I file and leave as a follow-up? Right now I'm looking for parity with v1.1.1 https://registry.bazel.build/modules/caseyduquettesc_rules_python_pytest



if __name__ == "__main__":
pytest_args = ["--ignore=external"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other template had different args around disabling caching, check if they are needed here.

] + srcs,
main = shim_label,
args = [
"--capture=no",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason to have this defined here, rather than in the shim?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I didn't write this code. Maybe Casey is willing to be the PR author.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This absolutely can be in the shim. In the very beginning, the shim was a trivial wrapper around pytest.main and adding default arguments here was less code than in the shim. As more pytest features were hooked up to native Bazel features, the shim gathered more argument parsing logic and I simply missed calling this out in the pull request that first began adding default arguments in the shim. I see no reason this shouldn't be part of pytest_args = ["--ignore=external"] in the shim.

main = shim_label,
args = [
"--capture=no",
] + args + ["$(location :%s)" % x for x in srcs],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these automatically spill? The arg limit length could be easily hit here.


if pytest_deps == None:
pytest_deps = ["@{}//pytest".format(pip_repo)]
if kwargs.get("shard_count", 1) > 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 1 seems unnecessary? Check for None, which would be falsey?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can explicitly say shard_count = 1 which I hoped to account for

args = [
"--capture=no",
] + args + ["$(location :%s)" % x for x in srcs],
deps = deps + pytest_deps,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to swap the ordering, so that this levels pytest dependencies will "win" over any defined transitively (perhaps there is a test helper as a py_library somewhere in the graph that consumes a different pytest version)

pytest/defs.bzl Outdated Show resolved Hide resolved
else:
pytest_args.extend(args)

print(pytest_args, file=sys.stderr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a little spammy, print it if there's some flag set?

@alexeagle
Copy link
Member Author

alexeagle commented Oct 3, 2024

I have very little time to prepare a talk for Monday and I think you're expecting a higher level of polish than Casey had, so I'm going to try the bazelbuild/rules_python#2044 approach instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants