I have some code below that I'm using to take an input of files, open and process, and then output some data. I've gotten the functionality working and I'm unit testing it now, below is an example of the code.
def foo(dir):
path_to_search = join(dir, "/baz/foo")
if isdir(path_to_search):
#path exists so do stuff...
for fname in listdir(path_to_search):
do_stuff()
else:
print "path doesn't exist"
I've been able to create a test where the past doesn't exist easily enough, but as you can see above I assert that the "/baz/foo" portion of the directory structure exists (in production the directory structure must have this file, in some cases it won't and we won't need to process it.)
I've tried to create a temporary directory structure using TempDir and join, but the code always kicks out saying the path doesn't exists.
Is it possible to mock the output of os.listdir such that I won't need to create a temporary directory structure that follows the needed /baz/foo convention?