import os
import pkgutil
import posixpath
from contextlib import contextmanager
from shutil import rmtree
from tempfile import mkdtemp


from pybtex import errors
from pybtex import bibtex
from pybtex.tests import diff


@contextmanager
def cd_tempdir():
    current_workdir = os.getcwd()
    tempdir = mkdtemp(prefix='pybtex_test_')
    os.chdir(tempdir)
    try:
        yield tempdir
    finally:
        os.chdir(current_workdir)
        rmtree(tempdir)


def copy_resource(package, resource):
    filename = posixpath.basename(resource)
    data = pkgutil.get_data(package, resource)
    with open(filename, 'wb') as data_file:
        data_file.write(data)


def copy_files(bib_name, bst_name):
    copy_resource('pybtex.tests.data', bib_name + '.bib')
    copy_resource('pybtex.tests.data', bst_name + '.bst')


def write_aux(aux_name, bib_name, bst_name):
    with open(aux_name, 'wb') as aux_file:
        aux_file.write('\\citation{*}\n')
        aux_file.write('\\bibstyle{{{0}}}\n'.format(bst_name))
        aux_file.write('\\bibdata{{{0}}}\n'.format(bib_name))


def check_make_bibliography(bib_name, bst_name):
    with cd_tempdir() as tempdir:
        copy_files(bib_name, bst_name)
        write_aux('test.aux', bib_name, bst_name)
        with errors.capture() as stderr:  # FIXME check error messages
            bibtex.make_bibliography('test.aux')
        with open('test.bbl', 'rb') as result_file:
            result = result_file.read()
        correct_result_name = '{0}_{1}.bbl'.format(bib_name, bst_name)
        correct_result = pkgutil.get_data('pybtex.tests.data', correct_result_name)
        assert result == correct_result, diff(correct_result, result)


def test_bibtex_engine():
    for bib_name, bst_name in [
        ('xampl', 'unsrt'),
        ('xampl', 'plain'),
        ('cyrillic', 'unsrt'),
    ]:
        yield check_make_bibliography, bib_name, bst_name

@@ -130,16 +130,38 @@
 dtemp%0A%0A%0A
+from pybtex import io%0A
 from pyb
@@ -178,16 +178,16 @@
  errors%0A
-
 from pyb
@@ -599,24 +599,58 @@
 e, resource)
+.decode(io.get_default_encoding())
 %0A    with op
@@ -647,20 +647,31 @@
    with 
+io.
 open
+_unicode
 (filenam
@@ -671,25 +671,24 @@
 filename, 'w
-b
 ') as data_f
@@ -932,20 +932,31 @@
    with 
+io.
 open
+_unicode
 (aux_nam
@@ -960,17 +960,16 @@
 name, 'w
-b
 ') as au
@@ -999,16 +999,17 @@
 e.write(
+u
 '%5C%5Ccitat
@@ -1038,24 +1038,25 @@
 _file.write(
+u
 '%5C%5Cbibstyle%7B
@@ -1106,16 +1106,17 @@
 e.write(
+u
 '%5C%5Cbibda
@@ -1453,20 +1453,31 @@
    with 
+io.
 open
+_unicode
 ('test.b
@@ -1483,17 +1483,16 @@
 bbl', 'r
-b
 ') as re
@@ -1605,32 +1605,32 @@
 name, bst_name)%0A
-
         correct_
@@ -1692,24 +1692,58 @@
 result_name)
+.decode(io.get_default_encoding())
 %0A        ass

Fix tests in Python 3.