import codecs
import os

import pytest

from . import v
assert v  # Silence pyflakes.


def test_syntax_error(v):
    v.scan("foo bar")
    assert int(v.report()) == 1


def test_null_byte(v):
    v.scan("\x00")
    assert int(v.report()) == 1


def test_confidence_range(v):
    v.scan("""\
def foo():
    pass
""")
    with pytest.raises(ValueError):
        v.get_unused_code(min_confidence=150)


def test_non_utf8_encoding(v):
    code = """\
def foo():
    pass

foo()
"""
    with open('non-utf8.py', 'wb') as f:
        f.write(codecs.BOM_UTF16_LE)
        f.write(code.encode('utf_16_le'))
    v.scavenge([f.name])
    os.remove(f.name)
    assert v.found_dead_code_or_error

@@ -10,18 +10,8 @@
 decs
-%0Aimport os
 %0A%0Aim
@@ -412,19 +412,27 @@
 coding(v
+, tmpdir
 ):%0A
-
     code
@@ -470,16 +470,87 @@
 o()%0A%22%22%22%0A
+    non_utf_8_file = str(tmpdir.mkdir(%22non_utf8%22).join(%22non_utf8.py%22))%0A
     with
@@ -559,21 +559,22 @@
 pen(
-'
 non
--utf8.py'
+_utf_8_file
 , 'w
@@ -662,16 +662,16 @@
 6_le'))%0A
+
     v.sc
@@ -691,30 +691,8 @@
 e%5D)%0A
-    os.remove(f.name)%0A
     

Use pytest's tmpdir fixture (#131)