1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.git;
20
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.IOException;
24
25 import org.apache.maven.scm.PlexusJUnit4TestSupport;
26 import org.codehaus.plexus.util.FileUtils;
27 import org.codehaus.plexus.util.cli.CommandLineException;
28 import org.junit.Assert;
29
30
31
32
33
34 public final class GitScmTestUtils {
35
36 public static final String GIT_COMMAND_LINE = "git";
37
38 private GitScmTestUtils() {}
39
40 public static void initRepo(File repository, File workingDirectory, File assertionDirectory) throws IOException {
41 initRepo("src/test/repository/", repository, workingDirectory);
42
43 FileUtils.deleteDirectory(assertionDirectory);
44
45 Assert.assertTrue(assertionDirectory.mkdirs());
46 }
47
48 public static void initRepo(String source, File repository, File workingDirectory) throws IOException {
49
50 File src = PlexusJUnit4TestSupport.getTestFile(source);
51
52 FileUtils.deleteDirectory(repository);
53
54 Assert.assertTrue(repository.mkdirs());
55
56 FileUtils.copyDirectoryStructure(src, repository);
57
58 File dotGitDirectory = new File(src, "dotgit");
59
60 if (dotGitDirectory.exists()) {
61 FileUtils.copyDirectoryStructure(dotGitDirectory, new File(repository, ".git"));
62 }
63
64 FileUtils.deleteDirectory(workingDirectory);
65
66 Assert.assertTrue(workingDirectory.mkdirs());
67 }
68
69 public static String getScmUrl(File repositoryRootFile, String provider) throws CommandLineException {
70 return "scm:" + provider + ":"
71 + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
72 }
73
74 public static void deleteAllDirectories(File startDirectory, String pattern) throws IOException {
75 if (startDirectory.isDirectory()) {
76 File[] childs = startDirectory.listFiles();
77 for (int i = 0; i < childs.length; i++) {
78 File child = childs[i];
79 if (child.isDirectory()) {
80 if (child.getName().equals(pattern)) {
81 FileUtils.deleteDirectory(child);
82 } else {
83 deleteAllDirectories(child, pattern);
84 }
85 }
86 }
87 }
88 }
89
90 public static void setDefaultUser(File repositoryRootFile) {
91 File gitConfigFile = new File(new File(repositoryRootFile, ".git"), "config");
92
93 FileWriter fw = null;
94 try {
95 fw = new FileWriter(gitConfigFile, true);
96 fw.append("[user]\n");
97 fw.append("\tname = John Doe\n");
98 fw.append("\temail = john.doe@nowhere.com\n");
99 fw.flush();
100 fw.close();
101 } catch (IOException e) {
102 System.err.println("cannot setup a default user for tests purpose inside " + gitConfigFile);
103 e.printStackTrace();
104 } finally {
105 if (fw != null) {
106 try {
107 fw.close();
108 } catch (IOException ignore) {
109
110 }
111 }
112 }
113 }
114 }