2.1 KiB · text History 34b2c06
defmodule GitGud.Workflows.GlobTest do
  @moduledoc """
  `paths:` patterns follow GitHub's cheat sheet, not shell globbing and not
  the looser matcher `branches:` uses: a workflow copied from GitHub's docs
  has to claim the same files here.
  """
  use ExUnit.Case, async: true

  alias GitGud.Workflows.Glob

  doctest Glob

  defp m?(pattern, path), do: Glob.match?(pattern, path)

  test "* stays within one directory" do
    assert m?("*.md", "a.md")
    refute m?("*.md", "docs/a.md")
  end

  test "**/ matches any depth, including none" do
    assert m?("**/*.md", "a.md")
    assert m?("**/*.md", "docs/a.md")
    assert m?("**/*.md", "docs/deep/a.md")
    refute m?("**/*.md", "docs/a.txt")
  end

  test "a trailing /** matches the files under the directory, not the directory" do
    assert m?("docs/**", "docs/a.txt")
    assert m?("docs/**", "docs/a/b.txt")
    refute m?("docs/**", "docs")
    refute m?("docs/**", "other/docs/a.txt")
  end

  test "a directory prefix is a whole path segment" do
    assert m?("android/**", "android/app/build.gradle")
    refute m?("android/**", "androidx/a")
  end

  test "** with no slash crosses directories" do
    assert m?("**.js", "a.js")
    assert m?("**.js", "a/b/c.js")
    assert m?("**", "anything/at/all")
  end

  test "? is one character, never a slash" do
    assert m?("a?c", "abc")
    refute m?("a?c", "ac")
    refute m?("a?c", "a/c")
  end

  test "character classes and ranges" do
    assert m?("[ab].txt", "a.txt")
    assert m?("[ab].txt", "b.txt")
    refute m?("[ab].txt", "c.txt")
    assert m?("v[0-9]", "v7")
    refute m?("v[0-9]", "vx")
  end

  test "an unclosed bracket is literal" do
    assert m?("a[b", "a[b")
  end

  test "dots and other regex characters are literal" do
    assert m?("mix.exs", "mix.exs")
    refute m?("mix.exs", "mixxexs")
    assert m?("a+b(c).txt", "a+b(c).txt")
  end

  test "patterns are anchored at both ends" do
    refute m?("lib", "lib/a.ex")
    refute m?("a.ex", "lib/a.ex")
  end

  test "a compiled pattern can be reused" do
    regex = Glob.compile("lib/**")
    assert Glob.match?(regex, "lib/a.ex")
    refute Glob.match?(regex, "test/a.exs")
  end
end