55import unittest
66import os
77import subprocess
8+ import tempfile
9+ import shutil
810from difflib import unified_diff
911from io import StringIO
1012from test .support .os_helper import TESTFN , unlink , temp_dir , change_cwd
@@ -136,7 +138,9 @@ class ProfileCLITests(unittest.TestCase):
136138 """Tests for the profile module's command line interface."""
137139
138140 def setUp (self ):
139- # Create a simple Python script to profile
141+ self .temp_dir = tempfile .mkdtemp ()
142+ self .addCleanup (shutil .rmtree , self .temp_dir )
143+
140144 self .script_content = """\
141145 def factorial(n):
142146 if n <= 1:
@@ -146,14 +150,12 @@ def factorial(n):
146150if __name__ == "__main__":
147151 factorial(10)
148152"""
149- self .script_file = TESTFN
153+ self .script_file = os . path . join ( self . temp_dir , "factorial_script.py" )
150154 with open (self .script_file , "w" ) as f :
151155 f .write (self .script_content )
152- self .addCleanup (unlink , self .script_file )
153156
154157 def _run_profile_cli (self , * args ):
155- """Helper to run the profile CLI with given arguments."""
156- cmd = [sys .executable , '-m' , 'profile' ] + list (args )
158+ cmd = [sys .executable , '-m' , 'profile' , * args ]
157159 proc = subprocess .Popen (
158160 cmd ,
159161 stdout = subprocess .PIPE ,
@@ -164,20 +166,17 @@ def _run_profile_cli(self, *args):
164166 return proc .returncode , stdout , stderr
165167
166168 def test_basic_profile (self ):
167- """Test basic profiling of a script."""
168169 returncode , stdout , stderr = self ._run_profile_cli (self .script_file )
169170 self .assertEqual (returncode , 0 )
170171 self .assertIn ("function calls" , stdout )
171172 self .assertIn ("factorial" , stdout )
173+ self .assertIn ("ncalls" , stdout )
172174
173175 def test_sort_options (self ):
174- """Test different sort options."""
175- # List of sort options known to work
176176 sort_options = ['calls' , 'cumulative' , 'cumtime' , 'file' ,
177177 'filename' , 'module' , 'ncalls' , 'pcalls' ,
178178 'line' , 'stdname' , 'time' , 'tottime' ]
179179
180- # Test each sort option individually
181180 for option in sort_options :
182181 with self .subTest (sort_option = option ):
183182 returncode , stdout , stderr = self ._run_profile_cli (
@@ -187,34 +186,28 @@ def test_sort_options(self):
187186 self .assertIn ("function calls" , stdout )
188187
189188 def test_output_file (self ):
190- """Test writing profile results to a file."""
191- output_file = TESTFN + '.prof'
192- self .addCleanup (unlink , output_file )
189+ output_file = os .path .join (self .temp_dir , "profile_output.prof" )
193190
194191 returncode , stdout , stderr = self ._run_profile_cli (
195192 '-o' , output_file , self .script_file
196193 )
197194 self .assertEqual (returncode , 0 )
198195
199- # Check that the output file exists and contains profile data
200196 self .assertTrue (os .path .exists (output_file ))
201197 stats = pstats .Stats (output_file )
202198 self .assertGreater (stats .total_calls , 0 )
203199
204200 def test_invalid_option (self ):
205- """Test behavior with an invalid option."""
206201 returncode , stdout , stderr = self ._run_profile_cli (
207202 '--invalid-option' , self .script_file
208203 )
209204 self .assertNotEqual (returncode , 0 )
210205 self .assertIn ("error" , stderr .lower ())
211206
212207 def test_no_arguments (self ):
213- """Test behavior with no arguments."""
214208 returncode , stdout , stderr = self ._run_profile_cli ()
215209 self .assertNotEqual (returncode , 0 )
216210
217- # Check either stdout or stderr for usage information
218211 combined_output = stdout .lower () + stderr .lower ()
219212 self .assertTrue (
220213 "usage:" in combined_output or
@@ -224,19 +217,30 @@ def test_no_arguments(self):
224217 )
225218
226219 def test_run_module (self ):
227- """Test profiling a module with -m option."""
228- # Create a small module
229- module_name = "test_profile_module"
230- module_file = f"{ module_name } .py"
231-
220+ module_name = "profilemod"
221+ module_file = os .path .join (self .temp_dir , f"{ module_name } .py" )
222+
232223 with open (module_file , "w" ) as f :
233224 f .write ("print('Module executed')\n " )
234-
235- self .addCleanup (unlink , module_file )
236-
237- returncode , stdout , stderr = self ._run_profile_cli (
238- '-m' , module_name
225+
226+ env = os .environ .copy ()
227+
228+ python_path = self .temp_dir
229+ if 'PYTHONPATH' in env :
230+ python_path = os .pathsep .join ([python_path , env ['PYTHONPATH' ]])
231+ env ['PYTHONPATH' ] = python_path
232+
233+ cmd = [sys .executable , '-m' , 'profile' , '-m' , module_name ]
234+ proc = subprocess .Popen (
235+ cmd ,
236+ stdout = subprocess .PIPE ,
237+ stderr = subprocess .PIPE ,
238+ universal_newlines = True ,
239+ env = env
239240 )
241+ stdout , stderr = proc .communicate ()
242+ returncode = proc .returncode
243+
240244 self .assertEqual (returncode , 0 )
241245 self .assertIn ("Module executed" , stdout )
242246 self .assertIn ("function calls" , stdout )
0 commit comments