Skip to content

Commit 6ef6977

Browse files
committed
Standardize stdout mocking
1 parent 4268572 commit 6ef6977

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed

tests/test_check_autopkg_recipes.py

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import os
32
import tempfile
43
import unittest
@@ -24,34 +23,35 @@ def test_validate_recipe_prefix_passes_with_valid_prefix(self):
2423

2524
def test_validate_recipe_prefix_fails_with_invalid_prefix(self):
2625
recipe = {"Identifier": "foo.test.recipe"}
27-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
26+
with mock.patch("builtins.print") as mock_print:
2827
result = target.validate_recipe_prefix(recipe, "file.recipe", ["local."])
29-
out = mock_stdout.getvalue()
3028
self.assertFalse(result)
31-
self.assertIn("file.recipe: identifier does not start with local.", out)
29+
mock_print.assert_called_with(
30+
"file.recipe: identifier does not start with local."
31+
)
3232

3333
def test_validate_recipe_prefix_multiple_prefixes(self):
3434
recipe = {"Identifier": "foo.test.recipe"}
35-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
35+
with mock.patch("builtins.print") as mock_print:
3636
result = target.validate_recipe_prefix(
3737
recipe, "file.recipe", ["local.", "bar."]
3838
)
39-
out = mock_stdout.getvalue()
4039
self.assertFalse(result)
41-
self.assertIn('one of: "local.", "bar."', out)
40+
mock_print.assert_called_with(
41+
'file.recipe: identifier does not start with one of: "local.", "bar."'
42+
)
4243

4344
def test_validate_comments_warns_on_html_comment_non_strict(self):
4445
with tempfile.NamedTemporaryFile("w+", delete=False, suffix=".recipe") as tf:
4546
tf.write('{"Identifier": "local.test.recipe"} <!-- comment -->')
4647
tf.flush()
4748
tf_name = tf.name
4849
try:
49-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
50+
with mock.patch("builtins.print") as mock_print:
5051
result = target.validate_comments(tf_name, strict=False)
51-
out = mock_stdout.getvalue()
5252
self.assertTrue(result)
53-
self.assertIn(
54-
"WARNING: Recommend converting from <!-- --> style comments", out
53+
mock_print.assert_called_with(
54+
f"{tf_name}: WARNING: Recommend converting from <!-- --> style comments to a Comment key."
5555
)
5656
finally:
5757
os.unlink(tf_name)
@@ -62,11 +62,12 @@ def test_validate_comments_fails_on_html_comment_strict(self):
6262
tf.flush()
6363
tf_name = tf.name
6464
try:
65-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
65+
with mock.patch("builtins.print") as mock_print:
6666
result = target.validate_comments(tf_name, strict=True)
67-
out = mock_stdout.getvalue()
6867
self.assertFalse(result)
69-
self.assertIn("Convert from <!-- --> style comments to a Comment key", out)
68+
mock_print.assert_called_with(
69+
f"{tf_name}: Convert from <!-- --> style comments to a Comment key."
70+
)
7071
finally:
7172
os.unlink(tf_name)
7273

@@ -87,54 +88,55 @@ def test_validate_processor_keys_passes(self):
8788

8889
def test_validate_processor_keys_fails(self):
8990
process = [{"Processor": "TestProc"}, {"Arg": "val"}]
90-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
91+
with mock.patch("builtins.print") as mock_print:
9192
result = target.validate_processor_keys(process, "file.recipe")
92-
out = mock_stdout.getvalue()
9393
self.assertFalse(result)
94-
self.assertIn('missing "Processor" key', out)
94+
mock_print.assert_called_with(
95+
"file.recipe: Item in processor array is missing \"Processor\" key:\n{'Arg': 'val'}"
96+
)
9597

9698
def test_validate_endofcheckphase_passes_no_downloader(self):
9799
process = [{"Processor": "OtherProc"}]
98100
self.assertTrue(target.validate_endofcheckphase(process, "file.recipe"))
99101

100102
def test_validate_endofcheckphase_fails_missing_endofcheck(self):
101103
process = [{"Processor": "URLDownloader"}]
102-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
104+
with mock.patch("builtins.print") as mock_print:
103105
result = target.validate_endofcheckphase(process, "file.recipe")
104-
out = mock_stdout.getvalue()
105106
self.assertFalse(result)
106-
self.assertIn(
107-
"Contains a download processor, but no EndOfCheckPhase processor", out
107+
mock_print.assert_called_with(
108+
"file.recipe: Contains a download processor, but no EndOfCheckPhase processor."
108109
)
109110

110111
def test_validate_endofcheckphase_fails_wrong_order(self):
111112
process = [
112113
{"Processor": "EndOfCheckPhase"},
113114
{"Processor": "URLDownloader"},
114115
]
115-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
116+
with mock.patch("builtins.print") as mock_print:
116117
result = target.validate_endofcheckphase(process, "file.recipe")
117-
out = mock_stdout.getvalue()
118118
self.assertFalse(result)
119-
self.assertIn("EndOfCheckPhase typically goes after a download processor", out)
119+
mock_print.assert_called_with(
120+
"file.recipe: EndOfCheckPhase typically goes after a download processor, not before."
121+
)
120122

121123
def test_validate_minimumversion_non_string(self):
122124
process = [{"Processor": "AppPkgCreator"}]
123-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
125+
with mock.patch("builtins.print") as mock_print:
124126
result = target.validate_minimumversion(process, 1.0, "1.0", "file.recipe")
125-
out = mock_stdout.getvalue()
126127
self.assertFalse(result)
127-
self.assertIn("MinimumVersion should be a string", out)
128+
mock_print.assert_called_with("file.recipe: MinimumVersion should be a string.")
128129

129130
def test_validate_minimumversion_too_low(self):
130131
process = [{"Processor": "AppPkgCreator"}]
131-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
132+
with mock.patch("builtins.print") as mock_print:
132133
result = target.validate_minimumversion(
133134
process, "0.5", "1.0", "file.recipe"
134135
)
135-
out = mock_stdout.getvalue()
136136
self.assertFalse(result)
137-
self.assertIn("AppPkgCreator processor requires minimum AutoPkg version", out)
137+
mock_print.assert_called_with(
138+
"file.recipe: AppPkgCreator processor requires minimum AutoPkg version 1.0"
139+
)
138140

139141
def test_validate_minimumversion_passes(self):
140142
process = [{"Processor": "AppPkgCreator"}]
@@ -143,19 +145,21 @@ def test_validate_minimumversion_passes(self):
143145

144146
def test_validate_no_deprecated_procs_warns(self):
145147
process = [{"Processor": "CURLDownloader"}]
146-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
148+
with mock.patch("builtins.print") as mock_print:
147149
result = target.validate_no_deprecated_procs(process, "file.recipe")
148-
out = mock_stdout.getvalue()
149150
self.assertTrue(result)
150-
self.assertIn("WARNING: Deprecated processor CURLDownloader is used", out)
151+
mock_print.assert_called_with(
152+
"file.recipe: WARNING: Deprecated processor CURLDownloader is used."
153+
)
151154

152155
def test_validate_no_superclass_procs_warns(self):
153156
process = [{"Processor": "URLGetter"}]
154-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
157+
with mock.patch("builtins.print") as mock_print:
155158
result = target.validate_no_superclass_procs(process, "file.recipe")
156-
out = mock_stdout.getvalue()
157159
self.assertTrue(result)
158-
self.assertIn("intended to be used by other processors", out)
160+
mock_print.assert_called_with(
161+
"file.recipe: WARNING: The processor URLGetter is intended to be used by other processors, not used directly in recipes."
162+
)
159163

160164
def test_validate_jamf_processor_order_warns(self):
161165
process = [
@@ -166,11 +170,12 @@ def test_validate_jamf_processor_order_warns(self):
166170
"Processor": "com.github.grahampugh.jamf-upload.processors/JamfCategoryUploader"
167171
},
168172
]
169-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
173+
with mock.patch("builtins.print") as mock_print:
170174
result = target.validate_jamf_processor_order(process, "file.recipe")
171-
out = mock_stdout.getvalue()
172175
self.assertTrue(result)
173-
self.assertIn("JamfUploader processors are not in the recommended order", out)
176+
mock_print.assert_called_with(
177+
"file.recipe: WARNING: JamfUploader processors are not in the recommended order: JamfCategoryUploader, JamfPolicyUploader."
178+
)
174179

175180
def test_validate_no_var_in_app_path_fails(self):
176181
process = [
@@ -179,11 +184,12 @@ def test_validate_no_var_in_app_path_fails(self):
179184
"Arguments": {"input_path": "/Applications/%NAME%.app"},
180185
}
181186
]
182-
with mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
187+
with mock.patch("builtins.print") as mock_print:
183188
result = target.validate_no_var_in_app_path(process, "file.recipe")
184-
out = mock_stdout.getvalue()
185189
self.assertFalse(result)
186-
self.assertIn("Use actual app name instead of %NAME%.app", out)
190+
mock_print.assert_called_with(
191+
"file.recipe: Use actual app name instead of %NAME%.app in CodeSignatureVerifier processor argument."
192+
)
187193

188194
def test_validate_no_var_in_app_path_passes(self):
189195
process = [

0 commit comments

Comments
 (0)