diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index 0dcdca80..3432ee5b 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -3703,8 +3703,13 @@ def chdir(self, path): OSError: if user lacks permission to enter the argument directory or if the target is not a directory. """ - path = self.filesystem.resolve_path( - path, allow_fd=True) + try: + path = self.filesystem.resolve_path( + path, allow_fd=True) + except OSError as exc: + if self.filesystem.is_macos and exc.errno == errno.EBADF: + raise OSError(errno.ENOTDIR, "Not a directory: " + str(path)) + raise self.filesystem.confirmdir(path) directory = self.filesystem.resolve(path) # A full implementation would check permissions all the way diff --git a/pyfakefs/tests/fake_open_test.py b/pyfakefs/tests/fake_open_test.py index 0b0c502e..beeb4908 100644 --- a/pyfakefs/tests/fake_open_test.py +++ b/pyfakefs/tests/fake_open_test.py @@ -1726,7 +1726,8 @@ def test_chdir_uses_open_fd_as_path(self): if self.is_pypy: # unclear behavior with PyPi self.skip_real_fs() - self.assert_raises_os_error(errno.EBADF, self.os.chdir, 10) + error = errno.ENOTDIR if self.is_macos else errno.EBADF + self.assert_raises_os_error(error, self.os.chdir, 10) dir_path = self.make_path('foo', 'bar') self.create_dir(dir_path)