Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format generated code only when clang-format is available #101

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/unifex/interface_IO.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ defmodule Unifex.InterfaceIO do
File.write!("#{out_base_path}.c", source)
File.write!("#{out_base_path}.cpp", source)

Mix.shell().cmd(
"clang-format -style=\"{BasedOnStyle: llvm, IndentWidth: 2}\" -i " <>
"#{out_base_path}.h #{out_base_path}.c #{out_base_path}.cpp"
)
# Format generated code only when clang-format is available
if Unifex.Utils.clang_format_installed?() do
System.cmd(
"clang-format",
[
"-style={BasedOnStyle: llvm, IndentWidth: 2}",
"-i",
"#{out_base_path}.h",
"#{out_base_path}.c",
"#{out_base_path}.cpp"
]
)
end

:ok
end
Expand Down
6 changes: 6 additions & 0 deletions lib/unifex/utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Unifex.Utils do
@moduledoc false

@spec clang_format_installed?() :: boolean()
def clang_format_installed?(), do: System.find_executable("clang-format") != nil
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Unifex.MixProject do
use Mix.Project

@version "1.0.1"
@version "1.0.2"
@github_url "https://github.com/membraneframework/unifex"

def project do
Expand Down
8 changes: 7 additions & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
ExUnit.start()
# clang-format is required to format the generated code
# it won't match the reference files otherwise
if Unifex.Utils.clang_format_installed?() do
ExUnit.start()
else
raise "clang-format has to be installed on the system to run Unifex tests."
end
4 changes: 0 additions & 4 deletions test/unifex/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ defmodule Unifex.IntegrationTest do
defp do_test_project(project, interface, language) do
run_projects_tests(project, language)

# clang-format is required to format the generated code
# it won't match the reference files otherwise
assert {_output, 0} = System.cmd("clang-format", ~w(--version))
mat-hek marked this conversation as resolved.
Show resolved Hide resolved

test_common(project)
test_particular(project, interface)
end
Expand Down