@@ -80,7 +80,6 @@ def _create_infile(self):
80
80
def test_infile_stdout (self ):
81
81
infile = self ._create_infile ()
82
82
rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile )
83
- self .assertEqual (rc , 0 )
84
83
self .assertEqual (out .splitlines (), self .expect .encode ().splitlines ())
85
84
self .assertEqual (err , b'' )
86
85
@@ -91,14 +90,12 @@ def test_infile_outfile(self):
91
90
self .addCleanup (os .remove , outfile )
92
91
with open (outfile , "r" ) as fp :
93
92
self .assertEqual (fp .read (), self .expect )
94
- self .assertEqual (rc , 0 )
95
93
self .assertEqual (out , b'' )
96
94
self .assertEqual (err , b'' )
97
95
98
96
def test_infile_same_outfile (self ):
99
97
infile = self ._create_infile ()
100
98
rc , out , err = assert_python_ok ('-m' , 'json.tool' , infile , infile )
101
- self .assertEqual (rc , 0 )
102
99
self .assertEqual (out , b'' )
103
100
self .assertEqual (err , b'' )
104
101
@@ -107,40 +104,23 @@ def test_unavailable_outfile(self):
107
104
rc , out , err = assert_python_failure ('-m' , 'json.tool' , infile , '/bla/outfile' )
108
105
self .assertEqual (rc , 2 )
109
106
self .assertEqual (out , b'' )
110
- err = err .decode ().splitlines ()
111
- self .assertEqual (err [0 ], 'usage: python -m json.tool [-h] [--sort-keys] [infile] [outfile]' )
112
- self .assertEqual (err [1 ], "python -m json.tool: error: can't open '/bla/outfile': [Errno 2] No such file or directory: '/bla/outfile'" )
107
+ self .assertIn (b"error: can't open '/bla/outfile': [Errno 2]" , err )
113
108
114
109
def test_help_flag (self ):
115
110
rc , out , err = assert_python_ok ('-m' , 'json.tool' , '-h' )
116
- self .assertEqual (rc , 0 )
117
111
self .assertTrue (out .startswith (b'usage: ' ))
118
112
self .assertEqual (err , b'' )
119
113
120
114
def test_sort_keys_flag (self ):
121
115
infile = self ._create_infile ()
122
116
rc , out , err = assert_python_ok ('-m' , 'json.tool' , '--sort-keys' , infile )
123
- self .assertEqual (rc , 0 )
124
117
self .assertEqual (out .splitlines (),
125
118
self .expect_without_sort_keys .encode ().splitlines ())
126
119
self .assertEqual (err , b'' )
127
120
128
121
def test_no_fd_leak_infile_outfile (self ):
129
- closed = []
130
- opened = []
131
- io_open = io .open
132
-
133
- def open (* args , ** kwargs ):
134
- fd = io_open (* args , ** kwargs )
135
- opened .append (fd )
136
- fd_close = fd .close
137
- def close (self ):
138
- closed .append (self )
139
- fd_close ()
140
- fd .close = types .MethodType (close , fd )
141
- return fd
142
-
143
122
infile = self ._create_infile ()
123
+ closed , opened , open = mock_open ()
144
124
with mock .patch ('builtins.open' , side_effect = open ):
145
125
with mock .patch .object (sys , 'argv' , ['tool.py' , infile , infile + '.out' ]):
146
126
import json .tool
@@ -151,28 +131,31 @@ def close(self):
151
131
self .assertEqual (len (opened ), len (closed ))
152
132
153
133
def test_no_fd_leak_same_infile_outfile (self ):
154
- closed = []
155
- opened = []
156
- io_open = io .open
157
-
158
- def open (* args , ** kwargs ):
159
- fd = io_open (* args , ** kwargs )
160
- opened .append (fd )
161
- fd_close = fd .close
162
- def close (self ):
163
- closed .append (self )
164
- fd_close ()
165
- fd .close = types .MethodType (close , fd )
166
- return fd
167
-
168
134
infile = self ._create_infile ()
135
+ closed , opened , open = mock_open ()
169
136
with mock .patch ('builtins.open' , side_effect = open ):
170
137
with mock .patch .object (sys , 'argv' , ['tool.py' , infile , infile ]):
171
138
try :
172
139
import json .tool
173
140
json .tool .main ()
174
- except SystemExit : # We expect SystemExit to happen on c9d43c
141
+ except SystemExit :
175
142
pass
176
143
177
144
self .assertEqual (opened , closed )
178
145
self .assertEqual (len (opened ), len (closed ))
146
+
147
+ def mock_open ():
148
+ closed = []
149
+ opened = []
150
+ io_open = io .open
151
+
152
+ def _open (* args , ** kwargs ):
153
+ fd = io_open (* args , ** kwargs )
154
+ opened .append (fd )
155
+ fd_close = fd .close
156
+ def close (self ):
157
+ closed .append (self )
158
+ fd_close ()
159
+ fd .close = types .MethodType (close , fd )
160
+ return fd
161
+ return closed , opened , _open
0 commit comments