Skip to content

Commit 0ca77f5

Browse files
committed
tests: seed CodeEntropy regression subprocess
1 parent 0352289 commit 0ca77f5

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎tests/regression/helpers.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
import subprocess
6+
import sys
67
import tarfile
78
import urllib.request
89
from dataclasses import dataclass
@@ -366,12 +367,18 @@ def run_codeentropy_with_config(*, workdir: Path, config_src: Path) -> RunResult
366367

367368
(workdir / "config.yaml").write_text(yaml.safe_dump(cooked, sort_keys=False))
368369

370+
env = {
371+
**os.environ,
372+
"PYTHONHASHSEED": "0",
373+
"CODEENTROPY_RANDOM_SEED": "0",
374+
}
375+
369376
proc = subprocess.run(
370-
["python", "-m", "CodeEntropy"],
377+
[sys.executable, "-m", "tests.regression.seeded_runner"],
371378
cwd=str(workdir),
372379
capture_output=True,
373380
text=True,
374-
env={**os.environ},
381+
env=env,
375382
)
376383

377384
(workdir / "codeentropy_stdout.txt").write_text(proc.stdout or "")

‎tests/regression/seeded_runner.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import random
5+
import runpy
6+
7+
import numpy as np
8+
9+
10+
def main() -> None:
11+
"""Seed supported random generators and start CodeEntropy."""
12+
seed = int(os.environ.get("CODEENTROPY_RANDOM_SEED", "0"))
13+
14+
random.seed(seed)
15+
np.random.seed(seed)
16+
17+
runpy.run_module("CodeEntropy", run_name="__main__")
18+
19+
20+
if __name__ == "__main__":
21+
main()

0 commit comments

Comments
 (0)