Skip to content

Commit

Permalink
Added GLOB Pattern matching for "File/Directory Should/Should Not exi…
Browse files Browse the repository at this point in the history
…st (#394)

Closes #104
  • Loading branch information
URunDEAD authored Nov 5, 2021
1 parent f9a5ff7 commit 17a46d2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions atest/file_and_dir_exists.robot
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,19 @@ File Should Not Exist Using Relative Path
SSH.File Should Not Exist ${target}
Run Keyword And Expect Error File '${target}' does not exist.
... SSH.File Should Exist ${target}

File Should Exist Using GLOB Patterns
${target} = Set Variable ${REMOTE TEST ROOT NAME}/${SUBDIRECTORY NAME}/?[a][!b]*.txt
SSH.File Should Exist ${target}

File Should Not Exist Using GLOB Patterns
${target} = Set Variable ${REMOTE TEST ROOT NAME}/${SUBDIRECTORY NAME}/?[a]z[!b]*.txt
SSH.File Should Not Exist ${target}

Directory Should Exist Using GLOB Patterns
${target} = Set Variable ${REMOTE TEST ROOT}/[abcDAWF][!b]?*
SSH.Directory Should Exist ${target}

Directory Should Not Exist Using GLOB Patterns
${target} = Set Variable ${REMOTE TEST ROOT}/z*
SSH.Directory Should Not Exist ${target}
28 changes: 28 additions & 0 deletions src/SSHLibrary/abstractclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import glob
import posixpath
import ntpath
import fnmatch

from .config import (Configuration, IntegerEntry, NewlineEntry, StringEntry,
TimeEntry)
Expand Down Expand Up @@ -714,15 +715,42 @@ def list_dirs_in_dir(self, path, pattern=None, absolute=False):
def is_dir(self, path):
"""Calls :py:meth:`AbstractSFTPClient.is_dir` with the given `path`.
:param str path: Path to check for directory. Supports GLOB Patterns.
:returns: Boolean indicating is the directory is present or not.
:rtype: bool
See :py:meth:`AbstractSFTPClient.is_dir` for more documentation.
"""
has_glob = bool([ops for ops in '*?![' if(ops in path)])
if has_glob:
dir_dir = path[:(-len(path.split(self.config.path_separator)[-1]))]
dirs = self.sftp_client.list_dirs_in_dir(dir_dir)
for dirname in dirs:
if fnmatch.fnmatch(dirname, path.split(self.config.path_separator)[-1]):
return self.sftp_client.is_dir(dir_dir + dirname)
return self.sftp_client.is_dir(path)

def is_file(self, path):
"""Calls :py:meth:`AbstractSFTPClient.is_file` with the given `path`.
:param str path: Path to check for file. Supports GLOB Patterns.
:returns: Boolean indicating is the file is present or not.
:rtype: bool
See :py:meth:`AbstractSFTPClient.is_file` for more documentation.
"""
if bool([ops for ops in '*?![' if(ops in path)]):
file_dir = path[:(-len(path.split(self.config.path_separator)[-1]))]
if file_dir == '':
return self.sftp_client.is_file(path)
files = self.sftp_client.list_files_in_dir(file_dir)
for filename in files:
if fnmatch.fnmatch(filename, path.split(self.config.path_separator)[-1]):
return self.sftp_client.is_file(file_dir + filename)
return self.sftp_client.is_file(path)

def _create_client(self, scp):
Expand Down
12 changes: 12 additions & 0 deletions src/SSHLibrary/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,8 +1833,11 @@ def _run_command(self, command, *args):
def file_should_exist(self, path):
"""Fails if the given ``path`` does NOT point to an existing file.
Supports wildcard expansions described in `glob patterns`.
Example:
| `File Should Exist` | /boot/initrd.img |
| `File Should Exist` | /boot/*.img |
Note that symlinks are followed:
| `File Should Exist` | /initrd.img | # Points to /boot/initrd.img |
Expand All @@ -1845,8 +1848,11 @@ def file_should_exist(self, path):
def file_should_not_exist(self, path):
"""Fails if the given ``path`` points to an existing file.
Supports wildcard expansions described in `glob patterns`.
Example:
| `File Should Not Exist` | /non/existing |
| `File Should Not Exist` | /non/* |
Note that this keyword follows symlinks. Thus the example fails if
``/non/existing`` is a link that points an existing file.
Expand All @@ -1857,8 +1863,11 @@ def file_should_not_exist(self, path):
def directory_should_exist(self, path):
"""Fails if the given ``path`` does not point to an existing directory.
Supports wildcard expansions described in `glob patterns`, but only on the current directory.
Example:
| `Directory Should Exist` | /usr/share/man |
| `Directory Should Exist` | /usr/share/* |
Note that symlinks are followed:
| `Directory Should Exist` | /usr/local/man | # Points to /usr/share/man/ |
Expand All @@ -1869,8 +1878,11 @@ def directory_should_exist(self, path):
def directory_should_not_exist(self, path):
"""Fails if the given ``path`` points to an existing directory.
Supports wildcard expansions described in `glob patterns`, but only on the current directory.
Example:
| `Directory Should Not Exist` | /non/existing |
| `Directory Should Not Exist` | /non/* |
Note that this keyword follows symlinks. Thus the example fails if
``/non/existing`` is a link that points to an existing directory.
Expand Down

0 comments on commit 17a46d2

Please sign in to comment.