A better way to deal with filesystem-required cli #82
HansBug
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When we need to use click to build CLI applications, it is often inevitable to read and write external files. In fact, there is special support for such a common situation in click, which is called isolated file system.
This is a demo of cli implement (provided by click offical documentation).
This is the unittest code for the cli code above.
Its principle is to create a temporary path in the context of the with block and temporarily change the current working directory to the temporary diretory. Therefore, the running test involving file IO can be realized, and the running files will not appear under the current code path to ensure the cleanness of the repository.
Similarly, when we need to test the other functions which involve file IO as well, we can manually build a tempfile-based test block like the code below.
References:
Beta Was this translation helpful? Give feedback.
All reactions