Last active
August 29, 2015 14:17
-
-
Save bewt85/ff05784866aa4c699042 to your computer and use it in GitHub Desktop.
mocking os.path.isdir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── bar | |
├── foo | |
├── scripts | |
│ ├── foobar.py | |
│ └── __init__.py | |
└── tests | |
├── foobar_test.py | |
└── __init__.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
python -m unittest discover -s tests/ -p '*_test.py' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def get_dirname(filename): | |
return os.path.dirname(filename) | |
def get_parent_dirname(filename): | |
return os.path.abspath(os.path.join(filename, os.pardir)) | |
def get_dir_contents(filename): | |
return os.listdir(filename) | |
def fullpaths(dirname, names): | |
return [os.path.join(dirname, name) for name in names] | |
def just_files(folder_contents): | |
is_a_file = lambda f: os.path.isfile(f) and not os.path.islink(f) | |
return filter(is_a_file, folder_contents) | |
def just_dirs(folder_contents): | |
return filter(os.path.isdir, folder_contents) | |
def just_links(folder_contents): | |
return filter(os.path.islink, folder_contents) | |
if __name__ == '__main__': | |
my_directory = get_dirname(__file__) | |
my_parent = get_parent_dirname(my_directory) | |
my_siblings = get_dir_contents(my_parent) | |
full_my_siblings = fullpaths(my_parent, my_siblings) | |
my_sibling_files = just_files(full_my_siblings) | |
print my_sibling_files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import mock | |
from scripts import foobar | |
class TestFooBar(unittest.TestCase): | |
def is_a_file(self, filename): | |
return 'file' in filename or self.is_a_link(filename) | |
def is_a_dir(self, filename): | |
return 'dir' in filename | |
def is_a_link(self, filename): | |
return 'link' in filename | |
@mock.patch('scripts.foobar.os') | |
def test_just_files(self, os_mock): | |
os_mock.path.isfile.side_effect = self.is_a_file | |
os_mock.path.islink.side_effect = self.is_a_link | |
os_mock.path.isdir.side_effect = self.is_a_dir | |
files = foobar.just_files(['file_foo', 'dir_bar', 'link_baz']) | |
self.assertEqual(files, ['file_foo']) | |
@mock.patch('scripts.foobar.os') | |
def test_just_dirs(self, os_mock): | |
os_mock.path.isfile.side_effect = self.is_a_file | |
os_mock.path.islink.side_effect = self.is_a_link | |
os_mock.path.isdir.side_effect = self.is_a_dir | |
files = foobar.just_dirs(['file_foo', 'dir_bar', 'link_baz']) | |
self.assertEqual(files, ['dir_bar']) | |
@mock.patch('scripts.foobar.os') | |
def test_just_links(self, os_mock): | |
os_mock.path.isfile.side_effect = self.is_a_file | |
os_mock.path.islink.side_effect = self.is_a_link | |
os_mock.path.isdir.side_effect = self.is_a_dir | |
files = foobar.just_links(['file_foo', 'dir_bar', 'link_baz']) | |
self.assertEqual(files, ['link_baz']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment