Skip to content

Commit 03a3777

Browse files
authoredApr 12, 2024··
Create temporary files in a temporary directory
But keep it if tests fail. Closes #1061
1 parent ca8565b commit 03a3777

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed
 

‎src/scripts/limnoria_test.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,27 @@
3636
import time
3737
import shutil
3838
import fnmatch
39+
from tempfile import TemporaryDirectory
3940
started = time.time()
4041

4142
import supybot
4243
import logging
4344
import traceback
4445

4546
# We need to do this before we import conf.
46-
if not os.path.exists('test-conf'):
47-
os.mkdir('test-conf')
47+
main_temp_dir = TemporaryDirectory()
4848

49-
registryFilename = os.path.join('test-conf', 'test.conf')
50-
fd = open(registryFilename, 'w')
51-
fd.write("""
49+
os.makedirs(os.path.join(main_temp_dir.name, 'conf'))
50+
os.makedirs(os.path.join(main_temp_dir.name, 'data'))
51+
os.makedirs(os.path.join(main_temp_dir.name, 'logs'))
52+
53+
registryFilename = os.path.join(main_temp_dir.name, 'conf', 'test.conf')
54+
with open(registryFilename, 'w') as fd:
55+
fd.write("""
5256
supybot.directories.backup: /dev/null
53-
supybot.directories.conf: %(base_dir)s/test-conf
54-
supybot.directories.data: %(base_dir)s/test-data
55-
supybot.directories.log: %(base_dir)s/test-logs
57+
supybot.directories.conf: {temp_conf}
58+
supybot.directories.data: {temp_data}
59+
supybot.directories.log: {temp_logs}
5660
supybot.reply.whenNotCommand: True
5761
supybot.log.stdout: False
5862
supybot.log.stdout.level: ERROR
@@ -67,8 +71,11 @@
6771
supybot.networks.testnet3.server: should.not.need.this
6872
supybot.nick: test
6973
supybot.databases.users.allowUnregistration: True
70-
""" % {'base_dir': os.getcwd()})
71-
fd.close()
74+
""".format(
75+
temp_conf=os.path.join(main_temp_dir.name, 'conf'),
76+
temp_data=os.path.join(main_temp_dir.name, 'data'),
77+
temp_logs=os.path.join(main_temp_dir.name, 'logs')
78+
))
7279

7380
import supybot.registry as registry
7481
registry.open_registry(registryFilename)
@@ -251,6 +258,9 @@ def main():
251258
if result.wasSuccessful():
252259
sys.exit(0)
253260
else:
261+
# Deactivate autocleaning for the temporary directiories to allow inspection.
262+
main_temp_dir._finalizer.detach()
263+
print(f"Temporary directory path: {main_temp_dir.name}")
254264
sys.exit(1)
255265

256266

0 commit comments

Comments
 (0)
Please sign in to comment.