Skip to content

Commit

Permalink
1. test coverage
Browse files Browse the repository at this point in the history
2. support for configurign behaviour via system props, env or config file
3. tests executed in IDE by default are skipped from logs removal
  • Loading branch information
zeldigas committed Aug 30, 2020
1 parent cc9bf60 commit af7cd02
Show file tree
Hide file tree
Showing 20 changed files with 798 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased changes
## 0.1.0 - 2020-08-30
### Added
- TestExecutionListener that discards all log events related to passing tests
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Junit5 test execution listener that hides log events for "green" tests.
## Rationale

Have you ever faced with the logging noise during your project build? Maybe you had opportunity to download tens
megabytes of log to analyze issue just because some chatty package has enabled DEBUG logging "in case of something goes wrong"?
megabytes of log to analyze issue just because DEBUG level was enabled on some chatty package "in case of something goes wrong"?
Big chance you did, if your project uses some logging framework (like slf4j), and you write unit tests.

The reason is simple - logs allow you to get extra information if something goes wrong,
Expand Down Expand Up @@ -39,13 +39,34 @@ engine, it will work as well.
Right now only Logback is supported, although it should be possible to add support for other frameworks as well (e.g. log4j2)
in case of need and request from community.

### Configuration
Default behavior for listener is to work only when running via build system (e.g. maven) execution and do nothing with logs
when executed from IDE's run-test option. This provides reasonable balance between log amount and usability - IDE is usually
smart enough to provide good navigation in logs and show only relevant info.

This can be adjusted with defining configuration properties in the following ways (in the order of priority):
1. `reasonable-test-logs.properties` in root of classpath
2. System properties (one specified with `-D` key)
3. Environment variables

| Property name | Env variable format | Description |
|---------------|---------------------|-------------|
| `rtlogs.mode` | `RTLOGS_MODE` | Type of listener to use. Possible options: <br/> `auto` - "default" mode that disables logic in IDE and enables in regular build system run. <br/> `nop` - explicit NOP mode (IDE mode) <br/> `reasonable` - explicit logs processing mode (always build system) |
| `rtlogs.debug` | `RTLOGS_DEBUG` | Used in NOP mode for listener debugging purposes |

### Limitations
It is assumed that tests are executed in sequential manner. Parallel test execution support was not researched so far.

## Examples

Check `example` directory for various test-samples. The simplest way to run it -- execute the following command from repo root:
```
mvn clean package
```
Take a look at an amount of logs and then compare it to the mode when listener is disabled:
```
mvn clean package -Drtlogs.mode=nop
```

## Roadmap

Expand All @@ -61,4 +82,18 @@ in build system (e.g. surefire params for junit5).

Anyway, some techniques published on [github](https://github.com/nt-ca-aqe/testit-testutils/tree/master/testutils-logsuppressor-logback)
were useful.

# Development

## Release

1. Make sure your `~/.m2/settings.xml` contains
- profile with property `gpg.keyname` valid gpg key id
- server definition with id `ossrh` with valid credentials to nexus oss repo
2. Upload of artifacts is as simple as:
```bash
cd reasonable-test-logs
mvn clean deploy -Prelease
```


19 changes: 10 additions & 9 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@
<dependency>
<groupId>com.github.zeldigas</groupId>
<artifactId>reasonable-test-logs</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<version>0.1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
Expand All @@ -49,14 +44,20 @@
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<version>2.22.2</version>
<configuration>
<!-- Ignoring failures as the main idea of this module to introduce them in tests to show displayed logs -->
<testFailureIgnore>true</testFailureIgnore>
Expand Down
143 changes: 141 additions & 2 deletions reasonable-test-logs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,50 @@

<groupId>com.github.zeldigas</groupId>
<artifactId>reasonable-test-logs</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.1.0</version>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<scm>
<connection>scm:git:[email protected]:zeldigas/reasonable-test-logs.git</connection>
<developerConnection>scm:git:[email protected]:zeldigas/reasonable-test-logs.git</developerConnection>
<url>https://github.com/zeldigas/Reasonable-Test-Logs</url>
</scm>

<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

<developers>
<developer>
<name>Dmitry Pavlov</name>
<email>[email protected]</email>
</developer>
</developers>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.6.2</version>
<version>1.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -26,6 +58,113 @@
<version>1.2.3</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<notimestamp>true</notimestamp>
</configuration>
</plugin>
<plugin>
<groupId>io.github.zlika</groupId>
<artifactId>reproducible-build-maven-plugin</artifactId>
<version>0.12</version>
<executions>
<execution>
<goals>
<goal>strip-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
<passphraseServerId>${gpg.keyname}</passphraseServerId>
</configuration>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.github.zeldigas.rtlogs;

import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

public class Config {

public static final String CONFIG_FILE = "/reasonable-test-logs.properties";
private static final String PROPS_PREFIX = "rtlogs.";
private static final String PROP_CONTROLLER_TYPE = PROPS_PREFIX + "mode";
private static final String PROP_DEBUG = PROPS_PREFIX + "debug";

enum ControllerType {
AUTO, NOP, REASONABLE
}

private final ControllerType controllerType;
private final boolean debug;

public Config(ControllerType controllerType, boolean debug) {
this.controllerType = controllerType;
this.debug = debug;
}

public ControllerType getControllerType() {
return controllerType;
}

public boolean isDebug() {
return debug;
}

public static Config load() {
return load(CONFIG_FILE);
}

static Config load(String file) {
Properties properties = defaultProperties();
mergeTo(loadConfigFile(file), properties);
mergeTo(System.getProperties(), properties);
mergeTo(propertiesFromEnvVars(), properties);

return new Config(
ControllerType.valueOf(properties.getProperty(PROP_CONTROLLER_TYPE).toUpperCase()),
toBool(properties.getProperty(PROP_DEBUG))
);
}

private static Properties loadConfigFile(String file) {
Properties properties = new Properties();
InputStream config = Config.class.getResourceAsStream(file);
if (config != null) {
try {
properties.load(config);
} catch (Exception e) {
System.err.println("Failed to load properties from " + file + " file");
e.printStackTrace();
}
}
return properties;
}

private static void mergeTo(Properties source, Properties dest) {
source.forEach((k, v) -> {
if (k.toString().startsWith(PROPS_PREFIX)) {
dest.put(k, v);
}
});
}

private static boolean toBool(String value) {
return Boolean.parseBoolean(value);
}

private static Properties defaultProperties() {
Properties properties = new Properties();
properties.setProperty(PROP_CONTROLLER_TYPE, "auto");
properties.setProperty(PROP_DEBUG, "false");
return properties;
}

private static Properties propertiesFromEnvVars() {
Properties properties = new Properties();
Map<String, String> envVars = System.getenv();
if (envVars.containsKey("RTLOGS_MODE")) {
properties.setProperty(PROP_CONTROLLER_TYPE, envVars.get("RTLOGS_MODE"));
}
if (envVars.containsKey("RTLOGS_DEBUG")) {
properties.setProperty(PROP_DEBUG, envVars.get("RTLOGS_DEBUG"));
}
return properties;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.zeldigas.rtlogs;

public class ExecutionEnvironment {

public static boolean ideMode() {
String val = System.getProperties().getProperty("sun.java.command");
return isRunningInIntellij(val);
}

private static boolean isRunningInIntellij(String val) {
return val.startsWith("com.intellij.rt.");
}

}
Loading

0 comments on commit af7cd02

Please sign in to comment.