-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test that generated pycs are valid after a fresh install
- Loading branch information
Showing
4 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#!/usr/bin/env python | ||
|
||
import contextlib | ||
import importlib._bootstrap_external | ||
import importlib.util | ||
import os | ||
import shutil | ||
import site | ||
import subprocess | ||
import tempfile | ||
import venv | ||
from pathlib import Path | ||
|
||
import _imp | ||
import pytest | ||
|
||
python_exe = "python.exe" if os.name == "nt" else "python" | ||
envBuilder = venv.EnvBuilder(clear=True, with_pip=True, symlinks=(os.name == "nt")) | ||
hy_source = Path(__file__).parent.parent | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def _restore_hy(): | ||
running_in_ci = False | ||
if not (hy_source / "hy").exists() and (hy_source / "_hy_src").exists(): | ||
running_in_ci = True | ||
shutil.move(hy_source / "_hy_src", hy_source / "hy") | ||
yield | ||
if running_in_ci: | ||
shutil.move(hy_source / "hy", hy_source / "_hy_src") | ||
|
||
|
||
def check_pyc(source_or_pyc_fname): | ||
try: | ||
source_fname = importlib.util.source_from_cache(source_or_pyc_fname) | ||
pycname = source_or_pyc_fname | ||
except ValueError: | ||
pycname = importlib.util.cache_from_source(source_or_pyc_fname) | ||
source_fname = source_or_pyc_fname | ||
|
||
stat = os.stat(source_fname) | ||
basename = os.path.basename(source_fname) | ||
with open(pycname, "rb") as fp: | ||
pyc = fp.read() | ||
|
||
exc_details = {"name": source_fname, "path": pycname} | ||
|
||
flags = importlib._bootstrap_external._classify_pyc(pyc, basename, exc_details) | ||
|
||
if flags & 0b1: | ||
# hash-based | ||
with open(source_fname, "rb") as fp: | ||
source_bytes = fp.read() | ||
source_hash = _imp.source_hash( | ||
importlib.util._RAW_MAGIC_NUMBER, | ||
source_bytes, | ||
) | ||
importlib._bootstrap_external._validate_hash_pyc( | ||
pyc, source_hash, basename, exc_details | ||
) | ||
else: | ||
# timestamp-based | ||
mtime = int(stat.st_mtime) | ||
importlib._bootstrap_external._validate_timestamp_pyc( | ||
pyc, mtime, stat.st_size, basename, exc_details | ||
) | ||
|
||
|
||
@contextlib.contextmanager | ||
def fresh_env(*extra_packages): | ||
with tempfile.TemporaryDirectory() as name: | ||
envBuilder.create(name) | ||
base = Path(name) | ||
if extra_packages: | ||
pip = base / "bin" / "pip" | ||
subprocess.run([pip, "install", *extra_packages], check=True) | ||
yield base | ||
|
||
|
||
def with_and_without_wheel(f): | ||
def _wrapped(): | ||
with fresh_env() as base: | ||
pip = base / "bin" / "pip" | ||
sitepackages = Path(site.getsitepackages([base])[0]) | ||
f(pip, sitepackages) | ||
with fresh_env("wheel") as base: | ||
pip = base / "bin" / "pip" | ||
sitepackages = Path(site.getsitepackages([base])[0]) | ||
f(pip, sitepackages) | ||
|
||
return _wrapped | ||
|
||
|
||
@pytest.mark.online | ||
@with_and_without_wheel | ||
def test_install_dot(pip, sitepackages): | ||
subprocess.run([pip, "install", hy_source], check=True) | ||
for path in (sitepackages / "hy").glob("**/*.hy"): | ||
check_pyc(path) | ||
|
||
|
||
@pytest.mark.online | ||
def test_install_sdist(): | ||
with fresh_env() as base: | ||
root = base / "build" | ||
python = base / "bin" / python_exe | ||
shutil.copytree( | ||
hy_source, root, ignore=shutil.ignore_patterns(".git", "build", "dist") | ||
) | ||
subprocess.run( | ||
[python, root / "setup.py", "sdist"], | ||
env={**os.environ, "HY_VERSION": "unknown"}, | ||
check=True, | ||
) | ||
tarball = root / "dist" / "hy-unknown.tar.gz" | ||
|
||
@with_and_without_wheel | ||
def _test_install_sdist(pip, sitepackages): | ||
subprocess.run([pip, "install", tarball], check=True) | ||
for path in (sitepackages / "hy").glob("**/*.hy"): | ||
check_pyc(path) | ||
|
||
_test_install_sdist() |