Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing CLI tests #1933

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cli/src/test/java/de/jplag/cli/DebugTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.jplag.cli;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;

public class DebugTest extends CliTest {
@Test
void testDefaultDebug() throws IOException, ExitException {
JPlagOptions options = runCliForOptions();
assertFalse(options.debugParser());
}

@Test
void testSetDebug() throws IOException, ExitException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.DEBUG, true));
assertTrue(options.debugParser());
}
}
29 changes: 29 additions & 0 deletions cli/src/test/java/de/jplag/cli/ExcludeFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.jplag.cli;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;

public class ExcludeFileTest extends CliTest {
private static final String excludeFileName = "exclusions";
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved

@Test
void testNoDefaultExcludeFile() throws IOException, ExitException {
JPlagOptions options = runCliForOptions();
assertNull(options.exclusionFileName());
}

@Test
void testSetExcludeFile() throws IOException, ExitException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.EXCLUDE_FILES, excludeFileName));
assertEquals(excludeFileName, options.exclusionFileName());
}
}
28 changes: 28 additions & 0 deletions cli/src/test/java/de/jplag/cli/LogLevelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.jplag.cli;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.slf4j.event.Level;

import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;

public class LogLevelTest extends CliTest {
private static final Level DEFAULT_LOG_LEVEL = Level.INFO;

@Test
void testDefaultLogLevel() throws IOException, ExitException {
Level level = runCliForLogLevel();
assertEquals(DEFAULT_LOG_LEVEL, level);
}

@Test
void testSetLogLevel() throws IOException, ExitException {
Level level = runCliForLogLevel(args -> args.with(CliArgument.LOG_LEVEL, Level.ERROR.name()));
assertEquals(Level.ERROR, level);
}
}
68 changes: 68 additions & 0 deletions cli/src/test/java/de/jplag/cli/ResultFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package de.jplag.cli;

import static org.junit.jupiter.api.Assertions.assertEquals;

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

import org.junit.jupiter.api.Test;

import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;

public class ResultFileTest extends CliTest {
private static final String DEFAULT_RESULT_FILE = "results.zip";
private static final String TEST_RESULT_FILE = "customResults.zip";
private static final String TEST_RESULT_FILE_WITH_AVOIDANCE = "customResults(1).zip";
private static final String TEST_RESULT_FILE_WITHOUT_ZIP = "customResults";

@Test
void testDefaultResultFolder() throws IOException, ExitException {
String targetPath = runCliForTargetPath();
assertEquals(DEFAULT_RESULT_FILE, targetPath);
}

@Test
void testSetResultFolder() throws IOException, ExitException {
String targetPath = runCliForTargetPath(args -> args.with(CliArgument.RESULT_FILE, TEST_RESULT_FILE));
assertEquals(TEST_RESULT_FILE, targetPath);
}

@Test
void testSetResultFolderWithoutZip() throws IOException, ExitException {
String targetPath = runCliForTargetPath(args -> args.with(CliArgument.RESULT_FILE, TEST_RESULT_FILE_WITHOUT_ZIP));
assertEquals(TEST_RESULT_FILE, targetPath);
}

@Test
void testResultFileOverrideAvoidance() throws IOException, ExitException {
File testDir = Files.createTempDirectory("JPlagResultFileTest").toFile();
File targetFile = new File(testDir, TEST_RESULT_FILE);
File expectedTargetFile = new File(testDir, TEST_RESULT_FILE_WITH_AVOIDANCE);
targetFile.createNewFile();

String actualTargetPath = runCliForTargetPath(args -> args.with(CliArgument.RESULT_FILE, targetFile.getAbsolutePath()));

targetFile.delete();
testDir.delete();

assertEquals(expectedTargetFile.getAbsolutePath(), actualTargetPath);
}

@Test
void testResultFileOverwrite() throws IOException, ExitException {
File testDir = Files.createTempDirectory("JPlagResultFileTest").toFile();
File targetFile = new File(testDir, TEST_RESULT_FILE);
targetFile.createNewFile();

String actualTargetPath = runCliForTargetPath(
args -> args.with(CliArgument.RESULT_FILE, targetFile.getAbsolutePath()).with(CliArgument.OVERWRITE_RESULT_FILE, true));

targetFile.delete();
testDir.delete();

assertEquals(targetFile.getAbsolutePath(), actualTargetPath);
}
}
29 changes: 29 additions & 0 deletions cli/src/test/java/de/jplag/cli/SubdirectoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.jplag.cli;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;

public class SubdirectoryTest extends CliTest {
private static final String TEST_SUBDIRECTORY = "dir";

@Test
void testDefaultSubdirectory() throws IOException, ExitException {
JPlagOptions options = runCliForOptions();
assertNull(options.subdirectoryName());
}

@Test
void testSetSubdirectory() throws IOException, ExitException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.SUBDIRECTORY, TEST_SUBDIRECTORY));
assertEquals(TEST_SUBDIRECTORY, options.subdirectoryName());
}
}
30 changes: 30 additions & 0 deletions cli/src/test/java/de/jplag/cli/SuffixesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.jplag.cli;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.util.List;

import org.junit.jupiter.api.Test;

import de.jplag.cli.test.CliArgument;
import de.jplag.cli.test.CliTest;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;

public class SuffixesTest extends CliTest {
private static final List<String> JAVA_SUFFIXES = List.of(".java", ".JAVA");
private static final List<String> CUSTOM_SUFFIXES = List.of(".j", ".jva");

@Test
void testDefaultSuffixes() throws IOException, ExitException {
JPlagOptions options = runCliForOptions();
assertEquals(JAVA_SUFFIXES, options.fileSuffixes());
}

@Test
void testSetSuffixes() throws IOException, ExitException {
JPlagOptions options = runCliForOptions(args -> args.with(CliArgument.SUFFIXES, CUSTOM_SUFFIXES.toArray(new String[0])));
assertEquals(CUSTOM_SUFFIXES, options.fileSuffixes());
}
}
6 changes: 6 additions & 0 deletions cli/src/test/java/de/jplag/cli/test/CliArgument.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public record CliArgument<T>(String name, boolean isPositional) {

public static CliArgument<String> RESULT_FILE = new CliArgument<>("r", false);
public static CliArgument<Boolean> OVERWRITE_RESULT_FILE = new CliArgument<>("overwrite", false);

public static CliArgument<String> LOG_LEVEL = new CliArgument<>("log-level", false);
public static CliArgument<Boolean> DEBUG = new CliArgument<>("d", false);

public static CliArgument<String> SUBDIRECTORY = new CliArgument<>("subdirectory", false);
public static CliArgument<String> EXCLUDE_FILES = new CliArgument<>("x", false);
}
4 changes: 3 additions & 1 deletion cli/src/test/java/de/jplag/cli/test/CliResult.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.jplag.cli.test;

import org.slf4j.event.Level;

import de.jplag.options.JPlagOptions;

public record CliResult(JPlagOptions jPlagOptions, String targetPath) {
public record CliResult(JPlagOptions jPlagOptions, String targetPath, Level logLevel) {
}
27 changes: 26 additions & 1 deletion cli/src/test/java/de/jplag/cli/test/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.event.Level;

import de.jplag.JPlagResult;
import de.jplag.cli.*;
import de.jplag.cli.logger.CollectedLogger;
import de.jplag.cli.picocli.CliInputHandler;
import de.jplag.exceptions.ExitException;
import de.jplag.options.JPlagOptions;
Expand Down Expand Up @@ -105,6 +107,29 @@ protected String runCliForTargetPath(Consumer<CliArgumentBuilder> additionalOpti
return runCli(additionalOptionsBuilder).targetPath();
}

/**
* Runs the cli
* @return The log level set by the cli
* @throws ExitException If JPlag throws an exception
* @throws IOException If JPlag throws an exception
* @see #runCli()
*/
protected Level runCliForLogLevel() throws IOException, ExitException {
return runCli().logLevel();
}

/**
* Runs the cli using custom options
* @param additionalOptionsBuilder May modify the {@link CliArgumentBuilder} object to set custom options for this run.
* @return The log level set by the cli
* @throws ExitException If JPlag throws an exception
* @throws IOException If JPlag throws an exception
* @see #runCli()
*/
protected Level runCliForLogLevel(Consumer<CliArgumentBuilder> additionalOptionsBuilder) throws IOException, ExitException {
return runCli(additionalOptionsBuilder).logLevel();
}

/**
* Runs the cli
* @return The options returned by the cli
Expand All @@ -129,7 +154,7 @@ protected CliResult runCli(Consumer<CliArgumentBuilder> additionalOptionsBuilder

String targetPath = (String) getWritableFileMethod.invoke(cli);

return new CliResult(optionsBuilder.buildOptions(), targetPath);
return new CliResult(optionsBuilder.buildOptions(), targetPath, CollectedLogger.getLogLevel());
} catch (IllegalAccessException | InvocationTargetException e) {
Assumptions.abort("Could not access private field in CLI for test.");
return null; // will not be executed
Expand Down