Skip to content

Conversation

mattem
Copy link
Contributor

@mattem mattem commented Jun 6, 2022

Adds a py_pytest_main entrypoint rule for templating a launcher for pytest that works in conjunction with the Gazelle extension.

Comment on lines 38 to 39
print("Pytest exit code: " + str(exit_code))
print("Ran pytest.main with " + str(args))

Choose a reason for hiding this comment

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

Suggested change
print("Pytest exit code: " + str(exit_code))
print("Ran pytest.main with " + str(args))
print("Pytest exit code: " + str(exit_code), file=sys.stderr)
print("Ran pytest.main with " + str(args), file=sys.stderr)

I may be missing a reason that we wouldn't send error logs to stderr. But normally you should.

ctx.actions.expand_template(
template = ctx.file.template,
output = ctx.outputs.out,
substitutions = dict(substitutions, **ctx.var),

Choose a reason for hiding this comment

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

How does this **ctx.var overriding get used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Via --define generally

@@ -1 +1,2 @@
requests==2.25.1
pytest

Choose a reason for hiding this comment

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

Why not pin the version, at least to ~=?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do, given this is locked by pip_compile and it won't change we shouldn't really care about the version of pytest, so the open ended version here seemed fine, but I'm not strongly opinionated about it.

import pytest

if __name__ == "__main__":
$$CHDIR$$

Choose a reason for hiding this comment

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

Is there a benefit/difference to subbing in an os.chdir(...) string here vs. passing the directory as the first argument to pytest.main? (Passing as 1st arg is how we do it at our company)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

Copy link
Contributor Author

@mattem mattem Jun 10, 2022

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.

@mattem
Copy link
Contributor Author

mattem commented Jul 7, 2022

Well that merge went wrong :(

@f0rmiga f0rmiga force-pushed the feat/py_pytest_main branch from 0213618 to 3f10990 Compare July 11, 2022 22:51
Signed-off-by: Thulio Ferraz Assis <[email protected]>
@domenic-donato
Copy link

What is the status of this PR, is it still being worked on?

@f0rmiga
Copy link
Member

f0rmiga commented Nov 8, 2022

Following the decision that rules_python will not be test-framework-specific, this effort landed in aspect-build/rules_py@6b1fcb7.

@f0rmiga f0rmiga closed this Nov 8, 2022
@f0rmiga f0rmiga deleted the feat/py_pytest_main branch November 8, 2022 18:35
@arrdem arrdem mentioned this pull request Feb 6, 2023
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.

4 participants