From 97bb3c0ce1d98b818b0b2db1ee2071c61d765f65 Mon Sep 17 00:00:00 2001 From: John Pfuntner Date: Tue, 19 Dec 2023 11:09:42 -0500 Subject: [PATCH] Teaching GNUFile methods to follow symlinks On Linux systems, if File.mode is used on a file that's a symlink, it will return 0x777 because that's the "mode" of the symlink but mode is pretty meaningless for a symlink. By doing `stat -L ...` the command will automatically resolve symlinks. --- testinfra/modules/file.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/testinfra/modules/file.py b/testinfra/modules/file.py index 25354851..13307f49 100644 --- a/testinfra/modules/file.py +++ b/testinfra/modules/file.py @@ -223,38 +223,38 @@ def get_module_class(cls, host): class GNUFile(File): @property def user(self): - return self.check_output("stat -c %%U %s", self.path) + return self.check_output("stat -Lc %%U %s", self.path) @property def uid(self): - return int(self.check_output("stat -c %%u %s", self.path)) + return int(self.check_output("stat -Lc %%u %s", self.path)) @property def group(self): - return self.check_output("stat -c %%G %s", self.path) + return self.check_output("stat -Lc %%G %s", self.path) @property def gid(self): - return int(self.check_output("stat -c %%g %s", self.path)) + return int(self.check_output("stat -Lc %%g %s", self.path)) @property def mode(self): # Supply a base of 8 when parsing an octal integer # e.g. int('644', 8) -> 420 - return int(self.check_output("stat -c %%a %s", self.path), 8) + return int(self.check_output("stat -Lc %%a %s", self.path), 8) @property def mtime(self): - ts = self.check_output("stat -c %%Y %s", self.path) + ts = self.check_output("stat -Lc %%Y %s", self.path) return datetime.datetime.fromtimestamp(float(ts)) @property def size(self): - return int(self.check_output("stat -c %%s %s", self.path)) + return int(self.check_output("stat -Lc %%s %s", self.path)) @property def inode(self): - return int(self.check_output("stat -c %%i %s", self.path)) + return int(self.check_output("stat -Lc %%i %s", self.path)) @property def md5sum(self):