Skip to content

Commit

Permalink
All unit tests use the same testing logging provider (#8593)
Browse files Browse the repository at this point in the history
Make sure that the correct test logging provider is loaded in `project-manager/Test`, so that only WARN and ERROR log messages are displayed. Also, make sure that the test log provider parses the correct configuration file - Rename all the `application.conf` files in the test resources to `application-test.conf`.

The problem was introduced in #8467
  • Loading branch information
Akirathan authored Dec 21, 2023
1 parent ac9d8c7 commit edc9859
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 182 deletions.
25 changes: 12 additions & 13 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ lazy val enso = (project in file("."))
`syntax-definition`,
`syntax-rust-definition`,
`text-buffer`,
logger,
pkg,
cli,
`task-progress-notifications`,
Expand Down Expand Up @@ -528,13 +527,6 @@ lazy val compileModuleInfo = taskKey[Unit]("Compiles `module-info.java`")
// === Internal Libraries =====================================================
// ============================================================================

lazy val logger = (project in file("lib/scala/logger"))
.settings(
frgaalJavaCompilerSetting,
version := "0.1",
libraryDependencies ++= scalaCompiler
)

lazy val `syntax-definition` =
project in file("lib/scala/syntax/definition")

Expand Down Expand Up @@ -907,7 +899,10 @@ lazy val `project-manager` = (project in file("lib/scala/project-manager"))
"org.apache.commons" % "commons-lang3" % commonsLangVersion,
"com.beachape" %% "enumeratum-circe" % enumeratumCirceVersion,
"com.miguno.akka" %% "akka-mock-scheduler" % akkaMockSchedulerVersion % Test,
"org.mockito" %% "mockito-scala" % mockitoScalaVersion % Test
"org.mockito" %% "mockito-scala" % mockitoScalaVersion % Test,
"junit" % "junit" % junitVersion % Test,
"com.github.sbt" % "junit-interface" % junitIfVersion % Test,
"org.hamcrest" % "hamcrest-all" % hamcrestVersion % Test
),
addCompilerPlugin(
"org.typelevel" %% "kind-projector" % kindProjectorVersion cross CrossVersion.full
Expand Down Expand Up @@ -1016,7 +1011,7 @@ lazy val `project-manager` = (project in file("lib/scala/project-manager"))
.dependsOn(`json-rpc-server-test` % Test)
.dependsOn(testkit % Test)
.dependsOn(`runtime-version-manager-test` % Test)
.dependsOn(`logging-service-logback` % Test)
.dependsOn(`logging-service-logback` % "test->test")

/* Note [Classpath Separation]
* ~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -1207,8 +1202,13 @@ val truffleRunOptionsSettings = Seq(
javaOptions ++= "-ea" +: benchOnlyOptions
)

/** Explicitly provide `application-test.conf` as the resource that should be used for
* parsing the logging configuration. Explicitly setting `config.resource` prevents
* the potential conflicts with other *.conf files.
*/
val testLogProviderOptions = Seq(
"-Dslf4j.provider=org.enso.logger.TestLogProvider"
"-Dslf4j.provider=org.enso.logger.TestLogProvider",
"-Dconfig.resource=application-test.conf"
)

lazy val `polyglot-api` = project
Expand Down Expand Up @@ -2126,8 +2126,7 @@ lazy val launcher = project
.dependsOn(buildNativeImage)
.dependsOn(LauncherShimsForTest.prepare())
.evaluated,
Test / fork := true,
Test / javaOptions += s"-Dconfig.file=${sourceDirectory.value}/test/resources/application.conf"
Test / fork := true
)
.dependsOn(cli)
.dependsOn(`runtime-version-manager`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.enso.interpreter.test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import org.junit.Test;
import org.slf4j.spi.SLF4JServiceProvider;

/**
* In the `runtime/Test` testing suite, {@link org.enso.logger.TestLogProvider} should be among the
* logging providers, because it is explicitly chosen as the logging provider for the tests.
*/
public class TestLogProviderOnClasspath {
@Test
public void testLogProviderOnClasspath() {
var sl = ServiceLoader.load(SLF4JServiceProvider.class);
var serviceIterator = sl.iterator();
List<SLF4JServiceProvider> providers = new ArrayList<>();
while (serviceIterator.hasNext()) {
providers.add(serviceIterator.next());
}
List<String> providerNames =
providers.stream().map(elem -> elem.getClass().getName()).collect(Collectors.toList());
assertThat(providerNames, hasItem("org.enso.logger.TestLogProvider"));
}

@Test
public void testLogProviderIsInUnnamedModule() throws Exception {
var testLogProviderClass = Class.forName("org.enso.logger.TestLogProvider");
var mod = testLogProviderClass.getModule();
assertThat(mod, notNullValue());
assertThat("Should be an unnamed module - with null name", mod.getName(), nullValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty
3 changes: 0 additions & 3 deletions lib/scala/logger/README.md

This file was deleted.

1 change: 0 additions & 1 deletion lib/scala/logger/src/main/java/org/enso/package-info.java

This file was deleted.

165 changes: 0 additions & 165 deletions lib/scala/logger/src/main/scala/org/enso/Logger.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TestLogProvider implements SLF4JServiceProvider {
@Override
public ILoggerFactory getLoggerFactory() {
ILoggerFactory factory = underlying.getLoggerFactory();
assert factory instanceof LoggerContext;
if (!initialized) {
try {
new LogbackSetup((LoggerContext) factory).setup();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.enso.projectmanager;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import org.junit.Test;
import org.slf4j.spi.SLF4JServiceProvider;

/**
* In the `runtime/Test` testing suite, {@link }org.enso.logger.TestLogProvider} should be among the
* logging providers, because it is explicitly chosen as the logging provider for the tests.
*
* <p>Note that the same test is in the `runtime/Test` project.
*/
public class TestLogProviderOnClasspath {
@Test
public void testLogProviderIsOnClasspath() {
var sl = ServiceLoader.load(SLF4JServiceProvider.class);
var serviceIterator = sl.iterator();
List<SLF4JServiceProvider> providers = new ArrayList<>();
while (serviceIterator.hasNext()) {
providers.add(serviceIterator.next());
}
List<String> providerNames =
providers.stream().map(elem -> elem.getClass().getName()).collect(Collectors.toList());
assertThat(providerNames, hasItem("org.enso.logger.TestLogProvider"));
}

@Test
public void testLogProviderIsInUnnamedModule() {
Class<?> testLogProviderClass = null;
try {
testLogProviderClass = Class.forName("org.enso.logger.TestLogProvider");
} catch (ClassNotFoundException e) {
fail("TestLogProvider class not found");
}
var mod = testLogProviderClass.getModule();
assertThat(mod, notNullValue());
assertThat("Should be an unnamed module - with null name", mod.getName(), nullValue());
}
}

0 comments on commit edc9859

Please sign in to comment.