We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
tests create figure hashes directory
when running `py.tests` on single file it creates unneeded tmp directories.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `sunpy/conftest.py`
Content:
```
1 from __future__ import absolute_import, print_function
2 from functools import partial
3 
4 import os
5 import socket
6 import tempfile
7 import json
8 
9 from sunpy.extern.six.moves.urllib.request import urlopen
10 from sunpy.extern.six.moves.urllib.error import URLError
11 
12 import pytest
13 
14 
15 # Force MPL to use non-gui backends for testing.
16 try:
17     import matplotlib
18 except ImportError:
19     pass
20 else:
21     matplotlib.use('Agg')
22 
23 from astropy.tests import disable_internet
24 
25 from sunpy.tests import hash
26 
27 hash_library_original_len = len(hash.hash_library)
28 
29 GOOGLE_URL = 'http://www.google.com'
30 
31 
32 def site_reachable(url):
33     try:
34         urlopen(url, timeout=1)
35     except (URLError, socket.timeout):
36         return False
37     else:
38         return True
39 
40 
41 is_online = partial(site_reachable, GOOGLE_URL)
42 
43 
44 def pytest_runtest_setup(item):
45     """
46     pytest hook to skip all tests that have the mark 'online' if the
47     client is online (simply detected by checking whether http://www.google.com
48     can be requested).
49     """
50     if isinstance(item, item.Function):
51         if 'online' in item.keywords and not is_online():
52             msg = 'skipping test {0} (reason: client seems to be offline)'
53             pytest.skip(msg.format(item.name))
54 
55         if 'online' not in item.keywords:
56             disable_internet.turn_off_internet()
57 
58 
59 def pytest_runtest_teardown(item, nextitem):
60     disable_internet.turn_on_internet()
61 
62 
63 def pytest_unconfigure(config):
64     tempdir = tempfile.mkdtemp(suffix="_figures")
65     # the hash_library is indexed by the name of the test but we want to look
66     # things up with the hash value
67     inv_hash_library = {v: k for k, v in hash.hash_library.items()}
68 
69     for h in hash.file_list:
70         test_name = inv_hash_library.get(h, '')
71         if test_name != '':
72             os.rename(hash.file_list[h], os.path.join(tempdir,
73                                                       test_name + '.png'))
74     print('All test files for figure hashes can be found in {0}'.format(
75         tempdir))
76 
77     # Check if additions have been made to the hash library
78     if len(hash.hash_library) > hash_library_original_len:
79         # Write the new hash library in JSON
80         tempdir = tempfile.mkdtemp()
81         hashfile = os.path.join(tempdir, hash.HASH_LIBRARY_NAME)
82         with open(hashfile, 'wb') as outfile:
83             json.dump(
84                 hash.hash_library,
85                 outfile,
86                 sort_keys=True,
87                 indent=4,
88                 separators=(',', ': '))
89         print(
90             "The hash library has expanded and should be copied to sunpy/tests/")
91         print("  " + hashfile)
92
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/sunpy/conftest.py b/sunpy/conftest.py
--- a/sunpy/conftest.py
+++ b/sunpy/conftest.py
@@ -22,9 +22,8 @@
 
 from astropy.tests import disable_internet
 
-from sunpy.tests import hash
-
-hash_library_original_len = len(hash.hash_library)
+from sunpy.tests.hash import HASH_LIBRARY_NAME
+from sunpy.tests.helpers import new_hash_library, figure_test_pngfiles
 
 GOOGLE_URL = 'http://www.google.com'
 
@@ -61,31 +60,17 @@
 
 
 def pytest_unconfigure(config):
-    tempdir = tempfile.mkdtemp(suffix="_figures")
-    # the hash_library is indexed by the name of the test but we want to look
-    # things up with the hash value
-    inv_hash_library = {v: k for k, v in hash.hash_library.items()}
-
-    for h in hash.file_list:
-        test_name = inv_hash_library.get(h, '')
-        if test_name != '':
-            os.rename(hash.file_list[h], os.path.join(tempdir,
-                                                      test_name + '.png'))
-    print('All test files for figure hashes can be found in {0}'.format(
-        tempdir))
-
-    # Check if additions have been made to the hash library
-    if len(hash.hash_library) > hash_library_original_len:
+    if len(figure_test_pngfiles) > 0:
+        tempdir = tempfile.mkdtemp(suffix="_figures")
+
+        # Rename each PNG with the name of the corresponding test
+        for test_name in figure_test_pngfiles:
+            os.rename(figure_test_pngfiles[test_name], os.path.join(tempdir, test_name + '.png'))
+
         # Write the new hash library in JSON
-        tempdir = tempfile.mkdtemp()
-        hashfile = os.path.join(tempdir, hash.HASH_LIBRARY_NAME)
-        with open(hashfile, 'wb') as outfile:
-            json.dump(
-                hash.hash_library,
-                outfile,
-                sort_keys=True,
-                indent=4,
-                separators=(',', ': '))
-        print(
-            "The hash library has expanded and should be copied to sunpy/tests/")
-        print("  " + hashfile)
+        hashfile = os.path.join(tempdir, HASH_LIBRARY_NAME)
+        with open(hashfile, 'w') as outfile:
+            json.dump(new_hash_library, outfile, sort_keys=True, indent=4, separators=(',', ': '))
+
+        print('All test files for figure hashes can be found in {0}'.format(tempdir))
+        print("The corresponding hash library is {0}".format(hashfile))

{"golden_diff": "diff --git a/sunpy/conftest.py b/sunpy/conftest.py\n--- a/sunpy/conftest.py\n+++ b/sunpy/conftest.py\n@@ -22,9 +22,8 @@\n \n from astropy.tests import disable_internet\n \n-from sunpy.tests import hash\n-\n-hash_library_original_len = len(hash.hash_library)\n+from sunpy.tests.hash import HASH_LIBRARY_NAME\n+from sunpy.tests.helpers import new_hash_library, figure_test_pngfiles\n \n GOOGLE_URL = 'http://www.google.com'\n \n@@ -61,31 +60,17 @@\n \n \n def pytest_unconfigure(config):\n-    tempdir = tempfile.mkdtemp(suffix=\"_figures\")\n-    # the hash_library is indexed by the name of the test but we want to look\n-    # things up with the hash value\n-    inv_hash_library = {v: k for k, v in hash.hash_library.items()}\n-\n-    for h in hash.file_list:\n-        test_name = inv_hash_library.get(h, '')\n-        if test_name != '':\n-            os.rename(hash.file_list[h], os.path.join(tempdir,\n-                                                      test_name + '.png'))\n-    print('All test files for figure hashes can be found in {0}'.format(\n-        tempdir))\n-\n-    # Check if additions have been made to the hash library\n-    if len(hash.hash_library) > hash_library_original_len:\n+    if len(figure_test_pngfiles) > 0:\n+        tempdir = tempfile.mkdtemp(suffix=\"_figures\")\n+\n+        # Rename each PNG with the name of the corresponding test\n+        for test_name in figure_test_pngfiles:\n+            os.rename(figure_test_pngfiles[test_name], os.path.join(tempdir, test_name + '.png'))\n+\n         # Write the new hash library in JSON\n-        tempdir = tempfile.mkdtemp()\n-        hashfile = os.path.join(tempdir, hash.HASH_LIBRARY_NAME)\n-        with open(hashfile, 'wb') as outfile:\n-            json.dump(\n-                hash.hash_library,\n-                outfile,\n-                sort_keys=True,\n-                indent=4,\n-                separators=(',', ': '))\n-        print(\n-            \"The hash library has expanded and should be copied to sunpy/tests/\")\n-        print(\"  \" + hashfile)\n+        hashfile = os.path.join(tempdir, HASH_LIBRARY_NAME)\n+        with open(hashfile, 'w') as outfile:\n+            json.dump(new_hash_library, outfile, sort_keys=True, indent=4, separators=(',', ': '))\n+\n+        print('All test files for figure hashes can be found in {0}'.format(tempdir))\n+        print(\"The corresponding hash library is {0}\".format(hashfile))\n", "issue": "tests create figure hashes directory\nwhen running `py.tests` on single file it creates unneeded tmp directories.\n\n", "before_files": [{"content": "from __future__ import absolute_import, print_function\nfrom functools import partial\n\nimport os\nimport socket\nimport tempfile\nimport json\n\nfrom sunpy.extern.six.moves.urllib.request import urlopen\nfrom sunpy.extern.six.moves.urllib.error import URLError\n\nimport pytest\n\n\n# Force MPL to use non-gui backends for testing.\ntry:\n    import matplotlib\nexcept ImportError:\n    pass\nelse:\n    matplotlib.use('Agg')\n\nfrom astropy.tests import disable_internet\n\nfrom sunpy.tests import hash\n\nhash_library_original_len = len(hash.hash_library)\n\nGOOGLE_URL = 'http://www.google.com'\n\n\ndef site_reachable(url):\n    try:\n        urlopen(url, timeout=1)\n    except (URLError, socket.timeout):\n        return False\n    else:\n        return True\n\n\nis_online = partial(site_reachable, GOOGLE_URL)\n\n\ndef pytest_runtest_setup(item):\n    \"\"\"\n    pytest hook to skip all tests that have the mark 'online' if the\n    client is online (simply detected by checking whether http://www.google.com\n    can be requested).\n    \"\"\"\n    if isinstance(item, item.Function):\n        if 'online' in item.keywords and not is_online():\n            msg = 'skipping test {0} (reason: client seems to be offline)'\n            pytest.skip(msg.format(item.name))\n\n        if 'online' not in item.keywords:\n            disable_internet.turn_off_internet()\n\n\ndef pytest_runtest_teardown(item, nextitem):\n    disable_internet.turn_on_internet()\n\n\ndef pytest_unconfigure(config):\n    tempdir = tempfile.mkdtemp(suffix=\"_figures\")\n    # the hash_library is indexed by the name of the test but we want to look\n    # things up with the hash value\n    inv_hash_library = {v: k for k, v in hash.hash_library.items()}\n\n    for h in hash.file_list:\n        test_name = inv_hash_library.get(h, '')\n        if test_name != '':\n            os.rename(hash.file_list[h], os.path.join(tempdir,\n                                                      test_name + '.png'))\n    print('All test files for figure hashes can be found in {0}'.format(\n        tempdir))\n\n    # Check if additions have been made to the hash library\n    if len(hash.hash_library) > hash_library_original_len:\n        # Write the new hash library in JSON\n        tempdir = tempfile.mkdtemp()\n        hashfile = os.path.join(tempdir, hash.HASH_LIBRARY_NAME)\n        with open(hashfile, 'wb') as outfile:\n            json.dump(\n                hash.hash_library,\n                outfile,\n                sort_keys=True,\n                indent=4,\n                separators=(',', ': '))\n        print(\n            \"The hash library has expanded and should be copied to sunpy/tests/\")\n        print(\"  \" + hashfile)\n", "path": "sunpy/conftest.py"}], "after_files": [{"content": "from __future__ import absolute_import, print_function\nfrom functools import partial\n\nimport os\nimport socket\nimport tempfile\nimport json\n\nfrom sunpy.extern.six.moves.urllib.request import urlopen\nfrom sunpy.extern.six.moves.urllib.error import URLError\n\nimport pytest\n\n\n# Force MPL to use non-gui backends for testing.\ntry:\n    import matplotlib\nexcept ImportError:\n    pass\nelse:\n    matplotlib.use('Agg')\n\nfrom astropy.tests import disable_internet\n\nfrom sunpy.tests.hash import HASH_LIBRARY_NAME\nfrom sunpy.tests.helpers import new_hash_library, figure_test_pngfiles\n\nGOOGLE_URL = 'http://www.google.com'\n\n\ndef site_reachable(url):\n    try:\n        urlopen(url, timeout=1)\n    except (URLError, socket.timeout):\n        return False\n    else:\n        return True\n\n\nis_online = partial(site_reachable, GOOGLE_URL)\n\n\ndef pytest_runtest_setup(item):\n    \"\"\"\n    pytest hook to skip all tests that have the mark 'online' if the\n    client is online (simply detected by checking whether http://www.google.com\n    can be requested).\n    \"\"\"\n    if isinstance(item, item.Function):\n        if 'online' in item.keywords and not is_online():\n            msg = 'skipping test {0} (reason: client seems to be offline)'\n            pytest.skip(msg.format(item.name))\n\n        if 'online' not in item.keywords:\n            disable_internet.turn_off_internet()\n\n\ndef pytest_runtest_teardown(item, nextitem):\n    disable_internet.turn_on_internet()\n\n\ndef pytest_unconfigure(config):\n    if len(figure_test_pngfiles) > 0:\n        tempdir = tempfile.mkdtemp(suffix=\"_figures\")\n\n        # Rename each PNG with the name of the corresponding test\n        for test_name in figure_test_pngfiles:\n            os.rename(figure_test_pngfiles[test_name], os.path.join(tempdir, test_name + '.png'))\n\n        # Write the new hash library in JSON\n        hashfile = os.path.join(tempdir, HASH_LIBRARY_NAME)\n        with open(hashfile, 'w') as outfile:\n            json.dump(new_hash_library, outfile, sort_keys=True, indent=4, separators=(',', ': '))\n\n        print('All test files for figure hashes can be found in {0}'.format(tempdir))\n        print(\"The corresponding hash library is {0}\".format(hashfile))\n", "path": "sunpy/conftest.py"}]}