Skip to content

Commit eeb9273

Browse files
James Crosettoljharb
James Crosetto
authored andcommitted
[Fix] version detection: Add tests that verify versioning works for sibling and child projects
Fixes #2218
1 parent acf7d62 commit eeb9273

File tree

12 files changed

+64
-13
lines changed

12 files changed

+64
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2020
* [`destructuring-assignment`]: fix a false positive for local prop named `context` in SFC ([#2929][] @SyMind)
2121
* [`jsx-no-target-blank`]: Allow rel="noreferrer" when `allowReferrer` is true ([#2925][] @edemaine)
2222
* [`boolean-prop-naming`]: add check for typescript "boolean" type ([#2930][] @vedadeepta)
23+
* version detection: Add tests that verify versioning works for sibling and child projects ([#2943][] @jcrosetto)
2324

2425
### Changed
2526
* [Docs] [`jsx-no-constructed-context-values`][]: fix invalid example syntax ([#2910][] @kud)
2627
* [readme] Replace lists of rules with tables in readme ([#2908][] @motato1)
2728
* [Docs] added missing curly braces ([#2923][] @Muditxofficial)
2829

30+
[#2943]: https://github.com/yannickcr/eslint-plugin-react/pull/2943
2931
[#2930]: https://github.com/yannickcr/eslint-plugin-react/pull/2930
3032
[#2929]: https://github.com/yannickcr/eslint-plugin-react/pull/2929
3133
[#2925]: https://github.com/yannickcr/eslint-plugin-react/pull/2925

lib/util/version.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const resolve = require('resolve');
9+
const path = require('path');
910
const error = require('./error');
1011

1112
let warnedForMissingVersion = false;
@@ -20,13 +21,14 @@ function resetDetectedVersion() {
2021
cachedDetectedReactVersion = undefined;
2122
}
2223

23-
function detectReactVersion() {
24+
// TODO, semver-major: remove context fallback
25+
function detectReactVersion(context) {
2426
if (cachedDetectedReactVersion) {
2527
return cachedDetectedReactVersion;
2628
}
2729

2830
try {
29-
const reactPath = resolve.sync('react', {basedir: process.cwd()});
31+
const reactPath = resolve.sync('react', {basedir: context ? path.dirname(context.getFilename()) : process.cwd()});
3032
const react = require(reactPath); // eslint-disable-line global-require, import/no-dynamic-require
3133
cachedDetectedReactVersion = react.version;
3234
return cachedDetectedReactVersion;
@@ -50,7 +52,7 @@ function getReactVersionFromContext(context) {
5052
if (context.settings && context.settings.react && context.settings.react.version) {
5153
let settingsVersion = context.settings.react.version;
5254
if (settingsVersion === 'detect') {
53-
settingsVersion = detectReactVersion();
55+
settingsVersion = detectReactVersion(context);
5456
}
5557
if (typeof settingsVersion !== 'string') {
5658
error('Warning: React version specified in eslint-plugin-react-settings must be a string; '
@@ -66,9 +68,10 @@ function getReactVersionFromContext(context) {
6668
return confVer.split('.').map((part) => Number(part));
6769
}
6870

69-
function detectFlowVersion() {
71+
// TODO, semver-major: remove context fallback
72+
function detectFlowVersion(context) {
7073
try {
71-
const flowPackageJsonPath = resolve.sync('flow-bin/package.json', {basedir: process.cwd()});
74+
const flowPackageJsonPath = resolve.sync('flow-bin/package.json', {basedir: context ? path.dirname(context.getFilename()) : process.cwd()});
7275
const flowPackageJson = require(flowPackageJsonPath); // eslint-disable-line global-require, import/no-dynamic-require
7376
return flowPackageJson.version;
7477
} catch (e) {
@@ -87,7 +90,7 @@ function getFlowVersionFromContext(context) {
8790
if (context.settings.react && context.settings.react.flowVersion) {
8891
let flowVersion = context.settings.react.flowVersion;
8992
if (flowVersion === 'detect') {
90-
flowVersion = detectFlowVersion();
93+
flowVersion = detectFlowVersion(context);
9194
}
9295
if (typeof flowVersion !== 'string') {
9396
error('Warning: Flow version specified in eslint-plugin-react-settings must be a string; '

tests/fixtures/version/detect-version-sibling/node_modules/flow-bin/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/version/detect-version-sibling/node_modules/react/index.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/version/detect-version-sibling/node_modules/react/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/version/detect-version-sibling/test.js

Whitespace-only changes.

tests/fixtures/version/detect-version/detect-version-child/node_modules/flow-bin/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/version/detect-version/detect-version-child/node_modules/react/index.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/version/detect-version/detect-version-child/node_modules/react/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/version/detect-version/detect-version-child/test.js

Whitespace-only changes.

tests/fixtures/version/detect-version/test.js

Whitespace-only changes.

tests/util/version.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,54 @@ const versionUtil = require('../../lib/util/version');
77

88
describe('Version', () => {
99
const base = path.resolve(__dirname, '..', 'fixtures', 'version');
10-
let cwd;
1110
let expectedErrorArgs = [];
1211

1312
beforeEach(() => {
14-
cwd = process.cwd();
15-
process.chdir(base);
1613
sinon.stub(console, 'error');
1714
expectedErrorArgs = [];
1815
versionUtil.resetWarningFlag();
1916
versionUtil.resetDetectedVersion();
2017
});
2118

2219
afterEach(() => {
23-
process.chdir(cwd);
24-
2520
const actualArgs = console.error.args; // eslint-disable-line no-console
2621
console.error.restore(); // eslint-disable-line no-console
2722
assert.deepEqual(actualArgs, expectedErrorArgs);
2823
});
2924

3025
describe('Detect version', () => {
31-
const context = {settings: {react: {version: 'detect', flowVersion: 'detect'}}};
26+
const context = {settings: {react: {version: 'detect', flowVersion: 'detect'}}, getFilename: () => path.resolve(base, 'test.js')};
27+
28+
afterEach(() => {
29+
if (context.getFilename.restore) {
30+
context.getFilename.restore();
31+
}
32+
});
3233

3334
it('matches detected version', () => {
34-
process.chdir('detect-version');
35+
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version', 'test.js'));
36+
3537
assert.equal(versionUtil.testReactVersion(context, '1.2.3'), true);
3638
assert.equal(versionUtil.testReactVersion(context, '1.2.4'), false);
3739
assert.equal(versionUtil.testFlowVersion(context, '0.92.0'), true);
3840
});
3941

42+
it('matches detected version in sibling project', () => {
43+
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version-sibling', 'test.js'));
44+
45+
assert.equal(versionUtil.testReactVersion(context, '2.3.4'), true);
46+
assert.equal(versionUtil.testReactVersion(context, '2.3.5'), false);
47+
assert.equal(versionUtil.testFlowVersion(context, '2.92.0'), true);
48+
});
49+
50+
it('matches detected version in child project', () => {
51+
sinon.stub(context, 'getFilename').callsFake(() => path.resolve(base, 'detect-version', 'detect-version-child', 'test.js'));
52+
53+
assert.equal(versionUtil.testReactVersion(context, '3.4.5'), true);
54+
assert.equal(versionUtil.testReactVersion(context, '3.4.6'), false);
55+
assert.equal(versionUtil.testFlowVersion(context, '3.92.0'), true);
56+
});
57+
4058
it('assumes latest version if react is not installed', () => {
4159
assert.equal(versionUtil.testReactVersion(context, '999.999.999'), true);
4260

0 commit comments

Comments
 (0)