Skip to content

Commit

Permalink
Fix: Create new rule only in sonar's root folder
Browse files Browse the repository at this point in the history
Fix #527
Close #540
  • Loading branch information
Anton Molleda authored and alrra committed Sep 26, 2017
1 parent dc182b0 commit 0cfb1bb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/lib/cli/rules/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const ruleScriptDir = 'src/lib/rules';
export const ruleDocDir = 'docs/user-guide/rules';
export const ruleTestDir = 'tests/lib/rules';
export const ruleDistScriptDir = `dist/${ruleScriptDir}`;
export const dir = packageRoot();
export const packageDir = packageRoot();
export const processDir = process.cwd();

/** Check if a rule exists. */
export const ruleExists = (ruleName: string, currentRules: Array<string>): boolean => {
Expand Down
14 changes: 9 additions & 5 deletions src/lib/cli/rules/create-core-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { debug as d } from '../../utils/debug';
import * as logger from '../../utils/logging';
import * as resourceLoader from '../../utils/resource-loader';
import {
dir, normalize,
processDir, packageDir, normalize,
NewRule,
ruleDocDir, ruleExists, ruleScriptDir, ruleTemplateDir, ruleTestDir
} from './common';
Expand Down Expand Up @@ -80,13 +80,13 @@ const loadRuleContent = (rule: NewRule, type: string): Promise<string> => {
const generateRuleContent = async (rule: NewRule): Promise<void> => {
const entries = [
{
file: path.join(dir, ruleScriptDir, rule.name, `${rule.name}.ts`),
file: path.join(packageDir, ruleScriptDir, rule.name, `${rule.name}.ts`),
type: 'script'
}, {
file: path.join(dir, ruleDocDir, `${rule.name}.md`),
file: path.join(packageDir, ruleDocDir, `${rule.name}.md`),
type: 'doc'
}, {
file: path.join(dir, ruleTestDir, rule.name, 'tests.ts'),
file: path.join(packageDir, ruleTestDir, rule.name, 'tests.ts'),
type: 'test'
}];

Expand All @@ -101,7 +101,7 @@ const generateRuleContent = async (rule: NewRule): Promise<void> => {

/** Adds a new rule to the index page. */
const updateRuleIndex = async (rule: NewRule): Promise<void> => {
const indexPath = path.join(dir, ruleDocDir, 'index.md');
const indexPath = path.join(packageDir, ruleDocDir, 'index.md');
let text;

try {
Expand Down Expand Up @@ -203,6 +203,10 @@ export const newRule = async (actions: CLIOptions): Promise<boolean> => {
return false;
}

if (packageDir !== processDir) {
return false;
}

const rule: NewRule = {
category: '',
description: { string: '' },
Expand Down
12 changes: 6 additions & 6 deletions src/lib/cli/rules/delete-core-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { debug as d } from '../../utils/debug';
import * as inquirer from 'inquirer';
import * as rimraf from 'rimraf';

import { dir, normalize, ruleDistScriptDir, ruleDocDir, ruleExists, ruleScriptDir, ruleTestDir } from './common';
import { packageDir, normalize, ruleDistScriptDir, ruleDocDir, ruleExists, ruleScriptDir, ruleTestDir } from './common';
import * as logger from '../../utils/logging';
import * as resourceLoader from '../../utils/resource-loader';

const debug = d(__filename);

/** Removes a given rule from the index page. */
const removeFromRuleIndex = async (ruleName: string): Promise<void> => {
const indexPath = path.join(dir, ruleDocDir, 'index.md');
const indexPath = path.join(packageDir, ruleDocDir, 'index.md');
let text;

try {
Expand Down Expand Up @@ -54,10 +54,10 @@ export const deleteRule = async (actions: CLIOptions): Promise<boolean> => {
const currentRules: Array<string> = resourceLoader.getCoreRules();

const normalizedName = normalize(results.name, '-');
const scriptPath = path.join(dir, ruleScriptDir, normalizedName);
const docPath = path.join(dir, ruleDocDir, `${normalizedName}.md`);
const testPath = path.join(dir, ruleTestDir, normalizedName);
const distScriptPath = path.join(dir, ruleDistScriptDir, normalizedName);
const scriptPath = path.join(packageDir, ruleScriptDir, normalizedName);
const docPath = path.join(packageDir, ruleDocDir, `${normalizedName}.md`);
const testPath = path.join(packageDir, ruleTestDir, normalizedName);
const distScriptPath = path.join(packageDir, ruleDistScriptDir, normalizedName);

if (!ruleExists(normalizedName, currentRules)) {
throw new Error(`This rule doesn't exist!`);
Expand Down
19 changes: 16 additions & 3 deletions tests/lib/cli/rules/create-core-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import test from 'ava';

import { CLIOptions } from '../../../../src/lib/types';
import { readFileAsync } from '../../../../src/lib/utils/misc';
import * as rulesCommon from '../../../../src/lib/cli/rules/common';

const actions = ({ newRule: true } as CLIOptions);
const ruleScriptDir = 'src/lib/rules';
Expand Down Expand Up @@ -40,6 +41,7 @@ const resourceLoader = { getCoreRules() { } };
const rules = ['axe', 'content-type'];

proxyquire('../../../../src/lib/cli/rules/create-core-rule', {
'./common': rulesCommon,
fs,
inquirer,
resourceLoader,
Expand Down Expand Up @@ -97,7 +99,6 @@ test.serial(`if core, 'generate' should call to write script, documentation, tes
t.true(mkdirpAsyncFn.args[1][0].includes(path.join(ruleDocDir, ''))); // so it uses the right separator
t.true(mkdirpAsyncFn.args[2][0].includes(path.join(ruleTestDir, results.name)));


// writeFileAsync
t.is(writeFileAsyncFn.callCount, 4);
// Write Script
Expand Down Expand Up @@ -190,8 +191,20 @@ test.serial(`Throw an error if a new rule already exists when calling 'generate'
sandbox.restore();
});

test.serial('if version is not an option, it should return false', async (t) => {
const result = await rule.newRule(({}) as CLIOptions);
test.serial('if newRule is not an option, it should return false', async (t) => {
const result = await rule.newRule({} as CLIOptions);

t.false(result);
});

test.serial('if newRule is not executed in the right path, it should return false', async (t) => {
const processDir = sinon.stub(rulesCommon, 'processDir').get(() => {
return 'another directory';
});

const result = await rule.newRule(actions);

processDir.restore();

t.false(result);
});

0 comments on commit 0cfb1bb

Please sign in to comment.