diff --git a/services/teamcity.js b/services/teamcity.js index 8eca2e2..f411358 100644 --- a/services/teamcity.js +++ b/services/teamcity.js @@ -2,17 +2,18 @@ const javaProperties = require('java-properties'); -function getProperties(env) { - const file = env.TEAMCITY_BUILD_PROPERTIES_FILE; - if (!file) { - return {}; - } - const properties = javaProperties.of(file); - return { - root: properties.get('teamcity.build.workingDir'), - branch: properties.get('teamcity.build.branch'), - }; -} +const PROPERTIES_MAPPING = {root: 'teamcity.build.workingDir', branch: 'teamcity.build.branch'}; + +const getProperties = env => { + const properties = env.TEAMCITY_BUILD_PROPERTIES_FILE + ? javaProperties.of(env.TEAMCITY_BUILD_PROPERTIES_FILE) + : undefined; + + return Object.keys(PROPERTIES_MAPPING).reduce((result, key) => { + const property = properties ? properties.get(PROPERTIES_MAPPING[key]) : undefined; + return Object.assign(result, {[key]: typeof property === 'undefined' ? env[PROPERTIES_MAPPING[key]] : property}); + }, {}); +}; module.exports = { detect({env}) { diff --git a/test/services/teamcity.test.js b/test/services/teamcity.test.js index 55db9e4..fbef6fd 100644 --- a/test/services/teamcity.test.js +++ b/test/services/teamcity.test.js @@ -10,17 +10,7 @@ const env = { TEAMCITY_BUILDCONF_NAME: 'owner/repo', }; -test('Push (no properties file)', t => { - t.deepEqual(teamcity.configuration({env}), { - name: 'TeamCity', - service: 'teamcity', - commit: '5678', - build: '91011', - slug: 'owner/repo', - }); -}); - -test('Push', t => { +test('Push - with properties file', t => { const propertiesFile = tempy.file({extension: 'properties'}); const properties = ['teamcity.build.branch=master', 'teamcity.build.workingDir=/']; fs.writeFileSync(propertiesFile, properties.join('\n') + '\n'); @@ -35,3 +25,45 @@ test('Push', t => { slug: 'owner/repo', }); }); + +test('Push - without properties file', t => { + t.deepEqual( + teamcity.configuration({ + env: Object.assign({}, env, {'teamcity.build.workingDir': '/', 'teamcity.build.branch': 'master'}), + }), + { + name: 'TeamCity', + service: 'teamcity', + commit: '5678', + build: '91011', + branch: 'master', + root: '/', + slug: 'owner/repo', + } + ); +}); + +test('Push - prioritize properties file values', t => { + const propertiesFile = tempy.file({extension: 'properties'}); + const properties = ['teamcity.build.branch=master']; + fs.writeFileSync(propertiesFile, properties.join('\n') + '\n'); + + t.deepEqual( + teamcity.configuration({ + env: Object.assign({}, env, { + TEAMCITY_BUILD_PROPERTIES_FILE: propertiesFile, + 'teamcity.build.workingDir': '/other', + 'teamcity.build.branch': 'other', + }), + }), + { + name: 'TeamCity', + service: 'teamcity', + commit: '5678', + build: '91011', + branch: 'master', + root: '/other', + slug: 'owner/repo', + } + ); +});