# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""
Tests for conda API functions
"""

# Standard library imports
import os

# Third party imports
from qtpy.QtCore import Qt  # analysis:ignore
import pytest
import pytestqt.qtbot as qtbot  # analysis:ignore

# Local imports
from anaconda_navigator.api.anaconda_api import AnacondaAPI
from anaconda_navigator.utils.fixtures import tmpfolder  # analysis:ignore


@pytest.fixture
def manager(qtbot):
    api = AnacondaAPI()
    envs = api.conda_get_envs()
    env_names = [env.rsplit(os.sep, 1)[-1] for env in envs]
    if 'navigator_test' in env_names:
        index = env_names.index('navigator_test')
        import shutil
        shutil.rmtree(envs[index], ignore_errors=True)
    return qtbot, api


class TestManagerAPI:

    def test_create_yaml(self, manager, tmpfolder):
        qtbot, api = manager
        myfile = os.path.join(tmpfolder, 'env.yaml')
        version = os.environ.get('TRAVIS_PYTHON_VERSION', None)
        if version:
            vers = "="+version
        else:
            vers = ""
        with open(myfile, 'w') as f:
            f.write("name: navigator_test\n"
                    "dependencies:\n    - python{}\n".format(vers))
        worker = api.conda_create_yaml('navigator_test', myfile)
        with qtbot.waitSignal(worker.sig_finished, 120000, True):
            worker.start()
        env_names = [env.rsplit(os.sep, 1)[-1] for env in api.conda_get_envs()]
        assert 'navigator_test' in env_names
