Skip to content

Add regression test for changes to support hermetic interpreters #1397

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

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions tests/unittest_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
"""tests for the astroid builder and rebuilder module"""

import collections
import importlib
import os
import pathlib
import py_compile
import socket
import sys
import tempfile
import unittest

import pytest
Expand Down Expand Up @@ -790,5 +794,62 @@ def test_parse_module_with_invalid_type_comments_does_not_crash():
assert isinstance(node, nodes.Module)


class HermeticInterpreterTest(unittest.TestCase):
"""Modeled on https://github.com/PyCQA/astroid/pull/1207#issuecomment-951455588"""

@classmethod
def setUpClass(cls):
"""Simulate a hermetic interpreter environment having no code on the filesystem."""
with tempfile.TemporaryDirectory() as tmp_dir:
sys.path.append(tmp_dir)

# Write a python file and compile it to .pyc
cls.code_snippet = "def func(): return 42"
with tempfile.NamedTemporaryFile(
mode="w", dir=tmp_dir, suffix=".py", delete=False
) as tmp:
tmp.write(cls.code_snippet)
pyc_file = py_compile.compile(tmp.name)
cls.pyc_name = tmp.name.replace(".py", ".pyc")
os.remove(tmp.name)
os.rename(pyc_file, cls.pyc_name)

# Import the module
cls.imported_module_path = pathlib.Path(cls.pyc_name)
cls.imported_module = importlib.import_module(cls.imported_module_path.stem)

# Delete source code from module object, filesystem, and path
del cls.imported_module.__file__
os.remove(cls.imported_module_path)
sys.path.remove(tmp_dir)

def test_build_from_live_module_without_source_file(self):
"""Assert that inspect_build() is not called.
See comment in module_build() before the call to inspect_build():
"get a partial representation by introspection"

This "partial representation" was presumably causing unexpected behavior.
"""
# Sanity check
self.assertIsNone(
self.imported_module.__loader__.get_source(self.imported_module_path.stem)
)
with self.assertRaises(AttributeError):
_ = self.imported_module.__file__

my_builder = builder.AstroidBuilder()
with unittest.mock.patch.object(
self.imported_module.__loader__,
"get_source",
return_value=self.code_snippet,
):
with unittest.mock.patch.object(
my_builder, "inspect_build", side_effect=AssertionError
):
my_builder.module_build(
self.imported_module, modname=self.imported_module_path.stem
)


if __name__ == "__main__":
unittest.main()