Skip to content

Commit

Permalink
Add method to escape secret chars for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Aug 21, 2024
1 parent 8f37c66 commit b39e0ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.smallrye.common.os.OS;

public class QuarkusConfigCommandResult {

final String applicationPropertiesAsString;
Expand All @@ -19,10 +21,24 @@ public QuarkusConfigCommandResult assertCommandOutputNotContains(String expected
}

public QuarkusConfigCommandResult assertCommandOutputContains(String expected) {
assertTrue(output.contains(expected.trim()), "Expected output '" + output + "' does not contain '" + expected + "'");
if (OS.WINDOWS.isCurrent()) {
String windowsEscapedExpected = normalizeString(expected);
String windowsEscapedOutput = normalizeString(output);

assertTrue(windowsEscapedOutput.contains(windowsEscapedExpected),
"Expected output '" + windowsEscapedExpected + "'does not contain '" + windowsEscapedOutput + "'");
} else {
assertTrue(output.contains(expected.trim()),
"Expected output '" + output + "' does not contain '" + expected + "'");
}
return this;
}

private String normalizeString(String str) {
String noAnsi = str.replaceAll("\\x1B\\[[;\\d]*m", "");
return noAnsi.replaceAll("\"", "").replaceAll("\n", " ").trim();
}

public QuarkusConfigCommandResult assertApplicationPropertiesContains(String str) {
assertTrue(applicationPropertiesAsString.contains(str),
"Expected value '" + str + "' is missing in application.properties: " + applicationPropertiesAsString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import org.junit.jupiter.api.Assertions;

import io.quarkus.test.logging.Log;
import io.quarkus.test.util.QuarkusCLIUtils;
import io.quarkus.test.utils.Command;
import io.quarkus.test.utils.FileUtils;
import io.smallrye.common.os.OS;

public class QuarkusEncryptConfigCommandBuilder {

Expand All @@ -31,7 +33,12 @@ public class QuarkusEncryptConfigCommandBuilder {
}

public QuarkusEncryptConfigCommandBuilder secret(String secret) {
this.secret = secret;
if (OS.WINDOWS.isCurrent()) {
this.secret = QuarkusCLIUtils.escapeSecretCharsForWindows(secret);
} else {
this.secret = secret;
}

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,13 @@ public String toString() {
return "Plugin {groupId=" + getGroupId() + ", artifactId=" + getArtifactId() + ", version=" + getVersion() + "}";
}
}

/**
* Escapes a command-line secret chars for Windows OS.
*/
public static String escapeSecretCharsForWindows(String secret) {
return "\"" + secret
.replace("\"", "\\\"")
+ "\"";
}
}

0 comments on commit b39e0ac

Please sign in to comment.