|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const RuleTester = require('eslint').RuleTester;
|
4 |
| -const tester = new RuleTester(); |
| 4 | +const tester = new RuleTester({ |
| 5 | + parserOptions: { |
| 6 | + ecmaVersion: 6, |
| 7 | + sourceType: 'module', |
| 8 | + }, |
| 9 | +}); |
5 | 10 |
|
6 | 11 | const ruleName = 'detect-child-process';
|
7 | 12 | const rule = require(`../rules/${ruleName}`);
|
8 | 13 |
|
9 | 14 | tester.run(ruleName, rule, {
|
10 | 15 | valid: [
|
11 | 16 | "child_process.exec('ls')",
|
12 |
| - { |
13 |
| - code: ` |
14 |
| - var {} = require('child_process'); |
15 |
| - var result = /hello/.exec(str);`, |
16 |
| - parserOptions: { ecmaVersion: 6 }, |
17 |
| - }, |
18 |
| - { |
19 |
| - code: "var { spawn } = require('child_process'); spawn(str);", |
20 |
| - parserOptions: { ecmaVersion: 6 }, |
21 |
| - }, |
| 17 | + ` |
| 18 | + var {} = require('child_process'); |
| 19 | + var result = /hello/.exec(str);`, |
| 20 | + ` |
| 21 | + var {} = require('node:child_process'); |
| 22 | + var result = /hello/.exec(str);`, |
| 23 | + ` |
| 24 | + import {} from 'child_process'; |
| 25 | + var result = /hello/.exec(str);`, |
| 26 | + ` |
| 27 | + import {} from 'node:child_process'; |
| 28 | + var result = /hello/.exec(str);`, |
| 29 | + "var { spawn } = require('child_process'); spawn(str);", |
| 30 | + "var { spawn } = require('node:child_process'); spawn(str);", |
| 31 | + "import { spawn } from 'child_process'; spawn(str);", |
| 32 | + "import { spawn } from 'node:child_process'; spawn(str);", |
| 33 | + ` |
| 34 | + var foo = require('child_process'); |
| 35 | + function fn () { |
| 36 | + var foo = /hello/; |
| 37 | + var result = foo.exec(str); |
| 38 | + }`, |
| 39 | + "var child = require('child_process'); child.spawn(str)", |
| 40 | + "var child = require('node:child_process'); child.spawn(str)", |
| 41 | + "import child from 'child_process'; child.spawn(str)", |
| 42 | + "import child from 'node:child_process'; child.spawn(str)", |
| 43 | + ` |
| 44 | + var foo = require('child_process'); |
| 45 | + function fn () { |
| 46 | + var result = foo.spawn(str); |
| 47 | + }`, |
| 48 | + "require('child_process').spawn(str)", |
| 49 | + ` |
| 50 | + function fn () { |
| 51 | + require('child_process').spawn(str) |
| 52 | + }`, |
22 | 53 | ],
|
23 | 54 | invalid: [
|
24 | 55 | {
|
25 | 56 | code: "require('child_process')",
|
26 | 57 | errors: [{ message: 'Found require("child_process")' }],
|
27 | 58 | },
|
| 59 | + { |
| 60 | + code: "require('node:child_process')", |
| 61 | + errors: [{ message: 'Found require("node:child_process")' }], |
| 62 | + }, |
28 | 63 | {
|
29 | 64 | code: "var child = require('child_process'); child.exec(com)",
|
30 |
| - errors: [{ message: 'Found require("child_process")' }, { message: 'Found child_process.exec() with non Literal first argument' }], |
| 65 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument' }], |
31 | 66 | },
|
32 | 67 | {
|
33 |
| - code: "var child = require('child_process'); child.exec()", |
34 |
| - errors: [{ message: 'Found require("child_process")' }], |
| 68 | + code: "var child = require('node:child_process'); child.exec(com)", |
| 69 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument' }], |
| 70 | + }, |
| 71 | + { |
| 72 | + code: "import child from 'child_process'; child.exec(com)", |
| 73 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument' }], |
| 74 | + }, |
| 75 | + { |
| 76 | + code: "import child from 'node:child_process'; child.exec(com)", |
| 77 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument' }], |
35 | 78 | },
|
36 | 79 | {
|
37 | 80 | code: "var child = sinon.stub(require('child_process')); child.exec.returns({});",
|
38 | 81 | errors: [{ message: 'Found require("child_process")' }],
|
39 | 82 | },
|
| 83 | + { |
| 84 | + code: "var child = sinon.stub(require('node:child_process')); child.exec.returns({});", |
| 85 | + errors: [{ message: 'Found require("node:child_process")' }], |
| 86 | + }, |
40 | 87 | {
|
41 | 88 | code: `
|
42 | 89 | var foo = require('child_process');
|
43 | 90 | function fn () {
|
44 | 91 | var result = foo.exec(str);
|
45 | 92 | }`,
|
46 |
| - errors: [ |
47 |
| - { message: 'Found require("child_process")', line: 2 }, |
48 |
| - { message: 'Found child_process.exec() with non Literal first argument', line: 4 }, |
49 |
| - ], |
| 93 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument', line: 4 }], |
50 | 94 | },
|
51 | 95 | {
|
52 | 96 | code: `
|
53 |
| - var foo = require('child_process'); |
| 97 | + import foo from 'child_process'; |
| 98 | + function fn () { |
| 99 | + var result = foo.exec(str); |
| 100 | + }`, |
| 101 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument', line: 4 }], |
| 102 | + }, |
| 103 | + { |
| 104 | + code: ` |
| 105 | + import foo from 'node:child_process'; |
54 | 106 | function fn () {
|
55 |
| - var foo = /hello/; |
56 | 107 | var result = foo.exec(str);
|
57 | 108 | }`,
|
58 |
| - errors: [{ message: 'Found require("child_process")', line: 2 }], |
| 109 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument', line: 4 }], |
| 110 | + }, |
| 111 | + { |
| 112 | + code: ` |
| 113 | + require('child_process').exec(str)`, |
| 114 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument', line: 2 }], |
| 115 | + }, |
| 116 | + { |
| 117 | + code: ` |
| 118 | + function fn () { |
| 119 | + require('child_process').exec(str) |
| 120 | + }`, |
| 121 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument', line: 3 }], |
| 122 | + }, |
| 123 | + { |
| 124 | + code: ` |
| 125 | + const {exec} = require('child_process'); |
| 126 | + exec(str)`, |
| 127 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument', line: 3 }], |
| 128 | + }, |
| 129 | + { |
| 130 | + code: ` |
| 131 | + const {exec} = require('node:child_process'); |
| 132 | + exec(str)`, |
| 133 | + errors: [{ message: 'Found child_process.exec() with non Literal first argument', line: 3 }], |
59 | 134 | },
|
60 | 135 | ],
|
61 | 136 | });
|
0 commit comments