isidentical/teyit

Empty files doesn't work

hakancelikdev opened this issue · 1 comments

Tox.ini

[tox]
envlist = py36, py37, py38, pre-commit

[testenv]
deps = -rrequirements-dev.txt
commands = python -m unittest

[testenv:pre-commit]
skip_install = true
deps = pre-commit
commands = pre-commit run --all-files

Error output

Traceback (most recent call last):
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/bin/teyit", line 8, in <module>
    sys.exit(main())
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 374, in main
    raise SystemExit(_refactor_files(**vars(options)))
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 331, in _refactor_files
    if len(refactors := _refactor_file(path)) > 0:
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 319, in _refactor_file
    refactored_source, refactors = refactor_until_deterministic(
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 279, in refactor_until_deterministic
    source, _refactors = rewrite_source(source, blacklist=blacklist)
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 254, in rewrite_source
    trailing_newline = source[-1] == "\n"
IndexError: string index out of range
All done! 4 left unchanged
Traceback (most recent call last):
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/bin/teyit", line 8, in <module>
    sys.exit(main())
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 374, in main
    raise SystemExit(_refactor_files(**vars(options)))
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 331, in _refactor_files
    if len(refactors := _refactor_file(path)) > 0:
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 319, in _refactor_file
    refactored_source, refactors = refactor_until_deterministic(
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 279, in refactor_until_deterministic
    source, _refactors = rewrite_source(source, blacklist=blacklist)
  File "/home/hakan/.cache/pre-commit/repouz20n0b2/py_env-python3.9/lib/python3.9/site-packages/teyit.py", line 254, in rewrite_source
    trailing_newline = source[-1] == "\n"
IndexError: string index out of range

If source contains nothing (which is possible), this will lead to a crash. It should be guarded with an if source clause.