-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
121 add line ending options #129
Changes from all commits
3de7c2e
8b19490
55e3399
560a9b4
c438d52
1cd9a5c
7cc58d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from __future__ import absolute_import, print_function | ||
|
||
import sys | ||
import os | ||
import logging | ||
import optparse | ||
|
||
|
@@ -28,6 +29,14 @@ def format_usage(usage): | |
return usage | ||
|
||
def main(args=None): | ||
old_os_linesep = os.linesep | ||
try: | ||
return actual_main(args) | ||
finally: | ||
if sys.version_info < (3, 0): | ||
os.linesep = old_os_linesep | ||
|
||
def actual_main(args=None): | ||
"""Main program. | ||
|
||
Returns a suggested exit status (0, 1, 2). | ||
|
@@ -63,6 +72,10 @@ def main(args=None): | |
"(only useful for Python 2.6+).") | ||
parser.add_option("--no-six", action="store_true", default=False, | ||
help="Exclude fixes that depend on the six package.") | ||
parser.add_option("-U", "--unix-line-endings", action="store_true", default=False, | ||
help="Write files with Unix (LF) line endings.") | ||
parser.add_option("-W", "--windows-line-endings", action="store_true", default=False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these need short options, especially since we already have lowercase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. |
||
help="Write files with Windows (CRLF) line endings.") | ||
|
||
fixer_pkg = 'libmodernize.fixes' | ||
avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg)) | ||
|
@@ -94,6 +107,14 @@ def main(args=None): | |
if options.print_function: | ||
flags["print_function"] = True | ||
|
||
if options.unix_line_endings and options.windows_line_endings: | ||
print("-U/--unix-line-endings and -W/--windows-line-endings options conflict.") | ||
return 2 | ||
if options.unix_line_endings: | ||
fix_line_endings('\n') | ||
if options.windows_line_endings: | ||
fix_line_endings('\r\n') | ||
|
||
# Set up logging handler | ||
level = logging.DEBUG if options.verbose else logging.INFO | ||
logging.basicConfig(format='%(name)s: %(message)s', level=level) | ||
|
@@ -145,3 +166,12 @@ def main(args=None): | |
|
||
# Return error status (0 if rt.errors is zero) | ||
return int(bool(rt.errors)) | ||
|
||
def fix_line_endings(linesep): | ||
if sys.version_info < (3, 0): | ||
os.linesep = linesep | ||
else: | ||
def _to_system_newlines(s): | ||
return s.replace(os.linesep, linesep) | ||
|
||
refactor._to_system_newlines = _to_system_newlines | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure which I like less, changing an important value in the standard library or monkeypatching a private function in 2to3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative approach is to do the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from __future__ import absolute_import | ||
|
||
import os | ||
from utils import check_on_input, expect_error | ||
from test_fix_basestring import BASESTRING | ||
|
||
TESTCASE = [x.encode('ascii') for x in BASESTRING] | ||
|
||
|
||
def test_mixed_to_native_line_endings(): | ||
check_on_input(b'#\r\n' + TESTCASE[0], | ||
(b'#\n' + TESTCASE[1]).replace(b'\n', os.linesep.encode('ascii')), | ||
mode="b") | ||
|
||
def test_windows_to_unix_line_endings(): | ||
check_on_input(TESTCASE[0].replace(b'\n', b'\r\n'), | ||
TESTCASE[1], | ||
extra_flags=['--unix-line-endings'], | ||
mode="b") | ||
|
||
def test_unix_to_windows_line_endings(): | ||
check_on_input(TESTCASE[0], | ||
TESTCASE[1].replace(b'\n', b'\r\n'), | ||
extra_flags=['--windows-line-endings'], | ||
mode="b") | ||
|
||
def test_options_conflict(): | ||
expect_error(TESTCASE[0], | ||
extra_flags=['--unix-line-endings', '--windows-line-endings'], | ||
mode="b") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are on Python 3, the monkeypatch is never removed, which will make the tests do different things depending on what order they run in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Python 3 we never change
os.linesep
. Also, the monkeypatch torefactor._to_system_newlines
should not cause any nondeterminism in the tests because it is always done prior to calling intolib2to3
, and is idempotent. (Also, it's only affecting lib2to3, unlikeos.linesep
which can affect other things.) Nevertheless, I can unpatchrefactor._to_system_newlines
if you think that would be an improvement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a test runs with
--windows-line-endings
, it will monkeypatchrefactor._to_system_newlines
to convert everything to Windows newlines on write. Then any subsequent test which doesn't specify its newlines will still write files with the Windows style newlines. For most tests, that won't matter, but for instance if yourtest_mixed_to_native_line_endings
ran immediately aftertest_unix_to_windows_line_endings
, I think it would fail.I'm just finishing up a PR to achieve the same thing without monkeypatching, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I'm wrong, there is nondeterminism and this does need to be unpatched. (The patched function captures the
linesep
passed into it, so it is not idempotent.)