From ea889ec8c3ee2298d98a2898f5fd8bf7e958471b Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Tue, 24 Dec 2024 14:16:14 +0300 Subject: [PATCH] check --- src/main/java/com/rultor/Env.java | 22 +++++++++++++++----- src/test/java/com/rultor/EnvTest.java | 29 ++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/rultor/Env.java b/src/main/java/com/rultor/Env.java index 20a5e91da7..e04db5accb 100644 --- a/src/main/java/com/rultor/Env.java +++ b/src/main/java/com/rultor/Env.java @@ -45,6 +45,11 @@ */ public final class Env { + /** + * Environment variable. + */ + public static final String SETTINGS_XML = "SETTINGS_XML"; + /** * Private. */ @@ -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); @@ -69,7 +74,7 @@ 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( @@ -77,9 +82,16 @@ public static String read(final String name) { ); } 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; } diff --git a/src/test/java/com/rultor/EnvTest.java b/src/test/java/com/rultor/EnvTest.java index cafa601121..10f7ea1e18 100644 --- a/src/test/java/com/rultor/EnvTest.java +++ b/src/test/java/com/rultor/EnvTest.java @@ -55,7 +55,7 @@ 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"), @@ -63,10 +63,20 @@ void readsFromManifest() { ); } + @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( "", "", @@ -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( @@ -85,4 +95,17 @@ void readsFromSettingsXml() { Matchers.equalTo("hello") ); } + + @Test + void readsRevisionFromSettingsXml() { + this.environment.set( + Env.SETTINGS_XML, + "" + ); + MatcherAssert.assertThat( + "takes the right value", + Env.read("Rultor-Revision"), + Matchers.not(Matchers.emptyString()) + ); + } }