From c09f3b62460a30ddb73c5f41a7321c1d3c3ae671 Mon Sep 17 00:00:00 2001 From: Alex Zherdev Date: Wed, 12 Sep 2018 22:10:25 -0700 Subject: [PATCH] Add tests for react version detection --- .gitignore | 2 +- lib/util/version.js | 35 ++++++++++--------- .../node_modules/react/index.js | 5 +++ .../node_modules/react/package.json | 5 +++ tests/util/version.js | 34 ++++++++++++++++++ 5 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 tests/fixtures/version/detect-version/node_modules/react/index.js create mode 100644 tests/fixtures/version/detect-version/node_modules/react/package.json create mode 100644 tests/util/version.js diff --git a/.gitignore b/.gitignore index 4b8d368260..1568864758 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,7 @@ pids logs reports build -node_modules +/node_modules npm-debug.log sftp-config.json diff --git a/lib/util/version.js b/lib/util/version.js index 87e87e5319..4aa472ccaa 100644 --- a/lib/util/version.js +++ b/lib/util/version.js @@ -9,27 +9,30 @@ const log = require('./log'); let warnedForMissingVersion = false; +function detectReactVersion() { + try { + const reactPath = resolve.sync('react', {basedir: process.cwd()}); + const react = require(reactPath); + return react.version; + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' + + 'but the "react" package is not installed. Assuming latest React version for linting.'); + return '999.999.999'; + } + throw e; + } +} + function getReactVersionFromContext(context) { let confVer = '999.999.999'; // .eslintrc shared settings (http://eslint.org/docs/user-guide/configuring#adding-shared-settings) if (context.settings.react && context.settings.react.version) { - let settingsVer = context.settings.react.version; - if (settingsVer === 'detect') { - try { - const reactPath = resolve.sync('react', {basedir: process.cwd()}); - const react = require(reactPath); - settingsVer = react.version; - } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') { - log('Warning: React version was set to "detect" in eslint-plugin-react settings, ' + - 'but the "react" package is not installed. Assuming latest React version for linting.'); - settingsVer = '999.999.999'; - } else { - throw e; - } - } + let settingsVersion = context.settings.react.version; + if (settingsVersion === 'detect') { + settingsVersion = detectReactVersion(); } - confVer = settingsVer; + confVer = settingsVersion; } else if (!warnedForMissingVersion) { log('Warning: React version not specified in eslint-plugin-react settings. ' + 'See https://github.com/yannickcr/eslint-plugin-react#configuration.'); diff --git a/tests/fixtures/version/detect-version/node_modules/react/index.js b/tests/fixtures/version/detect-version/node_modules/react/index.js new file mode 100644 index 0000000000..022bb03217 --- /dev/null +++ b/tests/fixtures/version/detect-version/node_modules/react/index.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + version: '1.2.3' +}; diff --git a/tests/fixtures/version/detect-version/node_modules/react/package.json b/tests/fixtures/version/detect-version/node_modules/react/package.json new file mode 100644 index 0000000000..56d20a7d44 --- /dev/null +++ b/tests/fixtures/version/detect-version/node_modules/react/package.json @@ -0,0 +1,5 @@ +{ + "name": "react", + "version": "1.2.3", + "main": "index.js" +} diff --git a/tests/util/version.js b/tests/util/version.js new file mode 100644 index 0000000000..5112f8afcd --- /dev/null +++ b/tests/util/version.js @@ -0,0 +1,34 @@ +/* eslint-env mocha */ +'use strict'; + +const path = require('path'); +const assert = require('assert'); +const versionUtil = require('../../lib/util/version'); + +describe('Version', () => { + const base = path.resolve(__dirname, '..', 'fixtures', 'version'); + let cwd; + + beforeEach(() => { + cwd = process.cwd(); + process.chdir(base); + }); + + afterEach(() => { + process.chdir(cwd); + }); + + describe('Detect version', () => { + const context = {settings: {react: {version: 'detect'}}}; + + it('matches detected version', () => { + process.chdir('detect-version'); + assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true); + assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false); + }); + + it('assumes latest version if react is not installed', () => { + assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true); + }); + }); +});