-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSparseCheckoutPathsTest.java
More file actions
215 lines (179 loc) · 7.73 KB
/
SparseCheckoutPathsTest.java
File metadata and controls
215 lines (179 loc) · 7.73 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* The MIT License
*
* Copyright 2020 Mark Waite.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.git.extensions.impl;
import hudson.EnvVars;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import hudson.model.Run;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.util.LogTaskListener;
import org.jenkinsci.plugins.gitclient.CheckoutCommand;
import org.jenkinsci.plugins.gitclient.CloneCommand;
import org.jenkinsci.plugins.gitclient.GitClient;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class SparseCheckoutPathsTest {
private final SparseCheckoutPaths emptySparseCheckoutPaths;
private final List<SparseCheckoutPath> emptySparseCheckoutPathList;
private final SparseCheckoutPaths sparseCheckoutPaths;
private final List<SparseCheckoutPath> sparseCheckoutPathList;
private static final String SRC_DIR_NAME = "src";
private static final SparseCheckoutPath SRC_SPARSE_CHECKOUT_PATH = new SparseCheckoutPath(SRC_DIR_NAME);
private LogTaskListener listener;
private LogHandler handler;
private int logCount = 0;
public SparseCheckoutPathsTest() {
emptySparseCheckoutPathList = new ArrayList<>();
emptySparseCheckoutPaths = new SparseCheckoutPaths(emptySparseCheckoutPathList);
sparseCheckoutPathList = new ArrayList<>();
sparseCheckoutPathList.add(SRC_SPARSE_CHECKOUT_PATH);
sparseCheckoutPaths = new SparseCheckoutPaths(sparseCheckoutPathList);
listener = null;
handler = null;
}
@Before
public void createLogger() {
Logger logger = Logger.getLogger(this.getClass().getPackage().getName() + "-" + logCount++);
handler = new LogHandler();
handler.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
logger.addHandler(handler);
logger.setLevel(Level.ALL);
listener = new LogTaskListener(logger, Level.ALL);
}
@Test
public void testGetSparseCheckoutPaths() {
assertThat(sparseCheckoutPaths.getSparseCheckoutPaths(), hasItem(SRC_SPARSE_CHECKOUT_PATH));
}
@Test
public void testGetSparseCheckoutPathsEmpty() {
assertThat(emptySparseCheckoutPaths.getSparseCheckoutPaths(), is(empty()));
}
@Test
public void testDecorateCloneCommand() throws Exception {
GitSCM scm = null;
Run build = null;
GitClient git = null;
CloneCommand cmd = null;
sparseCheckoutPaths.decorateCloneCommand(scm, build, git, listener, cmd);
assertThat(handler.getMessages(), hasItem("Using no checkout clone with sparse checkout."));
}
@Test
public void testDecorateCloneCommandEmpty() throws Exception {
GitSCM scm = null;
Run build = null;
GitClient git = null;
CloneCommand cmd = null;
emptySparseCheckoutPaths.decorateCloneCommand(scm, build, git, listener, cmd);
assertThat(handler.getMessages(), is(empty()));
}
@Test
public void testDecorateCheckoutCommand() throws Exception {
GitSCM scm = null;
Run<?, ?> build = mock(Run.class);
when(build.getEnvironment(listener)).thenReturn(new EnvVars());
GitClient git = null;
MyCheckoutCommand cmd = new MyCheckoutCommand();
sparseCheckoutPaths.decorateCheckoutCommand(scm, build, git, listener, cmd);
assertThat(cmd.getSparsePathNames(), hasItems(SRC_DIR_NAME));
}
@Test
public void testDecorateCheckoutCommandExpandsEnvVariable() throws Exception {
GitSCM scm = null;
GitClient git = null;
Run<?, ?> build = mock(Run.class);
EnvVars envVars = new EnvVars();
envVars.put("SPARSE_CHECKOUT_DIRECTORY", SRC_DIR_NAME);
when(build.getEnvironment(listener)).thenReturn(envVars);
MyCheckoutCommand cmd = new MyCheckoutCommand();
List<SparseCheckoutPath> sparseCheckoutPathList = new ArrayList<>();
sparseCheckoutPathList.add(new SparseCheckoutPath("${SPARSE_CHECKOUT_DIRECTORY}"));
SparseCheckoutPaths sparseCheckoutPaths = new SparseCheckoutPaths(sparseCheckoutPathList);
sparseCheckoutPaths.decorateCheckoutCommand(scm, build, git, listener, cmd);
assertThat(cmd.getSparsePathNames(), hasItems(SRC_DIR_NAME));
}
@Test
public void equalsContract() {
EqualsVerifier.forClass(SparseCheckoutPaths.class).usingGetClass().verify();
}
@Test
public void testHashCode() {
SparseCheckoutPaths emptySparseCheckoutPathsCopy = new SparseCheckoutPaths(emptySparseCheckoutPathList);
assertThat(emptySparseCheckoutPaths.hashCode(), is(emptySparseCheckoutPathsCopy.hashCode()));
assertThat(emptySparseCheckoutPaths, is(emptySparseCheckoutPathsCopy));
}
@Test
public void testToString() {
assertThat(emptySparseCheckoutPaths.toString(), is("SparseCheckoutPaths{sparseCheckoutPaths=[]}"));
}
private class MyCheckoutCommand implements CheckoutCommand {
private List<String> sparsePathNames;
List<String> getSparsePathNames() {
return sparsePathNames;
}
@Override
public CheckoutCommand ref(String string) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public CheckoutCommand branch(String string) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public CheckoutCommand deleteBranchIfExist(boolean bln) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public CheckoutCommand sparseCheckoutPaths(List<String> list) {
this.sparsePathNames = list;
return this;
}
@Override
public CheckoutCommand timeout(Integer intgr) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public CheckoutCommand lfsRemote(String string) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public CheckoutCommand lfsCredentials(StandardCredentials sc) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void execute() throws GitException, InterruptedException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}