Skip to content

Commit ef39029

Browse files
committed
Validate test case counts correctly
Also fix tests for invalid encoding handling
1 parent 64df016 commit ef39029

File tree

8 files changed

+78
-31
lines changed

8 files changed

+78
-31
lines changed

atest/robot/parsing/data_formats/formats_resource.robot

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ Run Suite Dir And Check Results
5454
Check Syslog Contains | INFO \ | Data source '${path}${/}invalid.${type}' has no tests or tasks.
5555
Check Syslog Contains | INFO \ | Data source '${path}${/}empty.${type}' has no tests or tasks.
5656
Check Syslog Contains | INFO \ | Ignoring file or directory '${path}${/}not_a_picture.jpg'.
57-
Run Keyword If "${type}" != "rest"
58-
... Check Syslog Contains | ERROR | Parsing '${path}${/}invalid_encoding.${type}' failed: UnicodeDecodeError
5957

6058
Check Suite With Init
6159
[Arguments] ${suite}

atest/robot/parsing/invalid.robot

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,65 @@
22
Resource data_formats/formats_resource.robot
33

44
*** Variables ***
5-
${PARSING} parsing
6-
${NO TESTS} ${PARSING}${/}notests
7-
${EMPTY TC TABLE} ${PARSING}${/}empty_testcase_table.robot
8-
${NO TC TABLE MSG} File has no tests or tasks.
5+
${PARSING} ${DATADIR}/parsing
96

107
*** Test Cases ***
11-
Invalid Input
12-
Check Parsing Error unsupported.log Unsupported file format 'log'. ${PARSING}/unsupported.log
13-
148
Directory Containing No Test Cases
15-
Run Tests Without Processing Output ${EMPTY} ${NO TESTS}
16-
Stderr Should Be Equal To [ ERROR ] Suite 'Notests' contains no tests.${USAGE_TIP}\n
9+
Run tests and check error
10+
... ${PARSING}/notests
11+
... Suite 'Notests' contains no tests or tasks.
1712

1813
File Containing No Test Cases
19-
Run Tests Without Processing Output ${EMPTY} ${EMPTY TC TABLE}
20-
Stderr Should Be Equal To [ ERROR ] Suite 'Empty Testcase Table' contains no tests.${USAGE_TIP}\n
14+
Run tests and check error
15+
... ${PARSING}/empty_testcase_table.robot
16+
... Suite 'Empty Testcase Table' contains no tests or tasks.
17+
18+
Empty File
19+
Run tests and check error
20+
... ${ROBOTDIR}/empty.robot
21+
... Suite 'Empty' contains no tests or tasks.
22+
23+
Multisource Containing Empty File
24+
Run tests and check error
25+
... ${ROBOTDIR}/empty.robot ${ROBOTDIR}/sample.robot
26+
... Suite 'Empty' contains no tests or tasks.
2127

22-
Multisource Containing No Test Cases
23-
Run Tests Without Processing Output ${EMPTY} ${ROBOTDIR}/empty.robot ${ROBOTDIR}/sample.robot
24-
${path} = Normalize Path ${ROBOTDIR}/empty.robot
25-
Stderr Should Be Equal To [ ERROR ] Parsing '${path}' failed: ${NO TC TABLE MSG}${USAGE TIP}\n
28+
Multisource With Empty Directory
29+
Run tests and check error
30+
... ${ROBOTDIR}/sample.robot ${PARSING}/notests
31+
... Suite 'Notests' contains no tests or tasks.
2632

27-
Empty TSV File
28-
Check Parsing Error empty.tsv ${NO TC TABLE MSG} ${TSVDIR}/empty.tsv
33+
Multisource Containing Empty File With Non-standard Extension
34+
Run tests and check error
35+
... ${PARSING}/unsupported.log ${ROBOTDIR}/sample.robot
36+
... Suite 'Unsupported' contains no tests or tasks.
2937

30-
Empty TXT File
31-
Check Parsing Error empty.txt ${NO TC TABLE MSG} ${TXTDIR}/empty.txt
38+
File With Invalid Encoding
39+
Run tests and check parsing error
40+
... ${PARSING}/invalid_encoding/invalid_encoding.robot
41+
... UnicodeDecodeError: .*
42+
... ${PARSING}/invalid_encoding/invalid_encoding.robot
43+
44+
Directory Containing File With Invalid Encoding
45+
Run tests and check parsing error
46+
... ${PARSING}/invalid_encoding/
47+
... UnicodeDecodeError: .*
48+
... ${PARSING}/invalid_encoding/invalid_encoding.robot
49+
50+
Multisource Containing File With Invalid Encoding
51+
Run tests and check parsing error
52+
... ${PARSING}/invalid_encoding/invalid_encoding.robot ${PARSING}/invalid_encoding/a_valid_file.robot
53+
... UnicodeDecodeError: .*
54+
... ${PARSING}/invalid_encoding/invalid_encoding.robot
3255

3356
*** Keywords ***
34-
Check Parsing Error
35-
[Arguments] ${file} ${error} ${paths}
36-
Run Tests Without Processing Output ${EMPTY} ${paths}
37-
Check Stderr Matches Regexp \\[ ERROR \\] Parsing '.*[/\\\\]${file}' failed: ${error}${USAGE_TIP}
57+
Run tests and check error
58+
[Arguments] ${paths} ${error}
59+
${result}= Run Tests Without Processing Output ${EMPTY} ${paths}
60+
Should be equal ${result.rc} ${252}
61+
Check Stderr Matches Regexp \\[ ERROR \\] ${error}${USAGE_TIP}
62+
63+
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}

atest/testdata/parsing/data_formats/tsv/invalid_encoding.tsv

Lines changed: 0 additions & 1 deletion
This file was deleted.

atest/testdata/parsing/data_formats/txt/invalid_encoding.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*** Test Cases ***
2+
Test
3+
Log Hello

src/robot/model/testsuite.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ def test_count(self):
109109
"""Number of the tests in this suite, recursively."""
110110
return len(self.tests) + sum(suite.test_count for suite in self.suites)
111111

112+
@property
113+
def has_tests(self):
114+
if self.tests:
115+
return True
116+
return any(s.has_tests for s in self.suites)
117+
112118
def set_tags(self, add=None, remove=None, persist=False):
113119
"""Add and/or remove specified tags to the tests in this suite.
114120

src/robot/running/builder/builders.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,23 @@ def build(self, *paths):
4141
structure = SuiteStructureBuilder(self.include_suites,
4242
self.extension).build(paths)
4343
parser = SuiteStructureParser(self.rpa)
44-
parser.parse(structure)
45-
suite = parser.suite
46-
suite.rpa = parser.rpa
47-
suite.remove_empty_suites(preserve_direct_children=len(paths) > 1)
44+
suite = parser.parse(structure)
45+
self._validate_test_counts(suite, multisource=len(paths) > 1)
4846
return suite
4947

48+
def _validate_test_counts(self, suite, multisource=False):
49+
def validate(suite):
50+
if not suite.has_tests:
51+
raise DataError("Suite '%s' contains no tests or tasks."
52+
% suite.name)
53+
if not multisource:
54+
validate(suite)
55+
else:
56+
for s in suite.suites:
57+
validate(s)
58+
# TODO: do we need `preserve_direct_children`?
59+
suite.remove_empty_suites(preserve_direct_children=multisource)
60+
5061

5162
class ResourceFileBuilder(object):
5263

@@ -66,6 +77,8 @@ def __init__(self, rpa=None):
6677

6778
def parse(self, structure):
6879
structure.visit(self)
80+
self.suite.rpa = self.rpa
81+
return self.suite
6982

7083
def visit_file(self, structure):
7184
LOGGER.info("Parsing file '%s'." % structure.source)

0 commit comments

Comments
 (0)