I'm writing a project in Python that needs to make heavy use of filesystem operations, including changing ownership of files. This means I will need to query the passwd and group databases in order to convert between string representations of users/groups and numeric uid/gids.
I want to write unit tests to cover the module that will handle filesystem operations. However, I don't see any way of guaranteeing that the user names/group names/uids/gids will be in the passwd and group databases at testing time.
I thought about getting around this by mocking the pwd
and grp
modules in my unit tests, however, this means that my unit tests will tightly coupled to the implementation of my module. I don't want to care whether my module uses the pwd
and grp
modules at all.
How can I write my unit tests so that my module thinks it can run chown operations, which requires mapping users to uids and groups to gids, regardless of the actual contents of the passwd and group databases?
(There is a separate, but similar, question here about unit testing on file system operations, but I believe there is already an SO question or two or three or four out there on this topic, so I won't ask it again here - unfortunately, most of the answers given rely on mocking the os
module, thus coupling the tests and implementation.)