Skip to content

Commit

Permalink
check
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 24, 2024
1 parent ce7e085 commit ea889ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/main/java/com/rultor/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
*/
public final class Env {

/**
* Environment variable.
*/
public static final String SETTINGS_XML = "SETTINGS_XML";

/**
* Private.
*/
Expand All @@ -59,7 +64,7 @@ private Env() {
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static String read(final String name) {
final String xml = System.getenv("SETTINGS_XML");
final String xml = System.getenv(Env.SETTINGS_XML);
final String ret;
if (xml == null) {
ret = Manifests.read(name);
Expand All @@ -69,17 +74,24 @@ public static String read(final String name) {
new TextOf(new ResourceOf(res))
).asString();
final Matcher matcher = Pattern.compile(
String.format("%s: \\$\\{([^}]+)}", name)
String.format("%s: (\\$\\{[^}]+}|[^\\s]+)", name)
).matcher(manifest);
if (!matcher.find()) {
throw new IllegalArgumentException(
String.format("Can't find '%s' in %s:%n%s", name, res, manifest)
);
}
final String prop = matcher.group(1);
ret = new XMLDocument(xml).xpath(
String.format("/settings//*[name()='%s']/text()", prop)
).get(0);
if (prop.startsWith("${")) {
ret = new XMLDocument(xml).xpath(
String.format(
"/settings//*[name()='%s']/text()",
prop.substring(2, prop.length() - 1)
)
).get(0);
} else {
ret = prop;
}
}
return ret;
}
Expand Down
29 changes: 26 additions & 3 deletions src/test/java/com/rultor/EnvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,28 @@ final class EnvTest {

@Test
void readsFromManifest() {
this.environment.remove("SETTINGS_XML");
this.environment.remove(Env.SETTINGS_XML);
MatcherAssert.assertThat(
"takes the right value",
Env.read("Rultor-SecurityKey"),
Matchers.equalTo("${failsafe.security.key}")
);
}

@Test
void readsRevisionFromManifest() {
this.environment.remove(Env.SETTINGS_XML);
MatcherAssert.assertThat(
"takes the right value",
Env.read("Rultor-Revision"),
Matchers.not(Matchers.emptyString())
);
}

@Test
void readsFromSettingsXml() {
this.environment.set(
"SETTINGS_XML",
Env.SETTINGS_XML,
String.join(
"",
"<settings><profiles><profile><properties>",
Expand All @@ -76,7 +86,7 @@ void readsFromSettingsXml() {
);
MatcherAssert.assertThat(
"has the right XML in env",
System.getenv("SETTINGS_XML"),
System.getenv(Env.SETTINGS_XML),
Matchers.not(Matchers.nullValue())
);
MatcherAssert.assertThat(
Expand All @@ -85,4 +95,17 @@ void readsFromSettingsXml() {
Matchers.equalTo("hello")
);
}

@Test
void readsRevisionFromSettingsXml() {
this.environment.set(
Env.SETTINGS_XML,
"<settings/>"
);
MatcherAssert.assertThat(
"takes the right value",
Env.read("Rultor-Revision"),
Matchers.not(Matchers.emptyString())
);
}
}

0 comments on commit ea889ec

Please sign in to comment.