Dont change the env for go tests

This prevents got test caching from working greatly increasing the time
for running a test
This commit is contained in:
Kovid Goyal 2022-08-28 21:41:32 +05:30
parent e69b02ad46
commit 3b2c4561c2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 93 additions and 76 deletions

View File

@ -9,9 +9,12 @@ import shutil
import subprocess import subprocess
import sys import sys
import unittest import unittest
from contextlib import contextmanager
from functools import lru_cache from functools import lru_cache
from tempfile import TemporaryDirectory
from typing import ( from typing import (
Callable, Dict, Generator, Iterator, List, NoReturn, Sequence, Set, Tuple, Any, Callable, Dict, Generator, Iterator, List, NoReturn, Optional,
Sequence, Set, Tuple
) )
@ -160,6 +163,35 @@ def reduce_go_pkgs(module: str, names: Sequence[str]) -> Set[str]:
return go_packages return go_packages
def run_python_tests(args: Any, go_proc: Optional[subprocess.Popen[bytes]] = None) -> None:
tests = find_all_tests()
def print_go() -> None:
print(go_proc.stdout.read().decode(), end='', flush=True)
go_proc.stdout.close()
go_proc.wait()
if args.module:
tests = filter_tests_by_module(tests, args.module)
if not tests._tests:
if go_proc:
print_go()
raise SystemExit(go_proc.returncode)
raise SystemExit('No test module named %s found' % args.module)
if args.name:
tests = filter_tests_by_name(tests, *args.name)
if not tests._tests and not go_proc:
raise SystemExit('No test named %s found' % args.name)
python_tests_ok = run_cli(tests, args.verbosity)
exit_code = 0 if python_tests_ok else 1
if go_proc:
print_go()
if exit_code == 0:
exit_code = go_proc.returncode
raise SystemExit(exit_code)
def run_tests() -> None: def run_tests() -> None:
import argparse import argparse
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -175,30 +207,64 @@ def run_tests() -> None:
type_check() type_check()
go_pkgs = reduce_go_pkgs(args.module, args.name) go_pkgs = reduce_go_pkgs(args.module, args.name)
if go_pkgs: if go_pkgs:
go_proc = run_go(go_pkgs, args.name) go_proc: Optional[subprocess.Popen[bytes]] = run_go(go_pkgs, args.name)
tests = find_all_tests() else:
go_proc = None
with env_for_python_tests():
run_python_tests(args, go_proc)
def print_go() -> None:
print(go_proc.stdout.read().decode(), end='', flush=True)
go_proc.stdout.close()
go_proc.wait()
if args.module: @contextmanager
tests = filter_tests_by_module(tests, args.module) def env_vars(**kw: str) -> Iterator[None]:
if not tests._tests: originals = {k: os.environ.get(k) for k in kw}
if go_pkgs: os.environ.update(kw)
print_go() try:
raise SystemExit(go_proc.returncode) yield
raise SystemExit('No test module named %s found' % args.module) finally:
for k, v in originals.items():
if v is None:
os.environ.pop(k, None)
else:
os.environ[k] = v
if args.name:
tests = filter_tests_by_name(tests, *args.name) @contextmanager
if not tests._tests and not go_pkgs: def env_for_python_tests() -> Iterator[None]:
raise SystemExit('No test named %s found' % args.name) gohome = os.path.expanduser('~/go')
python_tests_ok = run_cli(tests, args.verbosity) go = shutil.which('go')
exit_code = 0 if python_tests_ok else 1 python = shutil.which('python') or shutil.which('python3')
if go_pkgs: current_home = os.path.expanduser('~') + os.sep
print_go() paths = os.environ.get('PATH', '/usr/local/sbin:/usr/local/bin:/usr/bin').split(os.pathsep)
if exit_code == 0: path = os.pathsep.join(x for x in paths if not x.startswith(current_home))
exit_code = go_proc.returncode launcher_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kitty', 'launcher')
raise SystemExit(exit_code) env = dict(
PYTHONWARNINGS='error',
)
if go:
if go.startswith(current_home):
path = f'{os.path.dirname(go)}{os.pathsep}{path}'
path = f'{launcher_dir}{os.pathsep}{path}'
if os.environ.get('CI') == 'true':
print('Using PATH in test environment:', path, flush=True)
python = shutil.which('python', path=path) or shutil.which('python3', path=path)
print('Python:', python)
go = shutil.which('go', path=path)
print('Go:', go)
with TemporaryDirectory() as tdir, env_vars(
HOME=tdir, USERPROFILE=tdir, PATH=path,
XDG_CONFIG_HOME=os.path.join(tdir, '.config'),
XDG_CONFIG_DIRS=os.path.join(tdir, '.config'),
XDG_DATA_DIRS=os.path.join(tdir, '.local', 'xdg'),
XDG_CACHE_HOME=os.path.join(tdir, '.cache'),
**env
):
if os.path.isdir(gohome):
os.symlink(gohome, os.path.join(tdir, os.path.basename(gohome)))
yield
def main() -> None:
import warnings
warnings.simplefilter('error')
run_tests()

51
test.py
View File

@ -2,60 +2,11 @@
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
import importlib import importlib
import os
import shutil
import warnings
from contextlib import contextmanager
from tempfile import TemporaryDirectory
from typing import Iterator
@contextmanager
def env_vars(**kw: str) -> Iterator[None]:
originals = {k: os.environ.get(k) for k in kw}
os.environ.update(kw)
try:
yield
finally:
for k, v in originals.items():
if v is None:
os.environ.pop(k, None)
else:
os.environ[k] = v
def main() -> None: def main() -> None:
warnings.simplefilter('error')
gohome = os.path.expanduser('~/go')
go = shutil.which('go')
python = shutil.which('python') or shutil.which('python3')
current_home = os.path.expanduser('~') + os.sep
paths = os.environ.get('PATH', '/usr/local/sbin:/usr/local/bin:/usr/bin').split(os.pathsep)
path = os.pathsep.join(x for x in paths if not x.startswith(current_home))
launcher_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kitty', 'launcher')
if go and go.startswith(current_home):
path = f'{os.path.dirname(go)}{os.pathsep}{path}'
path = f'{launcher_dir}{os.pathsep}{path}'
PYTHON_FOR_TYPE_CHECK = shutil.which('python') or shutil.which('python3') or ''
gohome = os.path.expanduser('~/go')
if os.environ.get('CI') == 'true':
print('Using PATH in test environment:', path, flush=True)
python = shutil.which('python', path=path) or shutil.which('python3', path=path)
print('Python:', python)
go = shutil.which('go', path=path)
print('Go:', go)
with TemporaryDirectory() as tdir, env_vars(
PYTHONWARNINGS='error', HOME=tdir, USERPROFILE=tdir, PATH=path,
XDG_CONFIG_HOME=os.path.join(tdir, '.config'),
XDG_CONFIG_DIRS=os.path.join(tdir, '.config'),
XDG_DATA_DIRS=os.path.join(tdir, '.local', 'xdg'),
XDG_CACHE_HOME=os.path.join(tdir, '.cache'),
PYTHON_FOR_TYPE_CHECK=PYTHON_FOR_TYPE_CHECK,
):
if os.path.isdir(gohome):
os.symlink(gohome, os.path.join(tdir, os.path.basename(gohome)))
m = importlib.import_module('kitty_tests.main') m = importlib.import_module('kitty_tests.main')
getattr(m, 'run_tests')() getattr(m, 'main')()
if __name__ == '__main__': if __name__ == '__main__':