Skip to content

Commit 1936d0b

Browse files
committed
Fix fake_filesystem.add_package_metadata
- pause patching to get the metadata path - seems to never have properly worked
1 parent 02bddcf commit 1936d0b

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ The released versions correspond to PyPI releases.
1919
* a warning is now issued if trying to create a nested fake filesystem with custom arguments
2020
(custom arguments are ignored in this case, as the existing fake filesystem is used)
2121

22+
## Fixes
23+
* fixed `fake_filesystem.add_package_metadata` that had never worked correctly
24+
(see [#1205](../../issues/1205))
25+
2226
## [Version 5.9.2](https://pypi.python.org/pypi/pyfakefs/5.9.2) (2025-07-30)
2327
Fixes interaction with pytest.
2428

pyfakefs/fake_filesystem.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,12 +2544,19 @@ def add_package_metadata(self, package_name: str) -> None:
25442544

25452545
from importlib.metadata import distribution, PackageNotFoundError
25462546

2547-
dist_files = distribution(package_name).files
2548-
if dist_files is None:
2549-
raise PackageNotFoundError(package_name)
2550-
2551-
for metadata_file in dist_files:
2552-
self.add_real_file(metadata_file.locate())
2547+
# we have to pause patching to get the distribution
2548+
# from the real filesystem if we are in patch mode
2549+
if self.patcher:
2550+
self.pause()
2551+
try:
2552+
dist_files = distribution(package_name).files
2553+
if dist_files is None:
2554+
raise PackageNotFoundError(package_name)
2555+
for metadata_file in dist_files:
2556+
self.add_real_file(metadata_file.locate())
2557+
finally:
2558+
if self.patcher:
2559+
self.resume()
25532560

25542561
def create_file_internally(
25552562
self,

pyfakefs/pytest_tests/pytest_fixture_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
12+
import pathlib
13+
import sys
1214

1315
# Example for a test using a custom pytest fixture with an argument to Patcher
1416

@@ -54,3 +56,17 @@ def check_that_example_file_is_in_fake_fs():
5456
assert file.read() == "stuff here"
5557
assert example.EXAMPLE_FILE.read_text() == "stuff here"
5658
assert example.EXAMPLE_FILE.is_file()
59+
60+
61+
pytest_parent_path = pathlib.Path(pytest.__file__).parent.parent
62+
63+
64+
@pytest.mark.skipif(
65+
sys.version_info < (3, 8), reason="importlib.metadata not available"
66+
)
67+
def test_add_package_metadata(fs):
68+
pytest_dist_path = pytest_parent_path / f"pytest-{pytest.__version__}.dist-info"
69+
assert not fs.exists(pytest_dist_path)
70+
fs.add_package_metadata("pytest")
71+
assert fs.exists(pytest_dist_path)
72+
assert fs.exists(pytest_dist_path / "METADATA")

0 commit comments

Comments
 (0)