|
15 | 15 | */
|
16 | 16 | package com.diffplug.freshmark;
|
17 | 17 |
|
| 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; |
18 | 26 | 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; |
19 | 32 |
|
20 | 33 | 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 | + |
21 | 70 | @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 | + } |
23 | 88 |
|
| 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(), ""); |
24 | 134 | }
|
25 | 135 | }
|
0 commit comments