|
| 1 | +defmodule Phoenix.SourceFilesTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + @repo_root Path.expand("../..", __DIR__) |
| 5 | + @git_repo? File.dir?(Path.join(@repo_root, ".git")) and System.find_executable("git") != nil |
| 6 | + @excluded_exts ~w(.br .foo .gz .ico .lock .map .pem .png) |
| 7 | + @excluded_files ~w(test/fixtures/hello.txt) |
| 8 | + |
| 9 | + @tag skip: if(not @git_repo?, do: "git or .git repository not available") |
| 10 | + test "all tracked text files in the repository end with a single newline" do |
| 11 | + {output, 0} = System.cmd("git", ["ls-files", "-z"], cd: @repo_root) |
| 12 | + |
| 13 | + target_files = |
| 14 | + output |
| 15 | + |> String.split("\0", trim: true) |
| 16 | + |> Enum.reject(fn file -> |
| 17 | + file in @excluded_files or Enum.any?(@excluded_exts, &String.ends_with?(file, &1)) |
| 18 | + end) |
| 19 | + |> Enum.map(&Path.expand(&1, @repo_root)) |
| 20 | + |> Enum.filter(&File.regular?/1) |
| 21 | + |
| 22 | + assert target_files != [], "No tracked files found in the repository" |
| 23 | + |
| 24 | + offending = |
| 25 | + target_files |
| 26 | + |> Enum.reject(fn file -> |
| 27 | + content = File.read!(file) |
| 28 | + content == "" or content =~ ~r/\S\n\z/ |
| 29 | + end) |
| 30 | + |> Enum.map(&Path.relative_to(&1, @repo_root)) |
| 31 | + |
| 32 | + assert offending == [], |
| 33 | + "Expected the following files to end with a single trailing newline:\n\n" <> |
| 34 | + Enum.map_join(offending, "\n", &" - #{&1}") |
| 35 | + end |
| 36 | +end |
0 commit comments