-
-
Notifications
You must be signed in to change notification settings - Fork 402
Expand file tree
/
Copy pathGitAPIBadInitTest.java
More file actions
62 lines (52 loc) · 2.07 KB
/
GitAPIBadInitTest.java
File metadata and controls
62 lines (52 loc) · 2.07 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
package hudson.plugins.git;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import hudson.EnvVars;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class GitAPIBadInitTest {
@TempDir
private File tempFolder;
private final EnvVars env = new EnvVars();
private File tempDir;
private TaskListener listener;
@BeforeEach
void setUp() throws Exception {
tempDir = newFolder(tempFolder, "junit");
listener = StreamTaskListener.fromStderr();
}
@Test
void testInitExistingDirectory() throws Exception {
GitClient git = new GitAPI("git", tempDir, listener, env);
git.init();
File gitDir = new File(tempDir, ".git");
assertTrue(gitDir.exists(), gitDir + " not created");
assertTrue(gitDir.isDirectory(), gitDir + " not a directory");
}
@Test
void testInitExistingFile() throws Exception {
File existingFile = new File(tempDir, "file-exists");
Files.writeString(existingFile.toPath(), "git init should fail due to this file", StandardCharsets.UTF_8);
GitClient git = new GitAPI("git", existingFile, listener, env);
GitException e = assertThrows(GitException.class, git::init);
assertThat(e.getMessage(), is("Could not init " + existingFile.getAbsolutePath()));
}
private static File newFolder(File root, String... subDirs) throws Exception {
String subFolder = String.join("/", subDirs);
File result = new File(root, subFolder);
if (!result.mkdirs()) {
throw new IOException("Couldn't create folders " + result);
}
return result;
}
}