Skip to content

Commit 08c712e

Browse files
committed
Added tests for FreshMarkConsole.
1 parent bea5933 commit 08c712e

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

src/main/java/com/diffplug/freshmark/FreshMarkConsole.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
import java.io.File;
1919
import java.io.FileInputStream;
20+
import java.io.IOException;
2021
import java.io.PrintStream;
2122
import java.nio.charset.Charset;
2223
import java.nio.charset.StandardCharsets;
2324
import java.nio.file.Files;
2425
import java.nio.file.StandardOpenOption;
2526
import java.util.ArrayList;
27+
import java.util.HashMap;
2628
import java.util.List;
2729
import java.util.Map;
2830
import java.util.Properties;
@@ -31,6 +33,8 @@
3133
import org.kohsuke.args4j.Option;
3234
import org.kohsuke.args4j.spi.MapOptionHandler;
3335

36+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37+
3438
public class FreshMarkConsole {
3539
/** Prints the usage for the given command. */
3640
public static void printUsage(String name, CmdLineParser parser, PrintStream stream) {
@@ -61,24 +65,25 @@ public boolean isWin() {
6165
private LineEnding lineEnding = LineEnding.PLATFORM_NATIVE;
6266

6367
@Option(name = "-P", handler = MapOptionHandler.class, usage = "sets the properties which are available in the script, -P KEY_1=VALUE_1 -P KEY_2=VALUE_2")
64-
private Map<String, String> properties;
68+
private Map<String, String> properties = new HashMap<>();
6569

6670
@Option(name = "-properties", usage = "loads properties from the given file")
6771
private File propFile;
6872

69-
@Option(name = "-file", usage = "applies freshmark to the given file (multiple are allowed)")
73+
@Option(name = "-file", required = true, usage = "applies freshmark to the given file (multiple are allowed)")
7074
private List<File> files = new ArrayList<File>();
7175

7276
private static final Charset CHARSET = StandardCharsets.UTF_8;
7377

78+
@SuppressFBWarnings(value = {"REC_CATCH_EXCEPTION", "UR_UNINIT_READ"}, justification = "we don't want to bother the console user with stacktraces, and fields are set by args4j magic")
7479
public FreshMarkConsole(String[] args) {
7580
CmdLineParser parser = new CmdLineParser(this);
7681
try {
7782
// parse the arguments and get the config
7883
parser.parseArgument(args);
7984
// make sure some files were specified
8085
if (files.isEmpty()) {
81-
throw new IllegalArgumentException("No files were specified.");
86+
throw new IOException("No files were specified.");
8287
}
8388
// if a propFile was specified, load it and its contents to the properties map
8489
if (propFile != null) {

src/test/java/com/diffplug/freshmark/FreshMarkConsoleTest.java

+111-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,121 @@
1515
*/
1616
package com.diffplug.freshmark;
1717

18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.PrintStream;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
24+
import org.junit.Assert;
25+
import org.junit.Rule;
1826
import org.junit.Test;
27+
import org.junit.rules.TemporaryFolder;
28+
29+
import com.diffplug.common.base.Errors;
30+
import com.diffplug.common.base.StringPrinter;
31+
import com.diffplug.freshmark.FreshMarkConsole.LineEnding;
1932

2033
public class FreshMarkConsoleTest {
34+
@Rule
35+
public TemporaryFolder folder = new TemporaryFolder();
36+
37+
/** Returns a File (in a temporary folder) which has the given contents. */
38+
protected File createTestFile(String filename, String content) throws IOException {
39+
File file = folder.newFile(filename);
40+
Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8));
41+
return file;
42+
}
43+
44+
/** Standard test case. */
45+
private void testCase(File toRead, String filenameAfter, String args, String consoleOutput) throws IOException {
46+
String consoleOutputActual = StringPrinter.buildString(printer -> {
47+
PrintStream out = System.out;
48+
PrintStream err = System.err;
49+
try {
50+
// capture sysOut and sysErr
51+
System.setOut(printer.toPrintStream());
52+
System.setErr(printer.toPrintStream());
53+
// now run the test, capturing the output as we go
54+
FreshMarkConsole.main(args.split(" "));
55+
// check the result
56+
String result = new String(Files.readAllBytes(toRead.toPath()), StandardCharsets.UTF_8)
57+
.replace(LineEnding.WINDOWS.string, LineEnding.UNIX.string);
58+
Assert.assertEquals(TestResource.getTestResource(filenameAfter), result);
59+
} catch (Exception e) {
60+
throw Errors.asRuntime(e);
61+
} finally {
62+
System.setOut(out);
63+
System.setErr(err);
64+
}
65+
});
66+
consoleOutputActual = consoleOutputActual.replace(LineEnding.WINDOWS.string, LineEnding.UNIX.string);
67+
Assert.assertEquals(consoleOutput, consoleOutputActual);
68+
}
69+
2170
@Test
22-
public void doTest() {
71+
public void testNoFile() throws IOException {
72+
File testFile = createTestFile("test.md", TestResource.getTestResource("full_before.txt"));
73+
testCase(testFile, "full_before.txt", "-P prop=key",
74+
"Option \"-file\" is required\n" +
75+
"\n" +
76+
"usage: freshmark [-P] [-endings [PLATFORM_NATIVE | WINDOWS | UNIX]] -file FILE [-properties FILE]\n" +
77+
" -P : sets the properties which are\n" +
78+
" available in the script, -P\n" +
79+
" KEY_1=VALUE_1 -P KEY_2=VALUE_2\n" +
80+
" (default: {prop=key})\n" +
81+
" -endings [PLATFORM_NATIVE | WINDOWS | : determines the line endings to use in\n" +
82+
" UNIX] the output (default: PLATFORM_NATIVE)\n" +
83+
" -file FILE : applies freshmark to the given file\n" +
84+
" (multiple are allowed)\n" +
85+
" -properties FILE : loads properties from the given file\n" +
86+
"");
87+
}
2388

89+
@Test
90+
public void testNoProps() throws IOException {
91+
File testFile = createTestFile("test.md", TestResource.getTestResource("full_before.txt"));
92+
testCase(testFile, "full_before.txt", "-file " + testFile.getAbsolutePath(),
93+
"Unknown key 'group'\n" +
94+
"Unknown key 'name'\n" +
95+
"Unknown key 'org'\n" +
96+
"Unknown key 'name'\n" +
97+
"Unknown key 'stable'\n" +
98+
"Unknown key 'org'\n" +
99+
"Unknown key 'name'\n" +
100+
"Unknown key 'org'\n" +
101+
"Unknown key 'name'\n" +
102+
"Unknown key 'stable'\n" +
103+
"Unknown key 'version'\n" +
104+
"Unknown key 'org'\n" +
105+
"Unknown key 'name'\n" +
106+
"ReferenceError: \"stable\" is not defined in <eval> at line number 24\n" +
107+
"\n" +
108+
"usage: freshmark [-P] [-endings [PLATFORM_NATIVE | WINDOWS | UNIX]] -file FILE [-properties FILE]\n" +
109+
" -P : sets the properties which are\n" +
110+
" available in the script, -P\n" +
111+
" KEY_1=VALUE_1 -P KEY_2=VALUE_2\n" +
112+
" (default: {})\n" +
113+
" -endings [PLATFORM_NATIVE | WINDOWS | : determines the line endings to use in\n" +
114+
" UNIX] the output (default: PLATFORM_NATIVE)\n" +
115+
" -file FILE : applies freshmark to the given file\n" +
116+
" (multiple are allowed)\n" +
117+
" -properties FILE : loads properties from the given file\n" +
118+
"");
119+
}
120+
121+
@Test
122+
public void testPropsOnCommandLine() throws IOException {
123+
File testFile = createTestFile("test.md", TestResource.getTestResource("full_before.txt"));
124+
testCase(testFile, "full_after.txt",
125+
"-P stable=3.2.0 -P version=3.3.0-SNAPSHOT -P group=com.diffplug.durian -P name=durian -P org=diffplug -file " + testFile.getAbsolutePath(),
126+
"");
127+
}
128+
129+
@Test
130+
public void testPropsFromFile() throws IOException {
131+
File testFile = createTestFile("test.md", TestResource.getTestResource("full_before.txt"));
132+
File propFile = createTestFile("props.properties", TestResource.getTestResource("full_props.properties"));
133+
testCase(testFile, "full_after.txt", "-properties " + propFile + " -file " + testFile.getAbsolutePath(), "");
24134
}
25135
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
stable=3.2.0
2+
version=3.3.0-SNAPSHOT
3+
group=com.diffplug.durian
4+
name=durian
5+
org=diffplug

0 commit comments

Comments
 (0)