Skip to content

Commit

Permalink
Added support for dir_fd argument in several os functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Jun 15, 2017
1 parent 39f00a0 commit 1ee12a4
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 83 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The release versions are PyPi releases.
## Version 3.3 (as yet unreleased)

#### New Features
* Added support for `dir_fd` argument in several `os` functions ([#206](../../issues/206)).
* Added support for basic modes in fake `os.open()` ([#204](../../issues/204)).
* Added fake `os.path.samefile` implementation ([#193](../../issues/193))
* Added support for `ns` argument in `os.utime()` (Python >= 3.3) ([#192](../../issues/192)).
Expand Down
127 changes: 125 additions & 2 deletions fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,7 @@ class FakeOsModuleLowLevelFileOpTest(FakeOsModuleTestBase):

def setUp(self):
super(FakeOsModuleLowLevelFileOpTest, self).setUp()
if sys.version_info < (3, ):
if sys.version_info < (3,):
self.operation_error = IOError
else:
self.operation_error = io.UnsupportedOperation
Expand Down Expand Up @@ -2451,6 +2451,129 @@ def testLowLevelWriteRead(self):
self.assertRaisesOSError(errno.EBADF, self.os.read, fileno, 10)


@unittest.skipIf(sys.version_info < (3, 3),
'dir_fd argument was introduced in Python 3.3')
class FakeOsModuleDirFdTest(FakeOsModuleTestBase):
def setUp(self):
super(FakeOsModuleDirFdTest, self).setUp()
self.os.supports_dir_fd = set()
self.filesystem.is_windows_fs = False
self.filesystem.CreateDirectory('/foo/bar')
self.dir_fd = self.os.open('/foo', os.O_RDONLY)
self.filesystem.CreateFile('/foo/baz')

def testAccess(self):
self.assertRaises(
NotImplementedError, self.os.access, 'baz', self.os.F_OK, dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.access)
self.assertTrue(self.os.access('baz', self.os.F_OK, dir_fd=self.dir_fd))

def testChmod(self):
self.assertRaises(
NotImplementedError, self.os.chmod, 'baz', 0o6543, dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.chmod)
self.os.chmod('baz', 0o6543, dir_fd=self.dir_fd)
st = self.os.stat('/foo/baz')
self.assertModeEqual(0o6543, st.st_mode)

def testChown(self):
self.assertRaises(
NotImplementedError, self.os.chown, 'baz', 100, 101, dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.chown)
self.os.chown('baz', 100, 101, dir_fd=self.dir_fd)
st = self.os.stat('/foo/baz')
self.assertEqual(st[stat.ST_UID], 100)
self.assertEqual(st[stat.ST_GID], 101)

def testLink(self):
self.assertRaises(
NotImplementedError, self.os.link, 'baz', '/bat', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.link)
self.os.link('baz', '/bat', dir_fd=self.dir_fd)
self.assertTrue(self.filesystem.Exists('/bat'))

def testSymlink(self):
self.assertRaises(
NotImplementedError, self.os.symlink, 'baz', '/bat', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.symlink)
self.os.symlink('baz', '/bat', dir_fd=self.dir_fd)
self.assertTrue(self.filesystem.Exists('/bat'))

def testReadlink(self):
self.filesystem.CreateLink('/meyer/lemon/pie', '/foo/baz')
self.filesystem.CreateLink('/geo/metro', '/meyer')
self.assertRaises(
NotImplementedError, self.os.readlink, '/geo/metro/lemon/pie', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.readlink)
self.assertEqual('/foo/baz', self.os.readlink(
'/geo/metro/lemon/pie', dir_fd=self.dir_fd))

def testStat(self):
self.assertRaises(
NotImplementedError, self.os.stat, 'baz', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.stat)
st = self.os.stat('baz', dir_fd=self.dir_fd)
self.assertEqual(st.st_mode, 0o100666)

def testLstat(self):
self.assertRaises(
NotImplementedError, self.os.lstat, 'baz', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.lstat)
st = self.os.lstat('baz', dir_fd=self.dir_fd)
self.assertEqual(st.st_mode, 0o100666)

def testMkdir(self):
self.assertRaises(
NotImplementedError, self.os.mkdir, 'newdir', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.mkdir)
self.os.mkdir('newdir', dir_fd=self.dir_fd)
self.assertTrue(self.filesystem.Exists('/foo/newdir'))

def testRmdir(self):
self.assertRaises(
NotImplementedError, self.os.rmdir, 'bar', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.rmdir)
self.os.rmdir('bar', dir_fd=self.dir_fd)
self.assertFalse(self.filesystem.Exists('/foo/bar'))

def testMknod(self):
self.assertRaises(
NotImplementedError, self.os.mknod, 'newdir', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.mknod)
self.os.mknod('newdir', dir_fd=self.dir_fd)
self.assertTrue(self.filesystem.Exists('/foo/newdir'))

def testRename(self):
self.assertRaises(
NotImplementedError, self.os.rename, 'baz', '/foo/batz', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.rename)
self.os.rename('bar', '/foo/batz', dir_fd=self.dir_fd)
self.assertTrue(self.filesystem.Exists('/foo/batz'))

def testRemove(self):
self.assertRaises(
NotImplementedError, self.os.remove, 'baz', dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.remove)
self.os.remove('baz', dir_fd=self.dir_fd)
self.assertFalse(self.filesystem.Exists('/foo/baz'))

def testUtime(self):
self.assertRaises(
NotImplementedError, self.os.utime, 'baz', (1, 2), dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.utime)
self.os.utime('baz', (1, 2), dir_fd=self.dir_fd)
st = self.os.stat('/foo/baz')
self.assertEqual(1, st.st_atime)
self.assertEqual(2, st.st_mtime)

def testOpen(self):
self.assertRaises(
NotImplementedError, self.os.open, 'baz', os.O_RDONLY, dir_fd=self.dir_fd)
self.os.supports_dir_fd.add(self.os.open)
fd = self.os.open('baz', os.O_RDONLY, dir_fd=self.dir_fd)
self.assertLess(0, fd)


@unittest.skipIf(sys.version_info < (3, 5), 'os.scandir was introduced in Python 3.5')
class FakeScandirTest(FakeOsModuleTestBase):
def setUp(self):
Expand Down Expand Up @@ -2793,7 +2916,7 @@ def testRealpathVsAbspath(self):
self.assertEqual('!george!washington!bridge',
self.os.path.realpath('bridge'))

@unittest.skipIf(TestCase.is_windows and sys.version_info < (3,2),
@unittest.skipIf(TestCase.is_windows and sys.version_info < (3, 2),
'No Windows support before 3.2')
def testSamefile(self):
file_path1 = '!foo!bar!baz'
Expand Down
2 changes: 1 addition & 1 deletion fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def test_symlink(self):
def test_stat(self):
path = 'foo!bar!baz'
self.filesystem.CreateFile(path, contents='1234567')
self.assertEqual(self.os.stat(path, os.R_OK), self.os.stat(self.path(path), os.R_OK))
self.assertEqual(self.os.stat(path), self.os.stat(self.path(path)))

def test_utime(self):
path = '!some_file'
Expand Down
Loading

0 comments on commit 1ee12a4

Please sign in to comment.