Skip to content

Commit db735a6

Browse files
committed
Handle non-readable directories gracefully
Fixes robotframework#3268
1 parent a5306fb commit db735a6

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

atest/resources/TestHelper.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from stat import S_IREAD, S_IWRITE
2+
from stat import S_IREAD, S_IWRITE, S_IEXEC
33

44
from robot.api import logger
55

@@ -12,6 +12,12 @@ def set_read_only(self, path):
1212
def set_read_write(self, path):
1313
os.chmod(path, S_IREAD | S_IWRITE)
1414

15+
def set_read_write_execute(self, path):
16+
os.chmod(path, S_IREAD | S_IWRITE | S_IEXEC)
17+
18+
def remove_permissions(self, path):
19+
os.chmod(path, 0)
20+
1521
def file_should_have_correct_line_separators(self, output, sep=os.linesep):
1622
if os.path.isfile(output):
1723
with open(output, 'rb') as infile:

atest/robot/parsing/invalid.robot

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Resource data_formats/formats_resource.robot
33

44
*** Variables ***
55
${PARSING} ${DATADIR}/parsing
6+
${SUITE DIR} %{TEMPDIR}/tmp
67

78
*** Test Cases ***
89
Directory Containing No Test Cases
@@ -53,6 +54,23 @@ Multisource Containing File With Invalid Encoding
5354
... UnicodeDecodeError: .*
5455
... ${PARSING}/invalid_encoding/invalid_encoding.robot
5556

57+
File without read permission
58+
[Setup] Create test data without permissions ${SUITE DIR}/sample.robot
59+
Run tests and check parsing error
60+
... ${SUITE DIR}/sample.robot
61+
... (IOError|PermissionError): .*
62+
... ${SUITE DIR}/sample.robot
63+
[Teardown] Remove test data without permissions ${SUITE DIR}/sample.robot
64+
65+
Directory without read permission
66+
[Setup] Create test data without permissions ${SUITE DIR}
67+
Run tests and check parsing error
68+
... ${SUITE DIR}
69+
... (OSError|PermissionError): .*
70+
... ${SUITE DIR}
71+
... Reading directory
72+
[Teardown] Remove test data without permissions ${SUITE DIR}
73+
5674
*** Keywords ***
5775
Run tests and check error
5876
[Arguments] ${paths} ${error}
@@ -61,6 +79,17 @@ Run tests and check error
6179
Check Stderr Matches Regexp \\[ ERROR \\] ${error}${USAGE_TIP}
6280

6381
Run tests and check parsing error
64-
[Arguments] ${paths} ${error} ${file}
65-
${file}= Normalize path ${file}
66-
Run tests and check error ${paths} Parsing '${file}' failed: ${error}
82+
[Arguments] ${paths} ${error} ${path} ${prefix}=Parsing
83+
${path}= Normalize path ${path}
84+
Run tests and check error ${paths} ${prefix} '${path}' failed: ${error}
85+
86+
Create test data without permissions
87+
[Arguments] ${remove permissions}
88+
Create directory ${SUITE DIR}
89+
Copy file ${ROBOTDIR}/sample.robot ${SUITE DIR}
90+
Remove permissions ${remove permissions}
91+
92+
Remove test data without permissions
93+
[Arguments] ${remove permissions}
94+
Set read write execute ${remove permissions}
95+
Remove directory ${SUITE DIR} recursive=True

src/robot/running/builder/suitestructure.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from robot.errors import DataError
1919
from robot.model import SuiteNamePatterns
2020
from robot.output import LOGGER
21-
from robot.utils import abspath, unic
21+
from robot.utils import abspath, get_error_message, unic
2222

2323

2424
class SuiteStructure(object):
@@ -112,7 +112,11 @@ def _get_child_paths(self, dirpath, incl_suites=None):
112112
def _list_dir(self, dir_path, incl_suites):
113113
# os.listdir returns Unicode entries when path is Unicode
114114
dir_path = unic(dir_path)
115-
names = os.listdir(dir_path)
115+
try:
116+
names = os.listdir(dir_path)
117+
except:
118+
raise DataError("Reading directory '%s' failed: %s"
119+
% (dir_path, get_error_message()))
116120
for name in sorted(names, key=lambda item: item.lower()):
117121
name = unic(name) # needed to handle nfc/nfd normalization on OSX
118122
path = os.path.join(dir_path, name)

0 commit comments

Comments
 (0)