1 package org.apache.rat.mp.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import static org.apache.rat.mp.util.ScmIgnoreParser.getExclusionsFromSCM;
20 import static org.apache.rat.mp.util.ignore.GlobIgnoreMatcher.isComment;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.BufferedWriter;
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.util.List;
31
32 import org.apache.commons.io.IOUtils;
33 import org.apache.maven.plugin.logging.Log;
34 import org.apache.rat.mp.util.ignore.GlobIgnoreMatcher;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.io.TempDir;
37 import org.mockito.Mockito;
38
39 public class ScmIgnoreParserTest {
40
41 @TempDir
42 private File tempDir;
43
44 private Log log = Mockito.mock(Log.class);
45
46 private static final String IGNORE_EXAMPLE = "**/*.java\r\n## Justus commentos\r\nignoredDirectory";
47
48 @Test
49 public void parseAsNoComments() {
50 assertFalse(isComment(null));
51 assertFalse(isComment(""));
52 assertFalse(isComment("This is a normal line"));
53 assertFalse(isComment("**/ignoreMe/*"));
54 assertFalse(isComment("C:\\No Space In FileNames Please"));
55 }
56
57 @Test
58 public void parseAsComments() {
59 assertTrue(isComment(" # comment that is"));
60 assertTrue(isComment("## comment that is"));
61 assertTrue(isComment("## comment that is ## "));
62 assertTrue(isComment(" // comment that is ## "));
63 assertTrue(isComment(" /** comment that is **/ "));
64 assertTrue(isComment(" /* comment that is */ "));
65 }
66
67 public List<String> getExcludesFromFile(final Log log, final File scmIgnore) {
68 return new GlobIgnoreMatcher(log, scmIgnore).getExclusionLines();
69 }
70
71 @Test
72 public void parseFromNonExistingFileOrDirectoryOrNull() {
73 assertTrue(getExcludesFromFile(log, new File("./mustNotExist-RAT-171")).isEmpty());
74 assertTrue(getExcludesFromFile(log, null).isEmpty());
75 assertTrue(getExcludesFromFile(log, new File(".")).isEmpty());
76 }
77
78 @Test
79 public void parseFromTargetDirectoryHopefullyWithoutSCMIgnores() {
80
81 assertTrue(getExclusionsFromSCM(log, new File("./target/classes")).isEmpty());
82 }
83
84 @Test
85 public void parseFromEmptyIgnoreFile() throws IOException {
86 File ignore = Files.createTempFile(tempDir.getName(), "sip").toFile();
87
88 assertTrue(ignore.exists());
89 writeToFile(IGNORE_EXAMPLE, ignore);
90
91 final List<String> excludes = getExcludesFromFile(log, ignore);
92 assertFalse(excludes.isEmpty());
93 assertEquals(2, excludes.size());
94 }
95
96 private static void writeToFile(String contents, File file) throws IOException {
97 BufferedWriter bw = null;
98 try {
99 FileWriter fw = new FileWriter(file.getAbsoluteFile());
100 bw = new BufferedWriter(fw);
101 bw.write(contents);
102 } finally {
103 IOUtils.closeQuietly(bw);
104 }
105 }
106
107
108 }