Skip to content

Commit

Permalink
Remove random runner from build tools (internal) tests (#88577)
Browse files Browse the repository at this point in the history
This removes unrequited overhead for writing build tool and build tool internal tests and mechanically ports all junit3 tests to junit4.
  • Loading branch information
breskeby committed Jul 20, 2022
1 parent ab2602e commit 160b14f
Show file tree
Hide file tree
Showing 28 changed files with 142 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

import org.elasticsearch.gradle.internal.BwcVersions;
import org.elasticsearch.gradle.internal.BwcVersions.VersionPair;
import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;

import java.io.File;
import java.util.Arrays;

public class AbstractDistributionDownloadPluginTests extends GradleUnitTestCase {
public class AbstractDistributionDownloadPluginTests {
protected static Project rootProject;
protected static Project archivesProject;
protected static Project packagesProject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
*/
package org.elasticsearch.gradle.internal;

import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;

public class ConcatFilesTaskTests extends GradleUnitTestCase {
import static org.junit.Assert.assertEquals;

public class ConcatFilesTaskTests {

@Test
public void testHeaderAdded() throws IOException {

Project project = createProject();
Expand All @@ -39,6 +42,7 @@ public void testHeaderAdded() throws IOException {
file.delete();
}

@Test
public void testConcatenationWithUnique() throws IOException {

Project project = createProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
*/
package org.elasticsearch.gradle.internal;

import com.carrotsearch.randomizedtesting.RandomizedTest;

import org.apache.tools.ant.taskdefs.condition.Os;
import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class EmptyDirTaskTests extends GradleUnitTestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

public class EmptyDirTaskTests {

@Test
public void testCreateEmptyDir() throws Exception {
Project project = ProjectBuilder.builder().build();
EmptyDirTask emptyDirTask = project.getTasks().create("emptyDirTask", EmptyDirTask.class);
Expand All @@ -40,8 +44,9 @@ public void testCreateEmptyDir() throws Exception {
newEmptyFolder.delete();
}

@Test
public void testCreateEmptyDirNoPermissions() throws Exception {
RandomizedTest.assumeFalse("Functionality is Unix specific", Os.isFamily(Os.FAMILY_WINDOWS));
assumeFalse("Functionality is Unix specific", Os.isFamily(Os.FAMILY_WINDOWS));

Project project = ProjectBuilder.builder().build();
EmptyDirTask emptyDirTask = project.getTasks().create("emptyDirTask", EmptyDirTask.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import org.elasticsearch.gradle.internal.distribution.InternalElasticsearchDistributionTypes;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Test;

import java.io.File;

public class InternalDistributionDownloadPluginTests extends AbstractDistributionDownloadPluginTests {

@Test
public void testLocalCurrentVersionPackages() {
ElasticsearchDistributionType[] types = { InternalElasticsearchDistributionTypes.RPM, InternalElasticsearchDistributionTypes.DEB };
for (ElasticsearchDistributionType packageType : types) {
Expand All @@ -33,6 +35,7 @@ public void testLocalCurrentVersionPackages() {
}
}

@Test
public void testLocalBwcPackages() {
ElasticsearchDistributionType[] types = { InternalElasticsearchDistributionTypes.RPM, InternalElasticsearchDistributionTypes.DEB };
for (ElasticsearchDistributionType packageType : types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@

package org.elasticsearch.gradle.internal;

import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

public class JdkDownloadPluginTests extends GradleUnitTestCase {
public class JdkDownloadPluginTests {
private static Project rootProject;

@BeforeClass
public static void setupRoot() {
rootProject = ProjectBuilder.builder().build();
}

@Test
public void testMissingVendor() {
assertJdkError(createProject(), "testjdk", null, "11.0.2+33", "linux", "x64", "vendor not specified for jdk [testjdk]");
}

@Test
public void testUnknownVendor() {
assertJdkError(
createProject(),
Expand All @@ -40,10 +44,12 @@ public void testUnknownVendor() {
);
}

@Test
public void testMissingVersion() {
assertJdkError(createProject(), "testjdk", "openjdk", null, "linux", "x64", "version not specified for jdk [testjdk]");
}

@Test
public void testBadVersionFormat() {
assertJdkError(
createProject(),
Expand All @@ -56,10 +62,12 @@ public void testBadVersionFormat() {
);
}

@Test
public void testMissingPlatform() {
assertJdkError(createProject(), "testjdk", "openjdk", "11.0.2+33", null, "x64", "platform not specified for jdk [testjdk]");
}

@Test
public void testUnknownPlatform() {
assertJdkError(
createProject(),
Expand All @@ -72,10 +80,12 @@ public void testUnknownPlatform() {
);
}

@Test
public void testMissingArchitecture() {
assertJdkError(createProject(), "testjdk", "openjdk", "11.0.2+33", "linux", null, "architecture not specified for jdk [testjdk]");
}

@Test
public void testUnknownArchitecture() {
assertJdkError(
createProject(),
Expand All @@ -97,7 +107,7 @@ private void assertJdkError(
final String architecture,
final String message
) {
IllegalArgumentException e = expectThrows(
IllegalArgumentException e = assertThrows(
IllegalArgumentException.class,
() -> createJdk(project, name, vendor, version, platform, architecture)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.elasticsearch.gradle.internal.checkstyle;

import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -17,40 +17,51 @@

import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

public class SnipptLengthCheckTests extends GradleUnitTestCase {
public class SnipptLengthCheckTests {

@Test
public void testNoSnippets() {
SnippetLengthCheck.checkFile(failOnError(), 10, "There a no snippets");
}

@Test
public void testEmptySnippet() {
SnippetLengthCheck.checkFile(failOnError(), 10, "// tag::foo", "// end::foo");
}

@Test
public void testSnippetWithSmallText() {
SnippetLengthCheck.checkFile(failOnError(), 10, "// tag::foo", "some words", "// end::foo");
}

@Test
public void testSnippetWithLeadingSpaces() {
SnippetLengthCheck.checkFile(failOnError(), 10, " // tag::foo", " some words", " // end::foo");
}

@Test
public void testSnippetWithEmptyLine() {
SnippetLengthCheck.checkFile(failOnError(), 10, " // tag::foo", "", " some words", " // end::foo");
}

@Test
public void testSnippetBrokenLeadingSpaces() {
List<String> collection = new ArrayList<>();
SnippetLengthCheck.checkFile(collect(collection), 10, " // tag::foo", "some words", " // end::foo");
assertThat(collection, equalTo(singletonList("2: snippet line should start with [ ]")));
}

@Test
public void testSnippetTooLong() {
List<String> collection = new ArrayList<>();
SnippetLengthCheck.checkFile(collect(collection), 10, " // tag::foo", " too long words", " // end::foo");
assertThat(collection, equalTo(singletonList("2: snippet line should be no more than [10] characters but was [14]")));
}

@Test
public void testLotsOfErrors() {
List<String> collection = new ArrayList<>();
SnippetLengthCheck.checkFile(collect(collection), 10, " // tag::foo", "asdfadf", " too long words", "asdfadf", " // end::foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,48 @@
*/
package org.elasticsearch.gradle.internal.doc;

import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.gradle.api.InvalidUserDataException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.elasticsearch.gradle.internal.doc.RestTestsFromSnippetsTask.replaceBlockQuote;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class RestTestFromSnippetsTaskTests extends GradleUnitTestCase {
public class RestTestFromSnippetsTaskTests {
@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public void testInvalidBlockQuote() {
String input = "\"foo\": \"\"\"bar\"";
expectedEx.expect(InvalidUserDataException.class);
expectedEx.expectMessage("Invalid block quote starting at 7 in:\n" + input);
replaceBlockQuote(input);
}

@Test
public void testSimpleBlockQuote() {
assertEquals("\"foo\": \"bort baz\"", replaceBlockQuote("\"foo\": \"\"\"bort baz\"\"\""));
}

@Test
public void testMultipleBlockQuotes() {
assertEquals(
"\"foo\": \"bort baz\", \"bar\": \"other\"",
replaceBlockQuote("\"foo\": \"\"\"bort baz\"\"\", \"bar\": \"\"\"other\"\"\"")
);
}

@Test
public void testEscapingInBlockQuote() {
assertEquals("\"foo\": \"bort\\\" baz\"", replaceBlockQuote("\"foo\": \"\"\"bort\" baz\"\"\""));
assertEquals("\"foo\": \"bort\\n baz\"", replaceBlockQuote("\"foo\": \"\"\"bort\n baz\"\"\""));
}

@Test
public void testIsDocWriteRequest() {
assertTrue((boolean) RestTestsFromSnippetsTask.shouldAddShardFailureCheck("doc-index/_search"));
assertFalse((boolean) RestTestsFromSnippetsTask.shouldAddShardFailureCheck("_cat"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
*/
package org.elasticsearch.gradle.internal.doc;

import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.junit.Test;

public class SnippetsTaskTests extends GradleUnitTestCase {
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class SnippetsTaskTests {

@Test
public void testMatchSource() {
SnippetsTask.Source source = SnippetsTask.matchSource("[source,console]");
assertTrue(source.getMatches());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package org.elasticsearch.gradle.internal.docker;

import org.elasticsearch.gradle.internal.test.GradleUnitTestCase;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
Expand All @@ -16,9 +16,11 @@
import static org.elasticsearch.gradle.internal.docker.DockerSupportService.deriveId;
import static org.elasticsearch.gradle.internal.docker.DockerSupportService.parseOsRelease;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class DockerSupportServiceTests extends GradleUnitTestCase {
public class DockerSupportServiceTests {

@Test
public void testParseOsReleaseOnOracle() {
final List<String> lines = List.of(
"NAME=\"Oracle Linux Server\"",
Expand Down Expand Up @@ -60,6 +62,7 @@ public void testParseOsReleaseOnOracle() {
/**
* Trailing whitespace should be removed
*/
@Test
public void testRemoveTrailingWhitespace() {
final List<String> lines = List.of("NAME=\"Oracle Linux Server\" ");

Expand All @@ -73,6 +76,7 @@ public void testRemoveTrailingWhitespace() {
/**
* Comments should be removed
*/
@Test
public void testRemoveComments() {
final List<String> lines = List.of("# A comment", "NAME=\"Oracle Linux Server\"");

Expand All @@ -83,6 +87,7 @@ public void testRemoveComments() {
assertThat(expected, equalTo(results));
}

@Test
public void testDeriveIdOnOracle() {
final Map<String, String> osRelease = new HashMap<>();
osRelease.put("ID", "ol");
Expand Down

0 comments on commit 160b14f

Please sign in to comment.