Coverage for tests/test_preExecInit.py: 15%
69 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-23 10:58 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-23 10:58 +0000
1# This file is part of ctrl_mpexec.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <https://www.gnu.org/licenses/>.
28"""Simple unit test for PreExecInit class.
29"""
31import contextlib
32import shutil
33import tempfile
34import unittest
36from lsst.ctrl.mpexec import PreExecInit
37from lsst.pipe.base.tests.simpleQGraph import AddTaskFactoryMock, makeSimpleQGraph
40@contextlib.contextmanager
41def temporaryDirectory():
42 """Context manager that creates and destroys temporary directory.
44 Difference from `tempfile.TemporaryDirectory` is that it ignores errors
45 when deleting a directory, which may happen with some filesystems.
46 """
47 tmpdir = tempfile.mkdtemp()
48 yield tmpdir
49 shutil.rmtree(tmpdir, ignore_errors=True)
52class PreExecInitTestCase(unittest.TestCase):
53 """A test case for PreExecInit"""
55 def test_saveInitOutputs(self):
56 taskFactory = AddTaskFactoryMock()
57 for extendRun in (False, True):
58 with self.subTest(extendRun=extendRun):
59 with temporaryDirectory() as tmpdir:
60 butler, qgraph = makeSimpleQGraph(root=tmpdir)
61 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, extendRun=extendRun)
62 preExecInit.saveInitOutputs(qgraph)
64 def test_saveInitOutputs_twice(self):
65 taskFactory = AddTaskFactoryMock()
66 for extendRun in (False, True):
67 with self.subTest(extendRun=extendRun):
68 with temporaryDirectory() as tmpdir:
69 butler, qgraph = makeSimpleQGraph(root=tmpdir)
70 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, extendRun=extendRun)
71 preExecInit.saveInitOutputs(qgraph)
72 if extendRun:
73 # will ignore this
74 preExecInit.saveInitOutputs(qgraph)
75 else:
76 # Second time it will fail
77 with self.assertRaises(Exception):
78 preExecInit.saveInitOutputs(qgraph)
80 def test_saveConfigs(self):
81 for extendRun in (False, True):
82 with self.subTest(extendRun=extendRun):
83 with temporaryDirectory() as tmpdir:
84 butler, qgraph = makeSimpleQGraph(root=tmpdir)
85 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun)
86 preExecInit.saveConfigs(qgraph)
88 def test_saveConfigs_twice(self):
89 for extendRun in (False, True):
90 with self.subTest(extendRun=extendRun):
91 with temporaryDirectory() as tmpdir:
92 butler, qgraph = makeSimpleQGraph(root=tmpdir)
93 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun)
94 preExecInit.saveConfigs(qgraph)
95 if extendRun:
96 # will ignore this
97 preExecInit.saveConfigs(qgraph)
98 else:
99 # Second time it will fail
100 with self.assertRaises(Exception):
101 preExecInit.saveConfigs(qgraph)
103 def test_savePackageVersions(self):
104 for extendRun in (False, True):
105 with self.subTest(extendRun=extendRun):
106 with temporaryDirectory() as tmpdir:
107 butler, qgraph = makeSimpleQGraph(root=tmpdir)
108 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun)
109 preExecInit.savePackageVersions(qgraph)
111 def test_savePackageVersions_twice(self):
112 for extendRun in (False, True):
113 with self.subTest(extendRun=extendRun):
114 with temporaryDirectory() as tmpdir:
115 butler, qgraph = makeSimpleQGraph(root=tmpdir)
116 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun)
117 preExecInit.savePackageVersions(qgraph)
118 if extendRun:
119 # If this is the same packages then it should not
120 # attempt to save.
121 preExecInit.savePackageVersions(qgraph)
122 else:
123 # second time it will fail
124 with self.assertRaises(Exception):
125 preExecInit.savePackageVersions(qgraph)
128if __name__ == "__main__":
129 unittest.main()