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

Change pants_integration_test.py to use Pytest-style functions #10675

Merged
merged 5 commits into from Aug 23, 2020

Conversation

Eric-Arellano
Copy link
Contributor

We've slowly been chipping away at using Pytest-style tests, meaning using assert rather than self.assertX and using top-level functions rather than test classes.

Now, integration tests can be written in Pytest style. We deprecate the old class-based style.

To land this change, we drop the ability to set the PANTS_PROFILE in an integration test. It appears that this was not used; the workaround is to run the test like you would on the command line.

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
return temporary_dir(root_dir=root, cleanup=cleanup, suffix=".pants.d")

@contextmanager
def overwrite_file_content(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was moved to contextutil.py.

Copy link
Contributor Author

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

I commented where there were actual meaningful changes. Almost everything else is routine conversion from a class to top-level functions.

Comment on lines +16 to +20
@ensure_daemon
def test_goal_validation():
result = run_pants(["blah", "::"])
result.assert_failure()
assert "Unknown goals: blah" in result.stdout
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as before. Only moved out of the test class, which as being skipped.

Comment on lines +23 to +28
def test_unimplemented_goals_noop() -> None:
# Running on a `files` target should usually fail, but it should no-op if no `run`
# implementations are activated.
with setup_tmpdir({"bad.txt": "", "BUILD": "files(sources=['f.txt')"}) as tmpdir:
run_pants(["run", tmpdir]).assert_success()
run_pants(["--backend-packages=['pants.backend.python']", "run", tmpdir]).assert_failure()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved out of the test class, along with having a fix. The old test only passed because of the v1 target definitions for Python target types.

Comment on lines -24 to -25
@ensure_daemon
def test_list(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deleted. We already have several tests for lint, both unit tests and integration tests.

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@coveralls
Copy link

Coverage Status

Coverage remained the same at 0.0% when pulling 34f863c on Eric-Arellano:it-assert into f7b260e on pantsbuild:master.



@contextmanager
def setup_tmpdir(files: Mapping[str, str]) -> Iterator[str]:
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

This feels like it should be a fixture.

For example, the fixture could take a marker that provides the files, but if that marker isn't provided it could default to taking the value of SOURCES in the requesting module, or something like that.

See, e.g., https://docs.pytest.org/en/stable/fixture.html#using-markers-to-pass-data-to-fixtures and https://docs.pytest.org/en/stable/fixture.html#fixtures-can-introspect-the-requesting-test-context.



@contextmanager
def temporary_workdir(cleanup: bool = True) -> Iterator[str]:
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

Ditto. There is already a tmpdir fixture, so tmp_workdir is more idiomatic.

@Eric-Arellano
Copy link
Contributor Author

I agree fixtures would be neat. I'm not sure how to handle though that these need to be called as normal functions too, such as PantsIntegrationTest calling those methods for their deprecated methods. Or run_pants called temporary_workdir():

    with temporary_workdir() as workdir:
        return run_pants_with_workdir(
            command,
            workdir=workdir,
            hermetic=hermetic,
            use_pantsd=use_pantsd,
            config=config,
            stdin_data=stdin_data,
            extra_env=extra_env,
            **kwargs,
        )

How do you feel about landing this as is and saving fixtures as an enhancement? Or is the solution to make those private functions, and have the fixtures + the non-fixture code call those private functions?

@benjyw
Copy link
Sponsor Contributor

benjyw commented Aug 23, 2020

We can fixturize later, it's not that critical.

@benjyw
Copy link
Sponsor Contributor

benjyw commented Aug 23, 2020

But maybe add TODOs (linked to an issue).

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.

None yet

3 participants