@@ -787,3 +787,91 @@ def test_patterns() -> None:
787787def test_extract_extra (line : str , expected_result : str ) -> None :
788788 """Test that the _extract_backtick_options function works as expected."""
789789 assert _extract_backtick_options (line ) == expected_result
790+
791+
792+ def _verify_no_trailing_whitespace (output_section : list [str ], label : str = "" ) -> None :
793+ """Helper to verify no trailing whitespace in output."""
794+ for line in output_section :
795+ assert line == line .rstrip (), f"{ label } Line has trailing whitespace: '{ line } '"
796+
797+
798+ def test_trailing_whitespace_trimming () -> None :
799+ """Test that trailing whitespace is properly trimmed from output."""
800+ # Test case 1: Python code with trailing whitespace
801+ input_lines = [
802+ "# Test trailing whitespace" ,
803+ "```python markdown-code-runner" ,
804+ "print('Hello World ')" ,
805+ "print('Line with spaces ')" ,
806+ "print('')" ,
807+ "print('After empty line')" ,
808+ "print(' ') # This will print only spaces" ,
809+ "```" ,
810+ "<!-- OUTPUT:START -->" ,
811+ "<!-- OUTPUT:END -->" ,
812+ ]
813+
814+ output = process_markdown (input_lines , backtick_standardize = False )
815+ start_idx = output .index ("<!-- OUTPUT:START -->" )
816+ end_idx = output .index ("<!-- OUTPUT:END -->" )
817+ output_section = output [start_idx + 2 : end_idx ]
818+
819+ _verify_no_trailing_whitespace (output_section , "Python " )
820+ assert output_section [0 ] == "Hello World"
821+ assert output_section [1 ] == "Line with spaces"
822+ assert output_section [2 ] == ""
823+ assert output_section [3 ] == "After empty line"
824+ assert output_section [4 ] == "" # Line with only spaces becomes empty
825+ assert output_section [5 ] == "" # Trailing empty line preserved
826+
827+ # Test case 2: Bash code with trailing whitespace
828+ input_lines_bash = [
829+ "```bash markdown-code-runner" ,
830+ "echo 'Hello from bash '" ,
831+ "echo 'Another line '" ,
832+ "echo ''" ,
833+ "echo 'After empty'" ,
834+ "echo ' ' # Only spaces" ,
835+ "```" ,
836+ "<!-- OUTPUT:START -->" ,
837+ "<!-- OUTPUT:END -->" ,
838+ ]
839+
840+ output_bash = process_markdown (input_lines_bash , backtick_standardize = False )
841+ start_idx_bash = output_bash .index ("<!-- OUTPUT:START -->" )
842+ end_idx_bash = output_bash .index ("<!-- OUTPUT:END -->" )
843+ output_section_bash = output_bash [start_idx_bash + 2 : end_idx_bash ]
844+
845+ _verify_no_trailing_whitespace (output_section_bash , "Bash " )
846+ assert output_section_bash [0 ] == "Hello from bash"
847+ assert output_section_bash [1 ] == "Another line"
848+ assert output_section_bash [2 ] == ""
849+ assert output_section_bash [3 ] == "After empty"
850+ assert output_section_bash [4 ] == "" # Line with only spaces becomes empty
851+ assert output_section_bash [5 ] == "" # Trailing empty line preserved
852+
853+ # Test case 3: Hidden code block with trailing whitespace
854+ input_lines_hidden = [
855+ "<!-- CODE:START -->" ,
856+ "<!-- print('Hidden output ') -->" ,
857+ "<!-- print('With spaces ') -->" ,
858+ "<!-- print('') -->" ,
859+ "<!-- print('Final line') -->" ,
860+ "<!-- print(' ') # Only spaces -->" ,
861+ "<!-- CODE:END -->" ,
862+ "<!-- OUTPUT:START -->" ,
863+ "<!-- OUTPUT:END -->" ,
864+ ]
865+
866+ output_hidden = process_markdown (input_lines_hidden , backtick_standardize = False )
867+ start_idx_hidden = output_hidden .index ("<!-- OUTPUT:START -->" )
868+ end_idx_hidden = output_hidden .index ("<!-- OUTPUT:END -->" )
869+ output_section_hidden = output_hidden [start_idx_hidden + 2 : end_idx_hidden ]
870+
871+ _verify_no_trailing_whitespace (output_section_hidden , "Hidden " )
872+ assert output_section_hidden [0 ] == "Hidden output"
873+ assert output_section_hidden [1 ] == "With spaces"
874+ assert output_section_hidden [2 ] == ""
875+ assert output_section_hidden [3 ] == "Final line"
876+ assert output_section_hidden [4 ] == "" # Line with only spaces becomes empty
877+ assert output_section_hidden [5 ] == "" # Trailing empty line preserved
0 commit comments