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

Add documentation/further enhancements to rewritten mock_open #48279

Merged
merged 9 commits into from Jun 25, 2018

Conversation

Projects
None yet
3 participants
@terminalmage
Member

terminalmage commented Jun 24, 2018

This adds documentation for the enhancements added to mock_open to the unit test docs. It also makes some further changes to the MockFH class to make it behave more like an actual filehandle:

  1. Bytestrings are not permitted to be written to filehandles opened for non-binary reading, and vice-versa with filehandles opened for binary reading.

  2. Filehandles not opened for reading will raise an IOError if you try to read from them, and filehandles not opened for writing will raise an IOError if you try to write to them.

  3. (Python 3 only) Filehandles with string read-data used to raise an exception when the file was opened in binary mode. This now only happens on read. This allows mock_open to be a bit more flexible for cases where one is testing code that only writes to a file. In those cases, a simple mock_open() can be used with no arguments. After all, why worry about doing mock_open(read_data=b'') when you're never going to read from the filehandle?

Where necessary, tests have been updated to reflect these changes (which is a good sign, as it means the updated tests are now more accurately testing file I/O on Python 3).

terminalmage added some commits Jun 23, 2018

2 MockFH enhancements:
1. Raise appropriate TypeErrors when wrong type is written
2. Don't raise exception about read_data unless we actually try to read
   from the filehandle. This makes it a bit more permissive when testing
   code where the file is only being written to.
Opening files in Salt is done using ``salt.utils.files.fopen()``. When testing
code that reads from files, the ``mock_open`` helper can be used to mock
filehandles. Note that is not the samw ``mock_open`` as

This comment has been minimized.

@damon-atkins

damon-atkins Jun 24, 2018

Member

I assume samw should be same

This comment has been minimized.

@terminalmage

terminalmage Jun 24, 2018

Member

Correct, fixed.

@rallytime rallytime merged commit 27a0e95 into saltstack:2018.3 Jun 25, 2018

6 of 9 checks passed

jenkins/PR/salt-pr-linode-ubuntu16-py3 Pull Requests » Salt PR - Linode Ubuntu16.04 - PY3 #10946 — ABORTED
Details
default Build finished.
Details
jenkins/PR/salt-pr-linode-cent7-py3 Pull Requests » Salt PR - Linode CentOS 7 - PY3 #5976 — FAILURE
Details
WIP ready for review
Details
jenkins/PR/salt-pr-clone Pull Requests » Salt PR - Clone #26180 — SUCCESS
Details
jenkins/PR/salt-pr-docs-n Pull Requests » Salt PR - Docs #18230 — SUCCESS
Details
jenkins/PR/salt-pr-linode-ubuntu14-n Pull Requests » Salt PR - Linode Ubuntu14.04 #23904 — SUCCESS
Details
jenkins/PR/salt-pr-lint-n Pull Requests » Salt PR - Code Lint #22865 — SUCCESS
Details
jenkins/PR/salt-pr-rs-cent7-n Pull Requests » Salt PR - RS CentOS 7 #20029 — SUCCESS
Details

@terminalmage terminalmage deleted the terminalmage:mock_open-docs branch Jun 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment