1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
# test_whitespace.py -- Tests for whitespace error detection
# Copyright (C) 2025 Dulwich contributors
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#
"""Tests for whitespace error detection."""
from dulwich.whitespace import (
DEFAULT_WHITESPACE_ERRORS,
WhitespaceChecker,
fix_whitespace_errors,
parse_whitespace_config,
)
from . import TestCase
class WhitespaceConfigTests(TestCase):
"""Test core.whitespace configuration parsing."""
def test_parse_default(self) -> None:
"""Test default whitespace configuration."""
errors, tab_width = parse_whitespace_config(None)
self.assertEqual(errors, DEFAULT_WHITESPACE_ERRORS)
self.assertEqual(tab_width, 8)
def test_parse_empty(self) -> None:
"""Test empty whitespace configuration."""
errors, tab_width = parse_whitespace_config("")
self.assertEqual(errors, set())
self.assertEqual(tab_width, 8)
def test_parse_single_error(self) -> None:
"""Test single error type."""
errors, tab_width = parse_whitespace_config("blank-at-eol")
self.assertEqual(errors, {"blank-at-eol"})
self.assertEqual(tab_width, 8)
def test_parse_multiple_errors(self) -> None:
"""Test multiple error types."""
errors, tab_width = parse_whitespace_config(
"blank-at-eol,space-before-tab,tab-in-indent"
)
self.assertEqual(errors, {"blank-at-eol", "space-before-tab", "tab-in-indent"})
self.assertEqual(tab_width, 8)
def test_parse_with_negation(self) -> None:
"""Test negation of default errors."""
errors, tab_width = parse_whitespace_config("-blank-at-eol")
# Should have defaults minus blank-at-eol
expected = DEFAULT_WHITESPACE_ERRORS - {"blank-at-eol"}
self.assertEqual(errors, expected)
self.assertEqual(tab_width, 8)
def test_parse_trailing_space_alias(self) -> None:
"""Test that trailing-space is an alias for blank-at-eol."""
errors, tab_width = parse_whitespace_config("trailing-space")
self.assertEqual(errors, {"blank-at-eol"})
self.assertEqual(tab_width, 8)
def test_parse_tabwidth(self) -> None:
"""Test tabwidth setting."""
errors, tab_width = parse_whitespace_config("blank-at-eol,tabwidth=4")
self.assertEqual(errors, {"blank-at-eol"})
self.assertEqual(tab_width, 4)
def test_parse_invalid_tabwidth(self) -> None:
"""Test invalid tabwidth defaults to 8."""
_errors, tab_width = parse_whitespace_config("tabwidth=invalid")
self.assertEqual(tab_width, 8)
_errors, tab_width = parse_whitespace_config("tabwidth=0")
self.assertEqual(tab_width, 8)
class WhitespaceCheckerTests(TestCase):
"""Test WhitespaceChecker functionality."""
def test_blank_at_eol(self) -> None:
"""Test detection of trailing whitespace."""
checker = WhitespaceChecker({"blank-at-eol"})
# No trailing whitespace
errors = checker.check_line(b"normal line", 1)
self.assertEqual(errors, [])
# Trailing space
errors = checker.check_line(b"trailing space ", 1)
self.assertEqual(errors, [("blank-at-eol", 1)])
# Trailing tab
errors = checker.check_line(b"trailing tab\t", 1)
self.assertEqual(errors, [("blank-at-eol", 1)])
# Multiple trailing whitespace
errors = checker.check_line(b"multiple \t ", 1)
self.assertEqual(errors, [("blank-at-eol", 1)])
def test_space_before_tab(self) -> None:
"""Test detection of space before tab in indentation."""
checker = WhitespaceChecker({"space-before-tab"})
# No space before tab
errors = checker.check_line(b"\tindented", 1)
self.assertEqual(errors, [])
# Space before tab in indentation
errors = checker.check_line(b" \tindented", 1)
self.assertEqual(errors, [("space-before-tab", 1)])
# Space before tab not in indentation (should not trigger)
errors = checker.check_line(b"code \t comment", 1)
self.assertEqual(errors, [])
def test_indent_with_non_tab(self) -> None:
"""Test detection of 8+ spaces at start of line."""
checker = WhitespaceChecker({"indent-with-non-tab"}, tab_width=8)
# Less than 8 spaces
errors = checker.check_line(b" code", 1)
self.assertEqual(errors, [])
# Exactly 8 spaces
errors = checker.check_line(b" code", 1)
self.assertEqual(errors, [("indent-with-non-tab", 1)])
# More than 8 spaces
errors = checker.check_line(b" code", 1)
self.assertEqual(errors, [("indent-with-non-tab", 1)])
# Tab after spaces resets count
errors = checker.check_line(b" \t code", 1)
self.assertEqual(errors, [])
# Custom tab width
checker = WhitespaceChecker({"indent-with-non-tab"}, tab_width=4)
errors = checker.check_line(b" code", 1)
self.assertEqual(errors, [("indent-with-non-tab", 1)])
def test_tab_in_indent(self) -> None:
"""Test detection of tabs in indentation."""
checker = WhitespaceChecker({"tab-in-indent"})
# No tabs
errors = checker.check_line(b" code", 1)
self.assertEqual(errors, [])
# Tab in indentation
errors = checker.check_line(b"\tcode", 1)
self.assertEqual(errors, [("tab-in-indent", 1)])
# Tab after non-whitespace (should not trigger)
errors = checker.check_line(b"code\tcomment", 1)
self.assertEqual(errors, [])
def test_cr_at_eol(self) -> None:
"""Test detection of carriage return at end of line."""
checker = WhitespaceChecker({"cr-at-eol"})
# No CR
errors = checker.check_line(b"normal line", 1)
self.assertEqual(errors, [])
# CR at end
errors = checker.check_line(b"line\r", 1)
self.assertEqual(errors, [("cr-at-eol", 1)])
def test_blank_at_eof(self) -> None:
"""Test detection of blank lines at end of file."""
checker = WhitespaceChecker({"blank-at-eof"})
# No trailing blank lines
content = b"line1\nline2\nline3"
errors = checker.check_content(content)
self.assertEqual(errors, [])
# One trailing blank line (normal for files ending with newline)
content = b"line1\nline2\nline3\n"
errors = checker.check_content(content)
self.assertEqual(errors, [])
# Multiple trailing blank lines
content = b"line1\nline2\n\n\n"
errors = checker.check_content(content)
self.assertEqual(errors, [("blank-at-eof", 5)])
# Only blank lines
content = b"\n\n\n"
errors = checker.check_content(content)
self.assertEqual(errors, [("blank-at-eof", 4)])
def test_multiple_errors(self) -> None:
"""Test detection of multiple error types."""
checker = WhitespaceChecker(
{"blank-at-eol", "space-before-tab", "tab-in-indent"}
)
# Line with multiple errors
errors = checker.check_line(b" \tcode ", 1)
error_types = {e[0] for e in errors}
self.assertEqual(
error_types, {"blank-at-eol", "space-before-tab", "tab-in-indent"}
)
def test_check_content_crlf(self) -> None:
"""Test content checking with CRLF line endings."""
checker = WhitespaceChecker({"blank-at-eol", "cr-at-eol"})
# CRLF line endings
content = b"line1\r\nline2 \r\nline3\r\n"
errors = checker.check_content(content)
# Should detect trailing space on line 2 but not CR (since CRLF is handled)
self.assertEqual(errors, [("blank-at-eol", 2)])
class WhitespaceFixTests(TestCase):
"""Test whitespace error fixing."""
def test_fix_blank_at_eol(self) -> None:
"""Test fixing trailing whitespace."""
content = b"line1 \nline2\t\nline3"
errors = [("blank-at-eol", 1), ("blank-at-eol", 2)]
fixed = fix_whitespace_errors(content, errors)
self.assertEqual(fixed, b"line1\nline2\nline3")
def test_fix_blank_at_eof(self) -> None:
"""Test fixing blank lines at end of file."""
content = b"line1\nline2\n\n\n"
errors = [("blank-at-eof", 4)]
fixed = fix_whitespace_errors(content, errors)
self.assertEqual(fixed, b"line1\nline2\n")
def test_fix_cr_at_eol(self) -> None:
"""Test fixing carriage returns."""
content = b"line1\r\nline2\r\nline3\r"
errors = [("cr-at-eol", 1), ("cr-at-eol", 2), ("cr-at-eol", 3)]
fixed = fix_whitespace_errors(content, errors)
# Our fix function removes all CRs when cr-at-eol errors are fixed
self.assertEqual(fixed, b"line1\nline2\nline3")
def test_fix_specific_types(self) -> None:
"""Test fixing only specific error types."""
content = b"line1 \nline2\n\n\n"
errors = [("blank-at-eol", 1), ("blank-at-eof", 4)]
# Fix only blank-at-eol
fixed = fix_whitespace_errors(content, errors, fix_types={"blank-at-eol"})
self.assertEqual(fixed, b"line1\nline2\n\n\n")
# Fix only blank-at-eof
fixed = fix_whitespace_errors(content, errors, fix_types={"blank-at-eof"})
self.assertEqual(fixed, b"line1 \nline2\n")
def test_fix_no_errors(self) -> None:
"""Test fixing with no errors returns original content."""
content = b"line1\nline2\nline3"
fixed = fix_whitespace_errors(content, [])
self.assertEqual(fixed, content)
|