From b4c912769e9547018ddbaa8d1b0339d744a10c9e Mon Sep 17 00:00:00 2001 From: Jack De Winter Date: Sat, 7 Dec 2024 19:56:52 -0800 Subject: [PATCH] https://github.com/jackdewinter/pymarkdown/issues/1280 --- .gitignore | 1 + batch_tests.cmd | 43 ++++++++++++++++++++++++++++++++++++++++ newdocs/src/changelog.md | 4 ++++ ptest.cmd | 28 ++++++++++++++++++++++++++ run.cmd | 4 ++++ test/pytest_execute.py | 23 +++++++++++++++++++-- test/utils.py | 11 ++++++++-- 7 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 batch_tests.cmd diff --git a/.gitignore b/.gitignore index 17ec8d647..756bc0ebb 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ build/ dist/ pymarkdownlnt.egg-info/ p0.prof +ff/ diff --git a/batch_tests.cmd b/batch_tests.cmd new file mode 100644 index 000000000..b41c82cf1 --- /dev/null +++ b/batch_tests.cmd @@ -0,0 +1,43 @@ +@echo off +setlocal EnableDelayedExpansion + +rem Set needed environment variables. + +:uniqLoop +set "XX_TEMPFILE=%tmp%\xx_~%RANDOM%.tmp" +if exist "%XX_TEMPFILE%" goto :uniqLoop + +set disable_list="extensions.markdown-task-list-items.enabled=@True" +set disable_list=%disable_list%;"extensions.markdown-strikethrough.enabled=@True" +set disable_list=%disable_list%;"extensions.markdown-extended-autolinks.enabled=@True" +set disable_list=%disable_list%;"extensions.markdown-disallow-raw-html.enabled=@True" +set disable_list=%disable_list%;"extensions.front-matter.enabled=@True" + +set filesCount=0 & for %%f in (ff\*) do @(set /a filesCount+=1 > nul) + +set "bool_replace=$^!" +for %%a in (%disable_list%) do ( + set temp_path=%%a + call set next_path=%%temp_path:@=!bool_replace!%% + echo. + echo Scanning with setting: !next_path! + set /a count = 0 + for %%i in (ff\*) do ( + set file_to_scan=%%i + set /a count += 1 + FOR /F "delims=" %%j IN ('python -c "print((!count! %% 10))"') DO set mcount=%%j + if "!mcount!"=="0" ( + echo !count! out of %filesCount% + ) + pipenv run python main.py --strict --set "!next_path!" --return-code-scheme minimal scan !file_to_scan! > %XX_TEMPFILE% + if ERRORLEVEL 1 ( + type %XX_TEMPFILE% + echo pipenv run python main.py --strict --set "!next_path!" --return-code-scheme minimal scan !file_to_scan! + echo BADD !file_to_scan! + ) + ) + echo !count! out of %filesCount% +) + +set xx= +erase %XX_TEMPFILE% \ No newline at end of file diff --git a/newdocs/src/changelog.md b/newdocs/src/changelog.md index 5075ada96..759ca1a27 100644 --- a/newdocs/src/changelog.md +++ b/newdocs/src/changelog.md @@ -7,6 +7,10 @@ - [Issue 810](https://github.com/jackdewinter/pymarkdown/issues/810) - Added fix for Rule Md012 +- [Issue 1280](https://github.com/jackdewinter/pymarkdown/issues/1280) + - Added capability to save all single Markdown documents in + a specified directory, then scanning them one at a time with + PyMarkdown and each of the extensions enabled. ### Fixed diff --git a/ptest.cmd b/ptest.cmd index 41a4f19f5..d496e41b6 100644 --- a/ptest.cmd +++ b/ptest.cmd @@ -1,5 +1,6 @@ @echo off setlocal EnableDelayedExpansion +set OLDDIR=%CD% pushd %~dp0 rem Required to make sure coverage is written to the right areas. @@ -34,6 +35,7 @@ if "%1" == "-h" ( echo -m Enabled multi-core testing. echo -f Produce a full report for the tests instead of a 'changes only' report. echo -p Publish project summaries instead of running tests. + echo -d Capture test cases in the specified existing directory. echo -k [keyword] Execute only the tests matching the specified keyword. GOTO real_end ) else if "%1" == "-p" ( @@ -55,6 +57,13 @@ if "%1" == "-h" ( goto error_end ) shift +) else if "%1" == "-d" ( + set PTEST_KEEP_DIRECTORY=%2 + if not defined PTEST_KEEP_DIRECTORY ( + echo Option -d requires a keyword argument to follow it. + goto error_end + ) + shift ) else if "%1" == "" ( goto after_process_arguments ) else ( @@ -88,6 +97,25 @@ if defined PTEST_MULTI_CORE_ARGS ( set PTEST_MULTI_CORE_ARGS=-n !CORES_TO_USE! --dist loadscope ) +if defined PTEST_KEEP_DIRECTORY ( + if not exist %PTEST_KEEP_DIRECTORY%\nul ( + echo. + echo {Path specified by the -d option is not an existing directory.} + goto error_end + ) else ( + pushd %OLDDIR% + cd + cd !PTEST_KEEP_DIRECTORY! + FOR /F "tokens=* USEBACKQ" %%F IN (`cd`) DO ( + SET my_var=%%F + ) + set "PTEST_KEEP_DIRECTORY=!my_var!" + popd + + erase !PTEST_KEEP_DIRECTORY!\*.md + ) +) + rem Enter main part of script. if defined PTEST_KEYWORD ( echo {Executing partial test suite...} diff --git a/run.cmd b/run.cmd index 4e0e69016..ad86fc69e 100644 --- a/run.cmd +++ b/run.cmd @@ -1 +1,5 @@ pipenv run python main.py %1 %2 %3 %4 %5 %6 %7 %8 %9 +set RETURN_CODE=%ERRORLEVEL% +echo !RETURN_CODE! +echo %RETURN_CODE% +exit /b %RETURN_CODE% \ No newline at end of file diff --git a/test/pytest_execute.py b/test/pytest_execute.py index 6c2f2dfb0..7a695a69a 100644 --- a/test/pytest_execute.py +++ b/test/pytest_execute.py @@ -7,6 +7,7 @@ import logging import os import sys +import tempfile import traceback from abc import ABC, abstractmethod @@ -247,7 +248,7 @@ def handle_normal_exception(cls): del trace_back return 1 - # pylint: disable=broad-except, too-many-arguments + # pylint: disable=broad-except, too-many-arguments, too-many-locals, too-many-branches def invoke_main( self, arguments=None, @@ -259,6 +260,24 @@ def invoke_main( """ Invoke the mainline so that we can capture results. """ + + keep_directory = os.getenv("PTEST_KEEP_DIRECTORY", None) + if keep_directory and os.path.isdir(keep_directory): + if ( + len(arguments) >= 2 + and arguments[-2] == "scan" + and os.path.isfile(arguments[-1]) + ): + with tempfile.NamedTemporaryFile( + "wt", + encoding="utf-8", + dir=keep_directory, + suffix=".md", + delete=False, + ) as temp_file: + with open(arguments[-1], "rt", encoding="utf-8") as fixed_file: + temp_file.write(fixed_file.read()) + if suppress_first_line_heading_rule: new_arguments = arguments.copy() if arguments else [] if "--disable-rules" not in new_arguments: @@ -305,4 +324,4 @@ def invoke_main( return InProcessResult(returncode, std_output, std_error) - # pylint: enable=broad-except, too-many-arguments + # pylint: enable=broad-except, too-many-arguments, too-many-locals, too-many-branches diff --git a/test/utils.py b/test/utils.py index f73985610..962f8f56b 100644 --- a/test/utils.py +++ b/test/utils.py @@ -26,7 +26,7 @@ # from test.verify_line_and_column_numbers import verify_line_and_column_numbers -# pylint: disable=too-many-arguments +# pylint: disable=too-many-arguments, too-many-locals def act_and_assert( source_markdown, expected_gfm, @@ -41,6 +41,13 @@ def act_and_assert( Act and assert on the expected behavior of parsing the source_markdown. """ + keep_directory = os.getenv("PTEST_KEEP_DIRECTORY", None) + if keep_directory and os.path.isdir(keep_directory): + with tempfile.NamedTemporaryFile( + "wt", encoding="utf-8", dir=keep_directory, suffix=".md", delete=False + ) as temp_file: + temp_file.write(source_markdown) + # Arrange logging.getLogger().setLevel(logging.DEBUG if show_debug else logging.WARNING) ParserLogger.sync_on_next_call() @@ -80,7 +87,7 @@ def act_and_assert( ) -# pylint: enable=too-many-arguments +# pylint: enable=too-many-arguments, too-many-locals def read_contents_of_text_file(source_path: str) -> str: