Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fixing PublishTests tests (running against unclean build folders) #3256

Merged
merged 1 commit into from
May 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,42 @@
import org.gradle.testfixtures.ProjectBuilder;
import org.gradle.api.Project;
import org.opensearch.gradle.test.GradleUnitTestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository;
import java.io.File;
import org.gradle.testkit.runner.BuildResult;
import java.io.FileWriter;
import java.io.Writer;
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
import static org.junit.Assert.assertEquals;
import java.nio.file.Files;

import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.FileReader;
import org.gradle.api.tasks.bundling.Zip;
import org.gradle.internal.impldep.org.junit.After;

import java.util.List;
import java.util.ArrayList;

public class PublishTests extends GradleUnitTestCase {
private TemporaryFolder projectDir;

@Before
public void setUp() throws IOException {
projectDir = new TemporaryFolder();
projectDir.create();
}

@After
public void tearDown() {
projectDir.delete();
}

@Test
public void testZipPublish() throws IOException, XmlPullParserException {
Expand All @@ -50,12 +67,11 @@ public void testZipPublish() throws IOException, XmlPullParserException {
assertTrue(project.getTasks().getNames().contains(zipPublishTask));
assertNotNull("Task to generate: ", project.getTasks().getByName(zipPublishTask));
// Run Gradle functional tests, but calling a build.gradle file, that resembles the plugin publish behavior
File projectDir = new File("build/functionalTest");

// Create a sample plugin zip file
File sampleZip = new File("build/functionalTest/sample-plugin.zip");
Files.createDirectories(projectDir.toPath());
File sampleZip = new File(projectDir.getRoot(), "sample-plugin.zip");
Files.createFile(sampleZip.toPath());
writeString(new File(projectDir, "settings.gradle"), "");
writeString(projectDir.newFile("settings.gradle"), "");
// Generate the build.gradle file
String buildFileContent = "apply plugin: 'maven-publish' \n"
+ "apply plugin: 'java' \n"
Expand All @@ -75,7 +91,7 @@ public void testZipPublish() throws IOException, XmlPullParserException {
+ " }\n"
+ " }\n"
+ "}";
writeString(new File(projectDir, "build.gradle"), buildFileContent);
writeString(projectDir.newFile("build.gradle"), buildFileContent);
// Execute the task publishPluginZipPublicationToZipStagingRepository
List<String> allArguments = new ArrayList<String>();
allArguments.add("build");
Expand All @@ -84,20 +100,22 @@ public void testZipPublish() throws IOException, XmlPullParserException {
runner.forwardOutput();
runner.withPluginClasspath();
runner.withArguments(allArguments);
runner.withProjectDir(projectDir);
runner.withProjectDir(projectDir.getRoot());
BuildResult result = runner.build();
// Check if task publishMavenzipPublicationToZipstagingRepository has ran well
assertEquals(SUCCESS, result.task(":" + zipPublishTask).getOutcome());
// check if the zip has been published to local staging repo
assertTrue(
new File("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.zip")
new File(projectDir.getRoot(), "local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.zip")
.exists()
);
assertEquals(SUCCESS, result.task(":" + "build").getOutcome());
// Parse the maven file and validate the groupID to org.opensearch.plugin
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(
new FileReader("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.pom")
new FileReader(
new File(projectDir.getRoot(), "local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.pom")
)
);
assertEquals(model.getGroupId(), "org.opensearch.plugin");
}
Expand Down