8000 Add rest support to new parser · pythonthings/robotframework@312a083 · GitHub
[go: up one dir, main page]

Skip to content

Commit 312a083

Browse files
yannepekkaklarck
authored andcommitted
Add rest support to new parser
1 parent ff3227f commit 312a083

File tree

13 files changed

+49
-57
lines changed

13 files changed

+49
-57
lines changed

atest/robot/parsing/data_formats/formats_resource.robot

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ${FORMATS DIR} ${DATA DIR}/parsing/data_formats
66
${TSV DIR} ${FORMATS DIR}/tsv
77
${TXT DIR} ${FORMATS DIR}/txt
88
${ROBOT DIR} ${FORMATS DIR}/robot
9-
${REST DIR} ${FORMATS DIR}/rest_directives
9+
${REST DIR} ${FORMATS DIR}/rest
1010
${MIXED DIR} ${FORMATS DIR}/mixed_data
1111
${RESOURCE DIR} ${FORMATS DIR}/resources
1212
@{SAMPLE TESTS} Passing Failing User Keyword Nön-äscïï Own Tags Default Tags Variable Table
@@ -63,13 +63,3 @@ Check Suite With Init
6363
Should Be Equal ${suite.teardown} ${None}
6464
Should Contain Suites ${suite} Sub Suite1 Sub Suite2
6565
Should Contain Tests ${suite} @{SUBSUITE_TESTS}
66-
67-
*** Keywords ***
68-
Check Automatic Parsing Deprecated Message
69-
[Arguments] ${index} ${path}
70-
${path} = Normalize Path ${path}
71-
${msg} = Catenate
72-
... Automatically parsing other than '*.robot' files is deprecated.
73-
... Convert '${path}' to '*.robot' format or use '--extension' to
74-
... explicitly configure which files to parse.
75-
Check Log Message @{ERRORS}[${index}] ${msg} WARN

atest/robot/parsing/data_formats/rest_directives.robot renamed to atest/robot/parsing/data_formats/rest.robot

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ ReST With reST Resource
1010
Previous Run Should Have Been Successful
1111
Check Test Case Resource File
1212

13-
Parsing reST files automatically is deprecated
14-
Previous Run Should Have Been Successful
15-
Check Automatic Parsing Deprecated Message 0 ${RESTDIR}/sample.rst
16-
Length should be ${ERRORS} 1
17-
18-
Using --extension avoids deprecation warning
19-
Run sample file and check tests --extension rst ${RESTDIR}/sample.rst
20-
Length should be ${ERRORS} 0
21-
2213
ReST Directory
2314
Run Suite Dir And Check Results ${EMPTY} ${RESTDIR}
15+
16+
Directory With reST Init
17+
Previous Run Should Have Been Successful
18+
Check Suite With Init ${SUITE.suites[1]}
Lines changed: 1 addition & 0 deletions
Loading

atest/testdata/parsing/data_formats/rest_directives/sample.rst renamed to atest/testdata/parsing/data_formats/rest/sample.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. include:: ../rest/empty.rest
1+
.. include:: empty.rest
22
.. include:: include.rst
33

44
ReST Test Data Example
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.. code:: robotframework
2+
3+
** Settings **
4+
Suite Setup Suite Setup
5+
Documentation Testing suite init file
6+
7+
** Variables **
8+
${msg} = Running suite setup
9+
10+
** Keywords **
11+
Suite Setup Log ${msg} # No more arguments!
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. code:: robotframework
2+
3+
*Test Case* Whatever
4+
Suite1 Test No Operation
5+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. code:: robotframework
2+
3+
********************* Test Cases* ***************************************
4+
| Suite2 Test | [Documentation] | FAIL | Expected failure |
5+
| | Fail | ${msg} | |
6+
| | | | | | | | | | |
7+
8+
********************* Variables *****************************************
9+
| ${msg} | Expected failure |

src/robot/parsing/lexerwrapper.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from robot.output import LOGGER
2020
from robot.utils import Utf8Reader, get_error_message
2121

22+
from .restreader import read_rest
23+
2224

2325
PROCESS_CURDIR = True
2426

@@ -35,7 +37,10 @@ def _read(self, path):
3537
try:
3638
# IronPython handles BOM incorrectly if not using binary mode:
3739
# https://ironpython.codeplex.com/workitem/34655
38-
return Utf8Reader(open(path, 'rb')).read()
40+
with open(path, 'rb') as data:
41+
if os.path.splitext(path)[1].lower() in ('.rest', '.rst'):
42+
return read_rest(data)
43+
return Utf8Reader(data).read()
3944
except:
4045
raise DataError(get_error_message())
4146

src/robot/parsing/restreader.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,16 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from io import BytesIO
1716

18-
from .htmlreader import HtmlReader
19-
from .robotreader import RobotReader
17+
def read_rest(rstfile):
18+
from .restsupport import publish_doctree, RobotDataStorage
2019

20+
doctree = publish_doctree(
21+
rstfile.read(), source_path=rstfile.name,
22+
settings_overrides={
23+
'input_encoding': 'UTF-8',
24+
'report_level': 4
25+
})
26+
store = RobotDataStorage(doctree)
27+
return store.get_data()
2128

22-
def RestReader():
23-
from .res 10000 tsupport import (publish_doctree, publish_from_doctree,
24-
RobotDataStorage)
25-
26-
class RestReader(object):
27-
28-
def read(self, rstfile, rawdata):
29-
doctree = publish_doctree(
30-
rstfile.read(), source_path=rstfile.name,
31-
settings_overrides={
32-
'input_encoding': 'UTF-8',
33-
'report_level': 4
34-
})
35-
store = RobotDataStorage(doctree)
36-
if store.has_data():
37-
return self._read_text(store.get_data(), rawdata, rstfile.name)
38-
return self._read_html(doctree, rawdata, rstfile.name)
39-
40-
def _read_text(self, data, rawdata, path):
41-
robotfile = BytesIO(data.encode('UTF-8'))
42-
return RobotReader().read(robotfile, rawdata, path)
43-
44-
def _read_html(self, doctree, rawdata, path):
45-
htmlfile = BytesIO()
46-
htmlfile.write(publish_from_doctree(
47-
doctree, writer_name='html',
48-
settings_overrides={'output_encoding': 'UTF-8'}))
49-
htmlfile.seek(0)
50-
return HtmlReader().read(htmlfile, rawdata, path)
51-
52-
return RestReader()

src/robot/parsing/restsupport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from robot.errors import DataError
1717

1818
try:
19-
from docutils.core import publish_doctree, publish_from_doctree
19+
from docutils.core import publish_doctree
2020
from docutils.parsers.rst.directives import register_directive
2121
from docutils.parsers.rst.directives.body import CodeBlock
2222
except ImportError:

0 commit comments

Comments
 (0)
0