Coverage for tests/unit/CLI.py: 96%

106 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-03 00:54 +0000

1# ==================================================================================================================== # 

2# _____ ____ _ _ _ _ ____ ___ ____ # 

3# _ __ _ _| ____| _ \ / \ / \ | | | |/ ___|_ _/ ___| # 

4# | '_ \| | | | _| | | | |/ _ \ / _ \ | | | | | | |\___ \ # 

5# | |_) | |_| | |___| |_| / ___ \ / ___ \ | |_| | |___ | | ___) | # 

6# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)___/ \____|___|____/ # 

7# |_| |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2021-2022 Electronic Design Automation Abstraction (EDA²) # 

15# # 

16# Licensed under the Apache License, Version 2.0 (the "License"); # 

17# you may not use this file except in compliance with the License. # 

18# You may obtain a copy of the License at # 

19# # 

20# http://www.apache.org/licenses/LICENSE-2.0 # 

21# # 

22# Unless required by applicable law or agreed to in writing, software # 

23# distributed under the License is distributed on an "AS IS" BASIS, # 

24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

25# See the License for the specific language governing permissions and # 

26# limitations under the License. # 

27# # 

28# SPDX-License-Identifier: Apache-2.0 # 

29# ==================================================================================================================== # 

30# 

31"""Testcase for CLI tests.""" 

32import sys 

33from io import StringIO 

34from unittest import TestCase 

35from unittest.mock import patch 

36 

37from pyEDAA.UCIS.CLI import Program, main 

38 

39 

40if __name__ == "__main__": # pragma: no cover 40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true

41 print("ERROR: you called a testcase declaration file as an executable module.") 

42 print("Use: 'python -m unitest <testcase module>'") 

43 exit(1) 

44 

45 

46PROGRAM = "pyedaa-ucis" 

47 

48class Help(TestCase): 

49 _program: Program 

50 

51 def setUp(self) -> None: 

52 self._program = Program() 

53 

54 @patch('sys.stderr', new_callable=StringIO) 

55 @patch('sys.stdout', new_callable=StringIO) 

56 def test_NoOptions(self, stdoutStream: StringIO, stderrStream: StringIO): 

57 sys.argv = [PROGRAM] 

58 

59 self._program.Run() 

60 

61 stdout = stdoutStream.getvalue() 

62 stderr = stderrStream.getvalue() 

63 self.assertIn("UCDB Service Program", stdout) 

64 self.assertIn(f"usage: {PROGRAM}", stdout) 

65 self.assertEqual("", stderr) 

66 

67 @patch('sys.stderr', new_callable=StringIO) 

68 @patch('sys.stdout', new_callable=StringIO) 

69 def test_HelpCommand(self, stdoutStream: StringIO, stderrStream: StringIO): 

70 sys.argv = [PROGRAM, "help"] 

71 

72 self._program.Run() 

73 

74 stdout = stdoutStream.getvalue() 

75 stderr = stderrStream.getvalue() 

76 self.assertIn("UCDB Service Program", stdout) 

77 self.assertIn(f"usage: {PROGRAM}", stdout) 

78 self.assertEqual("", stderr) 

79 

80 @patch('sys.stderr', new_callable=StringIO) 

81 @patch('sys.stdout', new_callable=StringIO) 

82 def test_HelpForExport(self, stdoutStream: StringIO, stderrStream: StringIO): 

83 sys.argv = [PROGRAM, "help", "export"] 

84 

85 self._program.Run() 

86 

87 stdout = stdoutStream.getvalue() 

88 stderr = stderrStream.getvalue() 

89 self.assertIn("UCDB Service Program", stdout) 

90 self.assertIn(f"usage: {PROGRAM}", stdout) 

91 self.assertEqual("", stderr) 

92 

93 @patch('sys.stderr', new_callable=StringIO) 

94 @patch('sys.stdout', new_callable=StringIO) 

95 def test_UnknownCommand(self, stdoutStream: StringIO, stderrStream: StringIO): 

96 sys.argv = [PROGRAM, "expand"] 

97 

98 with self.assertRaises(SystemExit) as ex: 

99 self._program.Run() 

100 

101 self.assertEqual(2, ex.exception.code) 

102 

103 stdout = stdoutStream.getvalue() 

104 stderr = stderrStream.getvalue() 

105 self.assertEqual("", stdout) 

106 self.assertIn(f"usage: {PROGRAM}", stderr) 

107 

108 @patch('sys.stderr', new_callable=StringIO) 

109 @patch('sys.stdout', new_callable=StringIO) 

110 def test_HelpCommandUnknownCommand(self, stdoutStream: StringIO, stderrStream: StringIO): 

111 sys.argv = [PROGRAM, "help", "expand"] 

112 

113 self._program.Run() 

114 

115 stdout = stdoutStream.getvalue() 

116 stderr = stderrStream.getvalue() 

117 self.assertIn("Command expand is unknown.", stdout) 

118 self.assertEqual("", stderr) 

119 

120 

121class Version(TestCase): 

122 _program: Program 

123 

124 def setUp(self) -> None: 

125 self._program = Program() 

126 

127 @patch('sys.stderr', new_callable=StringIO) 

128 @patch('sys.stdout', new_callable=StringIO) 

129 def test_VersionCommand(self, stdoutStream: StringIO, stderrStream: StringIO): 

130 sys.argv = [PROGRAM, "version"] 

131 

132 self._program.Run() 

133 

134 stdout = stdoutStream.getvalue() 

135 stderr = stderrStream.getvalue() 

136 self.assertIn("UCDB Service Program", stdout) 

137 self.assertIn("Version:", stdout) 

138 self.assertEqual("", stderr) 

139 

140 

141class Export(TestCase): 

142 _program: Program 

143 

144 def setUp(self) -> None: 

145 self._program = Program() 

146 

147 @patch('sys.stderr', new_callable=StringIO) 

148 @patch('sys.stdout', new_callable=StringIO) 

149 def test_ExportCommandNoFilenames(self, stdoutStream: StringIO, stderrStream: StringIO): 

150 sys.argv = [PROGRAM, "export"] 

151 

152 with self.assertRaises(SystemExit) as ex: 

153 self._program.Run() 

154 

155 self.assertEqual(3, ex.exception.code) 

156 

157 stdout = stdoutStream.getvalue() 

158 stderr = stderrStream.getvalue() 

159 self.assertIn("UCDB Service Program", stdout) 

160 self.assertEqual("", stderr) 

161 

162 @patch('sys.stderr', new_callable=StringIO) 

163 @patch('sys.stdout', new_callable=StringIO) 

164 def test_ExportCommandWithFilenames(self, stdoutStream: StringIO, stderrStream: StringIO): 

165 sys.argv = [PROGRAM, "export", "--ucdb", "file1.xml", "--cobertura", "file2.xml"] 

166 

167 with self.assertRaises(SystemExit) as ex: 

168 main() 

169 

170 self.assertEqual(1, ex.exception.code) 

171 

172 stdout = stdoutStream.getvalue() 

173 stderr = stderrStream.getvalue() 

174 self.assertIn("UCDB Service Program", stdout) 

175 self.assertIn("ERROR", stdout) 

176 self.assertEqual("", stderr)