1- from subprocess import PIPE
1+ import os
22import platform
3+ import re
4+ from subprocess import PIPE , STDOUT
35
4- from path import Path
56import pytest
7+ from path import Path
68
7- from nodely .bin import Command
89import nodely .bin
10+ from nodely import NodeCommandError
11+ from nodely .bin import Command
912
1013
1114WIN = platform .system () == 'Windows'
@@ -84,9 +87,20 @@ def test_Popen(
8487 universal_newlines = True )
8588
8689 out , err = process .communicate ()
90+ assert process .returncode is 0
8791 assert node_package_command_output_regex .match (out .strip ())
8892 assert not err
8993
94+ def test_Popen_non_zero_returncode (self , node_package_command ):
95+ command = Command (node_package_command )
96+ process = command .Popen (
97+ '--non-existent' , stdout = PIPE , stderr = PIPE ,
98+ universal_newlines = True )
99+
100+ out , err = process .communicate ()
101+ assert process .returncode is not 0
102+ assert not out and err
103+
90104 def test_call (
91105 self , capfd , node_package_command , node_package_command_args ,
92106 node_package_command_output_regex ):
@@ -97,15 +111,25 @@ def test_call(
97111 assert node_package_command_output_regex .match (out .strip ())
98112 assert not err
99113
100- def test_check_call (
101- self , capfd , node_package_command , node_package_command_args ,
102- node_package_command_output_regex ):
114+ def test_call_non_zero_returncode (self , capfd , node_package_command ,):
103115 command = Command (node_package_command )
104- assert command .check_call ( node_package_command_args ) is None
116+ assert command .call ([ '--non-existent' ] ) is not 0
105117
106118 out , err = capfd .readouterr ()
107- assert node_package_command_output_regex .match (out .strip ())
108- assert not err
119+ assert not out and err
120+
121+ def test_check_call_raises (self , capfd , node_package_command ):
122+ command = Command (node_package_command )
123+ with pytest .raises (NodeCommandError , match = (
124+ r"^Command '\[[^,]+, '--non-existent'\]' "
125+ r"returned non-zero exit status -?\d+ "
126+ r"in working directory {}$"
127+ .format (re .escape (repr (os .getcwd ()))))):
128+
129+ command .check_call (['--non-existent' ])
130+
131+ out , err = capfd .readouterr ()
132+ assert not out and err
109133
110134 def test_check_output (
111135 self , capfd , node_package_command , node_package_command_args ,
@@ -117,12 +141,38 @@ def test_check_output(
117141 out , err = capfd .readouterr ()
118142 assert not out and not err
119143
144+ def test_check_output_raises (self , capfd , node_package_command ):
145+ command = Command (node_package_command )
146+ with pytest .raises (NodeCommandError , match = (
147+ r"^Command '\[[^,]+, '--non-existent'\]' "
148+ r"returned non-zero exit status -?\d+ "
149+ r"in working directory {}$"
150+ .format (re .escape (repr (os .getcwd ()))))):
151+
152+ command .check_output (['--non-existent' ], stderr = STDOUT )
153+
154+ out , err = capfd .readouterr ()
155+ assert not out and not err
156+
120157 def test__call__ (
121158 self , capfd , node_package_command , node_package_command_args ,
122159 node_package_command_output_regex ):
123160 command = Command (node_package_command )
124161 assert node_package_command_output_regex .match (
125- command .check_output (node_package_command_args ))
162+ command (* node_package_command_args ))
163+
164+ out , err = capfd .readouterr ()
165+ assert not out and not err
166+
167+ def test__call__raises (self , capfd , node_package_command ):
168+ command = Command (node_package_command )
169+ with pytest .raises (NodeCommandError , match = (
170+ r"^Command '\[[^,]+, '--non-existent', '--and-invalid'\]' "
171+ r"returned non-zero exit status -?\d+ "
172+ r"in working directory {}$"
173+ .format (re .escape (repr (os .getcwd ()))))):
174+
175+ command ('--non-existent' , '--and-invalid' , stderr = STDOUT )
126176
127177 out , err = capfd .readouterr ()
128178 assert not out and not err
0 commit comments