From 49b4c623a7a438e0a10a3ab4dc4d31686eb69683 Mon Sep 17 00:00:00 2001 From: Tobias Heine Date: Thu, 8 Mar 2018 15:41:56 +0100 Subject: [PATCH] Introduce fallback for deprecated file api --- .../buildproperties/BuildProperties.groovy | 22 +++++++++++- .../BuildPropertiesTest.groovy | 35 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/groovy/com/novoda/buildproperties/BuildProperties.groovy b/plugin/src/main/groovy/com/novoda/buildproperties/BuildProperties.groovy index c576b66..60fb737 100644 --- a/plugin/src/main/groovy/com/novoda/buildproperties/BuildProperties.groovy +++ b/plugin/src/main/groovy/com/novoda/buildproperties/BuildProperties.groovy @@ -3,16 +3,19 @@ package com.novoda.buildproperties import com.novoda.buildproperties.internal.DefaultEntriesFactory import com.novoda.buildproperties.internal.DefaultExceptionFactory import org.gradle.api.Project +import org.gradle.api.logging.Logger class BuildProperties { private final String name private final DefaultEntriesFactory factory + private final Logger logger private EntriesChain chain BuildProperties(String name, Project project) { this.name = name - this.factory = new DefaultEntriesFactory(project.logger, new DefaultExceptionFactory(name)) + this.logger = project.logger + this.factory = new DefaultEntriesFactory(logger, new DefaultExceptionFactory(name)) } String getName() { @@ -67,6 +70,23 @@ class BuildProperties { newChain(project) } + EntriesChain file(File file, String description = '') { + logger.warn("""/!\\ WARNING /!\\ Detected use of 'file' method to consume build properties from a file. + | This api is deprecated and will be removed in an upcoming release, please use using() instead. + | For instance you could do: + | + | buildProperties { + | using secrets.properties + | } + | + |""".stripMargin()) + + if (!description.isEmpty()) { + setDescription description + } + newChain(file) + } + private EntriesChain newChain(def source) { chain = new EntriesChain(factory, source) return chain diff --git a/plugin/src/test/groovy/com/novoda/buildproperties/BuildPropertiesTest.groovy b/plugin/src/test/groovy/com/novoda/buildproperties/BuildPropertiesTest.groovy index 7ec3d83..15c831a 100644 --- a/plugin/src/test/groovy/com/novoda/buildproperties/BuildPropertiesTest.groovy +++ b/plugin/src/test/groovy/com/novoda/buildproperties/BuildPropertiesTest.groovy @@ -139,6 +139,41 @@ class BuildPropertiesTest { assertThat(project.buildProperties.test['f']).hasValue('value_f') } + @Test + void shouldProvideSpecifiedErrorMessageWhenAccessingNonExistingPropertyUsingLegacyApi() { + File propertiesFile = newPropertiesFile('test.properties', 'd=value_d\ne=value_e\nf=value_f') + + project.buildProperties { + test { + file(propertiesFile) + } + } + + assertThat(project.buildProperties.test['d']).hasValue('value_d') + assertThat(project.buildProperties.test['e']).hasValue('value_e') + assertThat(project.buildProperties.test['f']).hasValue('value_f') + } + + @Test + void shouldReturnPropertiesFileValuesUsingDeprecatedFileApi() { + File propertiesFile = newPropertiesFile('test.properties', 'd=value_d\ne=value_e\nf=value_f') + + project.buildProperties { + test { + file(propertiesFile, "This is an error message") + } + } + + try { + project.buildProperties.test['a'].string + fail('Exception not thrown') + } catch (Exception e){ + String message = e.getMessage() + assertThat(message).contains('Unable to find value for key \'a\' in properties set \'buildProperties.test\'.') + assertThat(message).contains('* buildProperties.test: This is an error message') + } + } + @Test void shouldReturnFallbackValuesWhenChainOfEntries() { File propertiesFile = newPropertiesFile('test.properties', 'd=value_d\ne=value_e\nf=value_f')