GitignoreTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Finder\Gitignore;
  13. class GitignoreTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider provider
  17. *
  18. * @param string $patterns
  19. * @param array $matchingCases
  20. * @param array $nonMatchingCases
  21. */
  22. public function testCases(string $patterns, array $matchingCases, array $nonMatchingCases)
  23. {
  24. $regex = Gitignore::toRegex($patterns);
  25. foreach ($matchingCases as $matchingCase) {
  26. $this->assertRegExp($regex, $matchingCase, sprintf('Failed asserting path [%s] matches gitignore patterns [%s] using regex [%s]', $matchingCase, $patterns, $regex));
  27. }
  28. foreach ($nonMatchingCases as $nonMatchingCase) {
  29. $this->assertNotRegExp($regex, $nonMatchingCase, sprintf('Failed asserting path [%s] not matching gitignore patterns [%s] using regex [%s]', $nonMatchingCase, $patterns, $regex));
  30. }
  31. }
  32. /**
  33. * @return array return is array of
  34. * [
  35. * [
  36. * '', // Git-ignore Pattern
  37. * [], // array of file paths matching
  38. * [], // array of file paths not matching
  39. * ],
  40. * ]
  41. */
  42. public function provider()
  43. {
  44. return [
  45. [
  46. '
  47. *
  48. !/bin/bash
  49. ',
  50. ['bin/cat', 'abc/bin/cat'],
  51. ['bin/bash'],
  52. ],
  53. [
  54. 'fi#le.txt',
  55. [],
  56. ['#file.txt'],
  57. ],
  58. [
  59. '
  60. /bin/
  61. /usr/local/
  62. !/bin/bash
  63. !/usr/local/bin/bash
  64. ',
  65. ['bin/cat'],
  66. ['bin/bash'],
  67. ],
  68. [
  69. '*.py[co]',
  70. ['file.pyc', 'file.pyc'],
  71. ['filexpyc', 'file.pycx', 'file.py'],
  72. ],
  73. [
  74. 'dir1/**/dir2/',
  75. ['dir1/dirA/dir2/', 'dir1/dirA/dirB/dir2/'],
  76. [],
  77. ],
  78. [
  79. 'dir1/*/dir2/',
  80. ['dir1/dirA/dir2/'],
  81. ['dir1/dirA/dirB/dir2/'],
  82. ],
  83. [
  84. '/*.php',
  85. ['file.php'],
  86. ['app/file.php'],
  87. ],
  88. [
  89. '\#file.txt',
  90. ['#file.txt'],
  91. [],
  92. ],
  93. [
  94. '*.php',
  95. ['app/file.php', 'file.php'],
  96. ['file.phps', 'file.phps', 'filephps'],
  97. ],
  98. [
  99. 'app/cache/',
  100. ['app/cache/file.txt', 'app/cache/dir1/dir2/file.txt', 'a/app/cache/file.txt'],
  101. [],
  102. ],
  103. [
  104. '
  105. #IamComment
  106. /app/cache/',
  107. ['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
  108. ['a/app/cache/file.txt'],
  109. ],
  110. ];
  111. }
  112. }