diff --git a/pyinfra/facts/files.py b/pyinfra/facts/files.py index 41b19ae69..7d54e03d7 100644 --- a/pyinfra/facts/files.py +++ b/pyinfra/facts/files.py @@ -59,17 +59,14 @@ def _parse_mode(mode): class File(FactBase): - # Types must match FLAG_TO_TYPE in .util.files.py type = 'file' - test_flag = '-e' def command(self, path): return make_formatted_string_command(( - '! test {test_flag} {0} || ' # only stat if the file exists + '! (test -e {0} || test -L {0} ) || ' # only stat if the path exists (file or symlink) '( {linux_stat_command} {0} 2> /dev/null || {bsd_stat_command} {0} )' ), QuoteString(path), - test_flag=self.test_flag, linux_stat_command=LINUX_STAT_COMMAND, bsd_stat_command=BSD_STAT_COMMAND, ) @@ -118,7 +115,6 @@ def process(self, output): class Link(File): type = 'link' - test_flag = '-L' class Directory(File): diff --git a/tests/facts/files.Directory/file.json b/tests/facts/files.Directory/file.json index 4d1ecd7f3..14a83cc7e 100644 --- a/tests/facts/files.Directory/file.json +++ b/tests/facts/files.Directory/file.json @@ -1,6 +1,6 @@ { "arg": "/path/to/a/file", - "command": "! test -e /path/to/a/file || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /path/to/a/file 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /path/to/a/file )", + "command": "! (test -e /path/to/a/file || test -L /path/to/a/file ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /path/to/a/file 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /path/to/a/file )", "output": [ "user=pyinfra group=pyinfra mode=-rwxrwxrwx atime=1594804767 mtime=1594804767 ctime=0 size=8 '/path/to/a/file'" ], diff --git a/tests/facts/files.Directory/link.json b/tests/facts/files.Directory/link.json index 124269932..838978fae 100644 --- a/tests/facts/files.Directory/link.json +++ b/tests/facts/files.Directory/link.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/mylink", - "command": "! test -e /home/pyinfra/mylink || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )", + "command": "! (test -e /home/pyinfra/mylink || test -L /home/pyinfra/mylink ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )", "output": [ "user=root group=root mode=lrwxrwxrwx atime=1594804774 mtime=1594804770 ctime=0 size=6 '/home/pyinfra/mylink' -> 'file.txt'" ], diff --git a/tests/facts/files.Directory/valid.json b/tests/facts/files.Directory/valid.json index b0d88dbb3..c45ce02ef 100644 --- a/tests/facts/files.Directory/valid.json +++ b/tests/facts/files.Directory/valid.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/myd@-_ir", - "command": "! test -e /home/pyinfra/myd@-_ir || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )", + "command": "! (test -e /home/pyinfra/myd@-_ir || test -L /home/pyinfra/myd@-_ir ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )", "output": [ "user=pyinfra group=pyinfra mode=drw-r--r-- atime=1594804583 mtime=1594804583 ctime=0 size=0 '/home/pyinfra/myd@-_ir'" ], diff --git a/tests/facts/files.File/directory.json b/tests/facts/files.File/directory.json index fb2ba8e42..e3923a22f 100644 --- a/tests/facts/files.File/directory.json +++ b/tests/facts/files.File/directory.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra", - "command": "! test -e /home/pyinfra || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra )", + "command": "! (test -e /home/pyinfra || test -L /home/pyinfra ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra )", "output": [ "user=root group=root mode=drw-r--r-- atime=1594804583 mtime=1594804583 ctime=0 size=0 '/home/pyinfra'" ], diff --git a/tests/facts/files.File/invalid_output.json b/tests/facts/files.File/invalid_output.json index 48f3577bc..9037bbb46 100644 --- a/tests/facts/files.File/invalid_output.json +++ b/tests/facts/files.File/invalid_output.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/fil-@_e.txt", - "command": "! test -e /home/pyinfra/fil-@_e.txt || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )", + "command": "! (test -e /home/pyinfra/fil-@_e.txt || test -L /home/pyinfra/fil-@_e.txt ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )", "output": [ "not-gonna-match" ], diff --git a/tests/facts/files.File/link.json b/tests/facts/files.File/link.json index 124269932..838978fae 100644 --- a/tests/facts/files.File/link.json +++ b/tests/facts/files.File/link.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/mylink", - "command": "! test -e /home/pyinfra/mylink || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )", + "command": "! (test -e /home/pyinfra/mylink || test -L /home/pyinfra/mylink ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )", "output": [ "user=root group=root mode=lrwxrwxrwx atime=1594804774 mtime=1594804770 ctime=0 size=6 '/home/pyinfra/mylink' -> 'file.txt'" ], diff --git a/tests/facts/files.File/valid.json b/tests/facts/files.File/valid.json index adbcf899f..f790b9c6c 100644 --- a/tests/facts/files.File/valid.json +++ b/tests/facts/files.File/valid.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/fil-@_e.txt", - "command": "! test -e /home/pyinfra/fil-@_e.txt || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )", + "command": "! (test -e /home/pyinfra/fil-@_e.txt || test -L /home/pyinfra/fil-@_e.txt ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )", "output": [ "user=pyinfra group=domain users mode=-rwxrwx--- atime=1594804767 mtime=1594804767 ctime=0 size=8 '/home/pyinfra/fil-@_e.txt'" ], diff --git a/tests/facts/files.File/valid_needs_quotes.json b/tests/facts/files.File/valid_needs_quotes.json index 535546b91..1d3eab6c4 100644 --- a/tests/facts/files.File/valid_needs_quotes.json +++ b/tests/facts/files.File/valid_needs_quotes.json @@ -1,6 +1,6 @@ { "arg": "fil () &&-@_e.txt", - "command": "! test -e 'fil () &&-@_e.txt' || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' 'fil () &&-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' 'fil () &&-@_e.txt' )", + "command": "! (test -e 'fil () &&-@_e.txt' || test -L 'fil () &&-@_e.txt' ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' 'fil () &&-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' 'fil () &&-@_e.txt' )", "output": [ "user=pyinfra group=domain users mode=-rwxrwx--- atime=1594804767 mtime=1594804767 ctime=0 size=8 'fil () &&-@_e.txt'" ], diff --git a/tests/facts/files.File/valid_with_space.json b/tests/facts/files.File/valid_with_space.json index 2fa7209b0..e4428c3e8 100644 --- a/tests/facts/files.File/valid_with_space.json +++ b/tests/facts/files.File/valid_with_space.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra dir/fil-@_e.txt", - "command": "! test -e '/home/pyinfra dir/fil-@_e.txt' || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' '/home/pyinfra dir/fil-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' '/home/pyinfra dir/fil-@_e.txt' )", + "command": "! (test -e '/home/pyinfra dir/fil-@_e.txt' || test -L '/home/pyinfra dir/fil-@_e.txt' ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' '/home/pyinfra dir/fil-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' '/home/pyinfra dir/fil-@_e.txt' )", "output": [ "user=pyinfra group=pyinfra mode=-rw-r--r-- atime=1594804348 mtime=1594804348 ctime=0 size=0 '/home/pyinfra dir/fil-@_e.txt'" ], diff --git a/tests/facts/files.Link/directory.json b/tests/facts/files.Link/directory.json index d172b7d7c..8650a31e4 100644 --- a/tests/facts/files.Link/directory.json +++ b/tests/facts/files.Link/directory.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/myd@-_ir", - "command": "! test -L /home/pyinfra/myd@-_ir || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )", + "command": "! (test -e /home/pyinfra/myd@-_ir || test -L /home/pyinfra/myd@-_ir ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )", "output": [ "user=root group=root mode=drw-r--r-- atime=1594804583 mtime=1594804583 ctime=0 size=0 '/home/pyinfra/myd@-_ir'" ], diff --git a/tests/facts/files.Link/file.json b/tests/facts/files.Link/file.json index f2f559d40..9ca8936fa 100644 --- a/tests/facts/files.Link/file.json +++ b/tests/facts/files.Link/file.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/fil-@_e.txt", - "command": "! test -L /home/pyinfra/fil-@_e.txt || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )", + "command": "! (test -e /home/pyinfra/fil-@_e.txt || test -L /home/pyinfra/fil-@_e.txt ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )", "output": [ "user=pyinfra group=pyinfra mode=-rwxrwxrwx atime=1594804767 mtime=1594804767 ctime=0 size=8 '/home/pyinfra/fil-@_e.txt'" ], diff --git a/tests/facts/files.Link/valid.json b/tests/facts/files.Link/valid.json index eff4b2c01..fb5069703 100644 --- a/tests/facts/files.Link/valid.json +++ b/tests/facts/files.Link/valid.json @@ -1,6 +1,6 @@ { "arg": "/home/pyinfra/my@-_link", - "command": "! test -L /home/pyinfra/my@-_link || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/my@-_link 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/my@-_link )", + "command": "! (test -e /home/pyinfra/my@-_link || test -L /home/pyinfra/my@-_link ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/my@-_link 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/my@-_link )", "output": [ "user=pyinfra group=pyinfra mode=lrwxrwxrwx atime=1594804774 mtime=1594804770 ctime=0 size=6 '/home/pyinfra/my@-_link' -> 'my_f@-ile.txt'" ],