From 501e9bc96f4d2133fd0e465b0833af56f2a701e9 Mon Sep 17 00:00:00 2001 From: Godot Date: Fri, 7 Jun 2024 12:45:41 +0200 Subject: [PATCH] added and tested os.getcwd and os.path.abspath (#182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added and tested os.getcwd and os.path.abspath * Update README.md --------- Co-authored-by: Godefroy Clair Co-authored-by: Tin Tvrtković --- README.md | 4 ++++ src/aiofiles/os.py | 3 +++ src/aiofiles/ospath.py | 2 ++ tests/test_os.py | 25 +++++++++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/README.md b/README.md index e380b91..cad133d 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,8 @@ several useful `os` functions that deal with files: - `listdir` - `scandir` - `access` +- `getcwd` +- `path.abspath` - `path.exists` - `path.isfile` - `path.isdir` @@ -183,6 +185,8 @@ async def test_stuff(): [#175](https://github.com/Tinche/aiofiles/issues/175) - Remove spurious items from `aiofiles.os.__all__` when running on Windows. - Switch to more modern async idioms: Remove types.coroutine and make AiofilesContextManager an awaitable instead a coroutine. +- Add `aiofiles.os.path.abspath` and `aiofiles.os.getcwd`. + [#174](https://github.com/Tinche/aiofiles/issues/181) #### 23.2.1 (2023-08-09) diff --git a/src/aiofiles/os.py b/src/aiofiles/os.py index 16ff875..92243fa 100644 --- a/src/aiofiles/os.py +++ b/src/aiofiles/os.py @@ -1,4 +1,5 @@ """Async executor versions of file functions from the os module.""" + import os from . import ospath as path @@ -22,6 +23,7 @@ "scandir", "access", "wrap", + "getcwd", ] if hasattr(os, "link"): __all__ += ["link"] @@ -46,6 +48,7 @@ listdir = wrap(os.listdir) scandir = wrap(os.scandir) access = wrap(os.access) +getcwd = wrap(os.getcwd) if hasattr(os, "link"): link = wrap(os.link) diff --git a/src/aiofiles/ospath.py b/src/aiofiles/ospath.py index 5f32a43..387d68d 100644 --- a/src/aiofiles/ospath.py +++ b/src/aiofiles/ospath.py @@ -1,4 +1,5 @@ """Async executor versions of file functions from the os.path module.""" + import asyncio from functools import partial, wraps from os import path @@ -26,3 +27,4 @@ async def run(*args, loop=None, executor=None, **kwargs): getctime = wrap(path.getctime) samefile = wrap(path.samefile) sameopenfile = wrap(path.sameopenfile) +abspath = wrap(path.abspath) diff --git a/tests/test_os.py b/tests/test_os.py index d3c0bda..fbda8ea 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -1,4 +1,5 @@ """Tests for asyncio's os module.""" + import asyncio import os import platform @@ -511,3 +512,27 @@ async def test_access(): print("mode:{}".format(mode)) assert not await aiofiles.os.access(temp_file, mode) assert not await aiofiles.os.access(temp_dir, mode) + + +@pytest.mark.asyncio +async def test_getcwd(): + """Test the getcwd call.""" + cwd = await aiofiles.os.getcwd() + assert cwd == os.getcwd() + + +@pytest.mark.asyncio +async def test_abspath_given_abspath(): + """Test the abspath call with an absolute path.""" + filename = join(dirname(__file__), "resources", "test_file1.txt") + file_abs_path = await aiofiles.os.path.abspath(filename) + assert file_abs_path == filename + + +@pytest.mark.asyncio +async def test_abspath(): + """Test the abspath call.""" + relative_filename = "./tests/resources/test_file1.txt" + abs_filename = join(dirname(__file__), "resources", "test_file1.txt") + result = await aiofiles.os.path.abspath(relative_filename) + assert result == abs_filename