Coverage for tests / test_preExecInit.py: 15%

74 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-06 08:33 +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/>. 

27 

28"""Simple unit test for PreExecInit class.""" 

29 

30import contextlib 

31import shutil 

32import tempfile 

33import unittest 

34 

35from lsst.ctrl.mpexec import PreExecInit 

36from lsst.pipe.base.tests.simpleQGraph import AddTaskFactoryMock, makeSimpleQGraph 

37 

38 

39@contextlib.contextmanager 

40def temporaryDirectory(): 

41 """Context manager that creates and destroys temporary directory. 

42 

43 Difference from `tempfile.TemporaryDirectory` is that it ignores errors 

44 when deleting a directory, which may happen with some filesystems. 

45 

46 Yields 

47 ------ 

48 `str` 

49 The temporary directory. 

50 """ 

51 tmpdir = tempfile.mkdtemp() 

52 yield tmpdir 

53 shutil.rmtree(tmpdir, ignore_errors=True) 

54 

55 

56class PreExecInitTestCase(unittest.TestCase): 

57 """A test case for PreExecInit.""" 

58 

59 def test_saveInitOutputs(self): 

60 taskFactory = AddTaskFactoryMock() 

61 for extendRun in (False, True): 

62 with self.subTest(extendRun=extendRun): 

63 with temporaryDirectory() as tmpdir: 

64 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

65 self.enterContext(butler) 

66 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, extendRun=extendRun) 

67 preExecInit.saveInitOutputs(qgraph) 

68 

69 def test_saveInitOutputs_twice(self): 

70 taskFactory = AddTaskFactoryMock() 

71 for extendRun in (False, True): 

72 with self.subTest(extendRun=extendRun): 

73 with temporaryDirectory() as tmpdir: 

74 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

75 self.enterContext(butler) 

76 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, extendRun=extendRun) 

77 preExecInit.saveInitOutputs(qgraph) 

78 if extendRun: 

79 # will ignore this 

80 preExecInit.saveInitOutputs(qgraph) 

81 else: 

82 # Second time it will fail 

83 with self.assertRaises(Exception): 

84 preExecInit.saveInitOutputs(qgraph) 

85 

86 def test_saveConfigs(self): 

87 for extendRun in (False, True): 

88 with self.subTest(extendRun=extendRun): 

89 with temporaryDirectory() as tmpdir: 

90 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

91 self.enterContext(butler) 

92 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

93 preExecInit.saveConfigs(qgraph) 

94 

95 def test_saveConfigs_twice(self): 

96 for extendRun in (False, True): 

97 with self.subTest(extendRun=extendRun): 

98 with temporaryDirectory() as tmpdir: 

99 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

100 self.enterContext(butler) 

101 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

102 preExecInit.saveConfigs(qgraph) 

103 if extendRun: 

104 # will ignore this 

105 preExecInit.saveConfigs(qgraph) 

106 else: 

107 # Second time it will fail 

108 with self.assertRaises(Exception): 

109 preExecInit.saveConfigs(qgraph) 

110 

111 def test_savePackageVersions(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 self.enterContext(butler) 

117 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

118 preExecInit.savePackageVersions(qgraph) 

119 

120 def test_savePackageVersions_twice(self): 

121 for extendRun in (False, True): 

122 with self.subTest(extendRun=extendRun): 

123 with temporaryDirectory() as tmpdir: 

124 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

125 self.enterContext(butler) 

126 preExecInit = PreExecInit(butler=butler, taskFactory=None, extendRun=extendRun) 

127 preExecInit.savePackageVersions(qgraph) 

128 if extendRun: 

129 # If this is the same packages then it should not 

130 # attempt to save. 

131 preExecInit.savePackageVersions(qgraph) 

132 else: 

133 # second time it will fail 

134 with self.assertRaises(Exception): 

135 preExecInit.savePackageVersions(qgraph) 

136 

137 

138if __name__ == "__main__": 

139 unittest.main()