Skip to content

Commit d848fc0

Browse files
committed
Added creation of a temporary .gitlab-ci.yml for testing if not present in repo.
1 parent 715f52f commit d848fc0

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

src/test/java/org/gitlab4j/api/TestPipelineApi.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.HashMap;
1111
import java.util.List;
1212
import java.util.Map;
13+
import java.util.Optional;
1314
import java.util.stream.Stream;
1415

1516
import org.gitlab4j.api.models.Pipeline;
@@ -29,9 +30,14 @@ public class TestPipelineApi extends AbstractIntegrationTest {
2930

3031
private static final String SCHEDULE_DESCRIPTION = "Test pipeline schedule - DELETE AFTER TEST";
3132
private static final String TRIGGER_DESCRIPTION = "Test pipeline trigger - DELETE AFTER TEST";
33+
private static final String TEST_GITLAB_CI_YML_CONTENT = "build:\n" +
34+
" stage: build\n script:\n - echo 'Empty build for testing variables with GitLab4J-API'";
35+
3236

3337
private static GitLabApi gitLabApi;
3438
private static Project testProject;
39+
private static RepositoryFile createdGitlabCiYml;
40+
private static RepositoryFile gitlabCiYml;
3541

3642
public TestPipelineApi() {
3743
super();
@@ -64,13 +70,37 @@ private static void deleteTestResources() {
6470
}
6571

6672
} catch (Exception ignore) {}
73+
74+
if (createdGitlabCiYml != null) {
75+
try {
76+
gitLabApi.getRepositoryFileApi().deleteFile(
77+
testProject, ".gitlab-ci.yml", "master", "No longer needed.");
78+
} catch (Exception ignore) {}
79+
}
6780
}
6881

6982
@BeforeClass
7083
public static void setup() {
7184
// Must setup the connection to the GitLab test server and get the test Project instance
7285
gitLabApi = baseTestSetup();
7386
testProject = getTestProject();
87+
88+
Optional<RepositoryFile> fileInfo =
89+
gitLabApi.getRepositoryFileApi().getOptionalFileInfo(testProject, ".gitlab-ci.yml", "master");
90+
if (fileInfo.isPresent()) {
91+
gitlabCiYml = fileInfo.get();
92+
} else {
93+
94+
try {
95+
RepositoryFile file = new RepositoryFile();
96+
file.setFilePath(".gitlab-ci.yml");
97+
file.setContent(TEST_GITLAB_CI_YML_CONTENT);
98+
createdGitlabCiYml = gitLabApi.getRepositoryFileApi().createFile(
99+
testProject, file, "master", "Need for testing pipelines.");
100+
gitlabCiYml = createdGitlabCiYml;
101+
System.out.println("Created .gitlab-ci.yml file for testing purposes.");
102+
} catch (Exception ignore) {}
103+
}
74104
}
75105

76106
@AfterClass
@@ -187,12 +217,8 @@ public void testTriggerAndCancelPipeline() throws GitLabApiException {
187217

188218
assertNotNull(testProject);
189219

190-
// Skip this test if no .gitlab-ci.yml file is found in the test project
191-
RepositoryFile fileInfo = null;
192-
try {
193-
fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(testProject, ".gitlab-ci.yml", "master");
194-
} catch (GitLabApiException ignore) {}
195-
assumeNotNull(fileInfo);
220+
// Skip this test if no .gitlab-ci.yml file is in the test project
221+
assumeNotNull(gitlabCiYml);
196222

197223
String triggerDescription = TRIGGER_DESCRIPTION + " - test triggerPipeline() - " + HelperUtils.getRandomInt(1000);
198224
Trigger createdTrigger = gitLabApi.getPipelineApi().createPipelineTrigger(testProject, triggerDescription);
@@ -212,7 +238,9 @@ public void testTriggerAndCancelPipeline() throws GitLabApiException {
212238

213239
@Test
214240
public void testCreatePipelineNoVariables() throws GitLabApiException {
215-
assumeNotNull(testProject);
241+
242+
// Skip this test if no .gitlab-ci.yml file is in the test project
243+
assumeNotNull(gitlabCiYml);
216244

217245
// Act
218246
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master");
@@ -225,7 +253,9 @@ public void testCreatePipelineNoVariables() throws GitLabApiException {
225253

226254
@Test
227255
public void testCreatePipelineWithVariables() throws GitLabApiException {
228-
assumeNotNull(testProject);
256+
257+
// Skip this test if no .gitlab-ci.yml file is in the test project
258+
assumeNotNull(gitlabCiYml);
229259

230260
// Arrange
231261
List<Variable> variableList = new ArrayList<>();
@@ -243,7 +273,9 @@ public void testCreatePipelineWithVariables() throws GitLabApiException {
243273

244274
@Test
245275
public void testCreatePipelineWithMapVariables() throws GitLabApiException {
246-
assumeNotNull(testProject);
276+
277+
// Skip this test if no .gitlab-ci.yml file is in the test project
278+
assumeNotNull(gitlabCiYml);
247279

248280
// Arrange
249281
Map<String, String> variableMap = new HashMap<>();

0 commit comments

Comments
 (0)