-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitignoreValidationTest.php
More file actions
160 lines (153 loc) · 4.45 KB
/
GitignoreValidationTest.php
File metadata and controls
160 lines (153 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Test Framework: PHPUnit
* Purpose: Validate critical .gitignore patterns are present and consistent.
*/
use PHPUnit\Framework\TestCase;
final class GitignoreValidationTest extends TestCase
{
private static $path = __DIR__ . '/../.gitignore';
private static $expected = [
// OS-specific
'.DS_Store',
'Thumbs.db',
'ehthumbs.db',
'Desktop.ini',
// Temp/log/backup
'*.log',
'*.tmp',
'*.swp',
'*.swo',
'*.bak',
'*.orig',
// Secrets
'.env',
'.env.*',
'*.key',
'*.pem',
// Build/cache
'dist/',
'build/',
'out/',
'tmp/',
'cache/',
'*.pyc',
'__pycache__/',
// IDE
'.vscode/',
'.history/',
'.idea/',
// Node
'node_modules/',
'npm-debug.log',
'yarn-error.log',
'*.lock',
// PHP
'vendor/',
'composer.phar',
'.phpunit.result.cache',
'phpunit.xml.dist',
'.php-cs-fixer.cache',
'.phpstan/',
'psalm.xml.dist',
'storage/',
'bootstrap/cache/',
'var/',
'*.cache',
'.env.backup',
'.env.local',
'.env.production',
'.env.testing',
'homestead.yaml',
'Homestead.yaml',
'Homestead.json',
// WordPress
'wp-content/uploads/',
'wp-content/cache/',
'wp-content/upgrade/',
'wp-content/debug.log',
// Coverage
'coverage/',
'*.lcov',
// Other binaries
'*.iml',
'*.class',
'*.o',
'*.out',
'*.so',
'*.dll',
'*.exe',
'*.pid',
];
private function readGitignore(): array
{
$this->assertFileExists(self::$path, ".gitignore must exist at repo root.");
$content = file_get_contents(self::$path);
$this->assertNotFalse($content, "Failed to read .gitignore");
// Normalize line endings and trim trailing spaces
$lines = preg_split('/\R/u', $content);
return array_map(static function ($l) {
return rtrim($l, " \t");
}, $lines);
}
public function test_expected_patterns_are_present(): void
{
$lines = $this->readGitignore();
$set = array_flip($lines);
foreach (self::$expected as $pattern) {
$this->assertArrayHasKey($pattern, $set, "Missing required .gitignore entry: {$pattern}");
}
}
public function test_no_trailing_whitespace_on_rule_lines(): void
{
$content = file_get_contents(self::$path);
$this->assertNotFalse($content);
$bad = [];
$i = 0;
foreach (preg_split('/\R/u', $content) as $line) {
$i++;
if ($line !== '' && $line[0] !== '#') {
if (preg_match('/[ \t]+$/', $line)) {
$bad[] = $i;
}
}
}
$this->assertCount(0, $bad, "Trailing whitespace detected on lines: " . implode(', ', $bad));
}
public function test_has_trailing_newline_at_eof(): void
{
$content = file_get_contents(self::$path);
$this->assertNotFalse($content);
$this->assertTrue(substr($content, -1) === "\n", "Expected .gitignore to end with a newline.");
}
public function test_no_duplicate_rules(): void
{
$lines = $this->readGitignore();
$counts = [];
foreach ($lines as $line) {
if ($line === '' || str_starts_with($line, '#')) continue;
$counts[$line] = ($counts[$line] ?? 0) + 1;
}
$dupes = array_keys(array_filter($counts, fn($c) => $c > 1));
$this->assertEmpty($dupes, "Duplicate .gitignore rules found: " . implode(', ', $dupes));
}
public function test_section_headers_exist(): void
{
$lines = $this->readGitignore();
$headers = [
'# OS依存ファイル',
'# 一時ファイル・ログ・バックアップ',
'# 環境変数・機密情報',
'# ビルド成果物・キャッシュ',
'# IDE / エディタ設定',
'# Node.js関連',
'# PHP関連設定',
'# カバレッジ関連',
'# その他',
];
$set = array_flip($lines);
foreach ($headers as $h) {
$this->assertArrayHasKey($h, $set, "Missing section header: {$h}");
}
}
}