Repository: sunpy/sunpy

Issue:
Tests should not make use of user config file
<!-- This comments are hidden when you submit the issue so you do not need to remove them!
Please be sure to check out our contributing guidelines: https://github.com/sunpy/sunpy/blob/master/CONTRIBUTING.rst
Please be sure to check out our code of conduct:
https://github.com/sunpy/sunpy/blob/master/CODE_OF_CONDUCT.rst -->

<!-- Please have a search on our GitHub repository to see if a similar issue has already been posted.
If a similar issue is closed, have a quick look to see if you are satisfied by the resolution.
If not please go ahead and open an issue! -->

### Description
Tests make use of the user provided config file but have hard coded dependencies on expectations from the default configuration. This is most easily seen in tests for time. Many tests are expecting a particular time format namely `%Y-%m-%d %H:%M:%S` but the user could have set that to another format. Those tests would then unexpectedly fail.  

### Expected behavior
I expected the tests to pass.

### Actual behavior
The tests failed.
<!-- Was the output confusing or poorly described? -->

### Steps to Reproduce
Change your default config time_format from `%Y-%m-%d %H:%M:%S` to `%Y-%m-%d %H:%M` and watch the tests fail.



Discussion:
Sure. Let me see if I can work around this. 

Resolving patch:
diff --git a/sunpy/conftest.py b/sunpy/conftest.py
--- a/sunpy/conftest.py
+++ b/sunpy/conftest.py
@@ -1,3 +1,4 @@
+import os
 import json
 import pathlib
 import warnings
@@ -34,6 +35,27 @@ def figure_base_dir(request):
         request.config.getoption("--figure_dir"))
 
 
+@pytest.fixture(scope='session', autouse=True)
+def tmp_config_dir(request):
+    """
+    Globally set the default config for all tests.
+    """
+    os.environ["SUNPY_CONFIGDIR"] = str(pathlib.Path(__file__).parent / "data")
+    yield
+    del os.environ["SUNPY_CONFIGDIR"]
+
+
+@pytest.fixture()
+def undo_config_dir_patch():
+    """
+    Provide a way for certain tests to not have the config dir.
+    """
+    oridir = os.environ["SUNPY_CONFIGDIR"]
+    del os.environ["SUNPY_CONFIGDIR"]
+    yield
+    os.environ["SUNPY_CONFIGDIR"] = oridir
+
+
 def pytest_runtest_setup(item):
     """
     pytest hook to skip all tests that have the mark 'remotedata' if the
diff --git a/sunpy/util/config.py b/sunpy/util/config.py
--- a/sunpy/util/config.py
+++ b/sunpy/util/config.py
@@ -60,7 +60,7 @@ def _find_config_files():
     # if a user configuration file exists, add that to list of files to read
     # so that any values set there will override ones specified in the default
     # config file
-    config_path = Path(CONFIG_DIR)
+    config_path = Path(_get_user_configdir())
     if config_path.joinpath(config_filename).exists():
         config_files.append(str(config_path.joinpath(config_filename)))
 
@@ -73,7 +73,7 @@ def get_and_create_download_dir():
     """
     download_dir = Path(sunpy.config.get('downloads', 'download_dir')).expanduser().resolve()
     if not _is_writable_dir(download_dir):
-        download_dir.mkdir(parents=True)
+        raise RuntimeError(f'Could not write to SunPy downloads directory="{download_dir}"')
 
     return sunpy.config.get('downloads', 'download_dir')
 
@@ -84,7 +84,7 @@ def get_and_create_sample_dir():
     """
     sample_dir = Path(sunpy.config.get('downloads', 'sample_dir')).expanduser().resolve()
     if not _is_writable_dir(sample_dir):
-        sample_dir.mkdir(parents=True)
+        raise RuntimeError(f'Could not write to SunPy sample data directory="{sample_dir}"')
 
     return sunpy.config.get('downloads', 'sample_dir')
 
@@ -104,22 +104,22 @@ def print_config():
 
 
 def _is_writable_dir(p):
-    """Checks to see if a directory is writable"""
+    """Checks to see if a directory exists and is writable."""
+    if not Path(p).exists():
+        Path(p).mkdir(parents=True)
     return Path(p).is_dir() and os.access(p, os.W_OK)
 
 
 def _get_user_configdir():
     """
     Return the string representing the configuration dir.
-    The default is "$HOME/.sunpy". You can override this with the
-    SUNPY_CONFIGDIR environment variable
-    """
-    configdir = os.environ.get('SUNPY_CONFIGDIR')
 
-    if configdir is not None:
-        if not _is_writable_dir(configdir):
-            raise RuntimeError('Could not write to SUNPY_CONFIGDIR="{0}"'
-                               .format(configdir))
-        return configdir
+    The default is set by "AppDirs" and can be accessed by importing
+    ``sunpy.util.config.CONFIG_DIR``.
+    You can override this with the "SUNPY_CONFIGDIR" environment variable.
+    """
+    configdir = os.environ.get('SUNPY_CONFIGDIR', CONFIG_DIR)
 
-    return CONFIG_DIR
+    if not _is_writable_dir(configdir):
+        raise RuntimeError(f'Could not write to SUNPY_CONFIGDIR="{configdir}"')
+    return configdir


Test patch:
diff --git a/sunpy/util/tests/test_config.py b/sunpy/util/tests/test_config.py
--- a/sunpy/util/tests/test_config.py
+++ b/sunpy/util/tests/test_config.py
@@ -11,18 +11,15 @@
 
 
 def test_is_writable_dir(tmpdir, tmp_path):
-    writeable_dir = tmpdir.mkdir("can_you_right_to_me")
-    just_a_path = tmp_path / "sub"
-    assert _is_writable_dir(writeable_dir)
-    assert not _is_writable_dir(just_a_path)
+    assert _is_writable_dir(tmpdir)
 
 
-def test_get_user_configdir(tmpdir):
+def test_get_user_configdir(tmpdir, tmp_path, undo_config_dir_patch):
     # Default
     assert USER in CONFIG_DIR
     assert CONFIG_DIR.split(os.path.sep)[-1] == "sunpy"
     assert CONFIG_DIR == _get_user_configdir()
-    # Try to set a manual one.
+    # Try to set a manual one (already created)
     tmp_config_dir = tmpdir.mkdir("config_here")
     os.environ["SUNPY_CONFIGDIR"] = tmp_config_dir.strpath
     assert tmp_config_dir == _get_user_configdir()
@@ -30,6 +27,13 @@ def test_get_user_configdir(tmpdir):
     if not (os.name == "nt"):
         os.unsetenv("SUNPY_CONFIGDIR")
     del os.environ["SUNPY_CONFIGDIR"]
+    # Try to set a manual one (not created)
+    os.environ["SUNPY_CONFIGDIR"] = str(tmp_path)
+    assert str(tmp_path) == _get_user_configdir()
+    # Bypass this under windows.
+    if not (os.name == "nt"):
+        os.unsetenv("SUNPY_CONFIGDIR")
+    del os.environ["SUNPY_CONFIGDIR"]
 
 
 def test_print_config_files():