Skip to content

Commit

Permalink
#5: added Tests for Help and Version Commandlets (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzLanger authored Oct 10, 2023
1 parent de6a617 commit 75425f7
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public final class HelpCommandlet extends Commandlet {

private static final String LOGO = """
protected static final String LOGO = """
__ ___ ___ ___
╲ ╲ |_ _| ╲| __|__ _ ____ _
> > | || |) | _|/ _` (_-< || |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void save() {
do {
line = reader.readLine();
if (line != null) {
VariableLine variableLine = VariableLine.of(DEFAULT_PROPERTIES, this.logger, reader);
VariableLine variableLine = VariableLine.of(line, this.logger, reader);
lines.add(variableLine);
}
} while (line != null);
Expand All @@ -128,7 +128,7 @@ public void save() {
if (newLine != line) {
this.logger.debug("Changed variable line from '{}' to '{}' in {}", line, newLine, newPropertiesFilePath);
}
writer.append(line.toString());
writer.append(newLine.toString());
writer.append(NEWLINE);
String name = line.getName();
if (name != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContextMock;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;

/**
* Integration test of {@link HelpCommandlet}.
*/
public class HelpCommandletTest extends AbstractIdeContextTest {

/**
* Test of {@link HelpCommandlet} does not require home.
*/
@Test
public void testThatHomeIsNotReqired() {

// arrange
IdeContext context = IdeTestContextMock.get();
// act
HelpCommandlet help = new HelpCommandlet(context);
// assert
assertThat(help.isIdeHomeRequired()).isEqualTo(false);
}

/**
* Test of {@link HelpCommandlet} run.
*/
@Test
public void testRun() {

// arrange
IdeContext context = IdeTestContextMock.get();
HelpCommandlet help = new HelpCommandlet(context);
// act
help.run();
// assert
assertLogoMessage(context);
assertLogMessage(context, IdeLogLevel.INFO, "Usage: ide [option]* [[commandlet] [arg]*]");
assertOptionLogMessages(context);
}

/**
* Test of {@link HelpCommandlet} run with a Commandlet.
*/
@Test
public void testRunWithCommandlet() {

// arrange
String path = "workspaces/foo-test/my-git-repo";
IdeContext context = newContext("basic", path, true);
HelpCommandlet help = context.getCommandletManager().getCommandlet(HelpCommandlet.class);
help.commandlet.setValueAsString("mvn");
// act
help.run();
// assert
assertLogoMessage(context);
assertLogMessage(context, IdeLogLevel.INFO, "Usage: ide [option]* mvn [<args>*]");
assertLogMessage(context, IdeLogLevel.INFO, "Tool commandlet for Maven (Build-Tool)");
assertOptionLogMessages(context);
}

/**
* Assertion for the options that should be displayed.
*/
public void assertOptionLogMessages(IdeContext context) {

assertLogMessage(context, IdeLogLevel.INFO, "--locale the locale (e.g. 'de' for German language)");
assertLogMessage(context, IdeLogLevel.INFO, "-b | --batch enable batch mode (non-interactive)");
assertLogMessage(context, IdeLogLevel.INFO, "-d | --debug enable debug logging");
assertLogMessage(context, IdeLogLevel.INFO, "-f | --force enable force mode");
assertLogMessage(context, IdeLogLevel.INFO,
"-o | --offline enable offline mode (skip updates or git pull, fail downloads or git clone)");
assertLogMessage(context, IdeLogLevel.INFO,
"-q | --quiet disable info logging (only log success, warning or error)");
assertLogMessage(context, IdeLogLevel.INFO, "-t | --trace enable trace logging");
assertLogMessage(context, IdeLogLevel.INFO, "-v | --version Print the IDE version and exit.");
}

/**
* Assertion for the IDE-Logo that should be displayed.
*/
public void assertLogoMessage(IdeContext context) {

assertLogMessage(context, IdeLogLevel.INFO, HelpCommandlet.LOGO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.cli.CliException;
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;


/**
* Integration test of {@link VersionGetCommandlet}.
*/
public class VersionGetCommandletTest extends AbstractIdeContextTest{

/**
* Test of {@link VersionGetCommandlet} run, when Installed Version is null.
*/
@Test
public void testVersionGetCommandletRunThrowsCliExeption(){
// arrange
String path = "workspaces/foo-test/my-git-repo";
IdeContext context = newContext("basic", path, false);
VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class);
versionGet.tool.setValueAsString("java");
// act
try {
versionGet.run();
failBecauseExceptionWasNotThrown(CliException.class);
} catch (CliException e) {
//assert
assertThat(e).hasMessageContaining("Tool java is not installed!");
}
}

/**
* Test of {@link VersionGetCommandlet} run.
*/
@Test
public void testVersionGetCommandletRun(){
//arrange
String path = "workspaces/foo-test/my-git-repo";
IdeContext context = newContext("basic", path, false);
VersionGetCommandlet versionGet = context.getCommandletManager().getCommandlet(VersionGetCommandlet.class);
//act
versionGet.tool.setValueAsString("mvn");
versionGet.run();
//assert
assertLogMessage(context, IdeLogLevel.INFO, "3.9.4");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContextMock;
import com.devonfw.tools.ide.log.IdeLogLevel;
import org.junit.jupiter.api.Test;


/**
* Integration test of {@link VersionListCommandlet}.
*/
public class VersionListCommandletTest extends AbstractIdeContextTest {

/**
* Test of {@link VersionListCommandlet} run.
*/
@Test
public void testVersionListCommandletRun() {
//arrange
String path = "workspaces/foo-test/my-git-repo";
IdeContext context = newContext("basic", path, false);
VersionListCommandlet versionList = context.getCommandletManager().getCommandlet(VersionListCommandlet.class);
versionList.tool.setValueAsString("mvn");
//act
versionList.run();
//assert
assertLogMessage(context, IdeLogLevel.INFO, "3.0.5");
assertLogMessage(context, IdeLogLevel.INFO, "3.1.0");
assertLogMessage(context, IdeLogLevel.INFO, "3.2.1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.devonfw.tools.ide.commandlet;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;

/**
* Integration test of {@link VersionSetCommandlet}.
*/
public class VersionSetCommandletTest extends AbstractIdeContextTest {

/**
* Test of {@link VersionSetCommandlet} run.
* @throws IOException on error.
*/
@Test
public void testVersionSetCommandletRun() throws IOException {
//arrange
String path = "workspaces/foo-test/my-git-repo";
IdeContext context = newContext("basic", path, true);
VersionSetCommandlet versionSet = context.getCommandletManager().getCommandlet(VersionSetCommandlet.class);
versionSet.tool.setValueAsString("mvn");
versionSet.version.setValueAsString("3.1.0");
//act
versionSet.run();
//assert
assertThat(Files.readAllLines(context.getSettingsPath().resolve("ide.properties"))).contains("MVN_VERSION=3.1.0");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"manual" : false,
"urls" : {
"-997329125" : {
"success" : {
"timestamp" : "2023-04-28T07:12:26.601818Z"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"manual" : false,
"urls" : {
"-997329125" : {
"success" : {
"timestamp" : "2023-04-28T07:12:26.601818Z"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

"manual" : false,
"urls" : {
"-997329125" : {
"success" : {
"timestamp" : "2023-04-28T07:12:26.601818Z"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9.4

0 comments on commit 75425f7

Please sign in to comment.