mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
test: change some test logic for test_diff.py
This commit is contained in:
+1
-1
@@ -105,7 +105,7 @@ def diff(self, target, a_ref=None, b_ref=None):
|
||||
string: string of output message with diff info
|
||||
"""
|
||||
diff_dct = self.scm.diff(target, a_ref=a_ref, b_ref=b_ref)
|
||||
msg = "dvc diff for '{}' from {} to {} \n\n".format(
|
||||
msg = "dvc diff for '{}' from {} to {}\n\n".format(
|
||||
target, diff_dct["a_tag"], diff_dct["b_tag"]
|
||||
)
|
||||
if diff_dct["equal"]:
|
||||
|
||||
@@ -17,6 +17,12 @@ class FileNotInRepoError(DvcException):
|
||||
"""
|
||||
|
||||
|
||||
class FileNotInCommitError(DvcException):
|
||||
"""Thrown when trying to find a file/directory that is not
|
||||
into the specified commit in the repository
|
||||
"""
|
||||
|
||||
|
||||
class FileNotInTargetSubdirError(DvcException):
|
||||
"""Thrown when trying to place .gitignore for a file that not in
|
||||
the file subdirectory."""
|
||||
|
||||
@@ -12,6 +12,7 @@ from dvc.scm.base import (
|
||||
SCMError,
|
||||
FileNotInRepoError,
|
||||
FileNotInTargetSubdirError,
|
||||
FileNotInCommitError,
|
||||
)
|
||||
from dvc.scm.git.tree import GitTree
|
||||
import dvc.logger as logger
|
||||
@@ -264,10 +265,10 @@ class Git(Base):
|
||||
return None, commits
|
||||
diff_obj = a_commit.diff(b_commit, paths=file_name)
|
||||
except (BadName, BadObject) as e:
|
||||
raise SCMError(e)
|
||||
raise SCMError("git problem", cause=e)
|
||||
if len(diff_obj) == 0:
|
||||
msg = "Have not found file/directory '{}' in the commits"
|
||||
raise FileNotInRepoError(msg.format(file_name))
|
||||
raise FileNotInCommitError(msg.format(file_name))
|
||||
return diff_obj[0], commits
|
||||
|
||||
def _extract_fname(self, is_deleted, blob_obj):
|
||||
|
||||
+17
-18
@@ -4,7 +4,7 @@ import os
|
||||
from dvc.main import main
|
||||
|
||||
|
||||
from dvc.scm.base import FileNotInRepoError
|
||||
from dvc.scm.base import FileNotInCommitError
|
||||
|
||||
|
||||
from tests.basic_env import TestDvc
|
||||
@@ -29,9 +29,13 @@ class TestDiff(TestDvc):
|
||||
|
||||
class TestDiffRepo(TestDiff):
|
||||
def test(self):
|
||||
new_ref = self.git.head.commit
|
||||
msg = self.dvc.diff(self.new_file, a_ref=self.a_ref)
|
||||
self.assertTrue("deleted file" in msg)
|
||||
self.assertTrue(self.new_file in msg)
|
||||
test_msg = "dvc diff for '{}' from ".format(self.new_file)
|
||||
test_msg += "{} to {}\n\n".format(new_ref, self.a_ref)
|
||||
test_msg += "-new_file with md5 25fa8325e4e0eb8180445e42558e60bd\n"
|
||||
test_msg += "deleted file with size -8 Bytes"
|
||||
self.assertEqual(test_msg, msg)
|
||||
|
||||
|
||||
class TestDiffCmdLine(TestDiff):
|
||||
@@ -66,9 +70,15 @@ class TestDiffDir(TestDvc):
|
||||
|
||||
class TestDiffDirRepo(TestDiffDir):
|
||||
def test(self):
|
||||
new_ref = self.git.head.commit
|
||||
msg = self.dvc.diff(self.DATA_DIR, a_ref=self.a_ref)
|
||||
self.assertTrue("1 file deleted" in msg, msg)
|
||||
self.assertTrue("size was decreased by " in msg, msg)
|
||||
test_msg = "dvc diff for '{}' from ".format(self.DATA_DIR)
|
||||
test_msg += "{} to {}\n\n".format(new_ref, self.a_ref)
|
||||
test_msg += "-data_dir with md5 d5782f3072167ad3a53ee80b92b30718\n"
|
||||
test_msg += "+data_dir with md5 bff5a787d16460f32e9f2e62b183b1cc\n\n"
|
||||
test_msg += "2 files didn't change, 0 files modified, 0 files added, "
|
||||
test_msg += "1 file deleted, size was decreased by 30 Bytes"
|
||||
self.assertEqual(msg, test_msg)
|
||||
|
||||
|
||||
class TestDiffFileNotFound(TestDiffDir):
|
||||
@@ -77,22 +87,11 @@ class TestDiffFileNotFound(TestDiffDir):
|
||||
self.unknown_file = "unknown_file_" + str(id(self))
|
||||
|
||||
def test(self):
|
||||
with self.assertRaises(
|
||||
FileNotInRepoError, msg="diff with incorrect arg"
|
||||
) as context:
|
||||
with self.assertRaises(FileNotInCommitError):
|
||||
self.dvc.scm.diff(self.unknown_file, self.a_ref)
|
||||
self.assertTrue(
|
||||
"Have not found file/directory" in str(context.exception),
|
||||
"scm raises with incorrect error message",
|
||||
)
|
||||
|
||||
|
||||
class TestDiffFileNotFoundRepo(TestDiffFileNotFound):
|
||||
def test(self):
|
||||
with self.assertRaises(
|
||||
FileNotInRepoError, msg="diff with incorrect arg"
|
||||
) as context:
|
||||
with self.assertRaises(FileNotInCommitError):
|
||||
self.dvc.diff(self.unknown_file, a_ref=self.a_ref)
|
||||
self.assertTrue(
|
||||
"Have not found file/directory" in str(context.exception)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user