import configobj

from dvc.main import main
from tests.basic_env import TestDvc


class TestConfigCLI(TestDvc):
    def _contains(self, section, field, value):
        config = configobj.ConfigObj(self.dvc.config.config_file)
        if section not in config.keys():
            return False

        if field not in config[section].keys():
            return False

        if config[section][field] != value:
            return False

        return True

    def test_root(self):
        ret = main(['root'])
        self.assertEqual(ret, 0)

    def test(self):
        section = 'setsection'
        field = 'setfield'
        section_field = '{}.{}'.format(section, field)
        value = 'setvalue'
        newvalue = 'setnewvalue'

        ret = main(['config', section_field, value])
        self.assertEqual(ret, 0)
        self.assertTrue(self._contains(section, field, value))

        ret = main(['config', section_field])
        self.assertEqual(ret, 0)

        ret = main(['config', section_field, newvalue])
        self.assertEqual(ret, 0)
        self.assertTrue(self._contains(section, field, newvalue))
        self.assertFalse(self._contains(section, field, value))

        ret = main(['config', section_field, '--unset'])
        self.assertEqual(ret, 0)
        self.assertFalse(self._contains(section, field, value))

    def test_non_existing(self):
        #FIXME check stdout/err

        ret = main(['config', 'non_existing_section.field'])
        self.assertEqual(ret, 1)

        ret = main(['config', 'global.non_existing_field'])
        self.assertEqual(ret, 1)

        ret = main(['config', 'non_existing_section.field', '-u'])
        self.assertEqual(ret, 1)

        ret = main(['config', 'global.non_existing_field', '-u'])
        self.assertEqual(ret, 1)

@@ -154,74 +154,200 @@
 alue
-):%0A        config = configobj.ConfigObj(self.dvc.config.config_fil
+, local=False):%0A        if local:%0A            fname = self.dvc.config.config_local_file%0A        else:%0A            fname = self.dvc.config.config_file%0A%0A        config = configobj.ConfigObj(fnam
 e)%0A 
@@ -669,24 +669,28 @@
 0)%0A%0A    def 
+_do_
 test(self):%0A
@@ -682,24 +682,37 @@
 do_test(self
+, local=False
 ):%0A        s
@@ -884,38 +884,115 @@
         
-ret = main(%5B'config', 
+base = %5B'config'%5D%0A        if local:%0A            base.append('--local')%0A%0A        ret = main(base + %5B
 section_
@@ -1091,32 +1091,39 @@
 on, field, value
+, local
 ))%0A%0A        ret 
@@ -1125,35 +1125,32 @@
  ret = main(
-%5B'config', 
+base + %5B
 section_fiel
@@ -1202,35 +1202,32 @@
  ret = main(
-%5B'config', 
+base + %5B
 section_fiel
@@ -1336,16 +1336,23 @@
 newvalue
+, local
 ))%0A     
@@ -1399,32 +1399,39 @@
 on, field, value
+, local
 ))%0A%0A        ret 
@@ -1437,27 +1437,24 @@
  = main(
-%5B'config', 
+base + %5B
 section_
@@ -1566,17 +1566,129 @@
 d, value
-)
+, local))%0A%0A    def test(self):%0A        self._do_test(False)%0A%0A    def test_local(self):%0A        self._do_test(True
 )%0A%0A    d

add test for local config