diff --git a/spyder/plugins/projects.py b/spyder/plugins/projects.py index f7595caed00..b77c0d840b5 100644 --- a/spyder/plugins/projects.py +++ b/spyder/plugins/projects.py @@ -179,7 +179,7 @@ def switch_to_plugin(self): self.toggle_view_action.setChecked(True) self.visibility_changed(True) - #------ Public API --------------------------------------------------------- + # ------ Public API ------------------------------------------------------- def setup_menu_actions(self): """Setup and update the menu actions.""" self.recent_project_menu.clear() @@ -188,15 +188,13 @@ def setup_menu_actions(self): for project in self.recent_projects: if self.is_valid_project(project): name = project.replace(get_home_dir(), '~') - - def slot(): - self.switch_to_plugin() - self.open_project(path=project) - - action = create_action(self, + action = create_action( + self, name, - icon = ima.icon('project'), - triggered=slot) + icon=ima.icon('project'), + triggered=( + lambda _, p=project: self.open_project(path=p)) + ) self.recent_projects_actions.append(action) else: self.recent_projects.remove(project) diff --git a/spyder/plugins/tests/test_projects.py b/spyder/plugins/tests/test_projects.py index 2edf6277bb3..3ca9785917f 100644 --- a/spyder/plugins/tests/test_projects.py +++ b/spyder/plugins/tests/test_projects.py @@ -120,5 +120,35 @@ def test_open_project_uses_visible_config(projects, tmpdir, value): assert projects.dockwidget.isVisible() == value +def test_recent_projects_menu_action(projects, tmpdir): + """ + Test that the actions of the submenu 'Recent Projects' in the 'Projects' + main menu are working as expected. + Regression test for Issue #8450. + """ + recent_projects_len = len(projects.recent_projects) + + # Create the directories. + path0 = to_text_string(tmpdir.mkdir('project0')) + path1 = to_text_string(tmpdir.mkdir('project1')) + path2 = to_text_string(tmpdir.mkdir('project2')) + + # Open projects in path0, path1, and path2. + projects.open_project(path=path0) + projects.open_project(path=path1) + projects.open_project(path=path2) + assert (len(projects.recent_projects_actions) == + recent_projects_len + 3 + 2) + assert projects.get_active_project().root_path == path2 + + # Trigger project1 in the list of Recent Projects actions. + projects.recent_projects_actions[1].trigger() + assert projects.get_active_project().root_path == path1 + + # Trigger project0 in the list of Recent Projects actions. + projects.recent_projects_actions[2].trigger() + assert projects.get_active_project().root_path == path0 + + if __name__ == "__main__": pytest.main()