Repository: spyder-ide/spyder

Issue:
Selected working directory in Preferences is not preserved after a restart
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

under preferance i chose a working directory as a start up directrory when starting spyder again reverts back to c:\users\user directory



## Versions

* Spyder version: 4.0.0b2 
* Python version: 3.7.3
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Windows 10

### Dependencies

```
pygments >=2.0     :  2.3.1 (OK)
qdarkstyle >=2.6.4 :  2.6.8 (OK)
sphinx >=0.6.6     :  1.8.5 (OK)
pyls >=0.19.0;<0.25:  0.24.0 (OK)
nbconvert >=4.0    :  5.4.1 (OK)
pandas >=0.13.1    :  0.24.2 (OK)
numpy >=1.7        :  1.16.2 (OK)
sympy >=0.7.3      :  1.3 (OK)
cython >=0.21      :  0.29.6 (OK)
qtconsole >=4.2.0  :  4.4.3 (OK)
IPython >=4.0      :  7.4.0 (OK)
matplotlib >=2.0.0 :  3.0.3 (OK)
pylint >=0.25      :  2.3.1 (OK)
```



Discussion:
@dalthviz, could you verify if this is happening to you on Windows or not?
yes windows 10
@ccordoba12 yes, testing it, it happens to me too. In the preferences, the selected woking directory shows the change but the working directory toolbar shows the home directory (after restarting Spyder manually this is what I see):

![imagen](https://user-images.githubusercontent.com/16781833/59482860-4218b280-8e30-11e9-927c-3fe4e4f1ab49.png)



Thanks for the confirmation @dalthviz. Could you also test what happens on Linux?
Testing on Linux (Ubuntu) the same behaviour happens:

![linux](https://user-images.githubusercontent.com/16781833/59528122-079c2d80-8ea3-11e9-9f3f-9ac953b9e18f.PNG)


Ok, thanks @dalthviz. I thought it was a Windows issue only.
Who could work on this @ccordoba12 ?
Let's give this one to @steff456 to this one.

Resolving patch:
diff --git a/spyder/config/main.py b/spyder/config/main.py
--- a/spyder/config/main.py
+++ b/spyder/config/main.py
@@ -307,6 +307,7 @@
               'console/use_project_or_home_directory': False,
               'console/use_cwd': True,
               'console/use_fixed_directory': False,
+              'startup/use_fixed_directory': False,
               }),
             ('shortcuts',
              {
diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py
--- a/spyder/plugins/ipythonconsole/plugin.py
+++ b/spyder/plugins/ipythonconsole/plugin.py
@@ -901,6 +901,9 @@ def register_client(self, client, give_focus=True):
             if (self.main.projects is not None and
                     self.main.projects.get_active_project() is not None):
                 cwd_path = self.main.projects.get_active_project_path()
+        elif CONF.get('workingdir', 'startup/use_fixed_directory'):
+            cwd_path = CONF.get('workingdir', 'startup/fixed_directory',
+                                default=get_home_dir())
         elif CONF.get('workingdir', 'console/use_fixed_directory'):
             cwd_path = CONF.get('workingdir', 'console/fixed_directory')
 
diff --git a/spyder/plugins/workingdirectory/plugin.py b/spyder/plugins/workingdirectory/plugin.py
--- a/spyder/plugins/workingdirectory/plugin.py
+++ b/spyder/plugins/workingdirectory/plugin.py
@@ -88,12 +88,7 @@ def __init__(self, parent, workdir=None, **kwds):
         self.pathedit.setMaxCount(self.get_option('working_dir_history'))
         wdhistory = self.load_wdhistory(workdir)
         if workdir is None:
-            if self.get_option('console/use_project_or_home_directory'):
-                workdir = get_home_dir()
-            else:
-                workdir = self.get_option('console/fixed_directory', default='')
-                if not osp.isdir(workdir):
-                    workdir = get_home_dir()
+            workdir = self.get_workdir()
         self.chdir(workdir)
         self.pathedit.addItems(wdhistory)
         self.pathedit.selected_text = self.pathedit.currentText()
@@ -113,7 +108,21 @@ def __init__(self, parent, workdir=None, **kwds):
                                       _('Change to parent directory'),
                                       triggered=self.parent_directory)
         self.toolbar.addAction(parent_action)
-                
+
+    def get_workdir(self):
+        """Get current workdir from the CONF file."""
+        if self.get_option('startup/use_fixed_directory'):
+            workdir = self.get_option('startup/fixed_directory',
+                                      default='')
+        elif self.get_option('console/use_project_or_home_directory'):
+            workdir = get_home_dir()
+        else:
+            workdir = self.get_option('console/fixed_directory',
+                                      default='')
+        if not osp.isdir(workdir):
+            workdir = get_home_dir()
+        return workdir
+
     #------ SpyderPluginWidget API ---------------------------------------------    
     def get_plugin_title(self):
         """Return widget title"""
@@ -149,7 +158,7 @@ def load_wdhistory(self, workdir=None):
             wdhistory = [name for name in wdhistory if os.path.isdir(name)]
         else:
             if workdir is None:
-                workdir = get_home_dir()
+                workdir = self.get_workdir()
             wdhistory = [ workdir ]
         return wdhistory
 


Test patch:
diff --git a/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py b/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
--- a/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
+++ b/spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
@@ -51,6 +51,7 @@
 SHELL_TIMEOUT = 20000
 TEMP_DIRECTORY = tempfile.gettempdir()
 NON_ASCII_DIR = osp.join(TEMP_DIRECTORY, u'測試', u'اختبار')
+NEW_DIR = 'new_workingdir'
 
 
 # =============================================================================
@@ -92,6 +93,15 @@ def __getattr__(self, attr):
     # Tests assume inline backend
     CONF.set('ipython_console', 'pylab/backend', 0)
 
+    # Start in a new working directory the console
+    use_startup_wdir = request.node.get_closest_marker('use_startup_wdir')
+    if use_startup_wdir:
+        new_wdir = osp.join(os.getcwd(), NEW_DIR)
+        if not osp.exists(new_wdir):
+            os.mkdir(new_wdir)
+        CONF.set('workingdir', 'console/use_fixed_directory', True)
+        CONF.set('workingdir', 'console/fixed_directory', new_wdir)
+
     # Test the console with a non-ascii temp dir
     non_ascii_dir = request.node.get_closest_marker('non_ascii_dir')
     if non_ascii_dir:
@@ -1169,5 +1179,17 @@ def test_remove_old_stderr_files(ipyconsole, qtbot):
     assert not osp.isfile(osp.join(tmpdir, 'foo.stderr'))
 
 
+@pytest.mark.slow
+@pytest.mark.use_startup_wdir
+def test_console_working_directory(ipyconsole, qtbot):
+    """Test for checking the working directory."""
+    shell = ipyconsole.get_current_shellwidget()
+    shell.execute('import os; cwd = os.getcwd()')
+
+    current_wdir = shell.get_value('cwd')
+    folders = osp.split(current_wdir)
+    assert folders[-1] == NEW_DIR
+
+
 if __name__ == "__main__":
     pytest.main()
diff --git a/spyder/plugins/workingdirectory/tests/test_workingdirectory.py b/spyder/plugins/workingdirectory/tests/test_workingdirectory.py
--- a/spyder/plugins/workingdirectory/tests/test_workingdirectory.py
+++ b/spyder/plugins/workingdirectory/tests/test_workingdirectory.py
@@ -7,14 +7,34 @@
 
 # Test library imports
 import pytest
+import os
+import os.path as osp
 
 # Local imports
 from spyder.plugins.workingdirectory.plugin import WorkingDirectory
+from spyder.config.base import get_home_dir
+from spyder.config.main import CONF
+
+
+NEW_DIR = 'new_workingdir'
 
 
 @pytest.fixture
-def setup_workingdirectory(qtbot):
-    """Setupp working directory plugin."""
+def setup_workingdirectory(qtbot, request):
+    """Setup working directory plugin."""
+    CONF.reset_to_defaults()
+    use_startup_wdir = request.node.get_closest_marker('use_startup_wdir')
+    if use_startup_wdir:
+        new_wdir = osp.join(os.getcwd(), NEW_DIR)
+        if not osp.exists(new_wdir):
+            os.mkdir(new_wdir)
+        CONF.set('workingdir', 'startup/use_fixed_directory', True)
+        CONF.set('workingdir', 'startup/fixed_directory', new_wdir)
+    else:
+        CONF.set('workingdir', 'startup/use_fixed_directory', False)
+        CONF.set('workingdir', 'console/use_fixed_directory', False)
+        CONF.set('workingdir', 'startup/fixed_directory', get_home_dir())
+
     workingdirectory = WorkingDirectory(None)
     qtbot.addWidget(workingdirectory)
     workingdirectory.show()
@@ -30,5 +50,25 @@ def test_basic_initialization(setup_workingdirectory):
     assert workingdirectory is not None
 
 
+def test_get_workingdir(setup_workingdirectory):
+    """Test the method that defines the working directory at home."""
+    workingdirectory, qtbot = setup_workingdirectory
+    # Start the working directory on the home directory
+    act_wdir = workingdirectory.get_workdir()
+    assert act_wdir == get_home_dir()
+
+
+@pytest.mark.first
+@pytest.mark.use_startup_wdir
+def test_get_workingdir_startup(setup_workingdirectory):
+    """Test the method that defines the working directory at home."""
+    workingdirectory, qtbot = setup_workingdirectory
+    # Start the working directory on the home directory
+    act_wdir = workingdirectory.get_workdir()
+    folders = osp.split(act_wdir)
+    assert folders[-1] == NEW_DIR
+    CONF.reset_to_defaults()
+
+
 if __name__ == "__main__":
     pytest.main()