Coverage for .tox/coverage/lib/python3.11/site-packages/wuttjamaican/testing.py: 100%
19 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-22 21:49 -0600
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-22 21:49 -0600
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# WuttJamaican -- Base package for Wutta Framework
5# Copyright © 2023 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24WuttJamaican - test utilities
25"""
27import os
28import shutil
29import tempfile
30from unittest import TestCase
33class FileConfigTestCase(TestCase):
34 """
35 Common base class for test suites which write temporary files, for
36 sake of testing the config constructor etc.
38 This inherits from :class:`python:unittest.TestCase` and adds the
39 following features:
41 Creates a temporary folder on setup, and removes it on teardown.
42 Adds the :meth:`write_file()` method to help with creating
43 temporary files.
45 .. attribute:: tempdir
47 Path to the temporary folder created during setup.
48 """
50 def setUp(self):
51 """ """
52 self.setup_file_config()
54 def setup_file_config(self):
55 """
56 Setup logic specific to the ``FileConfigTestCase``.
58 This creates the temporary folder.
59 """
60 self.tempdir = tempfile.mkdtemp()
62 def tearDown(self):
63 """ """
64 self.teardown_file_config()
66 def teardown_file_config(self):
67 """
68 Teardown logic specific to the ``FileConfigTestCase``.
70 This removes the temporary folder.
71 """
72 shutil.rmtree(self.tempdir)
74 def write_file(self, filename, content):
75 """
76 Write a new file (in temporary folder) with the given filename
77 and content, and return its full path. For instance::
79 myconf = self.write_file('my.conf', '<file contents>')
80 """
81 path = os.path.join(self.tempdir, filename)
82 with open(path, 'wt') as f:
83 f.write(content)
84 return path