diff --git a/services/teamcity.js b/services/teamcity.js index 11835bc..ebd0d59 100644 --- a/services/teamcity.js +++ b/services/teamcity.js @@ -1,22 +1,32 @@ // https://confluence.jetbrains.com/display/TCD10/Predefined+Build+Parameters const javaProperties = require('java-properties'); +const {branch} = require('../lib/git'); const PROPERTIES_MAPPING = {root: 'teamcity.build.workingDir', branch: 'teamcity.build.branch'}; -const getProperties = env => { +const safeReadProperties = filePath => { + try { + return javaProperties.of(filePath); + } catch (error) { + return undefined; + } +}; + +const getProperties = ({env, cwd}) => { const buildProperties = env.TEAMCITY_BUILD_PROPERTIES_FILE - ? javaProperties.of(env.TEAMCITY_BUILD_PROPERTIES_FILE) + ? safeReadProperties(env.TEAMCITY_BUILD_PROPERTIES_FILE) : undefined; const configFile = buildProperties ? buildProperties.get('teamcity.configuration.properties.file') : undefined; - const configProperties = configFile ? javaProperties.of(configFile) : configFile; + const configProperties = configFile ? safeReadProperties(configFile) : configFile; return Object.keys(PROPERTIES_MAPPING).reduce( (result, key) => Object.assign(result, { [key]: (buildProperties ? buildProperties.get(PROPERTIES_MAPPING[key]) : undefined) || - (configProperties ? configProperties.get(PROPERTIES_MAPPING[key]) : undefined), + (configProperties ? configProperties.get(PROPERTIES_MAPPING[key]) : undefined) || + (key === 'branch' ? branch({env, cwd}) : undefined), }), {} ); @@ -26,7 +36,7 @@ module.exports = { detect({env}) { return Boolean(env.TEAMCITY_VERSION); }, - configuration({env}) { + configuration({env, cwd}) { return Object.assign( { name: 'TeamCity', @@ -35,7 +45,7 @@ module.exports = { build: env.BUILD_NUMBER, slug: env.TEAMCITY_BUILDCONF_NAME, }, - getProperties(env) + getProperties({env, cwd}) ); }, }; diff --git a/test/services/teamcity.test.js b/test/services/teamcity.test.js index 62ea583..8d50eb1 100644 --- a/test/services/teamcity.test.js +++ b/test/services/teamcity.test.js @@ -2,6 +2,7 @@ import fs from 'fs'; import test from 'ava'; import tempy from 'tempy'; import teamcity from '../../services/teamcity'; +import {gitRepo} from '../helpers/git-utils'; const env = { TEAMCITY_VERSION: '2017.1.2 (build 46812)', @@ -26,18 +27,6 @@ test('Push - with build properties file', t => { }); }); -test('Push - without build properties file', t => { - t.deepEqual(teamcity.configuration({env}), { - name: 'TeamCity', - service: 'teamcity', - commit: '5678', - build: '91011', - branch: undefined, - root: undefined, - slug: 'owner/repo', - }); -}); - test('Push - with build and config properties files', t => { const buildFile = tempy.file({extension: 'properties'}); const configFile = tempy.file({extension: 'properties'}); @@ -79,3 +68,52 @@ test('Push - prioritize build properties file values', t => { slug: 'owner/repo', }); }); + +test('Push - without build properties file', async t => { + const {cwd} = await gitRepo(true); + + t.deepEqual(teamcity.configuration({env, cwd}), { + name: 'TeamCity', + service: 'teamcity', + commit: '5678', + build: '91011', + branch: 'master', + root: undefined, + slug: 'owner/repo', + }); +}); + +test('Push - with build and missing config properties files', async t => { + const {cwd} = await gitRepo(true); + + const buildFile = tempy.file({extension: 'properties'}); + const buildProperties = ['teamcity.build.branch=master', 'teamcity.configuration.properties.file=/tmp/null']; + fs.writeFileSync(buildFile, buildProperties.join('\n') + '\n'); + + t.deepEqual(teamcity.configuration({env: Object.assign({}, env, {TEAMCITY_BUILD_PROPERTIES_FILE: buildFile}), cwd}), { + name: 'TeamCity', + service: 'teamcity', + commit: '5678', + build: '91011', + branch: 'master', + root: undefined, + slug: 'owner/repo', + }); +}); + +test('Push - with missing build properties files', async t => { + const {cwd} = await gitRepo(true); + + t.deepEqual( + teamcity.configuration({env: Object.assign({}, env, {TEAMCITY_BUILD_PROPERTIES_FILE: '/tmp/null'}), cwd}), + { + name: 'TeamCity', + service: 'teamcity', + commit: '5678', + build: '91011', + branch: 'master', + root: undefined, + slug: 'owner/repo', + } + ); +});