From ed923db203d028976a57e2bd82e6ef0697435d3e Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 19 Jun 2017 21:31:31 -0700 Subject: [PATCH] Import cmd unit-tests --- dvc/command/import_file.py | 6 +- dvc/git_wrapper.py | 3 + tests/test_import.py | 117 +++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 tests/test_import.py diff --git a/dvc/command/import_file.py b/dvc/command/import_file.py index 6f1b63427..5c6aa6eac 100644 --- a/dvc/command/import_file.py +++ b/dvc/command/import_file.py @@ -66,10 +66,10 @@ class CmdImportFile(CmdBase): @staticmethod def verify_output(output, input): if CmdImportFile.is_dir_path(output) and not os.path.isdir(output): - raise ImportFileError(u'Import error: output directory {} does not exist'.format(output)) + raise ImportFileError(u'output directory {} does not exist'.format(output)) if len(input) > 1 and not os.path.isdir(output): - msg = u'Import error: output {} has to be directory for multiple file import' + msg = u'output {} has to be directory for multiple file import' raise ImportFileError(msg.format(output)) pass @@ -230,4 +230,4 @@ class CmdImportFile(CmdBase): if __name__ == '__main__': - Runtime.run(CmdDataImport) + Runtime.run(CmdImportFile) diff --git a/dvc/git_wrapper.py b/dvc/git_wrapper.py index 7295ee106..f466db785 100644 --- a/dvc/git_wrapper.py +++ b/dvc/git_wrapper.py @@ -70,6 +70,9 @@ class GitWrapperI(object): return result + def commit_all_changes_and_log_status(self, message): + pass + class GitWrapper(GitWrapperI): def __init__(self): diff --git a/tests/test_import.py b/tests/test_import.py new file mode 100644 index 000000000..fa3e95395 --- /dev/null +++ b/tests/test_import.py @@ -0,0 +1,117 @@ +import os +import tempfile +from copy import copy + +from dvc.command.import_file import CmdImportFile, ImportFileError +from tests.basic_env import DirHierarchyEnvironment + + +class TestCmdRemove(DirHierarchyEnvironment): + def setUp(self): + DirHierarchyEnvironment.init_environment(self) + + def test_import_file_to_dir(self): + with tempfile.NamedTemporaryFile() as temp: + content = 'Some data' + temp.write(content) + temp.flush() + + dir = os.path.join('data', self.dir1) + settings = copy(self.settings) + settings.set_args([temp.name, dir]) + cmd = CmdImportFile(settings) + cmd.run() + + dvc_name = os.path.join(dir, os.path.basename(temp.name)) + data_item = self.path_factory.existing_data_item(dvc_name) + + self.assertTrue(os.path.exists(data_item.data.relative)) + self.assertTrue(os.path.islink(data_item.data.relative)) + self.assertEqual(open(data_item.data.relative).read(), content) + + self.assertTrue(os.path.exists(data_item.cache.relative)) + self.assertEqual(open(data_item.cache.relative).read(), content) + + self.assertTrue(os.path.exists(data_item.state.relative)) + pass + + def test_import_file_to_file(self): + with tempfile.NamedTemporaryFile() as temp: + content = 'Some data' + temp.write(content) + temp.flush() + + target_file_name = 'targ.txt' + target_file = os.path.join('data', self.dir1, target_file_name) + settings = copy(self.settings) + settings.set_args([temp.name, target_file]) + cmd = CmdImportFile(settings) + cmd.run() + + self.assertTrue(os.path.exists(target_file)) + self.assertTrue(open(target_file).read(), content) + pass + + def test_import_to_existing_dir(self): + with tempfile.NamedTemporaryFile() as temp: + content = 'Some data' + temp.write(content) + temp.flush() + + dir = os.path.join('data', self.dir11) + settings = copy(self.settings) + settings.set_args([temp.name, dir]) + cmd = CmdImportFile(settings) + cmd.run() + + dvc_name = os.path.join(dir, os.path.basename(temp.name)) + + self.assertTrue(os.path.exists(dvc_name)) + self.assertTrue(open(dvc_name).read(), content) + pass + + def test_import_to_not_existing_dir(self): + with tempfile.NamedTemporaryFile() as temp: + content = 'Some data' + temp.write(content) + temp.flush() + + dir = os.path.join('data', self.dir1, 'not_existing_dir/') + settings = copy(self.settings) + settings.set_args([temp.name, dir]) + cmd = CmdImportFile(settings) + + with self.assertRaises(ImportFileError): + cmd.run() + + dvc_name = os.path.join(dir, os.path.basename(temp.name)) + + self.assertFalse(os.path.exists(dir)) + self.assertFalse(os.path.exists(dvc_name)) + pass + + def test_multiple_import(self): + with tempfile.NamedTemporaryFile() as temp1, tempfile.NamedTemporaryFile() as temp2: + content = 'Some data' + + temp1.write(content) + temp1.flush() + + temp2.write(content) + temp2.flush() + + dir = os.path.join('data', self.dir11) + settings = copy(self.settings) + settings.set_args([temp1.name, temp2.name, dir]) + cmd = CmdImportFile(settings) + cmd.run() + + dvc_name1 = os.path.join(dir, os.path.basename(temp1.name)) + dvc_name2 = os.path.join(dir, os.path.basename(temp2.name)) + + self.assertTrue(os.path.exists(dvc_name1)) + self.assertTrue(open(dvc_name1).read(), content) + + self.assertTrue(os.path.exists(dvc_name2)) + self.assertTrue(open(dvc_name2).read(), content) + pass \ No newline at end of file