git(['ls-files', '*.map']); $this->assertSame( [], $tracked, "소스맵이 git 에 추적되고 있습니다. 원본 코드가 배포판에 노출됩니다:\n ". implode("\n ", $tracked) ); } /** * .gitignore 가 소스맵을 실제로 무시하는지 테스트 * * 규칙이 존재해도 다른 규칙에 의해 무력화될 수 있으므로 git 에 직접 판정시킨다. */ public function test_gitignore_actually_ignores_sourcemap_files(): void { $probe = 'public/build/core/__sourcemap_guard_probe__.js.map'; $result = $this->git(['check-ignore', $probe]); $this->assertContains( $probe, $result, '.gitignore 가 .map 을 무시하지 않습니다. 빌드 산출 소스맵이 저장소에 유입될 수 있습니다.' ); } /** * 추적 중인 빌드 산출 JS 에 sourceMappingURL 주석이 없는지 테스트 * * 주석만 남고 .map 이 없으면 브라우저가 404 를 일으키고, 주석과 .map 이 함께 * 있으면 원본이 노출된다. 배포 산출물에는 둘 다 없어야 한다. * * 판정 대상은 워킹트리(= 지금 배포하면 나갈 상태)다. HEAD(직전 커밋)를 보면 * 워킹트리에서 이미 고친 내용을 반영하지 못한다. */ public function test_tracked_build_output_has_no_source_mapping_url(): void { $offenders = $this->git([ 'grep', '--files-with-matches', 'sourceMappingURL', '--', 'public/build/core/*.js', 'modules/_bundled/*/dist/js/*.js', 'plugins/_bundled/*/dist/js/*.js', 'templates/_bundled/*/dist/js/*.js', ]); $this->assertSame( [], $offenders, "배포 산출 JS 에 소스맵 참조 주석이 남아 있습니다(--production 재빌드 필요):\n ". implode("\n ", $offenders) ); } /** * git 명령을 실행하고 출력 라인 배열을 반환합니다. * * @param array $args git 인자 * @return array 출력 라인 (빈 줄 제외) */ private function git(array $args): array { $process = new Process(array_merge(['git'], $args), base_path()); $process->run(); $lines = preg_split('/\R/', trim($process->getOutput())) ?: []; return array_values(array_filter($lines, fn (string $line): bool => $line !== '')); } }