Skip to content

Commit

Permalink
Merge pull request #917 from efiop/master
Browse files Browse the repository at this point in the history
improve test coverage
  • Loading branch information
efiop authored Jul 19, 2018
2 parents 94551b3 + e598b0b commit bb09033
Show file tree
Hide file tree
Showing 30 changed files with 142 additions and 39 deletions.
2 changes: 1 addition & 1 deletion dvc/command/add.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dvc.exceptions import DvcException
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdAdd(CmdBase):
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion dvc/command/checkout.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdCheckout(CmdBase):
Expand Down
Empty file removed dvc/command/common/__init__.py
Empty file.
6 changes: 0 additions & 6 deletions dvc/command/common/common_error.py

This file was deleted.

2 changes: 1 addition & 1 deletion dvc/command/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import configobj

from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase
from dvc.logger import Logger
from dvc.config import Config

Expand Down
2 changes: 1 addition & 1 deletion dvc/command/data_sync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdDataBase(CmdBase):
Expand Down
8 changes: 4 additions & 4 deletions dvc/command/destroy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase
from dvc.prompt import prompt
from dvc.exceptions import DvcException


class CmdDestroy(CmdBase):
Expand All @@ -10,10 +11,9 @@ def run_cmd(self):
u'Are you sure you want to continue?'

if not self.args.force and not prompt(msg, False):
err = u'Cannot destroy without a confirmation from the user. ' \
msg = u'Cannot destroy without a confirmation from the user. ' \
u'Use \'-f\' to force.'
self.project.logger.error(err)
return 1
raise DvcException(err)

self.project.destroy()
except Exception as exc:
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/gc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdGC(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/imp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dvc.exceptions import DvcException
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdImport(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/install.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dvc.logger import Logger
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdInstall(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/lock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dvc.exceptions import DvcException
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdLockBase(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase
from dvc.exceptions import DvcException


Expand Down
2 changes: 1 addition & 1 deletion dvc/command/move.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dvc.exceptions import DvcException
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdMove(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from dvc.exceptions import DvcException
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdPipelineShow(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/remove.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dvc.exceptions import DvcException
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdRemove(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/repro.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdRepro(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/root.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


class CmdRoot(CmdBase):
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase
from dvc.logger import Logger
from dvc.exceptions import DvcException

Expand Down
2 changes: 1 addition & 1 deletion dvc/dependency/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def save(self):
raise self.DoesNotExistError(self.rel_path)

if not os.path.isfile(self.path) and not os.path.isdir(self.path):
raise self.NotFileOrDirError(self.rel_path)
raise self.IsNotFileOrDirError(self.rel_path)

if (os.path.isfile(self.path) and os.path.getsize(self.path) == 0) or \
(os.path.isdir(self.path) and len(os.listdir(self.path)) == 0):
Expand Down
2 changes: 1 addition & 1 deletion dvc/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dvc.logger import Logger
from dvc.cli import parse_args
from dvc.command.common.base import CmdBase
from dvc.command.base import CmdBase


def main(argv=None):
Expand Down
2 changes: 1 addition & 1 deletion dvc/output/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def save(self):
raise self.DoesNotExistError(self.rel_path)

if not os.path.isfile(self.path) and not os.path.isdir(self.path):
raise self.NotFileOrDirError(self.rel_path)
raise self.IsNotFileOrDirError(self.rel_path)

if (os.path.isfile(self.path) and os.path.getsize(self.path) == 0) or \
(os.path.isdir(self.path) and len(os.listdir(self.path)) == 0):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from dvc.command.gc import CmdGC
from dvc.command.config import CmdConfig
from dvc.command.checkout import CmdCheckout
from dvc.exceptions import DvcException
from dvc.command.base import CmdBase

from tests.basic_env import TestDvc

Expand Down Expand Up @@ -161,3 +163,16 @@ def test(self):
t = timeit.default_timer() - start

self.assertTrue(t < 0.3)


class TestFindRoot(TestDvc):
def test(self):
os.chdir("..")

class A(object):
quiet = False
verbose = True

args = A()
with self.assertRaises(DvcException):
CmdBase(args)
58 changes: 47 additions & 11 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import configobj

from dvc.main import main
from dvc.command.config import CmdConfig
from tests.basic_env import TestDvc


class TestConfigCLI(TestDvc):
def _contains(self, section, field, value):
config = configobj.ConfigObj(self.dvc.config.config_file)
def _contains(self, section, field, value, local=False):
if local:
fname = self.dvc.config.config_local_file
else:
fname = self.dvc.config.config_file

config = configobj.ConfigObj(fname)
if section not in config.keys():
return False

Expand All @@ -22,28 +28,38 @@ def test_root(self):
ret = main(['root'])
self.assertEqual(ret, 0)

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

ret = main(['config', section_field, value])
base = ['config']
if local:
base.append('--local')

ret = main(base + [section_field, value])
self.assertEqual(ret, 0)
self.assertTrue(self._contains(section, field, value))
self.assertTrue(self._contains(section, field, value, local))

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

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

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

def test(self):
self._do_test(False)

def test_local(self):
self._do_test(True)

def test_non_existing(self):
#FIXME check stdout/err
Expand All @@ -59,3 +75,23 @@ def test_non_existing(self):

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

ret = main(['config', 'core.remote', 'myremote'])
self.assertEqual(ret, 0)

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

def test_failed_write(self):
class A(object):
local = False
name = 'core.remote'
value = 'myremote'
unset = False

args = A()
cmd = CmdConfig(args)

cmd.configobj.write = None
ret = cmd.save()
self.assertNotEqual(ret, 0)
8 changes: 8 additions & 0 deletions tests/test_data_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,17 @@ def _test_cloud(self, remote=None):
sleep()
self.main(['status', '-c'] + args)

self.main(['fetch'] + args)
self.assertTrue(os.path.exists(cache))
self.assertTrue(os.path.isfile(cache))
self.assertTrue(os.path.isfile(cache_dir))

self.main(['pull'] + args)
self.assertTrue(os.path.exists(cache))
self.assertTrue(os.path.isfile(cache))
self.assertTrue(os.path.isfile(cache_dir))
self.assertTrue(os.path.isfile(self.FOO))
self.assertTrue(os.path.isdir(self.DATA_DIR))

sleep()
with open(cache, 'r') as fd:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import colorama
import logging

from dvc.command.base import CmdBase
from dvc.config import Config
from dvc.logger import Logger

Expand Down Expand Up @@ -32,3 +33,19 @@ def test_colorize(self):
msg = Logger.colorize(name, name)
# This is not a tty, so it should not colorize anything
self.assertEqual(msg, name)


class TestLoggerCLI(TestDvc):
def test(self):
class A(object):
quiet = True
verbose = False

args = A()
CmdBase._set_loglevel(args)
self.assertEqual(Logger.logger().getEffectiveLevel(), logging.CRITICAL)

args.quiet = False
args.verbose = True
CmdBase._set_loglevel(args)
self.assertEqual(Logger.logger().getEffectiveLevel(), logging.DEBUG)
6 changes: 6 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def test(self):
ret = main(['repro', '-f', '-m', stage.path])
self.assertNotEqual(ret, 0)

ret = main(['metrics', 'add', 'metrics'])
self.assertEqual(ret, 0)

ret = main(['repro', '-f', '-m', stage.path])
self.assertEqual(ret, 0)

def test_dir(self):
os.mkdir('metrics_dir')

Expand Down
4 changes: 4 additions & 0 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def test_commands(self):
ret = main(['pipeline', 'show', self.file1_stage, '--commands'])
self.assertEqual(ret, 0)

def test_outs(self):
ret = main(['pipeline', 'show', self.file1_stage, '--outs'])
self.assertEqual(ret, 0)

def test_not_dvc_file(self):
ret = main(['pipeline', 'show', self.file1])
self.assertNotEqual(ret, 0)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dvc.main import main
from dvc.command.remote import CmdRemoteAdd

from tests.basic_env import TestDvc

Expand All @@ -19,3 +20,17 @@ def test(self):
self.assertEqual(main(['remote', 'modify', remotes[0], 'option', 'value']), 0)

self.assertEqual(main(['remote', 'list']), 0)

def test_failed_write(self):
class A(object):
local = False
name = 'myremote'
url = 's3://remote'
unset = False

args = A()
cmd = CmdRemoteAdd(args)

cmd.configobj.write = None
ret = cmd.run()
self.assertNotEqual(ret, 0)
8 changes: 8 additions & 0 deletions tests/test_repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def setUp(self):
cmd='python {} {} {}'.format(self.CODE, self.FOO, self.file1))


class TestReproFail(TestRepro):
def test(self):
os.unlink(self.CODE)

ret = main(['repro', self.file1_stage])
self.assertNotEqual(ret, 0)


class TestReproDepUnderDir(TestDvc):
def test(self):
self.dir_stage = self.dvc.add(self.DATA_DIR)
Expand Down

0 comments on commit bb09033

Please sign in to comment.