summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSiddharth Ravikumar <sravik@bgsu.edu>2015-02-26 02:38:52 -0500
committerSiddharth Ravikumar <sravik@bgsu.edu>2015-02-26 02:38:52 -0500
commit6eb0555742dc1b7290c55a17b02ff108ae03591d (patch)
tree03999491d0b10723f2962833c935c9d45a815ef0
parentec1b156cd7926e5845c8b6b16762e5fc57ea3a8b (diff)
modded combox.file.hash_file function -- added new arg `file_content'
- `file_content' is set to None by default - If `file_content' is not None, hash of this is returned. Updated the test for hash_file function. modified: combox/file.py modified: tests/file_test.py
-rw-r--r--combox/file.py7
-rw-r--r--tests/file_test.py6
2 files changed, 9 insertions, 4 deletions
diff --git a/combox/file.py b/combox/file.py
index 959954a..d7602e9 100644
--- a/combox/file.py
+++ b/combox/file.py
@@ -301,14 +301,17 @@ def read_file(filename):
return file_.read()
-def hash_file(filename):
+def hash_file(filename, file_content=None):
"""Does a SHA512 hash on the contents of file.
Returns the hexdigest of the file content's hash.
filename: Absolute pathname of the file.
+ file_content: If not None, hash of file_content is returned.
"""
- file_content = read_file(filename)
+
+ if not file_content:
+ file_content = read_file(filename)
return sha512(file_content).hexdigest()
diff --git a/tests/file_test.py b/tests/file_test.py
index 807ed23..7c041b7 100644
--- a/tests/file_test.py
+++ b/tests/file_test.py
@@ -88,10 +88,12 @@ class TestFile(object):
"""
Tests the hashing function - hash_file
"""
- fhash = hash_file(self.TEST_FILE)
fcontent = read_file(self.TEST_FILE)
+ fhash_0 = hash_file(self.TEST_FILE)
+ fhash_1 = hash_file(self.TEST_FILE, fcontent)
- assert fhash == sha512(fcontent).hexdigest()
+ assert fhash_0 == sha512(fcontent).hexdigest()
+ assert fhash_1 == sha512(fcontent).hexdigest()
def test_relativepath(self):