from __future__ import print_function, division, absolute_import

import shutil
import sys
from os.path import dirname, join, isdir, exists

from conda_build import config, source

def create_files(dir_path, m):
    """
    Create the test files for pkg in the directory given.  The resulting
    test files are configuration (i.e. platform, architecture, Python and
    numpy version, CE/Pro) independent.
    Return False, if the package has no tests (for any configuration), and
    True if it has.
    """
    for fn in m.get_value('test/files'):
        path = join(m.path, fn)
        if isdir(path):
            shutil.copytree(path, join(dir_path, fn))
        else:
            shutil.copy(path, dir_path)

def create_shell_files(dir_path, m):
    has_tests = False
    if sys.platform == 'win32':
        name = 'run_test.bat'
    else:
        name = 'run_test.sh'
    if exists(join(m.path, name)):
        shutil.copy(join(m.path, name), dir_path)
        has_tests = True

    with open(join(dir_path, name), 'a') as f:
        f.write('\n\n')
        for cmd in m.get_value('test/commands'):
            f.write(cmd)
            f.write('\n')
            has_tests = True

    return has_tests

def create_py_files(dir_path, m):
    has_tests = False
    with open(join(dir_path, 'run_test.py'), 'w') as fo:
        fo.write("# tests for %s (this is a generated file)\n" % m.dist())
        fo.write("print('===== testing package: %s =====')\n" % m.dist())
        with open(join(dirname(__file__), 'header_test.py')) as fi:
            fo.write(fi.read() + '\n')

        for name in m.get_value('test/imports'):
            fo.write('print("import: %r")\n' % name)
            fo.write('import %s\n' % name)
            fo.write('\n')
            has_tests = True

        try:
            with open(join(m.path, 'run_test.py')) as fi:
                fo.write("# --- run_test.py (begin) ---\n")
                fo.write(fi.read())
                fo.write("# --- run_test.py (end) ---\n")
            has_tests = True
        except IOError:
            fo.write("# no run_test.py exists for this package\n")
        fo.write("\nprint('===== %s OK =====')\n" % m.dist())

    return has_tests

@@ -1,68 +1,161 @@
-from __future__ import print_function, division, absolute_import
+'''%0AModule to handle generating test files.%0A'''%0A%0Afrom __future__ import (absolute_import, division, print_function,%0A                        unicode_literals)
 %0A%0Aim
@@ -177,16 +177,36 @@
 ort sys%0A
+from io import open%0A
 from os.
@@ -1132,16 +1132,34 @@
 me), 'a'
+, encoding='utf-8'
 ) as f:%0A
@@ -1438,16 +1438,34 @@
 y'), 'w'
+, encoding='utf-8'
 ) as fo:
@@ -1937,16 +1937,16 @@
    try:%0A
-
         
@@ -1978,32 +1978,50 @@
 , 'run_test.py')
+, encoding='utf-8'
 ) as fi:%0A       

Switch create_test.py to using io.open instead of open, and made encoding handling explicit