From af6d426cd08a3c7bbbc0a47f4e8ace13210ad8f0 Mon Sep 17 00:00:00 2001 From: CJ Wang Date: Thu, 22 Dec 2022 13:03:56 -0800 Subject: [PATCH] fix: correctly check for RegExp --- .husky/commit-msg | 0 .husky/pre-commit | 0 __tests__/plugin.test.js | 118 +++++++++++++++++++++++++++++++++++++++ index.js | 2 +- 4 files changed, 119 insertions(+), 1 deletion(-) mode change 100644 => 100755 .husky/commit-msg mode change 100644 => 100755 .husky/pre-commit diff --git a/.husky/commit-msg b/.husky/commit-msg old mode 100644 new mode 100755 diff --git a/.husky/pre-commit b/.husky/pre-commit old mode 100644 new mode 100755 diff --git a/__tests__/plugin.test.js b/__tests__/plugin.test.js index 08df168..c5cd94b 100644 --- a/__tests__/plugin.test.js +++ b/__tests__/plugin.test.js @@ -1,4 +1,15 @@ const commitlintPluginSelectiveScope = require('../index') +const commitlintPluginSelectiveScopeResolver = + commitlintPluginSelectiveScope.rules['selective-scope'] +// Wrap the resolver so we don't have to pass in "always" +const commitlintPluginSelectiveScopeResolverWrapped = (ctx, rules) => + commitlintPluginSelectiveScopeResolver(ctx, 'always', rules) + +const TEST_RULES = { + feat: [/^frontend\/[^/]+$/, 'backend'], + perf: [], + ci: [null, 'codebuild', 'jenkins'] +} describe('commitlintPluginSelectiveScope', () => { it('should return a valid config', () => { @@ -7,4 +18,111 @@ describe('commitlintPluginSelectiveScope', () => { Object.keys(commitlintPluginSelectiveScope.rules).length ).toBeGreaterThan(0) }) + + it('should not enforce scope if the type does not appear in the rules', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'frontend/web', type: 'fix' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'anything', type: 'fix' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: null, type: 'fix' }, + TEST_RULES + )[0] + ).toBe(true) + }) + + it('should not allow scope if the type appears in the rules with an empty array', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: null, type: 'perf' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'something', type: 'perf' }, + TEST_RULES + )[0] + ).toBe(false) + }) + + describe('should only allow scopes defined if the type appears in the rule with a non-empty array', () => { + it('should properly match a string literal', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'backend', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'test', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(false) + }) + + it('should properly match a RegExp', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'frontend/web', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'frontend', type: 'feat' }, + TEST_RULES + )[0] + ).toBe(false) + }) + }) + + describe('optional scope', () => { + it('should allow scope to be optional if the type appears in the rules with a null in the array', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: null, type: 'ci' }, + TEST_RULES + )[0] + ).toBe(true) + }) + + it('should match scope if provided', () => { + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'codebuild', type: 'ci' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'jenkins', type: 'ci' }, + TEST_RULES + )[0] + ).toBe(true) + + expect( + commitlintPluginSelectiveScopeResolverWrapped( + { scope: 'github', type: 'ci' }, + TEST_RULES + )[0] + ).toBe(false) + }) + }) }) diff --git a/index.js b/index.js index 1e11218..25a54d2 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ module.exports = { allowedScopes.findIndex(s => { if ( typeof ctx.scope === 'string' && - Object.prototype.toString.call() === '[object RegExp]' + Object.prototype.toString.call(s) === '[object RegExp]' ) { return ctx.scope.match(s) }