diff --git a/src/local_ops.py b/src/local_ops.py index 925690f..caf7181 100644 --- a/src/local_ops.py +++ b/src/local_ops.py @@ -397,13 +397,18 @@ def get_name(self): return os.name # Work with dirs - def makedirs(self, path, remove_existing=False): + def makedirs( + self, + path: str, + remove_existing: bool = False, + ) -> None: + assert type(path) is str + assert type(remove_existing) is bool + if remove_existing: shutil.rmtree(path, ignore_errors=True) - try: - os.makedirs(path) - except FileExistsError: - pass + + os.makedirs(path, exist_ok=True) return def makedir(self, path: str): diff --git a/src/os_ops.py b/src/os_ops.py index ae0432c..18356fc 100644 --- a/src/os_ops.py +++ b/src/os_ops.py @@ -139,7 +139,13 @@ def get_name(self): raise NotImplementedError() # Work with dirs - def makedirs(self, path, remove_existing=False): + def makedirs( + self, + path: str, + remove_existing: bool = False, + ) -> None: + assert type(path) is str + assert type(remove_existing) is bool raise NotImplementedError() def makedir(self, path: str): diff --git a/src/remote_ops.py b/src/remote_ops.py index 0c910fa..d37d48f 100644 --- a/src/remote_ops.py +++ b/src/remote_ops.py @@ -346,7 +346,11 @@ def get_name(self): return stdout.strip() # Work with dirs - def makedirs(self, path, remove_existing=False): + def makedirs( + self, + path: str, + remove_existing: bool = False, + ) -> None: """ Create a directory in the remote server. Args: @@ -354,16 +358,15 @@ def makedirs(self, path, remove_existing=False): - remove_existing (bool): If True, the existing directory at the path will be removed. """ if remove_existing: - cmd = "rm -rf {} && mkdir -p {}".format(path, path) + cmd = ["rm", "-rf", path, "&&", "mkdir", "-p", path] else: - cmd = "mkdir -p {}".format(path) - try: - result = self.exec_command(cmd) - except ExecUtilException as e: - raise Exception("Couldn't create dir {} because of error {}".format(path, e.message)) + cmd = ["mkdir", "-p", path] - assert type(result) is bytes - return result + self.exec_command( + cmd, + encoding=get_default_encoding(), + ) + return def makedir(self, path: str): assert type(path) is str diff --git a/tests/test_os_ops_common.py b/tests/test_os_ops_common.py index c898b46..391db18 100644 --- a/tests/test_os_ops_common.py +++ b/tests/test_os_ops_common.py @@ -467,11 +467,18 @@ def test_makedirs_failure( RunConditions.skip_if_windows() - path = "/root/test_dir" + path = "/root/test_dir-{}".format(uuid.uuid4().bytes.hex()) # Test makedirs - with pytest.raises(Exception): + with pytest.raises(Exception) as x: os_ops.makedirs(path) + + if type(os_ops).__name__ == "LocalOperations": + assert type(x.value) is PermissionError + elif type(os_ops).__name__ == "RemoteOperations": + assert type(x.value) is ExecUtilException + else: + __class__.helper__bug_check__unknown_os_ops_type(os_ops) return def test_listdir( @@ -2461,3 +2468,14 @@ def test_readlines__BIN( assert lines == result_bin return + + @staticmethod + def helper__bug_check__unknown_os_ops_type( + os_ops: OsOperations, + ) -> typing.NoReturn: + assert isinstance(os_ops, OsOperations) + + err_msg = "[BUG CHECK] Unknown os_ops type [{}].".format( + type(os_ops).__name__, + ) + raise RuntimeError(err_msg)