14
14
import gc
15
15
import os
16
16
import os .path as osp
17
+ from pathlib import Path
17
18
import random
18
19
import re
19
20
import shutil
@@ -2535,9 +2536,15 @@ def example_def_2():
2535
2536
2536
2537
2537
2538
@flaky (max_runs = 3 )
2538
- def test_switcher_project_files (main_window , qtbot , tmpdir ):
2539
- """Test the number of items in the switcher when a project is active."""
2540
- # Wait until the window is fully up
2539
+ def test_switcher_projects_integration (main_window , pytestconfig , qtbot ,
2540
+ tmp_path ):
2541
+ """Test integration between the Switcher and Projects plugins."""
2542
+ # Disable pytest stdin capture to make calls to fzf work. Idea taken from:
2543
+ # https://github.com/pytest-dev/pytest/issues/2189#issuecomment-449512764
2544
+ capmanager = pytestconfig .pluginmanager .getplugin ('capturemanager' )
2545
+ capmanager .suspend_global_capture (in_ = True )
2546
+
2547
+ # Wait until the console is fully up
2541
2548
shell = main_window .ipyconsole .get_current_shellwidget ()
2542
2549
qtbot .waitUntil (
2543
2550
lambda : shell .spyder_kernel_ready and shell ._prompt_html is not None ,
@@ -2550,47 +2557,107 @@ def test_switcher_project_files(main_window, qtbot, tmpdir):
2550
2557
editorstack = main_window .editor .get_current_editorstack ()
2551
2558
2552
2559
# Create a temp project directory
2553
- project_dir = to_text_string (tmpdir .mkdir ('test' ))
2560
+ project_dir = tmp_path / 'test-projects-switcher'
2561
+ project_dir .mkdir ()
2562
+
2563
+ # Create some empty files in the project dir
2564
+ n_files_project = 3
2565
+ for i in range (n_files_project ):
2566
+ fpath = project_dir / f"test_file{ i } .py"
2567
+ fpath .touch ()
2568
+
2569
+ # Copy binary file from our source tree to the project to check it's not
2570
+ # displayed in the switcher.
2571
+ binary_file = Path (LOCATION ).parents [1 ] / 'images' / 'windows_app_icon.ico'
2572
+ binary_file_copy = project_dir / 'windows.ico'
2573
+ shutil .copyfile (binary_file , binary_file_copy )
2554
2574
2555
2575
# Create project
2556
2576
with qtbot .waitSignal (projects .sig_project_loaded ):
2557
- projects .create_project (project_dir )
2577
+ projects .create_project (str ( project_dir ) )
2558
2578
2559
- # Create four empty files in the project dir
2560
- for i in range (3 ):
2561
- main_window .editor .new ("test_file" + str (i )+ ".py" )
2579
+ # Check that the switcher has been populated in Projects
2580
+ qtbot .waitUntil (
2581
+ lambda : projects .get_widget ()._default_switcher_paths != [],
2582
+ timeout = 1000
2583
+ )
2562
2584
2585
+ # Assert that the number of items in the switcher is correct
2563
2586
switcher .open_switcher ()
2564
- n_files_project = len (projects .get_project_filenames ())
2565
2587
n_files_open = editorstack .get_stack_count ()
2588
+ assert switcher .count () == n_files_open + n_files_project
2589
+ switcher .on_close ()
2566
2590
2567
- # Assert that the number of items in the switcher is correct
2568
- assert switcher_widget .model .rowCount () == n_files_open + n_files_project
2591
+ # Assert only two items have visible sections
2592
+ switcher .open_switcher ()
2593
+
2594
+ sections = []
2595
+ for row in range (switcher .count ()):
2596
+ item = switcher_widget .model .item (row )
2597
+ if item ._section_visible :
2598
+ sections .append (item .get_section ())
2599
+
2600
+ assert len (sections ) == 2
2569
2601
switcher .on_close ()
2570
2602
2571
- # Close all files opened in editorstack
2572
- main_window .editor .close_all_files ()
2603
+ # Assert searching text in the switcher works as expected
2604
+ switcher .open_switcher ()
2605
+ switcher .set_search_text ('0' )
2606
+ qtbot .wait (500 )
2607
+ assert switcher .count () == 1
2608
+ switcher .on_close ()
2573
2609
2610
+ # Assert searching for a non-existent file leaves the switcher empty
2574
2611
switcher .open_switcher ()
2575
- n_files_project = len ( projects . get_project_filenames () )
2576
- n_files_open = editorstack . get_stack_count ( )
2577
- assert switcher_widget . model . rowCount () == n_files_open + n_files_project
2612
+ switcher . set_search_text ( 'foo' )
2613
+ qtbot . wait ( 500 )
2614
+ assert switcher . count () == 0
2578
2615
switcher .on_close ()
2579
2616
2580
- # Select file in the project explorer
2617
+ # Assert searching for a binary file leaves the switcher empty
2618
+ switcher .open_switcher ()
2619
+ switcher .set_search_text ('windows' )
2620
+ qtbot .wait (500 )
2621
+ assert switcher .count () == 0
2622
+ switcher .on_close ()
2623
+
2624
+ # Remove project file and check the switcher is updated
2625
+ n_files_project -= 1
2626
+ os .remove (str (project_dir / 'test_file1.py' ))
2627
+ qtbot .wait (500 )
2628
+ switcher .open_switcher ()
2629
+ assert switcher .count () == n_files_open + n_files_project
2630
+ switcher .on_close ()
2631
+
2632
+ # Check that a project file opened in the editor is not shown twice in the
2633
+ # switcher
2581
2634
idx = projects .get_widget ().treewidget .get_index (
2582
- osp .join (project_dir , 'test_file0.py' ))
2635
+ str (project_dir / 'test_file0.py' )
2636
+ )
2583
2637
projects .get_widget ().treewidget .setCurrentIndex (idx )
2584
-
2585
- # Press Enter there
2586
2638
qtbot .keyClick (projects .get_widget ().treewidget , Qt .Key_Enter )
2587
2639
2588
2640
switcher .open_switcher ()
2589
- n_files_project = len (projects .get_project_filenames ())
2590
2641
n_files_open = editorstack .get_stack_count ()
2591
- assert switcher_widget . model . rowCount () == n_files_open + n_files_project
2642
+ assert switcher . count () == n_files_open + n_files_project - 1
2592
2643
switcher .on_close ()
2593
2644
2645
+ # Check the switcher works without fzf
2646
+ fzf = projects .get_widget ()._fzf
2647
+ projects .get_widget ()._fzf = None
2648
+ projects .get_widget ()._default_switcher_paths = []
2649
+
2650
+ switcher .open_switcher ()
2651
+ switcher .set_search_text ('0' )
2652
+ qtbot .wait (500 )
2653
+ assert switcher .count () == 1
2654
+ switcher .on_close ()
2655
+
2656
+ projects .get_widget ()._fzf = fzf
2657
+
2658
+ # Resume capturing
2659
+ capmanager .resume_global_capture ()
2660
+
2594
2661
2595
2662
@flaky (max_runs = 3 )
2596
2663
@pytest .mark .skipif (sys .platform == 'darwin' ,
0 commit comments