Coverage for tests/test_preExecInit.py: 16%

68 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-25 00:47 -0800

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 preExecInit = PreExecInit(butler=butler, taskFactory=taskFactory, extendRun=extendRun) 

66 preExecInit.saveInitOutputs(qgraph) 

67 

68 def test_saveInitOutputs_twice(self): 

69 taskFactory = AddTaskFactoryMock() 

70 for extendRun in (False, True): 

71 with self.subTest(extendRun=extendRun): 

72 with temporaryDirectory() as tmpdir: 

73 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

75 preExecInit.saveInitOutputs(qgraph) 

76 if extendRun: 

77 # will ignore this 

78 preExecInit.saveInitOutputs(qgraph) 

79 else: 

80 # Second time it will fail 

81 with self.assertRaises(Exception): 

82 preExecInit.saveInitOutputs(qgraph) 

83 

84 def test_saveConfigs(self): 

85 for extendRun in (False, True): 

86 with self.subTest(extendRun=extendRun): 

87 with temporaryDirectory() as tmpdir: 

88 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

90 preExecInit.saveConfigs(qgraph) 

91 

92 def test_saveConfigs_twice(self): 

93 for extendRun in (False, True): 

94 with self.subTest(extendRun=extendRun): 

95 with temporaryDirectory() as tmpdir: 

96 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

98 preExecInit.saveConfigs(qgraph) 

99 if extendRun: 

100 # will ignore this 

101 preExecInit.saveConfigs(qgraph) 

102 else: 

103 # Second time it will fail 

104 with self.assertRaises(Exception): 

105 preExecInit.saveConfigs(qgraph) 

106 

107 def test_savePackageVersions(self): 

108 for extendRun in (False, True): 

109 with self.subTest(extendRun=extendRun): 

110 with temporaryDirectory() as tmpdir: 

111 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

113 preExecInit.savePackageVersions(qgraph) 

114 

115 def test_savePackageVersions_twice(self): 

116 for extendRun in (False, True): 

117 with self.subTest(extendRun=extendRun): 

118 with temporaryDirectory() as tmpdir: 

119 butler, qgraph = makeSimpleQGraph(root=tmpdir) 

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

121 preExecInit.savePackageVersions(qgraph) 

122 if extendRun: 

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

124 # attempt to save. 

125 preExecInit.savePackageVersions(qgraph) 

126 else: 

127 # second time it will fail 

128 with self.assertRaises(Exception): 

129 preExecInit.savePackageVersions(qgraph) 

130 

131 

132if __name__ == "__main__": 

133 unittest.main()