|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +const { RuleTester } = require('eslint'); |
| 11 | +const rule = require('./no_npx_playwright'); |
| 12 | +const dedent = require('dedent'); |
| 13 | + |
| 14 | +const ERROR_MSG = |
| 15 | + "Do not use 'npx playwright' — use 'node scripts/playwright' instead. " + |
| 16 | + 'npx may auto-install a different Playwright version, bypassing the pinned @playwright/test dependency.'; |
| 17 | + |
| 18 | +const ruleTester = new RuleTester({ |
| 19 | + parser: require.resolve('@typescript-eslint/parser'), |
| 20 | + parserOptions: { |
| 21 | + sourceType: 'module', |
| 22 | + ecmaVersion: 2020, |
| 23 | + ecmaFeatures: { |
| 24 | + jsx: true, |
| 25 | + }, |
| 26 | + }, |
| 27 | +}); |
| 28 | + |
| 29 | +ruleTester.run('@kbn/eslint/no_npx_playwright', rule, { |
| 30 | + valid: [ |
| 31 | + // Safe: uses node scripts/playwright |
| 32 | + { |
| 33 | + code: dedent` |
| 34 | + execSync('node scripts/playwright test --config config.ts'); |
| 35 | + `, |
| 36 | + }, |
| 37 | + // Safe: uses node_modules/.bin/playwright (programmatic, no auto-install risk) |
| 38 | + { |
| 39 | + code: dedent` |
| 40 | + spawn('./node_modules/.bin/playwright', ['test']); |
| 41 | + `, |
| 42 | + }, |
| 43 | + // Safe: unrelated exec call |
| 44 | + { |
| 45 | + code: dedent` |
| 46 | + exec('node scripts/jest src/foo.test.ts'); |
| 47 | + `, |
| 48 | + }, |
| 49 | + // Safe: npx with something other than playwright |
| 50 | + { |
| 51 | + code: dedent` |
| 52 | + execSync('npx some-other-tool'); |
| 53 | + `, |
| 54 | + }, |
| 55 | + // Safe: string not inside a shell call |
| 56 | + { |
| 57 | + code: dedent` |
| 58 | + const cmd = 'npx playwright test'; |
| 59 | + `, |
| 60 | + }, |
| 61 | + ], |
| 62 | + |
| 63 | + invalid: [ |
| 64 | + // exec with npx playwright |
| 65 | + { |
| 66 | + code: dedent` |
| 67 | + exec('npx playwright test --config config.ts'); |
| 68 | + `, |
| 69 | + errors: [{ message: ERROR_MSG }], |
| 70 | + }, |
| 71 | + // execSync with npx playwright |
| 72 | + { |
| 73 | + code: dedent` |
| 74 | + execSync('npx playwright test'); |
| 75 | + `, |
| 76 | + errors: [{ message: ERROR_MSG }], |
| 77 | + }, |
| 78 | + // spawn with npx playwright as first arg |
| 79 | + { |
| 80 | + code: dedent` |
| 81 | + spawn('npx playwright show-report ./output/reports'); |
| 82 | + `, |
| 83 | + errors: [{ message: ERROR_MSG }], |
| 84 | + }, |
| 85 | + // spawnSync with npx playwright |
| 86 | + { |
| 87 | + code: dedent` |
| 88 | + spawnSync('npx playwright show-trace ./trace.zip'); |
| 89 | + `, |
| 90 | + errors: [{ message: ERROR_MSG }], |
| 91 | + }, |
| 92 | + // execa with npx playwright |
| 93 | + { |
| 94 | + code: dedent` |
| 95 | + execa('npx playwright test', ['--config', 'config.ts']); |
| 96 | + `, |
| 97 | + errors: [{ message: ERROR_MSG }], |
| 98 | + }, |
| 99 | + // member expression: child_process.exec |
| 100 | + { |
| 101 | + code: dedent` |
| 102 | + child_process.exec('npx playwright test'); |
| 103 | + `, |
| 104 | + errors: [{ message: ERROR_MSG }], |
| 105 | + }, |
| 106 | + // template literal with npx playwright |
| 107 | + { |
| 108 | + code: dedent` |
| 109 | + execSync(\`npx playwright test --config \${configPath}\`); |
| 110 | + `, |
| 111 | + errors: [{ message: ERROR_MSG }], |
| 112 | + }, |
| 113 | + ], |
| 114 | +}); |
0 commit comments