Claudenw commented on code in PR #433: URL: https://github.com/apache/creadur-rat/pull/433#discussion_r2072429586
########## apache-rat-core/src/test/java/org/apache/rat/config/exclusion/fileProcessors/GitIgnoreBuilderTest.java: ########## @@ -125,13 +135,50 @@ public void test_RAT_335() { DocumentName candidate = DocumentName.builder() .setName("/home/claude/apache/creadur-rat/apache-rat-core/target/test-classes/GitIgnoreBuilderTest/src/dir1/file1.log") .setBaseName("home/claude/apache/creadur-rat/apache-rat-core/target/test-classes/GitIgnoreBuilderTest/src/").build(); - System.out.println("Decomposition for "+candidate); + System.out.println("Decomposition for " + candidate); - assertThat(matcher.toString()).isEqualTo("matcherSet(or('included dir1/.gitignore', 'included .gitignore'), or('excluded dir1/.gitignore', **/.gitignore, 'excluded .gitignore'))"); + assertThat(matcher.toString()).isEqualTo("matcherSet(or('included .gitignore', 'included .gitignore'), or('excluded .gitignore', **/.gitignore, 'excluded .gitignore'))"); List<String> notMatching = Arrays.asList("README.txt", "dir1/dir1.md", "dir2/dir2.txt", "dir3/file3.log", "dir1/file1.log"); - List<String> matching = Arrays.asList(".gitignore", "root.md", "dir1/.gitignore", "dir1/dir1.txt", "dir2/dir2.md", "dir3/dir3.log"); + List<String> matching = Arrays.asList(".gitignore", "root.md", "dir1/.gitignore", "dir1/dir1.txt", "dir2/dir2.md", "dir3/dir3.log"); + + assertCorrect(matcherSets, documentName.getBaseDocumentName(), matching, notMatching); + } + + /** + * Test that exclusions from a global gitignore are also applied + * + * https://issues.apache.org/jira/browse/RAT-473 + */ + @Test + public void test_global_gitignore() { + URL globalGitIgnoreUrl = GitIgnoreBuilderTest.class.getClassLoader().getResource("GitIgnoreBuilderTest/global-gitignore"); + String globalGitIgnore = globalGitIgnoreUrl.getFile(); + + GitIgnoreBuilder underTest = new GitIgnoreBuilder() { + @Override + protected String globalGitIgnore() { + return globalGitIgnore; + } + }; + URL url = GitIgnoreBuilderTest.class.getClassLoader().getResource("GitIgnoreBuilderTest/src/"); + File file = new File(url.getFile()); + + DocumentName documentName = DocumentName.builder(file).build(); + List<MatcherSet> matcherSets = underTest.build(documentName); + DocumentNameMatcher matcher = MatcherSet.merge(matcherSets).createMatcher(); + + DocumentName candidate = DocumentName.builder() + .setName("/home/claude/apache/creadur-rat/apache-rat-core/target/test-classes/GitIgnoreBuilderTest/src/dir1/file1.log") + .setBaseName("home/claude/apache/creadur-rat/apache-rat-core/target/test-classes/GitIgnoreBuilderTest/src/").build(); + System.out.println("Decomposition for " + candidate); + + assertThat(matcher.toString()).isEqualTo("matcherSet(or('included .gitignore', 'included .gitignore'), or('excluded .gitignore', **/.gitignore, 'excluded .gitignore', 'excluded " + globalGitIgnore.substring(1) + "'))"); Review Comment: Create a document name from the gitignore file name: ``` DocumentName globalIgnoreName = DocumentName.builder(new File(globalGitIgnoreUrl.getFile())).build(); ``` Then use `globalIgnoreName.localized("/").substring(1)` When dealing with Windows/Linux file name differences `DocumentName` is your friend. ** UPDATE ** In looking at the test case failure you may want to use `globalIgnoreName.localized("/")` and not call the substring. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@creadur.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org