8000 [#277] NodeApp::make_empty does not allow None/empty base_dir · postgrespro/testgres@1512fe7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1512fe7

Browse files
[#277] NodeApp::make_empty does not allow None/empty base_dir
1 parent 1062a97 commit 1512fe7

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

testgres/node_app.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,23 @@ def test_path(self) -> str:
5252

5353
def make_empty(
5454
self,
55-
base_dir: typing.Optional[str] = None,
55+
base_dir: str,
5656
port: typing.Optional[int] = None,
5757
bin_dir: typing.Optional[str] = None
5858
) -> PostgresNode:
59-
assert base_dir is None or type(base_dir) == str # noqa: E721
59+
assert type(base_dir) == str # noqa: E721
6060
assert port is None or type(port) == int # noqa: E721
6161
assert bin_dir is None or type(bin_dir) == str # noqa: E721
6262

6363
assert isinstance(self._os_ops, OsOperations)
6464
assert type(self._test_path) == str # noqa: E721
6565

66+
if base_dir is None:
67+
raise ValueError("Argument 'base_dir' is not defined.")
68+
69+
if base_dir == "":
70+
raise ValueError("Argument 'base_dir' is empty.")
71+
6672
real_base_dir = self._os_ops.build_path(self._test_path, base_dir)
6773
self._os_ops.rmdirs(real_base_dir, ignore_errors=True)
6874
self._os_ops.makedirs(real_base_dir)
@@ -74,7 +80,7 @@ def make_empty(
7480

7581
def make_simple(
7682
self,
77-
base_dir: typing.Optional[str] = None,
83+
base_dir: str,
7884
port: typing.Optional[int] = None,
7985
set_replication: bool = False,
8086
ptrack_enable: bool = False,
@@ -83,7 +89,7 @@ def make_simple(
8389
checksum: bool = True,
8490
bin_dir: typing.Optional[str] = None
8591
) -> PostgresNode:
86-
assert base_dir is None or type(base_dir) == str # noqa: E721
92+
assert type(base_dir) == str # noqa: E721
8793
assert port is None or type(port) == int # noqa: E721
8894
assert type(set_replication) == bool # noqa: E721
8995
assert type(ptrack_enable) == bool # noqa: E721

tests/test_testgres_common.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from .helpers.global_data import PostgresNodeService
24
from .helpers.global_data import PostgresNodeServices
35
from .helpers.global_data import OsOperations
@@ -13,6 +15,7 @@
1315
from testgres import ProcessType
1416
from testgres import NodeStatus
1517
from testgres import IsolationLevel
18+
from testgres import NodeApp
1619

1720
# New name prevents to collect test-functions in TestgresException and fixes
1821
# the problem with pytest warning.
@@ -1584,6 +1587,88 @@ def test_node__no_port_manager(self, node_svc: PostgresNodeService):
15841587
finally:
15851588
node_svc.port_manager.release_port(port)
15861589

1590+
class tag_rmdirs_protector:
1591+
_os_ops: OsOperations
1592+
_cwd: str
1593+
_old_rmdirs: any
1594+
_cwd: str
1595+
1596+
def __init__(self, os_ops: OsOperations):
1597+
self._os_ops = os_ops
1598+
self._cwd = os.path.abspath(os_ops.cwd())
1599+
self._old_rmdirs = os_ops.rmdirs
1600+
1601+
def __enter__(self):
1602+
assert self._os_ops.rmdirs == self._old_rmdirs
1603+
self._os_ops.rmdirs = self.proxy__rmdirs
1604+
return self
1605+
1606+
def __exit__(self, exc_type, exc_val, exc_tb):
1607+
assert self._os_ops.rmdirs == self.proxy__rmdirs
1608+
self._os_ops.rmdirs = self._old_rmdirs
1609+
return False
1610+
1611+
def proxy__rmdirs(self, path, ignore_errors=True):
1612+
raise Exception("Call of rmdirs is not expected!")
1613+
1614+
def test_node_app__make_empty__base_dir_is_None(self, node_svc: PostgresNodeService):
1615+
assert type(node_svc) == PostgresNodeService # noqa: E721
1616+
1617+
assert isinstance(node_svc.os_ops, OsOperations)
1618+
assert node_svc.port_manager is not None
1619+
assert isinstance(node_svc.port_manager, PortManager)
1620+
1621+
tmp_dir = node_svc.os_ops.mkdtemp()
1622+
assert tmp_dir is not None
1623+
assert type(tmp_dir) == str # noqa: E721
1624+
logging.info("temp directory is [{}]".format(tmp_dir))
1625+
1626+
# -----------
1627+
os_ops = node_svc.os_ops.create_clone()
1628+
assert os_ops is not node_svc.os_ops
1629+
1630+
# -----------
1631+
with __class__.tag_rmdirs_protector(os_ops):
1632+
node_app = NodeApp(test_path=tmp_dir, os_ops=os_ops)
1633+
1634+
with pytest.raises(expected_exception=ValueError) as x:
1635+
node_app.make_empty(base_dir=None)
1636+
1637+
assert str(x.value) == "Argument 'base_dir' is not defined."
1638+
1639+
# -----------
1640+
logging.info("temp directory [{}] is deleting".format(tmp_dir))
1641+
node_svc.os_ops.rmdir(tmp_dir)
1642+
1643+
def test_node_app__make_empty__base_dir_is_Empty(self, node_svc: PostgresNodeService):
1644+
assert type(node_svc) == PostgresNodeService # noqa: E721
1645+
1646+
assert isinstance(node_svc.os_ops, OsOperations)
1647+
assert node_svc.port_manager is not None
1648+
assert isinstance(node_svc.port_manager, PortManager)
1649+
1650+
tmp_dir = node_svc.os_ops.mkdtemp()
1651+
assert tmp_dir is not None
1652+
assert type(tmp_dir) == str # noqa: E721
1653+
logging.info("temp directory is [{}]".format(tmp_dir))
1654+
1655+
# -----------
1656+
os_ops = node_svc.os_ops.create_clone()
1657+
assert os_ops is not node_svc.os_ops
1658+
1659+
# -----------
1660+
with __class__.tag_rmdirs_protector(os_ops):
1661+
node_app = NodeApp(test_path=tmp_dir, os_ops=os_ops)
1662+
1663+
with pytest.raises(expected_exception=ValueError) as x:
1664+
node_app.make_empty(base_dir="")
1665+
1666+
assert str(x.value) == "Argument 'base_dir' is empty."
1667+
1668+
# -----------
1669+
logging.info("temp directory [{}] is deleting".format(tmp_dir))
1670+
node_svc.os_ops.rmdir(tmp_dir)
1671+
15871672
@staticmethod
15881673
def helper__get_node(
15891674
node_svc: PostgresNodeService,

0 commit comments

Comments
 (0)
0