From a285d681700e2ed62b9635b731fc8e35efb9fed8 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 10:00:21 +0200 Subject: [PATCH 01/11] move tests to test folder --- package.json | 2 +- test.js => test/hook.test.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename test.js => test/hook.test.js (98%) diff --git a/package.json b/package.json index 9e09e7b..de0f3db 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 test/*.test.js", "lint": "standard", "test": "npm run lint && npm run unit", "uninstall": "node uninstall.js" diff --git a/test.js b/test/hook.test.js similarity index 98% rename from test.js rename to test/hook.test.js index 22143d0..9228244 100644 --- a/test.js +++ b/test/hook.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) @@ -146,7 +146,7 @@ t.test('pre-commit', function (t) { t.test('overrides the `pre-commit` config property in package.json with the config inside `.pre-commit.json` if it exists', function (t) { t.plan(1) - const Hook = proxyquire('.', { + const Hook = proxyquire('..', { fs: { existsSync () { return true @@ -167,7 +167,7 @@ t.test('pre-commit', function (t) { t.test('should properly handle errors while trying to read and parse the contents of `.pre-commit.json`', function (t) { t.plan(4) - let Hook = proxyquire('.', { + let Hook = proxyquire('..', { fs: { existsSync () { return true @@ -180,7 +180,7 @@ t.test('pre-commit', function (t) { hook = new Hook(exit) - Hook = proxyquire('.', { + Hook = proxyquire('..', { fs: { existsSync () { return true }, readFileSync () { From 6aa8acf6855ec23cf7dbf61fb24ecc18d9fa5c9c Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 12:21:46 +0200 Subject: [PATCH 02/11] implement generic getFolderInPath --- getFolderInPath.js | 34 +++++++++++++++ install.js | 26 +---------- test/getFolderInPath.test.js | 43 +++++++++++++++++++ test/testfolders/empty/.gitkeep | 0 test/testfolders/file/module/sub/.gitkeep | 0 test/testfolders/file/module/target | 0 test/testfolders/recursive/root/sub/.gitkeep | 0 .../recursive/root/target/.gitkeep | 0 test/testfolders/root/target/.gitkeep | 0 .../submodule/moduleA/target/.gitkeep | 0 10 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 getFolderInPath.js create mode 100644 test/getFolderInPath.test.js create mode 100644 test/testfolders/empty/.gitkeep create mode 100644 test/testfolders/file/module/sub/.gitkeep create mode 100644 test/testfolders/file/module/target create mode 100644 test/testfolders/recursive/root/sub/.gitkeep create mode 100644 test/testfolders/recursive/root/target/.gitkeep create mode 100644 test/testfolders/root/target/.gitkeep create mode 100644 test/testfolders/submodule/moduleA/target/.gitkeep diff --git a/getFolderInPath.js b/getFolderInPath.js new file mode 100644 index 0000000..b9610ed --- /dev/null +++ b/getFolderInPath.js @@ -0,0 +1,34 @@ +'use strict'; +const fs = require('fs'); +const path = require('path') +const resolve = path.resolve; +const exists = fs.existsSync + +// Function to recursively finding a folder +function getFolderInPath(folder, path) { + const result = resolve(path, folder); + + if (!exists(result)) { + console.log('pre-commit:'); + console.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); + } + + if (fs.lstatSync(result).isDirectory()) { + console.log('pre-commit:'); + console.log('pre-commit: Found ' + folder + ' folder in', result); + return result; + } + return null +} + +module.exports = getFolderInPath +module.exports.getFolderInPath = getFolderInPath diff --git a/install.js b/install.js index 872f00b..cb54168 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('./getFolderInPath') 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) 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/test/getFolderInPath.test.js b/test/getFolderInPath.test.js new file mode 100644 index 0000000..7e8e752 --- /dev/null +++ b/test/getFolderInPath.test.js @@ -0,0 +1,43 @@ +const t = require('tap') +const resolve = require('path').resolve +const getFolderInPath = require('../getFolderInPath') + +t.test('getFolderInPath', function (t) { + t.plan(6) + + t.test('target folder is in root', function (t) { + t.plan(1) + const path = getFolderInPath('target', resolve(__dirname, 'testfolders/root')) + t.ok(path.endsWith('testfolders/root/target')) + }) + + t.test('test folder is in submodule', function (t) { + t.plan(1) + const path = getFolderInPath('target', resolve(__dirname, 'testfolders/submodule/moduleA')) + t.ok(path.endsWith('testfolders/submodule/moduleA/target')) + }) + + t.test('test folder is in submodule', function (t) { + t.plan(1) + const path = getFolderInPath('target', resolve(__dirname, 'testfolders/recursive/root/sub')) + t.ok(path.endsWith('testfolders/recursive/root/target')) + }) + + t.test('folder is root', function (t) { + t.plan(1) + const path = getFolderInPath('super-special-folder-which-should-never-be-found', '/') + t.same(path, null) + }) + + t.test('folder is empty', function (t) { + t.plan(1) + const path = getFolderInPath('target', resolve(__dirname, 'testfolders/empty')) + t.same(path, null) + }) + + t.test('folder is empty', function (t) { + t.plan(1) + const path = getFolderInPath('target', resolve(__dirname, 'testfolders/file/module/sub')) + t.same(path, null) + }) +}) diff --git a/test/testfolders/empty/.gitkeep b/test/testfolders/empty/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/testfolders/file/module/sub/.gitkeep b/test/testfolders/file/module/sub/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/testfolders/file/module/target b/test/testfolders/file/module/target new file mode 100644 index 0000000..e69de29 diff --git a/test/testfolders/recursive/root/sub/.gitkeep b/test/testfolders/recursive/root/sub/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/testfolders/recursive/root/target/.gitkeep b/test/testfolders/recursive/root/target/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/testfolders/root/target/.gitkeep b/test/testfolders/root/target/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/testfolders/submodule/moduleA/target/.gitkeep b/test/testfolders/submodule/moduleA/target/.gitkeep new file mode 100644 index 0000000..e69de29 From b7a1cb99ec79648a41261d2c81b82df8b700d19c Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 12:22:01 +0200 Subject: [PATCH 03/11] lint --- getFolderInPath.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/getFolderInPath.js b/getFolderInPath.js index b9610ed..3afa0cd 100644 --- a/getFolderInPath.js +++ b/getFolderInPath.js @@ -1,31 +1,31 @@ -'use strict'; -const fs = require('fs'); +'use strict' +const fs = require('fs') const path = require('path') -const resolve = path.resolve; +const resolve = path.resolve const exists = fs.existsSync // Function to recursively finding a folder -function getFolderInPath(folder, path) { - const result = resolve(path, folder); +function getFolderInPath (folder, path) { + const result = resolve(path, folder) if (!exists(result)) { - console.log('pre-commit:'); - console.log('pre-commit: Not found ' + folder + ' folder in', result); + console.log('pre-commit:') + console.log('pre-commit: Not found ' + folder + ' folder in', result) - const newPath = resolve(path, '..'); + const newPath = resolve(path, '..') // Stop if we on top folder if (path === newPath) { - return null; + return null } - return getFolderInPath(folder, newPath); + return getFolderInPath(folder, newPath) } if (fs.lstatSync(result).isDirectory()) { - console.log('pre-commit:'); - console.log('pre-commit: Found ' + folder + ' folder in', result); - return result; + console.log('pre-commit:') + console.log('pre-commit: Found ' + folder + ' folder in', result) + return result } return null } From f189d04dceb7250afa1edeb02e7f29bfba8bf331 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 12:41:10 +0200 Subject: [PATCH 04/11] try to fix macOS --- getFolderInPath.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getFolderInPath.js b/getFolderInPath.js index 3afa0cd..037f7af 100644 --- a/getFolderInPath.js +++ b/getFolderInPath.js @@ -2,7 +2,7 @@ const fs = require('fs') const path = require('path') const resolve = path.resolve -const exists = fs.existsSync +const exists = path.existsSync || fs.existsSync // Function to recursively finding a folder function getFolderInPath (folder, path) { From c0078aeb6a8d8062bbf83c608998038412c9dd0c Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 12:48:06 +0200 Subject: [PATCH 05/11] rename target to target_git to avoid issues in CI-pipeline --- test/getFolderInPath.test.js | 16 ++++++++-------- .../file/module/{target => target_git} | 0 .../root/{target => target_git}/.gitkeep | 0 .../root/{target => target_git}/.gitkeep | 0 .../moduleA/{target => target_git}/.gitkeep | 0 5 files changed, 8 insertions(+), 8 deletions(-) rename test/testfolders/file/module/{target => target_git} (100%) rename test/testfolders/recursive/root/{target => target_git}/.gitkeep (100%) rename test/testfolders/root/{target => target_git}/.gitkeep (100%) rename test/testfolders/submodule/moduleA/{target => target_git}/.gitkeep (100%) diff --git a/test/getFolderInPath.test.js b/test/getFolderInPath.test.js index 7e8e752..4fc0cba 100644 --- a/test/getFolderInPath.test.js +++ b/test/getFolderInPath.test.js @@ -7,20 +7,20 @@ t.test('getFolderInPath', function (t) { t.test('target folder is in root', function (t) { t.plan(1) - const path = getFolderInPath('target', resolve(__dirname, 'testfolders/root')) - t.ok(path.endsWith('testfolders/root/target')) + const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/root')) + t.ok(path.endsWith('testfolders/root/target_git')) }) t.test('test folder is in submodule', function (t) { t.plan(1) - const path = getFolderInPath('target', resolve(__dirname, 'testfolders/submodule/moduleA')) - t.ok(path.endsWith('testfolders/submodule/moduleA/target')) + const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/submodule/moduleA')) + t.ok(path.endsWith('testfolders/submodule/moduleA/target_git')) }) t.test('test folder is in submodule', function (t) { t.plan(1) - const path = getFolderInPath('target', resolve(__dirname, 'testfolders/recursive/root/sub')) - t.ok(path.endsWith('testfolders/recursive/root/target')) + const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/recursive/root/sub')) + t.ok(path.endsWith('testfolders/recursive/root/target_git')) }) t.test('folder is root', function (t) { @@ -31,13 +31,13 @@ t.test('getFolderInPath', function (t) { t.test('folder is empty', function (t) { t.plan(1) - const path = getFolderInPath('target', resolve(__dirname, 'testfolders/empty')) + const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/empty')) t.same(path, null) }) t.test('folder is empty', function (t) { t.plan(1) - const path = getFolderInPath('target', resolve(__dirname, 'testfolders/file/module/sub')) + const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/file/module/sub')) t.same(path, null) }) }) diff --git a/test/testfolders/file/module/target b/test/testfolders/file/module/target_git similarity index 100% rename from test/testfolders/file/module/target rename to test/testfolders/file/module/target_git diff --git a/test/testfolders/recursive/root/target/.gitkeep b/test/testfolders/recursive/root/target_git/.gitkeep similarity index 100% rename from test/testfolders/recursive/root/target/.gitkeep rename to test/testfolders/recursive/root/target_git/.gitkeep diff --git a/test/testfolders/root/target/.gitkeep b/test/testfolders/root/target_git/.gitkeep similarity index 100% rename from test/testfolders/root/target/.gitkeep rename to test/testfolders/root/target_git/.gitkeep diff --git a/test/testfolders/submodule/moduleA/target/.gitkeep b/test/testfolders/submodule/moduleA/target_git/.gitkeep similarity index 100% rename from test/testfolders/submodule/moduleA/target/.gitkeep rename to test/testfolders/submodule/moduleA/target_git/.gitkeep From 5f3e3784d3ef01d9cb584284f91fa75ebea83fed Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 13:35:30 +0200 Subject: [PATCH 06/11] normalize for windows --- test/getFolderInPath.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/getFolderInPath.test.js b/test/getFolderInPath.test.js index 4fc0cba..0cb072a 100644 --- a/test/getFolderInPath.test.js +++ b/test/getFolderInPath.test.js @@ -1,5 +1,6 @@ const t = require('tap') const resolve = require('path').resolve +const normalize = require('path').normalize const getFolderInPath = require('../getFolderInPath') t.test('getFolderInPath', function (t) { @@ -8,19 +9,19 @@ t.test('getFolderInPath', function (t) { t.test('target folder is in root', function (t) { t.plan(1) const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/root')) - t.ok(path.endsWith('testfolders/root/target_git')) + 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')) - t.ok(path.endsWith('testfolders/submodule/moduleA/target_git')) + 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')) - t.ok(path.endsWith('testfolders/recursive/root/target_git')) + t.ok(path.endsWith(normalize('testfolders/recursive/root/target_git'))) }) t.test('folder is root', function (t) { From 14b4680773a2bab91ee039c4f9f261556d6a51ab Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 13:41:52 +0200 Subject: [PATCH 07/11] apply requested changes --- getFolderInPath.js => get-folder-in-path.js | 0 test/hook.test.js => index.test.js | 51 +++++++++++++++++-- install.js | 2 +- package.json | 2 +- test/getFolderInPath.test.js | 44 ---------------- .../empty/.gitkeep | 0 .../file/module/sub/.gitkeep | 0 .../file/module/target_git | 0 .../recursive/root/sub/.gitkeep | 0 .../recursive/root/target_git/.gitkeep | 0 .../root/target_git/.gitkeep | 0 .../submodule/moduleA/target_git/.gitkeep | 0 12 files changed, 49 insertions(+), 50 deletions(-) rename getFolderInPath.js => get-folder-in-path.js (100%) rename test/hook.test.js => index.test.js (83%) delete mode 100644 test/getFolderInPath.test.js rename {test/testfolders => testfolders}/empty/.gitkeep (100%) rename {test/testfolders => testfolders}/file/module/sub/.gitkeep (100%) rename {test/testfolders => testfolders}/file/module/target_git (100%) rename {test/testfolders => testfolders}/recursive/root/sub/.gitkeep (100%) rename {test/testfolders => testfolders}/recursive/root/target_git/.gitkeep (100%) rename {test/testfolders => testfolders}/root/target_git/.gitkeep (100%) rename {test/testfolders => testfolders}/submodule/moduleA/target_git/.gitkeep (100%) diff --git a/getFolderInPath.js b/get-folder-in-path.js similarity index 100% rename from getFolderInPath.js rename to get-folder-in-path.js diff --git a/test/hook.test.js b/index.test.js similarity index 83% rename from test/hook.test.js rename to index.test.js index 9228244..52a762a 100644 --- a/test/hook.test.js +++ b/index.test.js @@ -1,8 +1,11 @@ 'use strict' -const Hook = require('..') +const Hook = require('.') const t = require('tap') const tty = require('tty') const ttySupportColor = tty.isatty(process.stdout.fd) +const resolve = require('path').resolve +const normalize = require('path').normalize +const getFolderInPath = require('./get-folder-in-path') const proxyquire = require('proxyquire') @@ -146,7 +149,7 @@ t.test('pre-commit', function (t) { t.test('overrides the `pre-commit` config property in package.json with the config inside `.pre-commit.json` if it exists', function (t) { t.plan(1) - const Hook = proxyquire('..', { + const Hook = proxyquire('.', { fs: { existsSync () { return true @@ -167,7 +170,7 @@ t.test('pre-commit', function (t) { t.test('should properly handle errors while trying to read and parse the contents of `.pre-commit.json`', function (t) { t.plan(4) - let Hook = proxyquire('..', { + let Hook = proxyquire('.', { fs: { existsSync () { return true @@ -180,7 +183,7 @@ t.test('pre-commit', function (t) { hook = new Hook(exit) - Hook = proxyquire('..', { + Hook = proxyquire('.', { fs: { existsSync () { return true }, readFileSync () { @@ -330,3 +333,43 @@ t.test('pre-commit', function (t) { }) }) }) + +t.test('getFolderInPath', function (t) { + t.plan(6) + + t.test('target folder is in root', function (t) { + t.plan(1) + const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/root')) + 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')) + 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')) + 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', '/') + t.same(path, null) + }) + + t.test('folder is empty', function (t) { + t.plan(1) + const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/empty')) + 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')) + t.same(path, null) + }) +}) diff --git a/install.js b/install.js index cb54168..9f096d9 100644 --- a/install.js +++ b/install.js @@ -8,7 +8,7 @@ const path = require('path') const os = require('os') const hook = path.join(__dirname, 'hook') const root = path.resolve(__dirname, '..', '..', '..') -const getFolderInPath = require('./getFolderInPath') +const getFolderInPath = require('./get-folder-in-path') const exists = fs.existsSync || path.existsSync // diff --git a/package.json b/package.json index 68c77c2..13a23ab 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/*.test.js", + "unit": "tap index.test.js", "lint": "standard", "test": "npm run unit", "uninstall": "node uninstall.js" diff --git a/test/getFolderInPath.test.js b/test/getFolderInPath.test.js deleted file mode 100644 index 0cb072a..0000000 --- a/test/getFolderInPath.test.js +++ /dev/null @@ -1,44 +0,0 @@ -const t = require('tap') -const resolve = require('path').resolve -const normalize = require('path').normalize -const getFolderInPath = require('../getFolderInPath') - -t.test('getFolderInPath', function (t) { - t.plan(6) - - t.test('target folder is in root', function (t) { - t.plan(1) - const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/root')) - 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')) - 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')) - 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', '/') - t.same(path, null) - }) - - t.test('folder is empty', function (t) { - t.plan(1) - const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/empty')) - 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')) - t.same(path, null) - }) -}) diff --git a/test/testfolders/empty/.gitkeep b/testfolders/empty/.gitkeep similarity index 100% rename from test/testfolders/empty/.gitkeep rename to testfolders/empty/.gitkeep diff --git a/test/testfolders/file/module/sub/.gitkeep b/testfolders/file/module/sub/.gitkeep similarity index 100% rename from test/testfolders/file/module/sub/.gitkeep rename to testfolders/file/module/sub/.gitkeep diff --git a/test/testfolders/file/module/target_git b/testfolders/file/module/target_git similarity index 100% rename from test/testfolders/file/module/target_git rename to testfolders/file/module/target_git diff --git a/test/testfolders/recursive/root/sub/.gitkeep b/testfolders/recursive/root/sub/.gitkeep similarity index 100% rename from test/testfolders/recursive/root/sub/.gitkeep rename to testfolders/recursive/root/sub/.gitkeep diff --git a/test/testfolders/recursive/root/target_git/.gitkeep b/testfolders/recursive/root/target_git/.gitkeep similarity index 100% rename from test/testfolders/recursive/root/target_git/.gitkeep rename to testfolders/recursive/root/target_git/.gitkeep diff --git a/test/testfolders/root/target_git/.gitkeep b/testfolders/root/target_git/.gitkeep similarity index 100% rename from test/testfolders/root/target_git/.gitkeep rename to testfolders/root/target_git/.gitkeep diff --git a/test/testfolders/submodule/moduleA/target_git/.gitkeep b/testfolders/submodule/moduleA/target_git/.gitkeep similarity index 100% rename from test/testfolders/submodule/moduleA/target_git/.gitkeep rename to testfolders/submodule/moduleA/target_git/.gitkeep From fec07b7d876d4697e933e4f3b2daebb283c0c12e Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 13:43:58 +0200 Subject: [PATCH 08/11] move get-folder-in-path.js to lib folder --- index.test.js | 2 +- install.js | 2 +- get-folder-in-path.js => lib/get-folder-in-path.js | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename get-folder-in-path.js => lib/get-folder-in-path.js (100%) diff --git a/index.test.js b/index.test.js index 52a762a..9225bf4 100644 --- a/index.test.js +++ b/index.test.js @@ -5,7 +5,7 @@ const tty = require('tty') const ttySupportColor = tty.isatty(process.stdout.fd) const resolve = require('path').resolve const normalize = require('path').normalize -const getFolderInPath = require('./get-folder-in-path') +const getFolderInPath = require('./lib/get-folder-in-path') const proxyquire = require('proxyquire') diff --git a/install.js b/install.js index 9f096d9..a8a3198 100644 --- a/install.js +++ b/install.js @@ -8,7 +8,7 @@ const path = require('path') const os = require('os') const hook = path.join(__dirname, 'hook') const root = path.resolve(__dirname, '..', '..', '..') -const getFolderInPath = require('./get-folder-in-path') +const getFolderInPath = require('./lib/get-folder-in-path') const exists = fs.existsSync || path.existsSync // diff --git a/get-folder-in-path.js b/lib/get-folder-in-path.js similarity index 100% rename from get-folder-in-path.js rename to lib/get-folder-in-path.js From 121bef63d42cfbbc7525dd0cea79b7777105758a Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 16:45:17 +0200 Subject: [PATCH 09/11] implement requested design --- .taprc | 5 +++- index.test.js | 43 ----------------------------- install.js | 2 +- lib/get-folder-in-path.js | 13 ++++----- lib/get-folder-in-path.test.js | 50 ++++++++++++++++++++++++++++++++++ package.json | 2 +- 6 files changed, 62 insertions(+), 53 deletions(-) create mode 100644 lib/get-folder-in-path.test.js diff --git a/.taprc b/.taprc index 381688a..7d7ead2 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' \ No newline at end of file diff --git a/index.test.js b/index.test.js index 9225bf4..4e4866d 100644 --- a/index.test.js +++ b/index.test.js @@ -3,9 +3,6 @@ const Hook = require('.') const t = require('tap') const tty = require('tty') const ttySupportColor = tty.isatty(process.stdout.fd) -const resolve = require('path').resolve -const normalize = require('path').normalize -const getFolderInPath = require('./lib/get-folder-in-path') const proxyquire = require('proxyquire') @@ -333,43 +330,3 @@ t.test('pre-commit', function (t) { }) }) }) - -t.test('getFolderInPath', function (t) { - t.plan(6) - - t.test('target folder is in root', function (t) { - t.plan(1) - const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/root')) - 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')) - 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')) - 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', '/') - t.same(path, null) - }) - - t.test('folder is empty', function (t) { - t.plan(1) - const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/empty')) - 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')) - t.same(path, null) - }) -}) diff --git a/install.js b/install.js index a8a3198..cfc3a87 100644 --- a/install.js +++ b/install.js @@ -18,7 +18,7 @@ const exists = fs.existsSync || path.existsSync // to work correctly. // -let git = getFolderInPath('.git', root) +let git = getFolderInPath('.git', root, console) let hooks let precommit diff --git a/lib/get-folder-in-path.js b/lib/get-folder-in-path.js index 037f7af..4e1d769 100644 --- a/lib/get-folder-in-path.js +++ b/lib/get-folder-in-path.js @@ -5,12 +5,12 @@ const resolve = path.resolve const exists = path.existsSync || fs.existsSync // Function to recursively finding a folder -function getFolderInPath (folder, path) { +function getFolderInPath (folder, path, logger) { const result = resolve(path, folder) if (!exists(result)) { - console.log('pre-commit:') - console.log('pre-commit: Not found ' + folder + ' folder in', result) + logger.log('pre-commit:') + logger.log('pre-commit: Not found ' + folder + ' folder in', result) const newPath = resolve(path, '..') @@ -19,16 +19,15 @@ function getFolderInPath (folder, path) { return null } - return getFolderInPath(folder, newPath) + return getFolderInPath(folder, newPath, logger) } if (fs.lstatSync(result).isDirectory()) { - console.log('pre-commit:') - console.log('pre-commit: Found ' + folder + ' folder in', result) + logger.log('pre-commit:') + logger.log('pre-commit: Found ' + folder + ' folder in', result) return result } return null } module.exports = getFolderInPath -module.exports.getFolderInPath = 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..26dfed0 --- /dev/null +++ b/lib/get-folder-in-path.test.js @@ -0,0 +1,50 @@ +'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('getFolderInPath', function (t) { + t.plan(6) + + 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 13a23ab..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 index.test.js", + "unit": "tap", "lint": "standard", "test": "npm run unit", "uninstall": "node uninstall.js" From 5a766254811482dcb137ff9a8f7afed5730ce164 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 16:48:15 +0200 Subject: [PATCH 10/11] add missing newline at EOF --- .taprc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.taprc b/.taprc index 7d7ead2..05802f2 100644 --- a/.taprc +++ b/.taprc @@ -1,4 +1,4 @@ check-coverage: false files: - 'index.test.js' - - 'lib/**/*.test.js' \ No newline at end of file + - 'lib/**/*.test.js' From 74235e936ca618254cac6f5ffcbc41150392edf5 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 26 Jul 2022 16:50:12 +0200 Subject: [PATCH 11/11] unwrap --- lib/get-folder-in-path.test.js | 72 ++++++++++++++++------------------ 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/lib/get-folder-in-path.test.js b/lib/get-folder-in-path.test.js index 26dfed0..cfea64d 100644 --- a/lib/get-folder-in-path.test.js +++ b/lib/get-folder-in-path.test.js @@ -9,42 +9,38 @@ const dummyLogger = { log: () => {} } -t.test('getFolderInPath', function (t) { - t.plan(6) - - 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) - }) +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) })