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

Issue #58: added some code coverage for Main #92

Merged
merged 1 commit into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.16.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
Expand Down Expand Up @@ -442,8 +448,8 @@
<regexes>
<regex>
<pattern>com.github.checkstyle.regression.Main</pattern>
<branchRate>0</branchRate>
<lineRate>0</lineRate>
<branchRate>75</branchRate>
<lineRate>77</lineRate>
</regex>
<regex>
<pattern>com.github.checkstyle.regression.configuration.ConfigGenerator</pattern>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/checkstyle/regression/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private Main() {
* @param args the CLI arguments
* @throws Exception execute failure
*/
public static void main(String[] args) throws Exception {
public static void main(String... args) throws Exception {
final Options options = createOptions();
final CommandLineParser parser = new DefaultParser();
final HelpFormatter formatter = new HelpFormatter();
Expand All @@ -96,7 +96,7 @@ public static void main(String[] args) throws Exception {
catch (ParseException ex) {
System.err.println(ex.getMessage());
formatter.printHelp("java -jar regression-tool.jar", options, true);
System.exit(1);
System.exit(-1);
}

final Arguments arguments = ImmutableArguments.builder()
Expand Down
157 changes: 157 additions & 0 deletions src/test/java/com/github/checkstyle/regression/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package com.github.checkstyle.regression;

import static com.github.checkstyle.regression.internal.TestUtils.assertUtilsClassHasPrivateConstructor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.Locale;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;

public final class MainTest {
private static final String USAGE = String.format(Locale.ROOT,
"usage: java -jar regression-tool.jar -r <arg> -p <arg> [-t <arg>]%n"
+ " [--stopAfterConfigGeneration]%n"
+ " -r,--checkstyleRepoPath <arg> the path of the checkstyle repository%n"
+ " -p,--patchBranch <arg> the name of the PR branch%n"
+ " -t,--checkstyleTesterPath <arg> the path of the checkstyle-tester%n"
+ " directory%n"
+ " --stopAfterConfigGeneration indicates that regression tool would%n"
+ " stop after generating config%n");

private static final String EOL = System.getProperty("line.separator");

@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
@Rule
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
@Rule
public final SystemErrRule systemErr = new SystemErrRule().enableLog().mute();
@Rule
public final SystemOutRule systemOut = new SystemOutRule().enableLog().mute();

@Test
public void testIsProperUtilsClass() throws Exception {
assertUtilsClassHasPrivateConstructor(Main.class);
}

@Test
public void testNoArguments() throws Exception {
exit.expectSystemExitWithStatus(-1);
exit.checkAssertionAfterwards(() -> {
assertEquals("Unexpected ouput log", USAGE, systemOut.getLog());
assertEquals("Unexpected system error log", "Missing required options: r, p" + EOL,
systemErr.getLog());
});
Main.main();
}

@Test
public void testRepoNonExistent() throws Exception {
try {
Main.main("-r", "BAD", "-p", "BAD");
fail("Exception is expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Invalid error message",
"path of local git repo must exist and be a directory",
ex.getLocalizedMessage());
}
}

@Test
public void testRepoEmpty() throws Exception {
try {
Main.main("-r", "", "-p", "BAD");
fail("Exception is expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Invalid error message",
"path of local git repo must exist and be a directory",
ex.getLocalizedMessage());
}
}

@Test
public void testRepoFile() throws Exception {
try {
final File file = temporaryFolder.newFile();
Main.main("-r", file.getCanonicalPath(), "-p", "BAD");
fail("Exception is expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Invalid error message",
"path of local git repo must exist and be a directory",
ex.getLocalizedMessage());
}
}

@Test
public void testMissingTester() throws Exception {
try {
final File directory = temporaryFolder.newFolder();
Main.main("-r", directory.getCanonicalPath(), "-p", "BAD");
fail("Exception is expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Invalid error message",
"missing checkstyleTesterPath, which is required if you are not using"
+ " --stopAfterConfigGeneration mode", ex.getLocalizedMessage());
}
}

@Test
public void testTesterNonExistent() throws Exception {
try {
final File directory = temporaryFolder.newFolder();
Main.main("-r", directory.getCanonicalPath(), "-t", "BAD", "-p", "BAD");
fail("Exception is expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Invalid error message",
"path of checkstyle tester must exist and be a directory",
ex.getLocalizedMessage());
}
}

@Test
public void testTesterFile() throws Exception {
try {
final File directory = temporaryFolder.newFolder();
final File file = temporaryFolder.newFile();
Main.main("-r", directory.getCanonicalPath(), "-t", file.getCanonicalPath(), "-p",
"BAD");
fail("Exception is expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Invalid error message",
"path of checkstyle tester must exist and be a directory",
ex.getLocalizedMessage());
}
}
}