Skip to content

Commit

Permalink
#531: prevent logging for processable commandlets #511: consolidate t…
Browse files Browse the repository at this point in the history
…est context (#541)
  • Loading branch information
hohwille authored Aug 20, 2024
1 parent 78cf4b6 commit b731286
Show file tree
Hide file tree
Showing 21 changed files with 348 additions and 366 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import com.devonfw.tools.ide.cli.CliAbortException;
import com.devonfw.tools.ide.cli.CliArgument;
Expand All @@ -34,8 +32,9 @@
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.io.FileAccessImpl;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeLogger;
import com.devonfw.tools.ide.log.IdeLoggerImpl;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.log.IdeSubLoggerNone;
import com.devonfw.tools.ide.merge.DirectoryMerger;
import com.devonfw.tools.ide.network.ProxyContext;
import com.devonfw.tools.ide.os.SystemInfo;
Expand All @@ -60,7 +59,7 @@ public abstract class AbstractIdeContext implements IdeContext {

private static final String IDE_URLS_GIT = "https://github.com/devonfw/ide-urls.git";

private final Map<IdeLogLevel, IdeSubLogger> loggers;
private final IdeLoggerImpl logger;

private Path ideHome;

Expand Down Expand Up @@ -116,8 +115,6 @@ public abstract class AbstractIdeContext implements IdeContext {

private DirectoryMerger workspaceMerger;

private final Function<IdeLogLevel, IdeSubLogger> loggerFactory;

private boolean offlineMode;

private boolean forceMode;
Expand All @@ -137,18 +134,15 @@ public abstract class AbstractIdeContext implements IdeContext {
/**
* The constructor.
*
* @param minLogLevel the minimum {@link IdeLogLevel} to enable. Should be {@link IdeLogLevel#INFO} by default.
* @param factory the {@link Function} to create {@link IdeSubLogger} per {@link IdeLogLevel}.
* @param logger the {@link IdeLogger}.
* @param userDir the optional {@link Path} to current working directory.
* @param toolRepository @param toolRepository the {@link ToolRepository} of the context. If it is set to {@code null} {@link DefaultToolRepository} will
* be used.
*/
public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubLogger> factory, Path userDir, ToolRepository toolRepository) {
public AbstractIdeContext(IdeLoggerImpl logger, Path userDir, ToolRepository toolRepository) {

super();
this.loggerFactory = factory;
this.loggers = new HashMap<>();
setLogLevel(minLogLevel);
this.logger = logger;
this.systemInfo = SystemInfoImpl.INSTANCE;
this.commandletManager = new CommandletManagerImpl(this);
this.fileAccess = new FileAccessImpl(this);
Expand Down Expand Up @@ -309,14 +303,6 @@ public String getMessageIdeHome() {
*/
public boolean isTest() {

return isMock();
}

/**
* @return {@code true} if this is a mock context for JUnits, {@code false} otherwise.
*/
public boolean isMock() {

return false;
}

Expand Down Expand Up @@ -708,9 +694,7 @@ protected ProcessContext createProcessContext() {
@Override
public IdeSubLogger level(IdeLogLevel level) {

IdeSubLogger logger = this.loggers.get(level);
Objects.requireNonNull(logger);
return logger;
return this.logger.level(level);
}

@Override
Expand Down Expand Up @@ -793,24 +777,6 @@ private static <O> void addMapping(Map<String, O> mapping, String key, O option)
}
}

/**
* Sets the log level.
*
* @param logLevel {@link IdeLogLevel}
*/
public void setLogLevel(IdeLogLevel logLevel) {

for (IdeLogLevel level : IdeLogLevel.values()) {
IdeSubLogger logger;
if (level.ordinal() < logLevel.ordinal()) {
logger = new IdeSubLoggerNone(level);
} else {
logger = this.loggerFactory.apply(level);
}
this.loggers.put(level, logger);
}
}

@Override
public Step getCurrentStep() {

Expand Down Expand Up @@ -918,7 +884,13 @@ private boolean applyAndRun(CliArguments arguments, Commandlet cmd) {
} else if (cmd.isIdeRootRequired() && (this.ideRoot == null)) {
throw new CliException(getMessageIdeRootNotFound(), ProcessResult.NO_IDE_ROOT);
}
if (!cmd.isProcessableOutput()) {
if (cmd.isProcessableOutput()) {
for (IdeLogLevel level : IdeLogLevel.values()) {
if (level != IdeLogLevel.INFO) {
this.logger.setLogLevel(level, false);
}
}
} else {
if (cmd.isIdeHomeRequired()) {
debug(getMessageIdeHomeFound());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.devonfw.tools.ide.io.IdeProgressBar;
import com.devonfw.tools.ide.io.IdeProgressBarConsole;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeLoggerImpl;
import com.devonfw.tools.ide.log.IdeSubLoggerOut;

import me.tongfei.progressbar.ProgressBarBuilder;
Expand All @@ -26,7 +27,7 @@ public class IdeContextConsole extends AbstractIdeContext {
*/
public IdeContextConsole(IdeLogLevel minLogLevel, Appendable out, boolean colored) {

super(minLogLevel, level -> new IdeSubLoggerOut(level, out, colored, minLogLevel), null, null);
super(new IdeLoggerImpl(minLogLevel, level -> new IdeSubLoggerOut(level, out, colored, minLogLevel)), null, null);
if (System.console() == null) {
debug("System console not available - using System.in as fallback");
this.scanner = new Scanner(System.in);
Expand Down
63 changes: 63 additions & 0 deletions cli/src/main/java/com/devonfw/tools/ide/log/IdeLoggerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.devonfw.tools.ide.log;

import java.util.Objects;
import java.util.function.Function;

/**
* Implementation of {@link IdeLogger}.
*/
public class IdeLoggerImpl implements IdeLogger {

private final Function<IdeLogLevel, IdeSubLogger> loggerFactory;

private final IdeSubLogger[] loggers;

/**
* @param minLogLevel the minimum enabled {@link IdeLogLevel}.
* @param factory the factory to create active {@link IdeSubLogger} instances.
*/
public IdeLoggerImpl(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubLogger> factory) {

super();
this.loggerFactory = factory;
this.loggers = new IdeSubLogger[IdeLogLevel.values().length];
setLogLevel(minLogLevel);
}

@Override
public IdeSubLogger level(IdeLogLevel level) {

IdeSubLogger logger = this.loggers[level.ordinal()];
Objects.requireNonNull(logger);
return logger;
}

/**
* Sets the log level.
*
* @param logLevel {@link IdeLogLevel}
*/
public void setLogLevel(IdeLogLevel logLevel) {

for (IdeLogLevel level : IdeLogLevel.values()) {
boolean enabled = level.ordinal() >= logLevel.ordinal();
setLogLevel(level, enabled);
}
}

/**
* @param logLevel the {@link IdeLogLevel} to modify.
* @param enabled - {@code true} to enable, {@code false} to disable.
*/
public void setLogLevel(IdeLogLevel logLevel, boolean enabled) {

IdeSubLogger logger;
if (enabled) {
logger = this.loggerFactory.apply(logLevel);
} else {
logger = IdeSubLoggerNone.of(logLevel);
}
this.loggers[logLevel.ordinal()] = logger;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
*/
public final class IdeSubLoggerNone extends AbstractIdeSubLogger {

private static final IdeSubLoggerNone[] LOGGERS;

static {
IdeLogLevel[] levels = IdeLogLevel.values();
LOGGERS = new IdeSubLoggerNone[levels.length];
for (int i = 0; i < levels.length; i++) {
LOGGERS[i] = new IdeSubLoggerNone(levels[i]);
}
}

/**
* The constructor.
*
* @param level the {@link #getLevel() log-level}.
*/
public IdeSubLoggerNone(IdeLogLevel level) {
private IdeSubLoggerNone(IdeLogLevel level) {

super(level);
}
Expand All @@ -27,4 +37,13 @@ public boolean isEnabled() {
return false;
}

/**
* @param level the {@link IdeLogLevel}.
* @return the {@link IdeSubLoggerNone} instance.
*/
public static IdeSubLoggerNone of(IdeLogLevel level) {

return LOGGERS[level.ordinal()];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ private IdeCompleter newCompleter() {

private IdeTestContext newTestContext() {

return new IdeTestContext(Path.of(""), "");
return new IdeTestContext(Path.of(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public abstract class AbstractIdeContextTest extends Assertions {
private static final int CHUNK_SIZE = 1024;

/**
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}. E.g.
* "basic".
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}.
* E.g. "basic".
* @return the {@link IdeTestContext} pointing to that project.
*/
protected IdeTestContext newContext(String testProject) {
Expand All @@ -44,8 +44,8 @@ protected IdeTestContext newContext(String testProject) {
}

/**
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}. E.g.
* "basic".
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}.
* E.g. "basic".
* @param projectPath the relative path inside the test project where to create the context.
* @return the {@link IdeTestContext} pointing to that project.
*/
Expand All @@ -55,11 +55,11 @@ protected static IdeTestContext newContext(String testProject, String projectPat
}

/**
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}. E.g.
* "basic".
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}.
* E.g. "basic".
* @param projectPath the relative path inside the test project where to create the context.
* @param copyForMutation - {@code true} to create a copy of the project that can be modified by the test, {@code false} otherwise (only to save resources if
* you are 100% sure that your test never modifies anything in that project.)
* @param copyForMutation - {@code true} to create a copy of the project that can be modified by the test, {@code false} otherwise (only to save resources
* if you are 100% sure that your test never modifies anything in that project.)
* @return the {@link IdeTestContext} pointing to that project.
*/
protected static IdeTestContext newContext(String testProject, String projectPath, boolean copyForMutation) {
Expand All @@ -68,11 +68,11 @@ protected static IdeTestContext newContext(String testProject, String projectPat
}

/**
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}. E.g.
* "basic".
* @param testProject the (folder)name of the project test case, in this folder a 'project' folder represents the test project in {@link #TEST_PROJECTS}.
* E.g. "basic".
* @param projectPath the relative path inside the test project where to create the context.
* @param copyForMutation - {@code true} to create a copy of the project that can be modified by the test, {@code false} otherwise (only to save resources if
* you are 100% sure that your test never modifies anything in that project.)
* @param copyForMutation - {@code true} to create a copy of the project that can be modified by the test, {@code false} otherwise (only to save resources
* if you are 100% sure that your test never modifies anything in that project.)
* @param logLevel the {@link IdeLogLevel} used as threshold for logging.
* @return the {@link IdeTestContext} pointing to that project.
*/
Expand Down Expand Up @@ -112,24 +112,6 @@ protected static IdeTestContext newContext(Path projectPath) {
return new IdeTestContext(projectPath);
}

/**
* @param projectPath the relative path inside the test project where to create the context.
* @param errors list of error messages.
* @param outs list of out messages.
* @param exitCode the exit code.
* @param isOnline boolean if it should be run in online mode.
* @return the {@link GitContextTestContext} pointing to that project.
*/
protected static GitContextTestContext newGitContext(Path projectPath, List<String> errors, List<String> outs, int exitCode, boolean isOnline) {

GitContextTestContext context;
context = new GitContextTestContext(isOnline, projectPath);
context.setErrors(errors);
context.setOuts(outs);
context.setExitCode(exitCode);
return context;
}

protected static IdeTestContextAssertion assertThat(IdeTestContext context) {

return new IdeTestContextAssertion(context);
Expand Down
Loading

0 comments on commit b731286

Please sign in to comment.