The following function calculates the size (bytes) of a given directory and its sub-directories
def get_dir_size(dir_path):
"""Determine the size of a dir.
This function also takes into account the allocated size of
directories (4096 bytes).
Note: The function may crash on symlinks with something like:
OSError: [Errno 40] Too many levels of symbolic links
:param dir_path (str): path to the directory
:return: size in bytes.
"""
tot_size = 0
for (root_path, dirnames, filenames) in os.walk(dir_path):
for f in filenames:
fpath = os.path.join(root_path, f)
tot_size += os.path.getsize(fpath)
tot_size += os.path.getsize(root_path)
return tot_size
I want to write a unittest that ensures the function is behaving as expected. To ensure the correct outcome I assume a have to mock the directory I'll pass to the function. testfixtures
looked promising bur turned out to create dirs and files in the /tmp
directory.
In [5]: from testfixtures import TempDirectory
In [8]: d = TempDirectory()
In [14]: d.makedir('output')
Out[14]: '/tmp/tmpLWnsCr/output'
What would be the right approach here?
Aucun commentaire:
Enregistrer un commentaire