Skip to content

Commit de4d7e5

Browse files
Copilotkwin
andcommitted
Address code review feedback: make buildHelperCommand static, add Windows batch helper, remove @DisabledOnOs checks
Co-authored-by: kwin <[email protected]>
1 parent b2c3350 commit de4d7e5

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/GitCredentialHelperMasterSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private String retrievePassword(String helperName, String url) throws IOExceptio
240240
return password;
241241
}
242242

243-
private String buildHelperCommand(String helperName) {
243+
static String buildHelperCommand(String helperName) {
244244
// If helper name contains a path separator, use it as-is (absolute or relative path)
245245
// Otherwise, prefix with "git-credential-"
246246
if (helperName.contains("/") || helperName.contains("\\")) {

src/test/java/org/codehaus/plexus/components/secdispatcher/internal/sources/GitCredentialHelperMasterSourceTest.java

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,40 @@ class GitCredentialHelperMasterSourceTest {
4747
@BeforeAll
4848
static void setup() throws IOException {
4949
// Create a mock git credential helper script
50-
mockHelperPath = tempDir.resolve("mock-git-credential-helper");
51-
String script = "#!/bin/sh\n"
52-
+ "if [ \"$1\" = \"get\" ]; then\n"
53-
+ " # Read input (we don't actually parse it in this simple mock)\n"
54-
+ " while IFS= read -r line; do\n"
55-
+ " [ -z \"$line\" ] && break\n"
56-
+ " done\n"
57-
+ " # Return mock credentials\n"
58-
+ " echo \"protocol=https\"\n"
59-
+ " echo \"host=maven.apache.org\"\n"
60-
+ " echo \"username=testuser\"\n"
61-
+ " echo \"password=testPassword123\"\n"
62-
+ "fi\n";
63-
64-
Files.writeString(mockHelperPath, script);
65-
// Make it executable
50+
// On Windows, create a batch file; on Unix-like systems, create a shell script
6651
if (System.getProperty("os.name").toLowerCase().contains("win")) {
67-
// On Windows, we can't easily make shell scripts executable, skip this test setup
52+
mockHelperPath = tempDir.resolve("mock-git-credential-helper.bat");
53+
String batchScript = "@echo off\r\n"
54+
+ "if \"%1\"==\"get\" (\r\n"
55+
+ " REM Read input until empty line\r\n"
56+
+ " :loop\r\n"
57+
+ " set /p line=\r\n"
58+
+ " if not defined line goto output\r\n"
59+
+ " goto loop\r\n"
60+
+ " :output\r\n"
61+
+ " echo protocol=https\r\n"
62+
+ " echo host=maven.apache.org\r\n"
63+
+ " echo username=testuser\r\n"
64+
+ " echo password=testPassword123\r\n"
65+
+ ")\r\n";
66+
Files.writeString(mockHelperPath, batchScript);
6867
} else {
68+
mockHelperPath = tempDir.resolve("mock-git-credential-helper");
69+
String script = "#!/bin/sh\n"
70+
+ "if [ \"$1\" = \"get\" ]; then\n"
71+
+ " # Read input (we don't actually parse it in this simple mock)\n"
72+
+ " while IFS= read -r line; do\n"
73+
+ " [ -z \"$line\" ] && break\n"
74+
+ " done\n"
75+
+ " # Return mock credentials\n"
76+
+ " echo \"protocol=https\"\n"
77+
+ " echo \"host=maven.apache.org\"\n"
78+
+ " echo \"username=testuser\"\n"
79+
+ " echo \"password=testPassword123\"\n"
80+
+ "fi\n";
81+
82+
Files.writeString(mockHelperPath, script);
83+
// Make it executable on Unix-like systems
6984
Files.setPosixFilePermissions(
7085
mockHelperPath,
7186
Set.of(
@@ -119,11 +134,6 @@ void testHandleThrowsExceptionForInvalidUrlParameter() {
119134

120135
@Test
121136
void testHandleWithMockHelper() throws SecDispatcherException {
122-
// Skip on Windows as shell script execution is problematic
123-
if (System.getProperty("os.name").toLowerCase().contains("win")) {
124-
return;
125-
}
126-
127137
GitCredentialHelperMasterSource source = new GitCredentialHelperMasterSource();
128138

129139
String config = mockHelperPath.toString() + "?url=https://maven.apache.org/master";
@@ -166,11 +176,6 @@ void testValidateConfigurationWithNonMatchingPrefix() {
166176

167177
@Test
168178
void testValidateConfigurationWithMockHelper() {
169-
// Skip on Windows
170-
if (System.getProperty("os.name").toLowerCase().contains("win")) {
171-
return;
172-
}
173-
174179
GitCredentialHelperMasterSource source = new GitCredentialHelperMasterSource();
175180

176181
String config = mockHelperPath.toString() + "?url=https://maven.apache.org/master";
@@ -182,33 +187,20 @@ void testValidateConfigurationWithMockHelper() {
182187
}
183188

184189
@Test
185-
void testBuildHelperCommandWithShortName() throws Exception {
186-
GitCredentialHelperMasterSource source = new GitCredentialHelperMasterSource();
187-
188-
// Use reflection to test the private buildHelperCommand method
189-
java.lang.reflect.Method method =
190-
GitCredentialHelperMasterSource.class.getDeclaredMethod("buildHelperCommand", String.class);
191-
method.setAccessible(true);
192-
193-
String result = (String) method.invoke(source, "cache");
190+
void testBuildHelperCommandWithShortName() {
191+
String result = GitCredentialHelperMasterSource.buildHelperCommand("cache");
194192
assertEquals("git-credential-cache", result);
195193

196-
result = (String) method.invoke(source, "store");
194+
result = GitCredentialHelperMasterSource.buildHelperCommand("store");
197195
assertEquals("git-credential-store", result);
198196
}
199197

200198
@Test
201-
void testBuildHelperCommandWithPath() throws Exception {
202-
GitCredentialHelperMasterSource source = new GitCredentialHelperMasterSource();
203-
204-
java.lang.reflect.Method method =
205-
GitCredentialHelperMasterSource.class.getDeclaredMethod("buildHelperCommand", String.class);
206-
method.setAccessible(true);
207-
208-
String result = (String) method.invoke(source, "/usr/local/bin/git-credential-osxkeychain");
199+
void testBuildHelperCommandWithPath() {
200+
String result = GitCredentialHelperMasterSource.buildHelperCommand("/usr/local/bin/git-credential-osxkeychain");
209201
assertEquals("/usr/local/bin/git-credential-osxkeychain", result);
210202

211-
result = (String) method.invoke(source, "./relative/path/helper");
203+
result = GitCredentialHelperMasterSource.buildHelperCommand("./relative/path/helper");
212204
assertEquals("./relative/path/helper", result);
213205
}
214206
}

0 commit comments

Comments
 (0)