from __future__ import print_function
import os

import pytest

from . import helper
from testfixtures import tempdir

from ..core import pysorter


@helper.tempdir
def test_bad_filetypes(d):
    os.chdir(d.path)
    filetypes = {}

    to_sort = 'files/'

    to_make = []
    helper.initialize_dir(d, filetypes, helper.build_path_tree(to_make, to_sort))

    args = [to_sort, '--filetypes', 'filetypes']

    with pytest.raises(OSError):
        pysorter.main(args)


@helper.tempdir
def test_unknown_filetypes_created(d):
    os.chdir(d.path)
    filetypes = {}

    to_sort = 'files/'

    to_make = []
    helper.initialize_dir(d, filetypes, helper.build_path_tree(to_make, to_sort))

    args = [to_sort, '-u', 'unknown_files']

    # --- compare sorted
    expected = helper.build_path_tree(['',
                                       'other/',
                                       'directories/'],
                                      to_sort) + \
               ['unknown_files',
                'filetypes.py']

    pysorter.main(args)
    d.compare(expected=expected)
