Skip to content

Use temp directory for os test #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/os.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from _os import *

if name == 'nt':
sep = '\\'
else:
sep = '/'

# Change environ to automatically call putenv(), unsetenv if they exist.
from _collections_abc import MutableMapping

Expand Down
71 changes: 43 additions & 28 deletions tests/snippets/stdlib_os.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import os
import time

from testutils import assert_raises

Expand All @@ -7,33 +8,6 @@

os.close(fd)
assert_raises(OSError, lambda: os.read(fd, 10))

FNAME = "test_file_that_no_one_will_have_on_disk"
CONTENT = b"testing"
CONTENT2 = b"rustpython"
CONTENT3 = b"BOYA"

class TestWithFile():
def __enter__(self):
open(FNAME, "wb")
return FNAME

def __exit__(self, exc_type, exc_val, exc_tb):
os.remove(FNAME)


with TestWithFile() as fname:
fd = os.open(fname, 1)
assert os.write(fd, CONTENT2) == len(CONTENT2)
assert os.write(fd, CONTENT3) == len(CONTENT3)
os.close(fd)

fd = os.open(fname, 0)
assert os.read(fd, len(CONTENT2)) == CONTENT2
assert os.read(fd, len(CONTENT3)) == CONTENT3
os.close(fd)


assert_raises(FileNotFoundError, lambda: os.open('DOES_NOT_EXIST', 0))


Expand All @@ -58,3 +32,44 @@ def __exit__(self, exc_type, exc_val, exc_tb):
os.putenv(ENV_KEY, ENV_VALUE)
os.unsetenv(ENV_KEY)
assert os.getenv(ENV_KEY) == None


if os.name == "nt":
assert os.sep == "\\"
else:
assert os.sep == "/"

class TestWithTempDir():
def __enter__(self):
if os.name == "nt":
base_folder = os.environ["TEMP"]
else:
base_folder = "/tmp"
name = base_folder + os.sep + "rustpython_test_os_" + str(int(time.time()))
os.mkdir(name)
self.name = name
return name

def __exit__(self, exc_type, exc_val, exc_tb):
# TODO: Delete temp dir
pass


FILE_NAME = "test1"
CONTENT = b"testing"
CONTENT2 = b"rustpython"
CONTENT3 = b"BOYA"

with TestWithTempDir() as tmpdir:
fname = tmpdir + os.sep + FILE_NAME
with open(fname, "wb"):
pass
fd = os.open(fname, 1)
assert os.write(fd, CONTENT2) == len(CONTENT2)
assert os.write(fd, CONTENT3) == len(CONTENT3)
os.close(fd)

fd = os.open(fname, 0)
assert os.read(fd, len(CONTENT2)) == CONTENT2
assert os.read(fd, len(CONTENT3)) == CONTENT3
os.close(fd)