1+ import imp
12import json
23import subprocess
34import sys
45import tempfile
56from pathlib import Path
67from time import time
8+ from typing import Tuple
79
810import 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 )
8595def 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):
125129def 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