Skip to content

7154-Improve-testdir-documentation-on-makefiles #7239

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

Conversation

symonk
Copy link
Member

@symonk symonk commented May 22, 2020

Good afternoon, hope you are in good health in the current climate. I have made some minor adjustments to TestDir and some of its accompanying documentation, thanks for your time and look forward to receiving some feedback. Helps with #7154

  • Added the ability to Testdir to track created files and allow tests to access this data, includes test coverage for this feature.

  • Added some documentation around makepyfile/maketxtfile to explain that when a name is not passed, the files contents will be overwritten in place

  • Include documentation when adding new features.

  • Include new tests or update existing tests when applicable.

  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.

@symonk
Copy link
Member Author

symonk commented May 22, 2020

forgot py35 support, will address

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks a lot for taking the time to contribute this @symonk!

The updated docs are definitely a plus, I'm a bit curious about the use case for Testdir.created_files though. Can you give some examples in tests where this can be useful?

testdir.makepyfile("foobar")
# to create multiple files, pass kwargs accordingly
testdir.makepyfile(custom="foobar")
# outcome is test_something.py & custom.py
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# outcome is test_something.py & custom.py
# at this point, both 'test_something.py' & 'custom.py' exist in the test directory

@@ -0,0 +1 @@
Testdir now keeps track of created file paths, updated Testdir documentation to reflect overwritten files
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Testdir now keeps track of created file paths, updated Testdir documentation to reflect overwritten files
``Testdir.created_files`` returns a ``Set`` of paths created by ``make*file`` family of methods.

But I'm curious, what's the use case for this new property?

return ret

@property
def created_files(self) -> Set:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def created_files(self) -> Set:
def created_files(self) -> Set[py.path.local]:

@@ -650,8 +652,17 @@ def to_text(s):
p.write(source.strip().encode(encoding), "wb")
if ret is None:
ret = p
if ret.strpath not in self._created_files:
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to replace both lines by:

self._created_files.add(ret)

No need to check for membership, and I think keeping a set of py.path.local is better. 👍

testdir.maketxtfile("foobar")
# to create multiple files, pass kwargs accordingly
testdir.maketxtfile(custom="foobar")
# outcome is test_something.txt & custom.txt
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# outcome is test_something.txt & custom.txt
# at this point, both 'test_something.txt' & 'custom.txt' exist in the test directory

"""Shortcut for .makefile() with a .py extension."""
r"""Shortcut for .makefile() with a .py extension
defaults to the test name .py e.g test_foobar.py
Calling without kwargs will result in overwriting the initial file contents
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove this phrase, because it conveys the impression that by calling with kwargs initial file contents are somehow not overwritten.

"""Shortcut for .makefile() with a .txt extension."""
r"""Shortcut for .makefile() with a .txt extension
defaults to the test name .txt e.g test_foobar.txt
Calling without kwargs will result in overwriting the initial file contents
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove this phrase, because it conveys the impression that by calling with kwargs initial file contents are somehow not overwritten.

def test_testdir_duplicate_paths(testdir):
testdir.makepyfile("foo")
testdir.makepyfile("bar")
assert len(testdir.created_files) == 1
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert len(testdir.created_files) == 1
assert testdir.created_files == {testdir.tmpdir.join("test_testdir_duplicate_paths.py")}

This is better because we are more explicit about the outcome, helping to figure out any problems in case the test fails.

def test_files_by_name_are_correct(testdir):
testdir.maketxtfile("foo")
testdir.maketxtfile(custom="foobar")
assert len(testdir.created_files) == 2
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert len(testdir.created_files) == 2
assert testdir.created_files == {testdir.tmpdir.join("test_files_by_name_are_correct.py"), testdir.tmpdir.join("custom.py")}

Ditto.

two = testdir.maketxtfile(data)
three = testdir.makeconftest(data)
four = testdir.makeini(data)
assert {one, two, three, four} == testdir.created_files
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert {one, two, three, four} == testdir.created_files
assert testdir.created_files == {one, two, three, four}

For consistency with the rest of the tests: "obtained == expected"

@symonk
Copy link
Member Author

symonk commented May 22, 2020

@nicoddemus thanks for the fast feedback, appreciate it - hope you are well. The new property i guess i jumped the gun a bit there, was doing some plugin development and was creating a lot of files as part of my testing, found it easier to use that rather than keep going to the temp directory and inspecting them on FS - updated accordingly the documentation and plucked out the work that isnt really beneficial.

Thanks again

@symonk symonk changed the title 7154-Testdir-created-files-and-documentation-improvements 7154-Improve-testdir-documentation-on-makefiles May 22, 2020
@symonk symonk requested a review from nicoddemus May 22, 2020 18:56
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

Thanks a lot @symonk, we appreciate it!

@nicoddemus nicoddemus merged commit 05c22ff into pytest-dev:master May 23, 2020
@symonk symonk deleted the 7154-track-testdir-files-and-update-documentation branch May 23, 2020 14:45
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.

2 participants