From f8065ea3a2ac4649cfd62d38056a5a57e1a5b07d Mon Sep 17 00:00:00 2001
From: Frederik Hennig <frederik.hennig@fau.de>
Date: Fri, 8 Dec 2023 17:20:34 +0100
Subject: [PATCH] moved clang-format binary check to post-init of config

---
 src/pystencilssfg/configuration.py         | 9 +++++++++
 src/pystencilssfg/emission/clang_format.py | 7 ++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/pystencilssfg/configuration.py b/src/pystencilssfg/configuration.py
index a7bced4..dc0aa21 100644
--- a/src/pystencilssfg/configuration.py
+++ b/src/pystencilssfg/configuration.py
@@ -85,6 +85,15 @@ class SfgCodeStyle:
         prefix = " " * self.indent_width
         return indent(s, prefix)
 
+    def __post__init__(self):
+        if self.force_clang_format:
+            import shutil
+
+            if not shutil.which(self.clang_format_binary):
+                raise SfgException(
+                    "`force_clang_format` set to true in code style, but clang-format binary not found."
+                )
+
 
 @dataclass
 class SfgOutputSpec:
diff --git a/src/pystencilssfg/emission/clang_format.py b/src/pystencilssfg/emission/clang_format.py
index 309908f..e663fec 100644
--- a/src/pystencilssfg/emission/clang_format.py
+++ b/src/pystencilssfg/emission/clang_format.py
@@ -8,11 +8,8 @@ from ..exceptions import SfgException
 def invoke_clang_format(code: str, codestyle: SfgCodeStyle) -> str:
     args = [codestyle.clang_format_binary, f"--style={codestyle.code_style}"]
 
-    if not shutil.which("clang-format"):
-        if codestyle.force_clang_format:
-            raise SfgException("Could not find clang-format binary.")
-        else:
-            return code
+    if not shutil.which(codestyle.clang_format_binary):
+        return code
 
     result = subprocess.run(args, input=code, capture_output=True, text=True)
 
-- 
GitLab