Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Introduce fallback for deprecated file api
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasheine committed Mar 8, 2018
1 parent c742c6d commit 49b4c62
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 49b4c62

Please sign in to comment.