Skip to content

Commit 66a25c8

Browse files
authored
Merge pull request #13 from bogdandm/eval_generated_code
Evaluate generated code in tests
2 parents bb6da51 + 59c8e39 commit 66a25c8

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def run_tests(self):
3939
},
4040
install_requires=required,
4141
cmdclass={"test": PyTest},
42-
tests_require=["pytest", "requests"],
42+
tests_require=["pytest", "requests", "attrs"],
4343
project_urls={
4444
'Source': URL
4545
},

test/test_cli/test_script.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import imp
12
import json
23
import subprocess
34
import sys
45
import tempfile
56
from pathlib import Path
67
from time import time
8+
from typing import Tuple
79

810
import pytest
911

@@ -71,24 +73,29 @@ def test_help():
7173
]
7274

7375

74-
@pytest.mark.parametrize("command", test_commands)
75-
def test_script(command):
76-
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
77-
stdout, stderr = proc.communicate()
76+
def _validate_result(proc: subprocess.Popen) -> Tuple[str, str]:
77+
stdout, stderr = map(bytes.decode, proc.communicate())
7878
assert not stderr, stderr
7979
assert stdout, stdout
8080
assert proc.returncode == 0
81-
print(stdout.decode())
81+
# Note: imp package is deprecated but I can't find a way to create dummy module using importlib
82+
module = imp.new_module("model")
83+
exec(compile(stdout, "model.py", "exec"), module.__dict__)
84+
return stdout, stderr
85+
86+
87+
@pytest.mark.parametrize("command", test_commands)
88+
def test_script(command):
89+
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
90+
stdout, stderr = _validate_result(proc)
91+
print(stdout)
8292

8393

8494
@pytest.mark.parametrize("command", test_commands)
8595
def test_script_attrs(command):
8696
command += " -f attrs"
8797
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
88-
stdout, stderr = map(bytes.decode, proc.communicate())
89-
assert not stderr, stderr
90-
assert stdout, stdout
91-
assert proc.returncode == 0
98+
stdout, stderr = _validate_result(proc)
9299
assert "@attr.s" in stdout
93100
print(stdout)
94101

@@ -98,10 +105,7 @@ def test_script_custom(command):
98105
command += " -f custom --code-generator json_to_models.models.attr.AttrsModelCodeGenerator"
99106
command += ' --code-generator-kwargs "meta=true"'
100107
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
101-
stdout, stderr = map(bytes.decode, proc.communicate())
102-
assert not stderr, stderr
103-
assert stdout, stdout
104-
assert proc.returncode == 0
108+
stdout, stderr = _validate_result(proc)
105109
assert "@attr.s" in stdout
106110
print(stdout)
107111

@@ -125,5 +129,4 @@ def test_script_custom(command):
125129
def test_wrong_arguments(command):
126130
print("Command:", command)
127131
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
128-
stdout, stderr = map(bytes.decode, proc.communicate())
129-
assert not stderr and proc.returncode == 0, stderr
132+
_validate_result(proc)

0 commit comments

Comments
 (0)