From 0af142990e4883c3e50c29a88afd5d2a436cc671 Mon Sep 17 00:00:00 2001 From: Hari Krishnan Date: Sun, 12 May 2024 15:30:26 +0530 Subject: [PATCH] evaluate env variables in git repo URI only when the value is available --- .../kotlin/in/specmatic/core/git/GitOperations.kt | 10 +++++++--- .../kotlin/in/specmatic/core/git/GitOperationsTest.kt | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt b/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt index 276446e3a..8e52f1484 100644 --- a/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt +++ b/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt @@ -119,9 +119,13 @@ fun evaluateEnvVariablesInGitRepoURI(gitRepositoryURI: String, environmentVariab val envVariableMatches = envVariableRegex.findAll(gitRepositoryURI) envVariableMatches.forEach { matchResult -> val envVariable = matchResult.groupValues[1] - val envVariableValue = environmentVariables.getValue(envVariable) - logger.log("Evaluating $envVariable in $gitRepositoryURI") - evaluatedGitRepoUrl = evaluatedGitRepoUrl.replace("\${$envVariable}", envVariableValue) + if (environmentVariables.containsKey(envVariable)) { + val envVariableValue = environmentVariables.getValue(envVariable) + logger.log("Evaluating $envVariable in $gitRepositoryURI") + evaluatedGitRepoUrl = evaluatedGitRepoUrl.replace("\${$envVariable}", envVariableValue) + } else { + logger.log("$envVariable in $gitRepositoryURI resembles an environment variable, but it is not available in the environment variables. Skipping evaluation.") + } } return evaluatedGitRepoUrl } diff --git a/core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt b/core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt index d4dc4c925..7fe212e40 100644 --- a/core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt +++ b/core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt @@ -30,4 +30,15 @@ class GitOperationsTest { ) assertThat(evaluatedGitRepositoryURI).isEqualTo("https://john:password@gitlab.com/group/project.git") } + + @Test + fun shouldNotEvaluatePatternsThatResembleEnvVarsWhenTheValueIsNotAvailable() { + val gitRepositoryURI = "https://gitlab-ci-token:${'$'}{CI_JOB_TOKEN}@gitlab.com/group/${'$'}{do-not-eval}/project.git" + val evaluatedGitRepositoryURI = + evaluateEnvVariablesInGitRepoURI( + gitRepositoryURI = gitRepositoryURI, + mapOf("CI_JOB_TOKEN" to "token") + ) + assertThat(evaluatedGitRepositoryURI).isEqualTo("https://gitlab-ci-token:token@gitlab.com/group/${'$'}{do-not-eval}/project.git") + } } \ No newline at end of file