Skip to content

Commit

Permalink
Make test actually fail if incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightenom committed Feb 6, 2024
1 parent 8ede55c commit 8f2eda5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void write(final BatchedLaunchWriter data, final Path projectRoot) throws

private void write0(final BatchedLaunchWriter data, final Path projectRoot) throws IOException
{
final Path launchJson = projectRoot.resolve(".vscode").resolve("launch.json");
final Path launchJson = resolveLaunchFilePath(projectRoot);
final JsonObject root;
if (data.getKeepCurrentContent() == WritingMode.MODIFY_CURRENT && Files.exists(launchJson))
{
Expand Down Expand Up @@ -154,6 +154,11 @@ private void write0(final BatchedLaunchWriter data, final Path projectRoot) thro
}
}

public static Path resolveLaunchFilePath(final Path projectRoot)
{
return projectRoot.resolve(".vscode").resolve("launch.json");
}

private Map<String, JsonObject> getExistingConfigs(final JsonArray configs)
{
final Map<String, JsonObject> result = new HashMap<>();
Expand Down
57 changes: 54 additions & 3 deletions src/test/java/cz/nightenom/vsclaunch/BatchedLaunchWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import cz.nightenom.vsclaunch.attribute.LocatorPathLike;
import cz.nightenom.vsclaunch.attribute.PathLike;
import cz.nightenom.vsclaunch.attribute.ShortCmdBehaviour;
import cz.nightenom.vsclaunch.writer.LaunchJsonV0_2_0;
import cz.nightenom.vsclaunch.writer.WritingMode;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -16,6 +19,7 @@
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNull;

public class BatchedLaunchWriterTest
{
Expand All @@ -31,8 +35,8 @@ void testWriteToLatestJson()
LocatorPathLike.ofNioExclude(Paths.get(".", "exclude")),
LocatorPathLike.ofRuntime(),
LocatorPathLike.ofTest(),
LocatorPathLike.ofWorkSpaceFolder(Paths.get("workspaceF")),
LocatorPathLike.ofWorkSpaceFolderExclude(Paths.get("workspaceFexclude")));
LocatorPathLike.ofWorkSpaceFolder(Paths.get("workspaceFolder")),
LocatorPathLike.ofWorkSpaceFolderExclude(Paths.get("workspaceFolderExclude")));

final Map<String, String> testEnv = new HashMap<>();
testEnv.put("whoami", "myself");
Expand Down Expand Up @@ -74,6 +78,53 @@ void testWriteToLatestJson()
.withTimeout(100)
.withProcessPicker();

assertDoesNotThrow(() -> writer.writeToLatestJson(targetDir.toAbsolutePath()));
assertDoesNotThrow(() -> writer.write(LaunchJsonV0_2_0.INSTANCE, targetDir.toAbsolutePath()));

final String mismatch = assertDoesNotThrow(
() -> areFileContentsEqual(LaunchJsonV0_2_0.resolveLaunchFilePath(Paths.get(".", "src", "test", "resources", "v0_2_0")),
LaunchJsonV0_2_0.resolveLaunchFilePath(targetDir)));
assertNull(mismatch, mismatch);
}

/**
* @return null if same or description of mismatch
*/
private static String areFileContentsEqual(final Path expected, final Path testResult) throws IOException
{
try (BufferedReader expectedReader = Files.newBufferedReader(expected);
BufferedReader testReader = Files.newBufferedReader(testResult))
{
final String expectedName = expected.toAbsolutePath().normalize().toString();
final String testName = testResult.toAbsolutePath().normalize().toString();

long lineNumber = 1;
String expectedLine = "", testLine = "";
while ((expectedLine = expectedReader.readLine()) != null)
{
testLine = testReader.readLine();
if (testLine == null)
{
return "Generated file " + testName + " is shorter than expected test output: " + expectedName;
}
if (!expectedLine.equals(testLine))
{
return "Generated file " + testName +
" has mismatch on line " +
lineNumber +
" compared to expected test output: " +
expectedName;
}
lineNumber++;
}
if (testReader.readLine() == null)
{
// there are same
return null;
}
else
{
return "Generated file " + testName + " is longer than expected test output: " + expectedName;
}
}
}
}
71 changes: 71 additions & 0 deletions src/test/resources/v0_2_0/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"request": "launch",
"name": "launchTest",
"presentation": {
"group": "test",
"order": 0
},
"projectName": "coolProject",
"mainClass": "coolProject.Main",
"args": [
"--help",
"--version"
],
"modulePaths": [
"$Auto",
"${workspaceFolder}\\..\\test",
"!.\\exclude",
"$Runtime",
"$Test",
"${workspaceFolder}\\workspaceFolder",
"!${workspaceFolder}\\workspaceFolderExclude"
],
"classPaths": [],
"encoding": "UTF-16",
"vmArgs": [
"-Xmx4G"
],
"cwd": "${workspaceFolder}\\run",
"env": {
"whoami": "myself",
"whoru": "yourself"
},
"envFile": "${workspaceFolder}\\..\\envFile",
"stopOnEntry": true,
"console": "internalConsole",
"shortenCommandLine": "argfile"
},
{
"type": "java",
"request": "attach",
"name": "attachTest",
"presentation": {
"group": "test",
"order": 1
},
"projectName": "coolProject",
"hostName": "localhost",
"port": 55555,
"timeout": 100,
"processId": 10
},
{
"type": "java",
"request": "attach",
"name": "attachTestProjectPicker",
"presentation": {
"group": "test",
"order": 2
},
"projectName": "coolProject",
"hostName": "localhost",
"port": 55555,
"timeout": 100,
"processId": "${command:PickJavaProcess}"
}
]
}

0 comments on commit 8f2eda5

Please sign in to comment.