Skip to content

Commit

Permalink
Add a sanity check for rules that should be mapped
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Nov 1, 2023
1 parent 0b48c18 commit b157605
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/jsts/tests/rules/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import { rules as mapping } from '../../src/rules';
import fs from 'fs';
import path from 'path';

describe('index', () => {
it('should map keys to rules definitions', () => {
// FIXME: This test runs with a time complexity of O(n^2) where n is the number of rules.
// This is because it iterates over all rules and checks if they are mapped. Once
// we use Sonar rule keys as ESLint rule keys, we can make this test O(n) just by
// checking if the rule key is in the mapping.
const ruleFolder = path.join(__dirname, '../../src/rules');
const sonarKeys = fs.readdirSync(ruleFolder).filter(name => /^S\d+/.test(name));
const mappedRules = Object.values(mapping);
const missing = [];
for (const sonarKey of sonarKeys) {
const { rule } = require(path.join(ruleFolder, sonarKey));
if (!mappedRules.some(mapped => mapped === rule)) {
missing.push(sonarKey);
}
}
expect(missing).toHaveLength(0);
});
});

0 comments on commit b157605

Please sign in to comment.