# -*- coding: utf-8 -*-
"""Test anaconda-client configuration set/get."""

# Standard library imports
from os.path import join
import os
import shutil
import tempfile
import unittest

# Third party imports
import mock

# Local imports
from binstar_client.utils import config


class Test(unittest.TestCase):

    def create_config_dirs(self):
        tmpdir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, tmpdir)
        system_dir = join(tmpdir, 'system')
        user_dir = join(tmpdir, 'user')
        os.mkdir(system_dir)
        os.mkdir(user_dir)
        return user_dir, system_dir

    def test_defaults(self):
        user_dir, system_dir = self.create_config_dirs()
        with open(join(user_dir, 'config.yaml'), 'wb') as fd:
            fd.write(b'')

        with mock.patch('binstar_client.utils.config.SEARCH_PATH',
                        [system_dir, user_dir]):
            cfg = config.get_config()
            self.assertEqual(cfg, config.DEFAULT_CONFIG)

    def test_global_url(self):
        """
        Test regression reported on:

        https://github.com/Anaconda-Platform/anaconda-client/issues/464
        """
        user_dir, system_dir = self.create_config_dirs()
        with open(join(user_dir, 'config.yaml'), 'wb') as fd:
            fd.write(b'')

        data = config.DEFAULT_CONFIG.copy()
        data.update({'url': 'https://blob.org'})

        with mock.patch('binstar_client.utils.config.SEARCH_PATH',
                        [system_dir, user_dir]):
            config.save_config(data, join(user_dir, 'config.yaml'))
            cfg = config.get_config()
            self.assertEqual(cfg, data)

    def test_merge(self):
        user_dir, system_dir = self.create_config_dirs()
        with open(join(system_dir, 'config.yaml'), 'wb') as fd:
            fd.write(b'''
ssl_verify: false
sites:
    develop:
        url: http://develop.anaconda.org
            ''')

        with open(join(user_dir, 'config.yaml'), 'wb') as fd:
            fd.write(b'''
ssl_verify: true
sites:
    develop:
        ssl_verify: false
            ''')

        with mock.patch('binstar_client.utils.config.SEARCH_PATH', [system_dir, user_dir]), \
                mock.patch('binstar_client.utils.config.DEFAULT_CONFIG', {}):
            cfg = config.get_config()

            self.assertEqual(cfg, {
                'ssl_verify': True,
                'sites': {
                    'develop': {
                        'url': 'http://develop.anaconda.org',
                        'ssl_verify': False,
                    }
                }
            })

    def test_support_tags(self):
        user_dir, system_dir = self.create_config_dirs()
        with open(join(user_dir, 'config.yaml'), 'wb') as fd:
            fd.write(b'''
!!python/unicode 'sites':
   !!python/unicode 'alpha': {!!python/unicode 'url': !!python/unicode 'foobar'}
   !!python/unicode 'binstar': {!!python/unicode 'url': !!python/unicode 'barfoo'}
ssl_verify: False
''')

        with mock.patch('binstar_client.utils.config.SEARCH_PATH', [user_dir]), \
                mock.patch('binstar_client.utils.config.DEFAULT_CONFIG', {}):
            cfg = config.get_config()

            self.assertEqual(cfg, {
                'ssl_verify': False,
                'sites': {
                    'alpha': {
                        'url': 'foobar',
                    },
                    'binstar': {
                        'url': 'barfoo',
                    },
                }
            })

@@ -1298,24 +1298,78 @@
 ')%0A%0A        
+url_data = %7B'url': 'https://blob.org'%7D%0A        config_
 data = confi
@@ -1400,16 +1400,23 @@
         
+config_
 data.upd
@@ -1423,35 +1423,16 @@
 ate(
-%7B'
 url
-': 'https://blob.org'%7D
+_data
 )%0A%0A 
@@ -1577,16 +1577,20 @@
 _config(
+url_
 data, jo
@@ -1690,16 +1690,23 @@
 al(cfg, 
+config_
 data)%0A%0A 

Improve test