Skip to content

Commit 455b189

Browse files
committed
Code to write all files
1 parent f0a3695 commit 455b189

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

src/test/java/org/kohsuke/github/junit/GitHubWireMockRule.java

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.lang.reflect.Type;
1717
import java.nio.file.Files;
1818
import java.nio.file.Path;
19+
import java.nio.file.Paths;
20+
import java.nio.file.StandardCopyOption;
1921
import java.util.Collection;
2022
import java.util.HashMap;
2123
import java.util.Map;
@@ -220,9 +222,10 @@ protected void before() {
220222
@Override
221223
protected void after() {
222224
super.after();
223-
if (!isTakeSnapshot()) {
224-
return;
225-
}
225+
// TEMP
226+
// if (!isTakeSnapshot()) {
227+
// return;
228+
// }
226229

227230
recordSnapshot(this.apiServer(), "https://api.github.com", false);
228231

@@ -318,6 +321,7 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
318321
}
319322
});
320323

324+
int longestFileName = 0;
321325
// Update all
322326
Files.walk(path).forEach(filePath -> {
323327
try {
@@ -326,10 +330,10 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
326330
return;
327331
}
328332

329-
Path renamedFilePath = renameFile(filePath, idToIndex);
330-
Path targetFilePath = renamedFilePath == null ? filePath : renamedFilePath;
331-
332333
if (filePath.toString().endsWith(".json")) {
334+
Path renamedFilePath = renameFile(filePath, idToIndex);
335+
Path targetFilePath = renamedFilePath == null ? filePath : renamedFilePath;
336+
333337
String fileText = new String(Files.readAllBytes(targetFilePath));
334338
// while recording responses we replaced all github calls localhost
335339
// now we reverse that for storage.
@@ -361,8 +365,11 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
361365

362366
// Can be Array or Map
363367
Object parsedObject = g.fromJson(fileText, Object.class);
364-
fileText = g.toJson(parsedObject);
365-
Files.write(targetFilePath, fileText.getBytes());
368+
String outputFileText = g.toJson(parsedObject);
369+
if (fileText.endsWith("\n")) {
370+
outputFileText += "\n";
371+
}
372+
Files.write(targetFilePath, outputFileText.getBytes());
366373
}
367374
} catch (Exception e) {
368375
throw new RuntimeException("Files could not be written: " + filePath.toString(), e);
@@ -381,10 +388,9 @@ private void addMappingId(Map<String, Object> parsedObject, Map<String, String>
381388
}
382389
}
383390

384-
private Map.Entry<String, String> getId(Path filePath, Map<String, String> idToIndex) throws IOException {
385-
String filePathString = filePath.toString();
391+
private Map.Entry<String, String> getId(String fileName, Map<String, String> idToIndex) throws IOException {
386392
for (Map.Entry<String, String> item : idToIndex.entrySet()) {
387-
if (filePathString.contains(item.getKey())) {
393+
if (fileName.contains(item.getKey())) {
388394
return item;
389395
}
390396
}
@@ -393,19 +399,40 @@ private Map.Entry<String, String> getId(Path filePath, Map<String, String> idToI
393399

394400
private Path renameFile(Path filePath, Map<String, String> idToIndex) throws IOException {
395401
Path targetPath = null;
396-
String renamedFilePathString = filePath.toString();
402+
String fileName = filePath.getFileName().toString();
403+
404+
// Short early segments of the file name
405+
// which tend to be "repos_hub4j-test-org_{repository}".
406+
fileName = fileName.replaceAll("^([a-zA-Z])[^_]+_([a-zA-Z])[^_]+_([a-zA-Z])[^_]+_", "$1_$2_$3_");
407+
fileName = fileName.replaceAll("^([a-zA-Z])[^_]+_([a-zA-Z])[^_]+_", "$1_$2_");
397408

398-
Map.Entry<String, String> idToIndexEntry = getId(filePath, idToIndex);
409+
Map.Entry<String, String> idToIndexEntry = getId(fileName, idToIndex);
399410
if (idToIndexEntry != null) {
400-
renamedFilePathString = renamedFilePathString.replace(idToIndexEntry.getKey(), idToIndexEntry.getValue());
411+
fileName = fileName.replace("-" + idToIndexEntry.getKey(), "");
412+
// put index number on the front for clarity
413+
fileName = idToIndexEntry.getValue() + "-" + fileName;
401414
}
402415

403416
// Replace GUID strings in file paths with abbreviated GUID to limit file path length for windows
404-
renamedFilePathString = renamedFilePathString.replaceAll("(_[a-f0-9]{8})[a-f0-9]{32}([-_])", "$1$2");
417+
fileName = fileName.replaceAll("(_[a-f0-9]{8})[a-f0-9]{32}([_.])", "$1$2");
418+
419+
// TEMP
420+
// Move all index numbers to the front of the file name for clarity
421+
fileName = fileName.replaceAll("^(.+?)-([0-9]+)\\.", "$2-$1.");
422+
// Short early segments of the file name
423+
// which tend to be "repos_hub4j-test-org_{repository}".
424+
fileName = fileName.replaceAll("^([0-9]+-[a-zA-Z])[^_]+_([a-zA-Z])[^_]+_([a-zA-Z])[^_]+_", "$1_$2_$3_");
425+
fileName = fileName.replaceAll("^([0-9]+-[a-zA-Z])[^_]+_([a-zA-Z])[^_]+_", "$1_$2_");
426+
427+
// If the file name is still longer than 60 characters, truncate it
428+
fileName = fileName.replaceAll("^([^.]{60})[^.]+\\.", "$1.");
405429

430+
String renamedFilePathString = Paths.get(filePath.getParent().toString(), fileName).toString();
406431
if (renamedFilePathString != filePath.toString()) {
407432
targetPath = new File(renamedFilePathString).toPath();
408-
Files.move(filePath, targetPath);
433+
// Files.move(filePath, targetPath);
434+
// TEMP
435+
Files.move(filePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
409436
}
410437
return targetPath;
411438
}

0 commit comments

Comments
 (0)