test: Add new fake_fs fixture backed by pyfakefs#6785
Open
TheRealFalcon wants to merge 2 commits intocanonical:mainfrom
Open
test: Add new fake_fs fixture backed by pyfakefs#6785TheRealFalcon wants to merge 2 commits intocanonical:mainfrom
fake_fs fixture backed by pyfakefs#6785TheRealFalcon wants to merge 2 commits intocanonical:mainfrom
Conversation
Also convert a few fake_filesystem tests to prove its usage: - tests/unittests/config/test_cc_keyboard.py - tests/unittests/config/test_cc_mounts.py - tests/unittests/config/test_cc_update_etc_hosts.py - tests/unittests/config/test_cc_puppet.py
blackboxsw
reviewed
Mar 30, 2026
Collaborator
blackboxsw
left a comment
There was a problem hiding this comment.
Thanks @TheRealFalcon. I think this approach looks good. I think it's worth also trying to create an issue to track the migration work to move away from fake_filesystem and retire that fixture from all unittests in favor of fake_fs. I believe it is prodent to not roll out our fake_filesystem when we can leverage a maintained solution from the broader community.
|
|
||
| self.fstab_path = os.path.join(self.new_root, "etc/fstab") | ||
| def setup(self, mocker, fake_fs): | ||
| self._makedirs("/etc") |
Collaborator
There was a problem hiding this comment.
Would this not be converted to fake_fs.create_dir("/etc")?
| @@ -75,8 +75,8 @@ def test_enable_fallback_on_failure(self, m_subp): | |||
| @pytest.mark.usefixtures("fake_filesystem") | |||
Collaborator
There was a problem hiding this comment.
Shouldn't this be fake_fs now?
blackboxsw
requested changes
Mar 30, 2026
Collaborator
blackboxsw
left a comment
There was a problem hiding this comment.
Minor change request as commented inline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed Commit Message
Additional Context
I originally raised this idea in #6548 , though after working with it more closely, I don't think it's possible/easy to insert the new fixture as a simple shim. The way it would interact with
tmpdiris incompatible with howfake_filesystemcurrently does, and tmpdir is core tofake_filesystem.Because of this, I would like to propose it as a new fixture while deprecating the original
fake_filesystem. Deprecation sounds big and scary and like a bigger maintenance burden, but I don't think that it has to be. Just because something is deprecated doesn't mean it needs to be removed or changed. It just means we shouldn't be writing new stuff ith it. Just liketmpdirandtmp_pathco-exist peacefully in pytest, I think thatfake_filesystemandfake_fscould co-exist peacefully here, with the assumption that new tests should usefake_fsinstead offake_filesystem, and thatfake_filesystemwon't be actively modified for any new use cases.As mentioned in the discussion, the main reason I think we should prefer
pyfakefsis because it is comprehensive. Even within the past year, new functions have been added to be patched byfake_filesystem. It's not a "complete" implementation, as it gets added to as new functions are used that need to be patched. That has worked ok, but it makes testing a way larger headache when you need to also update this fairly convoluted fixture. The biggest hole here ispathlib. In the past I have wanted to refactor some of our utility functions to supportpathlib, but I eventually abandoned those efforts because I didn't want to deal with thefake_filesystemheadache.Furthermore, I think tests actually look a lot cleaner with
pyfakefs. A common testing paradigm in this codebase is to patch some kind of "open file" call to instead redirect the path to atmpdirbased path that we've manually setup, and then test with that mocked version. Withpyfakefs, that extra level of patching is no longer required. See thetest_cc_puppet.pychanges. Instead of local patching or fakes, the code under test can just use the expected absolute path and it all "just works".I realize my implementation here changed what was agreed to in the discussion, so if this change doesn't fit the project plans, that's fine too.
Test Steps
Merge type