diff --git a/.taprc b/.taprc index 381688a..05802f2 100644 --- a/.taprc +++ b/.taprc @@ -1 +1,4 @@ -check-coverage: false \ No newline at end of file +check-coverage: false +files: + - 'index.test.js' + - 'lib/**/*.test.js' diff --git a/test.js b/index.test.js similarity index 99% rename from test.js rename to index.test.js index 22143d0..4e4866d 100644 --- a/test.js +++ b/index.test.js @@ -1,6 +1,6 @@ 'use strict' +const Hook = require('.') const t = require('tap') -const Hook = require('./') const tty = require('tty') const ttySupportColor = tty.isatty(process.stdout.fd) diff --git a/install.js b/install.js index 872f00b..cfc3a87 100644 --- a/install.js +++ b/install.js @@ -8,6 +8,7 @@ const path = require('path') const os = require('os') const hook = path.join(__dirname, 'hook') const root = path.resolve(__dirname, '..', '..', '..') +const getFolderInPath = require('./lib/get-folder-in-path') const exists = fs.existsSync || path.existsSync // @@ -17,33 +18,10 @@ const exists = fs.existsSync || path.existsSync // to work correctly. // -let git = getGitFolderPath(root) +let git = getFolderInPath('.git', root, console) let hooks let precommit -// Function to recursively finding .git folder -function getGitFolderPath (currentPath) { - const git = path.resolve(currentPath, '.git') - - if (!exists(git) || !fs.lstatSync(git).isDirectory()) { - console.log('pre-commit:') - console.log('pre-commit: Not found .git folder in', git) - - const newPath = path.resolve(currentPath, '..') - - // Stop if we on top folder - if (currentPath === newPath) { - return null - } - - return getGitFolderPath(newPath) - } - - console.log('pre-commit:') - console.log('pre-commit: Found .git folder in', git) - return git -} - // // Resolve git directory for submodules // diff --git a/lib/get-folder-in-path.js b/lib/get-folder-in-path.js new file mode 100644 index 0000000..4e1d769 --- /dev/null +++ b/lib/get-folder-in-path.js @@ -0,0 +1,33 @@ +'use strict' +const fs = require('fs') +const path = require('path') +const resolve = path.resolve +const exists = path.existsSync || fs.existsSync + +// Function to recursively finding a folder +function getFolderInPath (folder, path, logger) { + const result = resolve(path, folder) + + if (!exists(result)) { + logger.log('pre-commit:') + logger.log('pre-commit: Not found ' + folder + ' folder in', result) + + const newPath = resolve(path, '..') + + // Stop if we on top folder + if (path === newPath) { + return null + } + + return getFolderInPath(folder, newPath, logger) + } + + if (fs.lstatSync(result).isDirectory()) { + logger.log('pre-commit:') + logger.log('pre-commit: Found ' + folder + ' folder in', result) + return result + } + return null +} + +module.exports = getFolderInPath diff --git a/lib/get-folder-in-path.test.js b/lib/get-folder-in-path.test.js new file mode 100644 index 0000000..cfea64d --- /dev/null +++ b/lib/get-folder-in-path.test.js @@ -0,0 +1,46 @@ +'use strict' + +const t = require('tap') +const resolve = require('path').resolve +const normalize = require('path').normalize +const getFolderInPath = require('./get-folder-in-path') + +const dummyLogger = { + log: () => {} +} + +t.test('target folder is in root', function (t) { + t.plan(1) + const path = getFolderInPath('target_git', resolve(__dirname, '../testfolders/root'), dummyLogger) + t.ok(path.endsWith(normalize('testfolders/root/target_git'))) +}) + +t.test('test folder is in submodule', function (t) { + t.plan(1) + const path = getFolderInPath('target_git', resolve(__dirname, '../testfolders/submodule/moduleA'), dummyLogger) + t.ok(path.endsWith(normalize('testfolders/submodule/moduleA/target_git'))) +}) + +t.test('test folder is in submodule', function (t) { + t.plan(1) + const path = getFolderInPath('target_git', resolve(__dirname, '../testfolders/recursive/root/sub'), dummyLogger) + t.ok(path.endsWith(normalize('testfolders/recursive/root/target_git'))) +}) + +t.test('folder is root', function (t) { + t.plan(1) + const path = getFolderInPath('super-special-folder-which-should-never-be-found', '/', dummyLogger) + t.same(path, null) +}) + +t.test('folder is empty', function (t) { + t.plan(1) + const path = getFolderInPath('target_git', resolve(__dirname, '../testfolders/empty'), dummyLogger) + t.same(path, null) +}) + +t.test('folder is empty', function (t) { + t.plan(1) + const path = getFolderInPath('target_git', resolve(__dirname, '../testfolders/file/module/sub'), dummyLogger) + t.same(path, null) +}) diff --git a/package.json b/package.json index 82209b1..cd9f1be 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "example-fail": "echo \"This is the example hook, I exit with 1\" && exit 1", "example-pass": "echo \"This is the example hook, I exit with 0\" && exit 0", "install": "node install.js", - "unit": "tap test.js", + "unit": "tap", "lint": "standard", "test": "npm run unit", "uninstall": "node uninstall.js" diff --git a/testfolders/empty/.gitkeep b/testfolders/empty/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/testfolders/file/module/sub/.gitkeep b/testfolders/file/module/sub/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/testfolders/file/module/target_git b/testfolders/file/module/target_git new file mode 100644 index 0000000..e69de29 diff --git a/testfolders/recursive/root/sub/.gitkeep b/testfolders/recursive/root/sub/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/testfolders/recursive/root/target_git/.gitkeep b/testfolders/recursive/root/target_git/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/testfolders/root/target_git/.gitkeep b/testfolders/root/target_git/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/testfolders/submodule/moduleA/target_git/.gitkeep b/testfolders/submodule/moduleA/target_git/.gitkeep new file mode 100644 index 0000000..e69de29