Skip to content

Commit

Permalink
Make pyfakefs API PEP-8 conform
Browse files Browse the repository at this point in the history
- changed methods names
- added old method names as depracated version
- added possibility to switch on deprecation warning
- see pytest-dev#186
  • Loading branch information
mrbean-bremen committed Dec 5, 2017
1 parent e685eb1 commit 9a970d8
Show file tree
Hide file tree
Showing 20 changed files with 1,361 additions and 1,156 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The PyTest plugin provides the `fs` fixture for use in your test. For example:
```python
def my_fakefs_test(fs):
# "fs" is the reference to the fake file system
fs.CreateFile('/var/data/xx1.txt')
fs.create_file('/var/data/xx1.txt')
assert os.path.exists('/var/data/xx1.txt')
```

Expand All @@ -59,7 +59,7 @@ from fake_filesystem_unittest import Patcher

with Patcher() as patcher:
# access the fake_filesystem object via patcher.fs
patcher.fs.CreateFile('/foo/bar', contents='test')
patcher.fs.create_file('/foo/bar', contents='test')

# the following code works on the fake filesystem
with open('/foo/bar') as f:
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ environment:

matrix:
- PYTHON: "C:\\Python27-x64"
- PYTHON: "C:\\Python33-x64"
- PYTHON: "C:\\Python34-x64"
- PYTHON: "C:\\Python35-x64"
- PYTHON: "C:\\Python36-x64"

install:
Expand Down
16 changes: 6 additions & 10 deletions docs/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@ Public Modules and Classes
are shown. Methods that mimic the behavior of standard Python
functions are not listed - you may always use the standard functions.

*Style note:* most method names conform to the original Google style that does
not match PEP-8. In the next version, we plan to change the API to conform
to PEP-8 (maintaining upwards compatibility).

Fake filesystem module
----------------------
.. automodule:: pyfakefs.fake_filesystem

Fake filesystem classes
-----------------------
.. autoclass:: pyfakefs.fake_filesystem.FakeFilesystem
:members: AddMountPoint,
GetDiskUsage, SetDiskUsage, ChangeDiskUsage,
:members: add_mount_point,
get_disk_usage, set_disk_usage, change_disk_usage,
add_real_directory, add_real_file, add_real_paths,
CreateDirectory, CreateFile
create_dir, create_file, create_symlink

.. autoclass:: pyfakefs.fake_filesystem.FakeFile
:members: byte_contents, contents, GetPath, GetSize,
IsLargeFile, SetContents, SetSize
:members: byte_contents, contents, path, size,
is_large_file, set_contents

.. autoclass:: pyfakefs.fake_filesystem.FakeDirectory
:members: contents, GetEntry, GetSize, RemoveEntry
:members: contents, get_entry, size, remove_entry

Unittest module classes
-----------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The PyTest plugin provides the ``fs`` fixture for use in your test. For example:
def my_fakefs_test(fs):
# "fs" is the reference to the fake file system
fs.CreateFile('/var/data/xx1.txt')
fs.create_file('/var/data/xx1.txt')
assert os.path.exists('/var/data/xx1.txt')
Patch using fake_filesystem_unittest.Patcher
Expand Down Expand Up @@ -83,7 +83,7 @@ The following modules and functions can be patched:
fs = fake_fs.FakeFilesystem()
# Do some setup on the faked file system
fs.CreateFile('/foo/bar', contents='test')
fs.create_file('/foo/bar', contents='test')
# Replace some built-in file system related modules you use with faked ones
Expand Down
12 changes: 6 additions & 6 deletions dynamic_patch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ def testOsPatch(self):
import os

os.mkdir('test')
self.assertTrue(self.fs.Exists('test'))
self.assertTrue(self.fs.exists('test'))
self.assertTrue(os.path.exists('test'))

def testOsImportAsPatch(self):
import os as _os

_os.mkdir('test')
self.assertTrue(self.fs.Exists('test'))
self.assertTrue(self.fs.exists('test'))
self.assertTrue(_os.path.exists('test'))

def testOsPathPatch(self):
import os.path

os.mkdir('test')
self.assertTrue(self.fs.Exists('test'))
self.assertTrue(self.fs.exists('test'))
self.assertTrue(os.path.exists('test'))

@unittest.skipIf(sys.version_info < (3, 3), 'disk_usage new in Python 3.3')
def testShutilPatch(self):
import shutil

self.fs.SetDiskUsage(100)
self.fs.set_disk_usage(100)
self.assertEqual(100, shutil.disk_usage('/').total)

@unittest.skipIf(sys.version_info < (3, 4), 'pathlib new in Python 3.4')
Expand All @@ -69,8 +69,8 @@ def testPathlibPatch(self):
with path.open('w') as f:
f.write('test')

self.assertTrue(self.fs.Exists(file_path))
file_object = self.fs.GetObject(file_path)
self.assertTrue(self.fs.exists(file_path))
file_object = self.fs.get_object(file_path)
self.assertEqual('test', file_object.contents)


Expand Down
22 changes: 11 additions & 11 deletions example_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
Test the :py:class`pyfakefs.example` module to demonstrate the usage of the
:py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class.
Fake filesystem functions like `CreateFile()`, `CreateDirectory()` or `CreateLink()`
Fake filesystem functions like `create_file()`, `create_dir()` or `symlink()`
are often used to set up file structures at the beginning of a test.
While you could also use the familiar `open()`, `os.mkdirs()` and similar functions,
these functions can make the test code shorter and more readable.
`CreateFile()` is particularly convenient because it creates all parent directories
`create_file()` is particularly convenient because it creates all parent directories
and allows you to specify the contents or the size of the file.
"""

Expand Down Expand Up @@ -84,8 +84,8 @@ def test_create_file(self):

def test_delete_file(self):
"""Test example.delete_file() which uses `os.remove()`."""
self.fs.CreateFile('/test/full.txt',
contents='First line\n'
self.fs.create_file('/test/full.txt',
contents='First line\n'
'Second Line\n')
self.assertTrue(os.path.exists('/test/full.txt'))
example.delete_file('/test/full.txt')
Expand All @@ -94,13 +94,13 @@ def test_delete_file(self):
def test_file_exists(self):
"""Test example.path_exists() which uses `os.path.exists()`."""
self.assertFalse(example.path_exists('/test/empty.txt'))
self.fs.CreateFile('/test/empty.txt')
self.fs.create_file('/test/empty.txt')
self.assertTrue(example.path_exists('/test/empty.txt'))

def test_get_globs(self):
"""Test example.get_glob()."""
self.assertFalse(os.path.isdir('/test'))
self.fs.CreateDirectory('/test/dir1/dir2a')
self.fs.create_dir('/test/dir1/dir2a')
self.assertTrue(os.path.isdir('/test/dir1/dir2a'))
# os.mkdirs() works, too.
os.makedirs('/test/dir1/dir2b')
Expand All @@ -117,7 +117,7 @@ def test_get_globs(self):

def test_rm_tree(self):
"""Test example.rm_tree() using `shutil.rmtree()`."""
self.fs.CreateDirectory('/test/dir1/dir2a')
self.fs.create_dir('/test/dir1/dir2a')
# os.mkdirs() works, too.
os.makedirs('/test/dir1/dir2b')
self.assertTrue(os.path.isdir('/test/dir1/dir2b'))
Expand All @@ -133,10 +133,10 @@ def test_scandir(self):
The os module has been replaced with the fake os module so the
fake filesystem path entries are returned instead of `os.DirEntry` objects.
"""
self.fs.CreateFile('/test/text.txt')
self.fs.CreateDirectory('/test/dir')
self.fs.CreateFile('/linktest/linked')
self.fs.CreateLink('/test/linked_file', '/linktest/linked')
self.fs.create_file('/test/text.txt')
self.fs.create_dir('/test/dir')
self.fs.create_file('/linktest/linked')
self.fs.create_symlink('/test/linked_file', '/linktest/linked')

entries = sorted(example.scandir('/test'), key=lambda e: e.name)
self.assertEqual(3, len(entries))
Expand Down
10 changes: 5 additions & 5 deletions fake_filesystem_glob_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class FakeGlobUnitTest(fake_filesystem_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs()
directory = './xyzzy'
self.fs.CreateDirectory(directory)
self.fs.CreateDirectory('%s/subdir' % directory)
self.fs.CreateDirectory('%s/subdir2' % directory)
self.fs.CreateFile('%s/subfile' % directory)
self.fs.CreateFile('[Temp]')
self.fs.create_dir(directory)
self.fs.create_dir('%s/subdir' % directory)
self.fs.create_dir('%s/subdir2' % directory)
self.fs.create_file('%s/subfile' % directory)
self.fs.create_file('[Temp]')

def testGlobEmpty(self):
self.assertEqual(glob.glob(''), [])
Expand Down
20 changes: 10 additions & 10 deletions fake_filesystem_shutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def setUp(self):
self.filesystem = self.fs
self.os = os
self.open = open
self.fs.SetDiskUsage(1000)
self.fs.CreateDirectory(self.base_path)
self.fs.set_disk_usage(1000)
self.fs.create_dir(self.base_path)

def tearDown(self):
if self.useRealFs():
Expand Down Expand Up @@ -257,7 +257,7 @@ def testMoveFileInSameFilesystem(self):
self.skipRealFs()
src_file = '/original_xyzzy'
dst_file = '/moved_xyzzy'
src_object = self.fs.CreateFile(src_file)
src_object = self.fs.create_file(src_file)
src_ino = src_object.st_ino
src_dev = src_object.st_dev

Expand All @@ -267,24 +267,24 @@ def testMoveFileInSameFilesystem(self):
self.assertTrue(os.path.exists(dst_file))
self.assertFalse(os.path.exists(src_file))

dst_object = self.fs.GetObject(dst_file)
dst_object = self.fs.get_object(dst_file)
self.assertEqual(src_ino, dst_object.st_ino)
self.assertEqual(src_dev, dst_object.st_dev)

def testMoveFileIntoOtherFilesystem(self):
self.skipRealFs()
self.fs.AddMountPoint('/mount')
self.fs.add_mount_point('/mount')
src_file = '/original_xyzzy'
dst_file = '/mount/moved_xyzzy'
src_object = self.fs.CreateFile(src_file)
src_object = self.fs.create_file(src_file)
src_ino = src_object.st_ino
src_dev = src_object.st_dev

shutil.move(src_file, dst_file)
self.assertTrue(os.path.exists(dst_file))
self.assertFalse(os.path.exists(src_file))

dst_object = self.fs.GetObject(dst_file)
dst_object = self.fs.get_object(dst_file)
self.assertNotEqual(src_ino, dst_object.st_ino)
self.assertNotEqual(src_dev, dst_object.st_dev)

Expand Down Expand Up @@ -317,15 +317,15 @@ def testMoveDirectory(self):
@unittest.skipIf(sys.version_info < (3, 3), 'New in Python 3.3')
def testDiskUsage(self):
self.skipRealFs()
self.fs.CreateFile('/foo/bar', st_size=400)
self.fs.create_file('/foo/bar', st_size=400)
disk_usage = shutil.disk_usage('/')
self.assertEqual(1000, disk_usage.total)
self.assertEqual(400, disk_usage.used)
self.assertEqual(600, disk_usage.free)
self.assertEqual((1000, 400, 600), disk_usage)

self.fs.AddMountPoint('/mount', total_size=500)
self.fs.CreateFile('/mount/foo/bar', st_size=400)
self.fs.add_mount_point('/mount', total_size=500)
self.fs.create_file('/mount/foo/bar', st_size=400)
disk_usage = shutil.disk_usage('/mount/foo/')
self.assertEqual((500, 400, 100), disk_usage)

Expand Down
Loading

0 comments on commit 9a970d8

Please sign in to comment.