ducminh-phan / reformat-gherkin

Formatter for Gherkin language
MIT License
22 stars 13 forks source link

Triple quotes in doc strings are not escaped #31

Closed rayjolt closed 4 years ago

rayjolt commented 4 years ago

Doc strings with escaped triple quotes currently cause an internal error.

For example, take this document:

Feature: Doc strings

  Scenario: Escaping doc strings
    Given I have a docstring
    """
    This is a doc string.
      \"\"\"
      It has an escaped doc string inside it.
      \"\"\"
    """
    When I reformat it
    Then the resulting doc string is escaped

When I reformat it, I get the following error:

Error: cannot format /path/to/docstring_escape.feature: INTERNAL ERROR: Invalid file contents are produced:
Parser errors:
(8:7): expected: #EOF, #StepLine, #TagLine, #ScenarioLine, #ScenarioOutlineLine, #Comment, #Empty, got 'It has an escaped doc string inside it.'
(9:7): expected: #EOF, #StepLine, #TagLine, #ScenarioLine, #ScenarioOutlineLine, #Comment, #Empty, got '"""'
(10:5): expected: #EOF, #StepLine, #TagLine, #ScenarioLine, #ScenarioOutlineLine, #Comment, #Empty, got '"""'
Please report a bug on https://github.com/ducminh-phan/reformat-gherkin/issues.
This invalid output might be helpful:
/tmp/rfmt-ghk_jlm3bzbe.log

All done! 💥 💔 💥
3 files left unchanged, 1 file failed to reformat.

The contents of /tmp/rfmt-ghk_jlm3bzbe.log are as follows:

  File "/path/to/reformat-gherkin/reformat_gherkin/core.py", line 130, in assert_equivalent
    dst_ast = parse(dst)
  File "/path/to/reformat-gherkin/reformat_gherkin/parser.py", line 53, in parse
    raise InvalidInput(e) from e
Feature: Doc strings

  Scenario: Escaping doc strings
    Given I have a docstring
    """
    This is a doc string.
      """
      It has an escaped doc string inside it.
      """
    """
    When I reformat it
    Then the resulting doc string is escaped

To fix this, we need to escape """ inside doc strings with \"\"\".

Python version: 3.8.1 Reformat-gherkin version: latest develop (75efa435e745b9ccb09ef170d5cfbd69f8d91807)