pytorch/tools/test/test_trailing_newlines.py
Rong Rong (AI Infra) 7e619b9588 First step to rearrange files in tools folder (#60473)
Summary:
Changes including:
- introduced `linter/`, `testing/`, `stats/` folders in `tools/`
- move appropriate scripts into these folders
- change grepped references in the pytorch/pytorch repo

Next step
- introduce `build/` folder for build scripts

Pull Request resolved: https://github.com/pytorch/pytorch/pull/60473

Test Plan:
- CI (this is important b/c pytorch/test-infra also rely on some script reference.
- tools/tests/

Reviewed By: albanD

Differential Revision: D29352716

Pulled By: walterddr

fbshipit-source-id: bad40b5ce130b35dfd9e59b8af34f9025f3285fd
2021-06-24 10:13:58 -07:00

50 lines
1.6 KiB
Python

from tools.linter import trailing_newlines
import unittest
import tempfile
def correct_trailing_newlines(file_contents: str) -> bool:
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp:
filename = tmp.name
tmp.write(file_contents)
return trailing_newlines.correct_trailing_newlines(filename)
class TestTrailingNewlines(unittest.TestCase):
def test_empty(self) -> None:
self.assertTrue(correct_trailing_newlines(''))
def test_single_byte(self) -> None:
self.assertFalse(correct_trailing_newlines('a'))
def test_single_newline(self) -> None:
self.assertFalse(correct_trailing_newlines('\n'))
def test_two_newlines(self) -> None:
self.assertFalse(correct_trailing_newlines('\n\n'))
def test_three_newlines(self) -> None:
self.assertFalse(correct_trailing_newlines('\n\n\n'))
def test_hello_world(self) -> None:
self.assertFalse(correct_trailing_newlines('hello world'))
def test_hello_world_newline(self) -> None:
self.assertTrue(correct_trailing_newlines('hello world\n'))
def test_hello_world_two_newlines(self) -> None:
self.assertFalse(correct_trailing_newlines('hello world\n\n'))
def test_hello_world_three_newlines(self) -> None:
self.assertFalse(correct_trailing_newlines('hello world\n\n\n'))
def test_hello_world_multiline(self) -> None:
self.assertFalse(correct_trailing_newlines('hello\nworld'))
def test_hello_world_multiline_gap(self) -> None:
self.assertTrue(correct_trailing_newlines('hello\n\nworld\n'))
if __name__ == '__main__':
unittest.main()