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

Shorten test resource paths #1743

Merged
merged 10 commits into from
Nov 17, 2023
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
4 changes: 2 additions & 2 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1277,9 +1277,9 @@ public void testCheckMembership() throws Exception {
*/
@Test
public void testRef() throws IOException {
GHRef mainRef = gitHub.getRepository("jenkinsci/jenkins").getRef("heads/main");
GHRef mainRef = gitHub.getRepository("jenkinsci/jenkins").getRef("heads/master");
assertThat(mainRef.getUrl().toString(),
equalTo(mockGitHub.apiServer().baseUrl() + "/repos/jenkinsci/jenkins/git/refs/heads/main"));
equalTo(mockGitHub.apiServer().baseUrl() + "/repos/jenkinsci/jenkins/git/refs/heads/master"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected WireMockConfiguration getWireMockOptions() {
* the exception
*/
@Test
public void testRetriedRequestGetsNewAuthorizationTokenWhenOldOneExpires() throws IOException {
public void testNewWhenOldOneExpires() throws IOException {
snapshotNotAllowed();
gitHub = getGitHubBuilder().withAuthorizationProvider(new RefreshingAuthorizationProvider())
.withEndpoint(mockGitHub.apiServer().baseUrl())
Expand All @@ -55,7 +55,7 @@ public void testRetriedRequestGetsNewAuthorizationTokenWhenOldOneExpires() throw
* the exception
*/
@Test
public void testRetriedRequestDoesNotGetNewAuthorizationTokenWhenOldOneIsStillValid() throws IOException {
public void testNotNewWhenOldOneIsStillValid() throws IOException {
gitHub = getGitHubBuilder().withAuthorizationProvider(() -> "original token")
.withEndpoint(mockGitHub.apiServer().baseUrl())
.withRateLimitHandler(RateLimitHandler.WAIT)
Expand Down
57 changes: 39 additions & 18 deletions src/test/java/org/kohsuke/github/junit/GitHubWireMockRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -321,16 +322,16 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
// Update all
Files.walk(path).forEach(filePath -> {
try {
Map.Entry<String, String> entry = getId(filePath, idToIndex);
if (entry != null) {
filePath = renameFileToIndex(filePath, entry);
}
// For raw server, only fix up mapping files
if (isRawServer && !filePath.toString().contains("mappings")) {
return;
}

if (filePath.toString().endsWith(".json")) {
String fileText = new String(Files.readAllBytes(filePath));
Path renamedFilePath = renameFile(filePath, idToIndex);
Path targetFilePath = renamedFilePath == null ? filePath : renamedFilePath;

String fileText = new String(Files.readAllBytes(targetFilePath));
// while recording responses we replaced all github calls localhost
// now we reverse that for storage.
fileText = fileText.replace(this.apiServer().baseUrl(), "https://api.github.com");
Expand All @@ -354,14 +355,15 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
}

// point bodyFile in the mapping to the renamed body file
if (entry != null && filePath.toString().contains("mappings")) {
fileText = fileText.replace("-" + entry.getKey(), "-" + entry.getValue());
if (renamedFilePath != null && filePath.toString().contains("mappings")) {
fileText = fileText.replace(filePath.getFileName().toString(),
renamedFilePath.getFileName().toString());
}

// Can be Array or Map
Object parsedObject = g.fromJson(fileText, Object.class);
fileText = g.toJson(parsedObject);
Files.write(filePath, fileText.getBytes());
String outputFileText = g.toJson(parsedObject);
Files.write(targetFilePath, outputFileText.getBytes());
}
} catch (Exception e) {
throw new RuntimeException("Files could not be written: " + filePath.toString(), e);
Expand All @@ -380,22 +382,42 @@ private void addMappingId(Map<String, Object> parsedObject, Map<String, String>
}
}

private Map.Entry<String, String> getId(Path filePath, Map<String, String> idToIndex) throws IOException {
Path targetPath = filePath;
String filePathString = filePath.toString();
private Map.Entry<String, String> getId(String fileName, Map<String, String> idToIndex) throws IOException {
for (Map.Entry<String, String> item : idToIndex.entrySet()) {
if (filePathString.contains(item.getKey())) {
if (fileName.contains(item.getKey())) {
return item;
}
}
return null;
}

private Path renameFileToIndex(Path filePath, Map.Entry<String, String> idToIndex) throws IOException {
String filePathString = filePath.toString();
Path targetPath = new File(filePathString.replace(idToIndex.getKey(), idToIndex.getValue())).toPath();
Files.move(filePath, targetPath);
private Path renameFile(Path filePath, Map<String, String> idToIndex) throws IOException {
Path targetPath = null;
String fileName = filePath.getFileName().toString();

// Short early segments of the file name
// which tend to be "repos_hub4j-test-org_{repository}".
fileName = fileName.replaceAll("^([a-zA-Z])[^_]+_([a-zA-Z])[^_]+_([a-zA-Z])[^_]+_", "$1_$2_$3_");
fileName = fileName.replaceAll("^([a-zA-Z])[^_]+_([a-zA-Z])[^_]+_", "$1_$2_");

Map.Entry<String, String> idToIndexEntry = getId(fileName, idToIndex);
if (idToIndexEntry != null) {
fileName = fileName.replace("-" + idToIndexEntry.getKey(), "");
// put index number on the front for clarity
fileName = idToIndexEntry.getValue() + "-" + fileName;
}

// Replace GUID strings in file paths with abbreviated GUID to limit file path length for windows
fileName = fileName.replaceAll("(_[a-f0-9]{8})[a-f0-9]{32}([_.])", "$1$2");

// If the file name is still longer than 60 characters, truncate it
fileName = fileName.replaceAll("^([^.]{60})[^.]+\\.", "$1.");

String renamedFilePathString = Paths.get(filePath.getParent().toString(), fileName).toString();
if (renamedFilePathString != filePath.toString()) {
targetPath = new File(renamedFilePathString).toPath();
Files.move(filePath, targetPath);
}
return targetPath;
}

Expand Down Expand Up @@ -503,5 +525,4 @@ public String getName() {
return "github-api-url-rewrite";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"id": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
"name": "user",
"request": {
"url": "/user",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "application/vnd.github.v3+json"
}
}
},
"response": {
"status": 200,
"bodyFileName": "1-user.json",
"headers": {
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4930",
"X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding"
],
"ETag": "W/\"1cb30f031c67c499473b3aad01c7f7a5\"",
"Last-Modified": "Thu, 06 Feb 2020 17:29:39 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "unknown, github.v3",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "CC37:2605:3F884:4E941:5E3C5BFC"
}
},
"uuid": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
"persistent": true,
"insertionIndex": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": "574da117-6845-46d8-b2c1-4415546ca670",
"name": "repos_hub4j-test-org_temp-testratelimithandler_fail",
"request": {
"url": "/repos/hub4j-test-org/temp-testHandler_Fail",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "application/vnd.github.v3+json"
}
}
},
"response": {
"status": 200,
"bodyFileName": "3-r_h_t_fail.json",
"headers": {
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4922",
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding"
],
"ETag": "W/\"858224998ac7d1fd6dcd43f73d375297\"",
"Last-Modified": "Thu, 06 Feb 2020 18:33:43 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "repo",
"X-GitHub-Media-Type": "unknown, github.v3",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "CC37:2605:3FADC:4EA8C:5E3C5C02"
}
},
"uuid": "574da117-6845-46d8-b2c1-4415546ca670",
"persistent": true,
"scenarioName": "scenario-1-repos-hub4j-test-org-temp-testHandler_Fail",
"requiredScenarioState": "scenario-1-repos-hub4j-test-org-temp-testHandler_Fail-2",
"insertionIndex": 3
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"id": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
"name": "user",
"request": {
"url": "/user",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "application/vnd.github.v3+json"
}
}
},
"response": {
"status": 200,
"bodyFileName": "1-user.json",
"headers": {
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4930",
"X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding"
],
"ETag": "W/\"1cb30f031c67c499473b3aad01c7f7a5\"",
"Last-Modified": "Thu, 06 Feb 2020 17:29:39 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "unknown, github.v3",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "CC37:2605:3F884:4E941:5E3C5BFC"
}
},
"uuid": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
"persistent": true,
"insertionIndex": 1
}
Loading