Coverage for src/robotide/contrib/testrunner/FileWriter.py: 56%

38 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

1# Copyright 2010 Orbitz WorldWide 

2# 

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

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

5# You may obtain a copy of the License at 

6# 

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

8# 

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

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

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

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

13# limitations under the License. 

14 

15# Modified by NSN 

16# Copyright 2010-2012 Nokia Solutions and Networks 

17# Copyright 2013-2015 Nokia Networks 

18# Copyright 2016- Robot Framework Foundation 

19# 

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

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

22# You may obtain a copy of the License at 

23# 

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

25# 

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

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

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

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

30# limitations under the License. 

31 

32import codecs 

33import os 

34 

35from robotide.context import IS_WINDOWS 

36from sys import getfilesystemencoding 

37 

38OUTPUT_ENCODING = getfilesystemencoding() 

39 

40 

41def _write_windows(fh, lines): 

42 for item in lines: 

43 if isinstance(item, str): 

44 enc_arg = item.encode('UTF-8') # OUTPUT_ENCODING 

45 else: 

46 enc_arg = item 

47 try: 

48 fh.write(enc_arg) 

49 fh.write("\n".encode(OUTPUT_ENCODING)) 

50 except UnicodeError: 

51 fh.write(bytes(item, 'UTF-8')) 

52 fh.write(b"\n") 

53 

54 

55def _write_linux(fh, lines): 

56 for item in lines: 1cbd

57 if isinstance(item, str): 1cbd

58 fh.write(item) 1cd

59 fh.write("\n") 1cd

60 else: 

61 try: 1b

62 fh.write(item.decode('UTF-8')) 1b

63 fh.write("\n") 1b

64 except (UnicodeError, TypeError) as e: 

65 print(f"FileWriter: unexpected UnicodeError or TypeError at position: {fh.tell()}") 

66 raise e 

67 

68 

69class FileWriter: 

70 

71 @staticmethod 

72 def write(file_path, lines, windows_mode, mode='w'): 

73 if not os.path.exists(os.path.dirname(file_path)): 73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true1cbd

74 os.makedirs(os.path.dirname(file_path)) 

75 if IS_WINDOWS: 75 ↛ 76line 75 didn't jump to line 76 because the condition on line 75 was never true1cbd

76 f = codecs.open(file_path, mode=windows_mode) 

77 _write_windows(f, lines) 

78 else: 

79 f = codecs.open(file_path, mode, "UTF-8") 1cbd

80 # DEBUG: f.write("\n".join(lines)) 

81 _write_linux(f, lines) 1cbd

82 f.close() 1cbd