I'm trying to test some code that uses os.walk. I want to create a temporary, in-memory filesystem that I can populate with sample (empty) files and directories that os.walk will then return. This should save me the complexity of mocking os.walk calls to simulate recursion.
Specifically, the code I want to test is:
if recursive:
log.debug("Recursively searching for files under %s" % path)
for (dir_path, dirs, files) in os.walk(path):
log.debug("Found %d files in %s: %s" % (len(files), path, files))
for f in [os.path.join(dir_path, f) for f in files
if not re.search(exclude, f)]:
yield f
else:
log.debug("Non-recursively searching for files under %s" % path)
for (dir_path, dirs, files) in os.walk(path):
log.debug("Found %d files in %s: %s" % (len(files), path, files))
for f in [os.path.join(dir_path, f) for f in files
if not re.search(exclude, f)]:
yield f
Is this possible in python?