Repository: sunpy/sunpy

Issue:
[1.0.3] test_print_config_files fails with installed sunpy
<!-- 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

On Debian, we run the provided tests regularly on the installed package ("CI test") to ensure that it still works in the current environment. 
With Version 1.0.3 (or before; I didn't check 1.0 … 1.0.2), I see the following failure:

### Actual behavior
`print_test_config_files` tries to write to the installed package, which is denied for an ordinary user:
```
_____________________________________ test_print_config_files _____________________________________

undo_download_dir_patch = None

    def test_print_config_files(undo_download_dir_patch):
        # TODO: Tidy this up.
        stdout = sys.stdout
        out = io.StringIO()
        sys.stdout = out
>       print_config()

/usr/lib/python3/dist-packages/sunpy/util/tests/test_config.py:51: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib/python3/dist-packages/sunpy/util/config.py:107: in print_config
    for file_ in _find_config_files():
/usr/lib/python3/dist-packages/sunpy/util/config.py:69: in _find_config_files
    config_path = Path(_get_user_configdir())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def _get_user_configdir():
        """[…]"""
        configdir = os.environ.get('SUNPY_CONFIGDIR', CONFIG_DIR)
    
        if not _is_writable_dir(configdir):
>           raise RuntimeError(f'Could not write to SUNPY_CONFIGDIR="{configdir}"')
E           RuntimeError: Could not write to SUNPY_CONFIGDIR="/usr/lib/python3/dist-packages/sunpy/data"
```
This also happens when I explicitly set `SUNPY_CONFIGDIR` to a different directory; the path mentioned in the `RuntimeError` then still is `/usr/lib/python3/dist-packages/sunpy/data`.

### Steps to Reproduce

```
$ cd /tmp
$ SUNPY_CONFIGDIR=/tmp python3 -c "import sunpy; exit(sunpy.self_test(args=\"-k 'not figure and not online'\"))"
```


### System Details
<!-- We at least need to know the SunPy version you are using. -->
<!-- We provide a short function in SunPy that will provide some of the below information. -->
<!-- It is sunpy.util.system_info(), this is optional but strongly recommended. -->

  - SunPy Version: 1.0.3
  - Astropy Version: 3.2.1
  - Python Version: 3.7.4
  - OS information: Debian unstable



Discussion:
Thanks for the report, I will try figure out what seems like 2 separate bugs are here. 

The test is safe enough to skip, so if that helps, you can do that! 
I marked it as xfail until it is fixed.

Resolving patch:
diff --git a/sunpy/conftest.py b/sunpy/conftest.py
--- a/sunpy/conftest.py
+++ b/sunpy/conftest.py
@@ -46,7 +46,7 @@ def tmp_config_dir(request):
     """
     tmpdir = tempfile.TemporaryDirectory()
 
-    os.environ["SUNPY_CONFIGDIR"] = str(pathlib.Path(__file__).parent / "data")
+    os.environ["SUNPY_CONFIGDIR"] = str(tmpdir.name)
     astropy.config.paths.set_temp_config._temp_path = str(tmpdir.name)
     astropy.config.paths.set_temp_cache._temp_path = str(tmpdir.name)
 


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
@@ -1,6 +1,6 @@
 import io
 import os
-import sys
+from contextlib import redirect_stdout
 from pathlib import Path
 
 from sunpy import config
@@ -21,6 +21,7 @@ def test_is_writable_dir(tmpdir, tmp_path):
     # Checks a filepath instead of directory
     assert not _is_writable_dir(tmp_file)
 
+
 def test_get_user_configdir(tmpdir, tmp_path, undo_config_dir_patch):
     # Default
     assert USER in CONFIG_DIR
@@ -43,15 +44,10 @@ def test_get_user_configdir(tmpdir, tmp_path, undo_config_dir_patch):
     del os.environ["SUNPY_CONFIGDIR"]
 
 
-def test_print_config_files(undo_download_dir_patch):
-    # TODO: Tidy this up.
-    stdout = sys.stdout
-    out = io.StringIO()
-    sys.stdout = out
-    print_config()
-    sys.stdout = stdout
-    out.seek(0)
-    printed = out.read()
+def test_print_config_files(tmpdir, tmp_path, undo_download_dir_patch):
+    with io.StringIO() as buf, redirect_stdout(buf):
+        print_config()
+        printed = buf.getvalue()
     assert "time_format = %Y-%m-%d %H:%M:%S" in printed
     assert _find_config_files()[0] in printed
     assert get_and_create_download_dir() in printed