diff options
-rw-r--r-- | combox/file.py | 17 | ||||
-rw-r--r-- | tests/file_test.py | 24 |
2 files changed, 34 insertions, 7 deletions
diff --git a/combox/file.py b/combox/file.py index 0eb08b3..e6d4000 100644 --- a/combox/file.py +++ b/combox/file.py @@ -26,8 +26,14 @@ from glob import glob from combox.config import get_nodedirs -def relative_path(p, config): - """Returns the relative path to the `p' w. r. t combox directory. +def relative_path(p, config, comboxd=True): + """Returns the relative path to the `p' w. r. t combox or node directory. + + If `comboxd' is True, the relative path is w. r. t combox + directory. + + If `comboxd' is False, the relative path is w. r. t node + directory. p: path to a directory or file. @@ -35,9 +41,12 @@ def relative_path(p, config): combox. """ - combox_dir = '%s/' % config['combox_dir'] + if comboxd: + directory = '%s/' % config['combox_dir'] + else: + directory = '%s/' % get_nodedirs(config)[0] - return p.partition(combox_dir)[2] + return p.partition(directory)[2] def mk_nodedir(directory, config): diff --git a/tests/file_test.py b/tests/file_test.py index 2c6dce0..b3edc7a 100644 --- a/tests/file_test.py +++ b/tests/file_test.py @@ -24,9 +24,8 @@ from nose.tools import * from os import path, remove from combox.config import get_nodedirs -from combox.file import (split_data, glue_data, write_file, - read_file, write_shards, read_shards, - hash_file, rm_shards) +from combox.crypto import split_and_encrypt +from combox.file import * from tests.utils import get_config, rm_nodedirs, rm_configdir @@ -95,6 +94,25 @@ class TestFile(object): assert fhash == sha512(fcontent).hexdigest() + def test_relativepath(self): + """ + Tests the relative_path function + """ + + test_file_basename = path.basename(self.TEST_FILE) + print test_file_basename + assert test_file_basename == relative_path(self.TEST_FILE, + self.config) + + split_and_encrypt(self.TEST_FILE, self.config) + test_file_shard_0 = '%s.shard.0' % test_file_basename + test_file_shard_0_abspath = "%s/%s" % (get_nodedirs(self.config)[0], + test_file_shard_0) + + assert test_file_shard_0 == relative_path(test_file_shard_0_abspath, + self.config, False) + + @classmethod def teardown_class(self): """Purge the mess created by this test.""" |