Skip to content

PR: Add test for file directory change in the Project Explorer. #6413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 15, 2018
Merged
8 changes: 6 additions & 2 deletions spyder/widgets/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,14 +694,18 @@ def rename(self, fnames=None):
self.rename_file(fname)

@Slot()
def move(self, fnames=None):
def move(self, fnames=None, directory=None):
"""Move files/directories"""
if fnames is None:
fnames = self.get_selected_filenames()
orig = fixpath(osp.dirname(fnames[0]))
while True:
self.parent_widget.redirect_stdio.emit(False)
folder = getexistingdirectory(self, _("Select directory"), orig)
if directory is None:
folder = getexistingdirectory(self, _("Select directory"),
orig)
else:
folder = directory
self.parent_widget.redirect_stdio.emit(True)
if folder:
folder = fixpath(folder)
Expand Down
7 changes: 5 additions & 2 deletions spyder/widgets/projects/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,16 @@ def delete_project(self):
# Tests
#==============================================================================
class ProjectExplorerTest(QWidget):
def __init__(self):
def __init__(self, directory=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make directory an attribute of this class, i.e.

self.directory = directory 

QWidget.__init__(self)
vlayout = QVBoxLayout()
self.setLayout(vlayout)

self.explorer = ProjectExplorerWidget(None, show_all=True)
self.explorer.setup_project(osp.dirname(osp.abspath(__file__)))
if directory:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if direcotory: -> if self.directory is not None:

self.explorer.setup_project(directory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directory -> self.directory

else:
self.explorer.setup_project(osp.dirname(osp.abspath(__file__)))
vlayout.addWidget(self.explorer)

hlayout1 = QHBoxLayout()
Expand Down
34 changes: 32 additions & 2 deletions spyder/widgets/projects/tests/test_project_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"""
Tests for explorer.py
"""
# Standard imports
import os
import os.path as osp
import shutil

# Test library imports
import pytest
Expand All @@ -15,12 +19,38 @@
from spyder.widgets.projects.explorer import ProjectExplorerTest

@pytest.fixture
def setup_projects_explorer(qtbot):
def setup_projects_explorer(qtbot, directory=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this name to setup_project_explorer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought, let's rename the fixture to project_explorer (instead of setup_project_explorer).

"""Set up ProjectExplorerWidgetTest."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this docstring to

Setup Project Explorer widget

project_explorer = ProjectExplorerTest()
project_explorer = ProjectExplorerTest(directory=directory)
qtbot.addWidget(project_explorer)
return project_explorer


def test_change_directory_in_project_explorer(qtbot, tmpdir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to pass the fixture as an argument to the test, else it can't be initialized correctly.

"""Test changing a file from directory in the Project explorer."""
# Create a temp project directory
project_dir = str(tmpdir.mkdir('project'))
project_dir_tmp = osp.join(project_dir, 'tmpá')
project_file = osp.join(project_dir, 'script.py')

# Create an empty file in the project dir
os.mkdir(project_dir_tmp)
open(project_file, 'w').close()

# Create project
projects = setup_projects_explorer(qtbot, directory=project_dir)

# Move Python file
projects.treewidget.move(fnames=[osp.join(project_dir, 'script.py')],
directory=project_dir_tmp)

# Assert content was moved
assert osp.isfile(osp.join(project_dir_tmp, 'script.py'))

# Close project
projects.close_project()


def test_project_explorer(qtbot):
"""Run project explorer."""
project_explorer = setup_projects_explorer(qtbot)
Expand Down