From 7c479c00821f5637e9e8894e46f2644ab2f76bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Tosser?= Date: Thu, 25 Sep 2025 15:35:50 +0200 Subject: [PATCH 1/3] Fix file content check to handle missing files --- .claude/settings.local.json | 12 ++++++++++++ src/pyinfra/facts/files.py | 10 ++++++---- tests/facts/files.FileContents/file.json | 2 +- tests/facts/files.FileContents/no_file.json | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 .claude/settings.local.json diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 000000000..6e091ca64 --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,12 @@ +{ + "permissions": { + "allow": [ + "WebFetch(domain:github.com)", + "Bash(python -m pytest tests/ -k \"FileContents\" -v)", + "Bash(uv sync:*)", + "Bash(uv run:*)" + ], + "deny": [], + "ask": [] + } +} \ No newline at end of file diff --git a/src/pyinfra/facts/files.py b/src/pyinfra/facts/files.py index f58648c7a..f4da7d568 100644 --- a/src/pyinfra/facts/files.py +++ b/src/pyinfra/facts/files.py @@ -398,7 +398,7 @@ def process(self, output) -> Optional[str]: class Sha1File(HashFileFactBase, digits=40, cmds=["sha1sum", "shasum", "sha1"]): """ Returns a SHA1 hash of a file. Works with both sha1sum and sha1. Returns - ``None`` if the file doest not exist. + ``None`` if the file does not exist. """ @@ -664,14 +664,16 @@ def process(self, output): class FileContents(FactBase): """ - Returns the contents of a file as a list of lines. Works with both sha1sum and sha1. Returns - ``None`` if the file doest not exist. + Returns the contents of a file as a list of lines. Returns + ``None`` if the file does not exist. """ @override def command(self, path): - return make_formatted_string_command("cat {0}", QuoteString(path)) + return make_formatted_string_command("test -e {0} && cat {0} || true", QuoteString(path)) @override def process(self, output): + if not output: + return None return output diff --git a/tests/facts/files.FileContents/file.json b/tests/facts/files.FileContents/file.json index 8b71de1c1..8f4c1a988 100644 --- a/tests/facts/files.FileContents/file.json +++ b/tests/facts/files.FileContents/file.json @@ -1,6 +1,6 @@ { "arg": "myfile", - "command": "cat myfile", + "command": "test -e myfile && cat myfile || true", "output": ["line1", "line2"], "fact": ["line1", "line2"] } diff --git a/tests/facts/files.FileContents/no_file.json b/tests/facts/files.FileContents/no_file.json index cab556293..97cdf593d 100644 --- a/tests/facts/files.FileContents/no_file.json +++ b/tests/facts/files.FileContents/no_file.json @@ -1,6 +1,6 @@ { "arg": ["test"], - "command": "cat test", - "output": null, + "command": "test -e test && cat test || true", + "output": [], "fact": null } From 7b3ece88025c58a14fd65cab53545c0818087d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Tosser?= Date: Fri, 26 Sep 2025 09:18:16 +0200 Subject: [PATCH 2/3] remove return None --- src/pyinfra/facts/files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyinfra/facts/files.py b/src/pyinfra/facts/files.py index f4da7d568..058127a8e 100644 --- a/src/pyinfra/facts/files.py +++ b/src/pyinfra/facts/files.py @@ -675,5 +675,5 @@ def command(self, path): @override def process(self, output): if not output: - return None + return self.default() return output From 8e6b8cf324cfe5814ae56c1d8b19c91825aa3294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Tosser?= Date: Fri, 26 Sep 2025 09:38:15 +0200 Subject: [PATCH 3/3] remove claude code file --- .claude/settings.local.json | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 .claude/settings.local.json diff --git a/.claude/settings.local.json b/.claude/settings.local.json deleted file mode 100644 index 6e091ca64..000000000 --- a/.claude/settings.local.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "permissions": { - "allow": [ - "WebFetch(domain:github.com)", - "Bash(python -m pytest tests/ -k \"FileContents\" -v)", - "Bash(uv sync:*)", - "Bash(uv run:*)" - ], - "deny": [], - "ask": [] - } -} \ No newline at end of file