@@ -529,28 +529,31 @@ def test_autoprofile_exec_module(
529
529
assert ('Function: _main' in raw_output ) == main
530
530
531
531
532
+ @pytest .mark .parametrize ('view' , [True , False ])
533
+ @pytest .mark .parametrize ('prof_mod' , [True , False ])
532
534
@pytest .mark .parametrize (
533
535
('outfile' , 'expected_outfile' ),
534
536
[(None , 'kernprof-stdin-*.lprof' ),
535
537
('test-stdin.lprof' , 'test-stdin.lprof' )])
536
538
def test_autoprofile_from_stdin (
537
- outfile , expected_outfile ,
538
- ) -> None :
539
+ outfile , expected_outfile , prof_mod , view ) -> None :
539
540
"""
540
541
Test the profiling of a script read from stdin.
541
542
"""
542
543
with tempfile .TemporaryDirectory () as tmpdir :
543
544
temp_dpath = ub .Path (tmpdir )
544
545
546
+ kp_cmd = [sys .executable , '-m' , 'kernprof' , '-l' ]
547
+ if prof_mod :
548
+ kp_cmd += ['-p' 'test_mod.submod1,test_mod.subpkg.submod3' ]
549
+ if outfile :
550
+ kp_cmd += ['-o' , outfile ]
551
+ if view :
552
+ kp_cmd += ['-v' ]
553
+ kp_cmd += ['-' ]
545
554
with ub .ChDir (temp_dpath ):
546
555
script_fpath = _write_demo_module (ub .Path ())
547
-
548
- args = [sys .executable , '-m' , 'kernprof' ,
549
- '-p' , 'test_mod.submod1,test_mod.subpkg.submod3' , '-l' ]
550
- if outfile :
551
- args += ['-o' , outfile ]
552
- args += ['-' ]
553
- proc = subprocess .run (args ,
556
+ proc = subprocess .run (kp_cmd ,
554
557
input = script_fpath .read_text (),
555
558
text = True ,
556
559
capture_output = True )
@@ -559,25 +562,28 @@ def test_autoprofile_from_stdin(
559
562
proc .check_returncode ()
560
563
561
564
outfile , = temp_dpath .glob (expected_outfile )
562
- args = [sys .executable , '-m' , 'line_profiler' , str (outfile )]
563
- proc = ub .cmd (args )
564
- raw_output = proc .stdout
565
- print (raw_output )
566
- proc .check_returncode ()
565
+ if view :
566
+ raw_output = proc .stdout
567
+ else :
568
+ lp_cmd = [sys .executable , '-m' , 'line_profiler' , str (outfile )]
569
+ proc = ub .cmd (lp_cmd )
570
+ raw_output = proc .stdout
571
+ print (raw_output )
572
+ proc .check_returncode ()
567
573
568
- assert 'Function: add_one' in raw_output
574
+ assert ( 'Function: add_one' in raw_output ) == prof_mod
569
575
assert 'Function: add_two' not in raw_output
570
- assert 'Function: add_three' in raw_output
571
- assert 'Function: main' not in raw_output
576
+ assert ('Function: add_three' in raw_output ) == prof_mod
577
+ # If we're calling a separate process to view the results, the
578
+ # script file will already have been deleted
579
+ assert ('Function: main' in raw_output ) == view
572
580
573
581
574
582
@pytest .mark .parametrize (
575
583
('outfile' , 'expected_outfile' ),
576
584
[(None , 'kernprof-command-*.lprof' ),
577
585
('test-command.lprof' , 'test-command.lprof' )])
578
- def test_autoprofile_from_inlined_script (
579
- outfile , expected_outfile ,
580
- ) -> None :
586
+ def test_autoprofile_from_inlined_script (outfile , expected_outfile ) -> None :
581
587
"""
582
588
Test the profiling of an inlined script (supplied with the `-c`
583
589
flag).
@@ -587,32 +593,29 @@ def test_autoprofile_from_inlined_script(
587
593
588
594
_write_demo_module (temp_dpath )
589
595
590
- inlined_script = (
591
- 'from test_mod import submod1, submod2; '
592
- 'from test_mod.subpkg import submod3; '
593
- 'import statistics; '
594
- 'data = [1, 2, 3]; '
595
- 'val = submod1.add_one(data); '
596
- 'val = submod2.add_two(val); '
597
- 'val = submod3.add_three(val); '
598
- 'result = statistics.harmonic_mean(val); '
599
- 'print(result); '
600
- )
601
- prof_file = 'test-command.lprof'
602
-
603
- args = [sys .executable , '-m' , 'kernprof' ,
604
- '-p' , 'test_mod.submod1,test_mod.subpkg.submod3' , '-l' ]
596
+ inlined_script = ('from test_mod import submod1, submod2; '
597
+ 'from test_mod.subpkg import submod3; '
598
+ 'import statistics; '
599
+ 'data = [1, 2, 3]; '
600
+ 'val = submod1.add_one(data); '
601
+ 'val = submod2.add_two(val); '
602
+ 'val = submod3.add_three(val); '
603
+ 'result = statistics.harmonic_mean(val); '
604
+ 'print(result);' )
605
+
606
+ kp_cmd = [sys .executable , '-m' , 'kernprof' ,
607
+ '-p' , 'test_mod.submod1,test_mod.subpkg.submod3' , '-l' ]
605
608
if outfile :
606
- args += ['-o' , outfile ]
607
- args += ['-c' , inlined_script ]
608
- proc = ub .cmd (args , cwd = temp_dpath , verbose = 2 )
609
+ kp_cmd += ['-o' , outfile ]
610
+ kp_cmd += ['-c' , inlined_script ]
611
+ proc = ub .cmd (kp_cmd , cwd = temp_dpath , verbose = 2 )
609
612
print (proc .stdout )
610
613
print (proc .stderr )
611
614
proc .check_returncode ()
612
615
613
616
outfile , = temp_dpath .glob (expected_outfile )
614
- args = [sys .executable , '-m' , 'line_profiler' , str (outfile )]
615
- proc = ub .cmd (args )
617
+ lp_cmd = [sys .executable , '-m' , 'line_profiler' , str (outfile )]
618
+ proc = ub .cmd (lp_cmd )
616
619
raw_output = proc .stdout
617
620
print (raw_output )
618
621
proc .check_returncode ()
0 commit comments