From 2264d8bb981be85ffa2e0d8f0169b06e915df526 Mon Sep 17 00:00:00 2001 From: rozetko Date: Wed, 12 Dec 2018 09:47:38 +0300 Subject: [PATCH] Failed node.js test: no .git folder #314 (#318) * Check for branch file existence * Mock config.ts --- server/spec/setup_tests.ts | 6 ++++++ server/src/config.ts | 13 +++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/spec/setup_tests.ts b/server/spec/setup_tests.ts index 89d2823..f190669 100644 --- a/server/spec/setup_tests.ts +++ b/server/spec/setup_tests.ts @@ -1,2 +1,8 @@ console.log = jest.fn(); console.error = jest.fn(); + +jest.mock('../src/config.ts', () => ({ + HASTIC_API_KEY: 'fake-key', + DATA_PATH: 'fake-data-path', + ZMQ_IPC_PATH: 'fake-zmq-path' +})); diff --git a/server/src/config.ts b/server/src/config.ts index 8d58102..9a07d3e 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -72,13 +72,18 @@ function getGitInfo() { let gitRoot = path.join(__dirname, '../../.git'); let gitHeadFile = path.join(gitRoot, 'HEAD'); if(!fs.existsSync(gitHeadFile)) { - console.debug(`Can't find git HEAD file ${gitHeadFile}`); + console.error(`Can't find git HEAD file ${gitHeadFile}`); return null; } - const rev = fs.readFileSync(gitHeadFile).toString(); - let branchPath = rev.indexOf(':') === -1 ? rev : rev.slice(5, -1); + const ref = fs.readFileSync(gitHeadFile).toString(); + let branchPath = ref.indexOf(':') === -1 ? ref : ref.slice(5, -1); let branch = branchPath.split('/').pop(); - let commitHash = fs.readFileSync(`${gitRoot}/${branchPath}`).toString().slice(0, 7); + const branchFilename = `${gitRoot}/${branchPath}`; + if(!fs.existsSync(branchFilename)) { + console.error(`Can't find git branch file ${branchFilename}`); + return null; + } + let commitHash = fs.readFileSync(branchFilename).toString().slice(0, 7); return { branch, commitHash }; }