diff --git a/neatlynx/dvc_path.py b/neatlynx/dvc_path.py new file mode 100644 index 000000000..ad1e188a5 --- /dev/null +++ b/neatlynx/dvc_path.py @@ -0,0 +1,25 @@ +import os + + +class DvcPath(object): + def __init__(self, relative_raw, git_dir_abs, curr_dir_abs): + self._abs = os.path.realpath(relative_raw) + self._dvc = os.path.relpath(self.abs, git_dir_abs) + self._relative = os.path.relpath(self._abs, curr_dir_abs) + self._filename = os.path.basename(self._abs) + + @property + def dvc(self): + return self._dvc + + @property + def abs(self): + return self._abs + + @property + def relative(self): + return self._relative + + @property + def filename(self): + return self._filename diff --git a/neatlynx/git_wrapper.py b/neatlynx/git_wrapper.py index ad81509d1..d7d5fce9a 100644 --- a/neatlynx/git_wrapper.py +++ b/neatlynx/git_wrapper.py @@ -2,6 +2,7 @@ import os import sys import subprocess +from neatlynx.dvc_path import DvcPath from neatlynx.exceptions import NeatLynxException from neatlynx.logger import Logger from neatlynx.config import Config @@ -25,10 +26,17 @@ class GitWrapperI(object): def git_dir_abs(self): return os.path.realpath(self.git_dir) + @property + def curr_dir_abs(self): + return os.path.abspath(os.curdir) + @property def curr_commit(self): return self._commit + def build_dvc_path(self, relative): + return DvcPath(relative, self.git_dir_abs, self.curr_dir_abs) + class GitWrapper(GitWrapperI): def __init__(self): @@ -90,7 +98,7 @@ class GitWrapper(GitWrapperI): @property def curr_dir_nlx(self): - return os.path.relpath(os.path.abspath(os.curdir), self.git_dir_abs) + return os.path.relpath(self.curr_dir_abs, self.git_dir_abs) @property def git_dir(self): diff --git a/tests/test_data_file_obj.py b/tests/test_data_file_obj.py index 10702e7e0..71ecd481b 100644 --- a/tests/test_data_file_obj.py +++ b/tests/test_data_file_obj.py @@ -64,6 +64,7 @@ class TestNlxNames(BasicDataDirTest): pass def test_data_nlx(self): + print('==============================') self.assertEqual(self._dobj.data_file_nlx, 'file.txt') pass diff --git a/tests/test_dvc_path.py b/tests/test_dvc_path.py new file mode 100644 index 000000000..203f6529b --- /dev/null +++ b/tests/test_dvc_path.py @@ -0,0 +1,65 @@ +import os +import shutil +from unittest import TestCase + +from neatlynx.git_wrapper import GitWrapperI + + +class TestDvcPathTest(TestCase): + def setUp(self): + self.test_dir = os.path.realpath('/tmp/ntx_unit_test/dvc_path') + shutil.rmtree(self.test_dir, ignore_errors=True) + os.makedirs(os.path.join(self.test_dir, 'data')) + os.makedirs(os.path.join(self.test_dir, 'code', 'lib')) + os.makedirs(os.path.join(self.test_dir, 'd1', 'd2', 'dir3', 'd4', 'dir5')) + + self.git = GitWrapperI(self.test_dir) + + def _validate_dvc_path(self, path, dvc_file_name, relative_file_name): + self.assertEqual(path.dvc, dvc_file_name) + self.assertEqual(path.filename, os.path.basename(dvc_file_name)) + self.assertEqual(path.relative, relative_file_name) + self.assertEquals(path.abs[0], os.path.sep) + self.assertTrue(path.abs.endswith(dvc_file_name)) + + def basic_test(self): + print('----------------------------------------------') + os.chdir(self.test_dir) + + file = os.path.join('data', 'file.txt') + path = self.git.build_dvc_path(file) + self._validate_dvc_path(path, file, file) + pass + + def from_dir_test(self): + os.chdir(os.path.join(self.test_dir, 'code')) + + file_dvc_path = os.path.join('data', 'file1.txt') + file_relative_path = os.path.join('..', file_dvc_path) + + path = self.git.build_dvc_path(file_relative_path) + self._validate_dvc_path(path, file_dvc_path, file_relative_path) + print('+++++++++++++++++++++++++++++') + pass + + def from_deep_dirs_test(self): + deep_dir = os.path.join('d1', 'd2', 'dir3') + os.chdir(os.path.join(self.test_dir, deep_dir)) + + file_dvc = os.path.join('code', 'lib', 'context_switcher_structs.asm') + file_relative = os.path.join('..', '..', '..', file_dvc) + + path = self.git.build_dvc_path(file_relative) + self._validate_dvc_path(path, file_dvc, file_relative) + pass + + def go_deeper_test(self): + deep_dir = os.path.join('d1', 'd2', 'dir3') + os.chdir(os.path.join(self.test_dir, deep_dir)) + + file_relative_path = os.path.join(deep_dir, 'd4', 'dir5', 'rawdata.tsv') + file_dvc_path = os.path.join(deep_dir, file_relative_path) + + path = self.git.build_dvc_path(file_relative_path) + self._validate_dvc_path(path, file_dvc_path, file_relative_path) + pass diff --git a/unittests.sh b/unittests.sh index 691ef8b4c..d48d0fcc6 100755 --- a/unittests.sh +++ b/unittests.sh @@ -1,4 +1,2 @@ - -coverage run --include 'neatlynx*' -m unittest discover -s tests -coverage report +nosetests --cover-inclusive --cover-erase --cover-package=neatlynx --with-coverage CODECLIMATE_REPO_TOKEN=a668a3d02db00ab993ad82e1b3d5d2eb00c3c2e26e659153ae4cfd2f12aaad23 codeclimate-test-reporter