Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 23cb761

Browse files
TestOsOpsCommon is updated (+LocalOperations::is_executable)
New tests: - test_is_executable_true - test_is_executable_false - test_makedirs_and_rmdirs_success - test_makedirs_failure LocalOperations::is_executable is corrected
1 parent e43a384 commit 23cb761

File tree

3 files changed

+62
-55
lines changed

3 files changed

+62
-55
lines changed

testgres/operations/local_ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ def find_executable(self, executable):
156156

157157
def is_executable(self, file):
158158
# Check if the file is executable
159-
return os.stat(file).st_mode & stat.S_IXUSR
159+
assert stat.S_IXUSR != 0
160+
return (os.stat(file).st_mode & stat.S_IXUSR) == stat.S_IXUSR
160161

161162
def set_env(self, var_name, var_val):
162163
# Check if the directory is already in PATH

tests/test_os_ops_common.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,66 @@ def test_exec_command_failure__expect_error(self, os_ops: OsOperations):
8989
assert b"nonexistent_command" in error
9090
assert b"not found" in error
9191

92+
def test_is_executable_true(self, os_ops: OsOperations):
93+
"""
94+
Test is_executable for an existing executable.
95+
"""
96+
assert isinstance(os_ops, OsOperations)
97+
98+
RunConditions.skip_if_windows()
99+
100+
response = os_ops.is_executable("/bin/sh")
101+
102+
assert response is True
103+
104+
def test_is_executable_false(self, os_ops: OsOperations):
105+
"""
106+
Test is_executable for a non-executable.
107+
"""
108+
assert isinstance(os_ops, OsOperations)
109+
110+
response = os_ops.is_executable(__file__)
111+
112+
assert response is False
113+
114+
def test_makedirs_and_rmdirs_success(self, os_ops: OsOperations):
115+
"""
116+
Test makedirs and rmdirs for successful directory creation and removal.
117+
"""
118+
assert isinstance(os_ops, OsOperations)
119+
120+
RunConditions.skip_if_windows()
121+
122+
cmd = "pwd"
123+
pwd = os_ops.exec_command(cmd, wait_exit=True, encoding='utf-8').strip()
124+
125+
path = "{}/test_dir".format(pwd)
126+
127+
# Test makedirs
128+
os_ops.makedirs(path)
129+
assert os.path.exists(path)
130+
assert os_ops.path_exists(path)
131+
132+
# Test rmdirs
133+
os_ops.rmdirs(path)
134+
assert not os.path.exists(path)
135+
assert not os_ops.path_exists(path)
136+
137+
def test_makedirs_failure(self, os_ops: OsOperations):
138+
"""
139+
Test makedirs for failure.
140+
"""
141+
# Try to create a directory in a read-only location
142+
assert isinstance(os_ops, OsOperations)
143+
144+
RunConditions.skip_if_windows()
145+
146+
path = "/root/test_dir"
147+
148+
# Test makedirs
149+
with pytest.raises(Exception):
150+
os_ops.makedirs(path)
151+
92152
def test_listdir(self, os_ops: OsOperations):
93153
"""
94154
Test listdir for listing directory contents.

tests/test_remote.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
from ..testgres import ExecUtilException
88
from ..testgres import RemoteOperations
9-
from ..testgres import LocalOperations
109
from ..testgres import ConnectionParams
11-
from ..testgres import utils as testgres_utils
1210

1311

1412
class TestRemoteOperations:
@@ -20,58 +18,6 @@ def setup(self):
2018
ssh_key=os.getenv('RDBMS_TESTPOOL_SSHKEY'))
2119
self.operations = RemoteOperations(conn_params)
2220

23-
def test_is_executable_true(self):
24-
"""
25-
Test is_executable for an existing executable.
26-
"""
27-
local_ops = LocalOperations()
28-
cmd = testgres_utils.get_bin_path2(local_ops, "pg_config")
29-
cmd = local_ops.exec_command([cmd, "--bindir"], encoding="utf-8")
30-
cmd = cmd.rstrip()
31-
cmd = os.path.join(cmd, "pg_config")
32-
response = self.operations.is_executable(cmd)
33-
34-
assert response is True
35-
36-
def test_is_executable_false(self):
37-
"""
38-
Test is_executable for a non-executable.
39-
"""
40-
cmd = "python"
41-
response = self.operations.is_executable(cmd)
42-
43-
assert response is False
44-
45-
def test_makedirs_and_rmdirs_success(self):
46-
"""
47-
Test makedirs and rmdirs for successful directory creation and removal.
48-
"""
49-
cmd = "pwd"
50-
pwd = self.operations.exec_command(cmd, wait_exit=True, encoding='utf-8').strip()
51-
52-
path = "{}/test_dir".format(pwd)
53-
54-
# Test makedirs
55-
self.operations.makedirs(path)
56-
assert os.path.exists(path)
57-
assert self.operations.path_exists(path)
58-
59-
# Test rmdirs
60-
self.operations.rmdirs(path)
61-
assert not os.path.exists(path)
62-
assert not self.operations.path_exists(path)
63-
64-
def test_makedirs_failure(self):
65-
"""
66-
Test makedirs for failure.
67-
"""
68-
# Try to create a directory in a read-only location
69-
path = "/root/test_dir"
70-
71-
# Test makedirs
72-
with pytest.raises(Exception):
73-
self.operations.makedirs(path)
74-
7521
def test_mkdtemp__default(self):
7622
path = self.operations.mkdtemp()
7723
logging.info("Path is [{0}].".format(path))

0 commit comments

Comments
 (0)