Coverage for src/robotide/controller/validators.py: 94%

42 statements  

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

1# Copyright 2008-2015 Nokia Networks 

2# Copyright 2016- Robot Framework Foundation 

3# 

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

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

6# You may obtain a copy of the License at 

7# 

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

9# 

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

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

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

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

14# limitations under the License. 

15 

16import os 1ij

17 

18from ..publish.messages import RideInputValidationError 1ij

19 

20ERROR_ILLEGAL_CHARACTERS = "Filename contains illegal characters" 1ij

21ERROR_EMPTY_FILENAME = "Empty filename" 1ij

22ERROR_NEWLINES_IN_THE_FILENAME = "Newlines in the filename" 1ij

23ERROR_FILE_ALREADY_EXISTS = "File %s already exists" 1ij

24 

25 

26class BaseNameValidator(object): 1ij

27 

28 def __init__(self, new_basename): 1ij

29 self._new_basename = new_basename 1mefkldghabc

30 

31 def validate(self, context): 1ij

32 # Try-except is needed to check if file can be created if named like this, using open() 

33 # http://code.google.com/p/robotframework-ride/issues/detail?id=1111 

34 import pathlib 1mefkldghabc

35 try: 1mefkldghabc

36 file_name = '%s.%s' % (self._new_basename, context.get_format()) 1mefkldghabc

37 file_path = os.path.join(context.directory, file_name) 1mefkldghabc

38 if self._file_exists(file_path): 1mefkldghabc

39 RideInputValidationError(message=ERROR_FILE_ALREADY_EXISTS % file_path).publish() 1m

40 return False 1m

41 if '\\n' in self._new_basename or '\n' in self._new_basename: 1efkldghabc

42 RideInputValidationError(message=ERROR_NEWLINES_IN_THE_FILENAME).publish() 1l

43 return False 1l

44 if len(self._new_basename.strip()) == 0: 1efkdghabc

45 RideInputValidationError(message=ERROR_EMPTY_FILENAME).publish() 1k

46 return False 1k

47 created_dir = False 1efdghabc

48 try: 1efdghabc

49 if pathlib.PurePath(file_path).parent != pathlib.PurePath('.'): 1efdghabc

50 # print("DEBUG: Creating dirs %s", pathlib.PurePath(filePath).parent) 

51 pathlib.Path(pathlib.PurePath(file_path).parent).mkdir(parents=False, exist_ok=True) 1dabc

52 created_dir = True 1dabc

53 # print("DEBUG: Creating file %s", filePath) 

54 open(file_path, "w").close() 1efdghabc

55 finally: 

56 try: 1efdghabc

57 os.remove(file_path) # If file creation failed, then this will trigger validation error 1efdghabc

58 if created_dir: 1efdghabc

59 os.rmdir(pathlib.PurePath(file_path).parent) 1dabc

60 except Exception as e: 1abc

61 print(e) 1abc

62 return True 1efdghabc

63 except (IOError, OSError): 

64 RideInputValidationError(message=ERROR_ILLEGAL_CHARACTERS).publish() 

65 return False 

66 

67 @staticmethod 1ij

68 def _file_exists(filename): 1ij

69 return os.path.exists(filename) 1efkldghabc