Skip to content

Commit

Permalink
Cleaned up strict mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdewinter committed May 16, 2021
1 parent 747037d commit 90eae2c
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 20 deletions.
1 change: 0 additions & 1 deletion issues.md
Expand Up @@ -3,7 +3,6 @@
## Priority 1 - Must Solve Before Initial

- command line and configuration documentation
- make sure new strict mode reports config properly

## Priority 2 - Like To Solve Before Initial

Expand Down
2 changes: 1 addition & 1 deletion publish/coverage.json
@@ -1 +1 @@
{"projectName": "pymarkdown", "reportSource": "pytest", "branchLevel": {"totalMeasured": 2098, "totalCovered": 2089}, "lineLevel": {"totalMeasured": 7238, "totalCovered": 7226}}
{"projectName": "pymarkdown", "reportSource": "pytest", "branchLevel": {"totalMeasured": 2100, "totalCovered": 2092}, "lineLevel": {"totalMeasured": 7250, "totalCovered": 7238}}
2 changes: 1 addition & 1 deletion publish/test-results.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pymarkdown/application_properties.py
Expand Up @@ -265,7 +265,7 @@ def get_property(
# pylint: enable=raise-missing-from

def get_boolean_property(
self, property_name, default_value=None, is_required=False
self, property_name, default_value=None, is_required=False, strict_mode=None
):
"""
Get a boolean property from the configuration.
Expand All @@ -276,7 +276,7 @@ def get_boolean_property(
default_value=default_value,
valid_value_fn=None,
is_required=is_required,
strict_mode=None,
strict_mode=strict_mode,
)

# pylint: disable=too-many-arguments
Expand Down
17 changes: 16 additions & 1 deletion pymarkdown/main.py
Expand Up @@ -328,6 +328,9 @@ def __initialize_parser(self, args):
try:
self.__tokenizer = TokenizedMarkdown(resource_path)
self.__tokenizer.apply_configuration(self.__properties)
except ValueError as this_exception:
formatted_error = "Configuration Error: " + str(this_exception)
self.__handle_error(formatted_error)
except BadTokenizationError as this_exception:
formatted_error = f"{str(type(this_exception).__name__)} encountered while initializing tokenizer:\n{str(this_exception)}"
self.__handle_error(formatted_error)
Expand Down Expand Up @@ -379,7 +382,15 @@ def __set_initial_state(self, args):
)
if args.set_configuration:
self.__properties.set_manual_property(args.set_configuration)
if args.strict_configuration:

def __initialize_strict_mode(self, args):
effective_strict_configuration = args.strict_configuration
if not effective_strict_configuration:
effective_strict_configuration = self.__properties.get_boolean_property(
"mode.strict-config", strict_mode=True
)

if effective_strict_configuration:
self.__properties.enable_strict_mode()

def __initialize_logging(self, args):
Expand Down Expand Up @@ -429,6 +440,7 @@ def main(self):
new_handler = None
total_error_count = 0
try:
self.__initialize_strict_mode(args)
new_handler = self.__initialize_logging(args)

if args.primary_subparser == PluginManager.argparse_subparser_name():
Expand Down Expand Up @@ -460,6 +472,9 @@ def main(self):
self.__handle_scan_error(next_file, this_exception)
except BadTokenizationError as this_exception:
self.__handle_scan_error(next_file, this_exception)
except ValueError as this_exception:
formatted_error = f"Configuration Error: {this_exception}"
self.__handle_error(formatted_error)
finally:
if new_handler:
new_handler.close()
Expand Down
11 changes: 6 additions & 5 deletions pymarkdown/plugins/rule_md_018.py
Expand Up @@ -96,11 +96,12 @@ def __next_token_paragraph_non_text_inline(self, token):
self.__delayed_line = None
self.__paragraph_index += 1
self.__first_line_after_hard_break = True
elif (
token.is_inline_emphasis
or token.is_inline_emphasis_end
or token.is_inline_autolink
):
else:
assert (
token.is_inline_emphasis
or token.is_inline_emphasis_end
or token.is_inline_autolink
)
self.__delayed_line = None

def __next_token_paragraph_text_inline(self, token, context):
Expand Down
20 changes: 11 additions & 9 deletions pymarkdown/tokenized_markdown.py
Expand Up @@ -36,12 +36,13 @@ def __init__(self, resource_path=None):
Initializes a new instance of the TokenizedMarkdown class.
"""

self.tokenized_document, self.stack, self.source_provider, self.__properties = (
None,
None,
None,
ApplicationProperties(),
)
(
self.tokenized_document,
self.stack,
self.source_provider,
self.__properties,
self.__front_matter_enabled,
) = (None, None, None, ApplicationProperties(), None)

if not resource_path:
resource_path = os.path.join(os.path.split(__file__)[0], "resources")
Expand All @@ -52,6 +53,9 @@ def apply_configuration(self, application_properties):
Apply any configuration map.
"""
self.__properties = application_properties
self.__front_matter_enabled = self.__properties.get_boolean_property(
"extensions.front-matter.enabled", default_value=False
)

def transform_from_provider(self, source_provider):
"""
Expand Down Expand Up @@ -565,9 +569,7 @@ def __handle_blank_line_in_block_quote(parser_state):

def __process_header_if_present(self, token_to_use, line_number, requeue):

if self.__properties.get_boolean_property(
"extensions.front-matter.enabled", default_value=False
):
if self.__front_matter_enabled:
(
token_to_use,
line_number,
Expand Down
70 changes: 70 additions & 0 deletions test/test_main.py
Expand Up @@ -1227,3 +1227,73 @@ def test_markdown_logger_arg_list_out_of_sync():


# pylint: enable=broad-except


def test_markdown_with_bad_strict_config_type():
"""
Test to make sure that we can set the strict configuration mode from
the configuration file, capturing any bad errors.
"""

# Arrange
scanner = MarkdownScanner()
supplied_configuration = {"mode": {"strict-config": 2}}
configuration_file = None
try:
configuration_file = write_temporary_configuration(supplied_configuration)
supplied_arguments = [
"-c",
configuration_file,
"scan",
"test/resources/rules/md047/end_with_blank_line.md",
]

expected_return_code = 1
expected_output = ""
expected_error = "Configuration Error: The value for property 'mode.strict-config' must be of type 'bool'."

# Act
execute_results = scanner.invoke_main(arguments=supplied_arguments)

# Assert
execute_results.assert_results(
expected_output, expected_error, expected_return_code
)
finally:
if configuration_file and os.path.exists(configuration_file):
os.remove(configuration_file)


def test_markdown_with_good_strict_config_type():
"""
Test to make sure that we can set the strict configuration mode from
the configuration file, capturing any bad errors.
"""

# Arrange
scanner = MarkdownScanner()
supplied_configuration = {"mode": {"strict-config": True}, "log": {"file": 0}}
configuration_file = None
try:
configuration_file = write_temporary_configuration(supplied_configuration)
supplied_arguments = [
"-c",
configuration_file,
"scan",
"test/resources/rules/md047/end_with_blank_line.md",
]

expected_return_code = 1
expected_output = ""
expected_error = "Configuration Error: The value for property 'log.file' must be of type 'str'.\n"

# Act
execute_results = scanner.invoke_main(arguments=supplied_arguments)

# Assert
execute_results.assert_results(
expected_output, expected_error, expected_return_code
)
finally:
if configuration_file and os.path.exists(configuration_file):
os.remove(configuration_file)
29 changes: 29 additions & 0 deletions test/test_md001.py
Expand Up @@ -256,3 +256,32 @@ def test_md001_front_matter_with_alternate_title():
execute_results.assert_results(
expected_output, expected_error, expected_return_code
)


@pytest.mark.rules
def test_md001_front_matter_with_bad_enable_in_strict_mode():
"""
Test to make
"""

# Arrange
scanner = MarkdownScanner()
supplied_arguments = [
"--strict-config",
"--set",
"extensions.front-matter.enabled=True",
"scan",
"test/resources/rules/md001/front_matter_with_title.md",
]

expected_return_code = 1
expected_output = ""
expected_error = "Configuration Error: The value for property 'extensions.front-matter.enabled' must be of type 'bool'."

# Act
execute_results = scanner.invoke_main(arguments=supplied_arguments)

# Assert
execute_results.assert_results(
expected_output, expected_error, expected_return_code
)

0 comments on commit 90eae2c

Please sign in to comment.