From 7664f2ee536dd9e0774380c5a43eca22a5b99219 Mon Sep 17 00:00:00 2001 From: bogdandm Date: Wed, 28 Nov 2018 15:12:44 +0300 Subject: [PATCH] Evaluate generated code in tests --- setup.py | 2 +- test/test_cli/test_script.py | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index 3f7add9..d2a9070 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ def run_tests(self): }, install_requires=required, cmdclass={"test": PyTest}, - tests_require=["pytest", "requests"], + tests_require=["pytest", "requests", "attrs"], project_urls={ 'Source': URL }, diff --git a/test/test_cli/test_script.py b/test/test_cli/test_script.py index 7177b87..0832b69 100644 --- a/test/test_cli/test_script.py +++ b/test/test_cli/test_script.py @@ -1,9 +1,11 @@ +import imp import json import subprocess import sys import tempfile from pathlib import Path from time import time +from typing import Tuple import pytest @@ -71,24 +73,29 @@ def test_help(): ] -@pytest.mark.parametrize("command", test_commands) -def test_script(command): - proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = proc.communicate() +def _validate_result(proc: subprocess.Popen) -> Tuple[str, str]: + stdout, stderr = map(bytes.decode, proc.communicate()) assert not stderr, stderr assert stdout, stdout assert proc.returncode == 0 - print(stdout.decode()) + # Note: imp package is deprecated but I can't find a way to create dummy module using importlib + module = imp.new_module("model") + exec(compile(stdout, "model.py", "exec"), module.__dict__) + return stdout, stderr + + +@pytest.mark.parametrize("command", test_commands) +def test_script(command): + proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = _validate_result(proc) + print(stdout) @pytest.mark.parametrize("command", test_commands) def test_script_attrs(command): command += " -f attrs" proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = map(bytes.decode, proc.communicate()) - assert not stderr, stderr - assert stdout, stdout - assert proc.returncode == 0 + stdout, stderr = _validate_result(proc) assert "@attr.s" in stdout print(stdout) @@ -98,10 +105,7 @@ def test_script_custom(command): command += " -f custom --code-generator json_to_models.models.attr.AttrsModelCodeGenerator" command += ' --code-generator-kwargs "meta=true"' proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = map(bytes.decode, proc.communicate()) - assert not stderr, stderr - assert stdout, stdout - assert proc.returncode == 0 + stdout, stderr = _validate_result(proc) assert "@attr.s" in stdout print(stdout) @@ -125,5 +129,4 @@ def test_script_custom(command): def test_wrong_arguments(command): print("Command:", command) proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = map(bytes.decode, proc.communicate()) - assert not stderr and proc.returncode == 0, stderr + _validate_result(proc)