Skip to content

Commit

Permalink
Autofix to increased latest version range (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Feb 22, 2022
1 parent 896f0a0 commit 33c92bb
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/dependency-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
compareVersionRanges,
compareVersionRangesSafe,
versionRangeToRange,
getLatestVersion,
getIncreasedLatestVersion,
getHighestRangeType,
} from './semver.js';
import semver from 'semver';
Expand Down Expand Up @@ -273,7 +273,7 @@ export function fixMismatchingVersions(
);
let fixedVersion;
try {
fixedVersion = getLatestVersion(versions);
fixedVersion = getIncreasedLatestVersion(versions);
} catch {
// Skip this dependency.
notFixed.push(mismatchingVersion);
Expand Down
7 changes: 5 additions & 2 deletions lib/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import chalk from 'chalk';
import type { MismatchingDependencyVersions } from './dependency-versions.js';
import { compareVersionRangesSafe, getLatestVersion } from './semver.js';
import {
compareVersionRangesSafe,
getIncreasedLatestVersion,
} from './semver.js';
import { table } from 'table';

export function mismatchingVersionsToOutput(
Expand Down Expand Up @@ -67,7 +70,7 @@ export function mismatchingVersionsFixedToOutput(
.flatMap((mismatchingVersion) => {
let version;
try {
version = getLatestVersion(
version = getIncreasedLatestVersion(
mismatchingVersion.versions.map((v) => v.version)
);
} catch {
Expand Down
29 changes: 29 additions & 0 deletions lib/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,32 @@ export function getHighestRangeType(ranges: string[]): string {
const sorted = ranges.sort(compareRanges);
return sorted[sorted.length - 1]; // Range with highest precedence will be sorted to end of list.
}

// Example input: ['1.5.0', '^1.0.0'], output: '^1.5.0'
export function getIncreasedLatestVersion(versions: string[]): string {
const latestVersion = getLatestVersion(versions);
const latestVersionBare = semver.coerce(latestVersion);

let result = latestVersion;
let resultBare = latestVersionBare;
for (const version of versions) {
if (version === latestVersion) {
continue;
}

const versionBare = semver.coerce(version);

if (
latestVersionBare &&
semver.satisfies(latestVersionBare, version) &&
versionBare &&
semver.gt(latestVersionBare, versionBare) &&
resultBare
) {
result = version.replace(String(versionBare), String(resultBare));
resultBare = semver.coerce(result);
}
}

return result;
}
8 changes: 8 additions & 0 deletions test/fixtures/increasable-range/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"workspaces": [
"*"
],
"devDependencies": {
"foo": "^1.0.0"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/increasable-range/package1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "package1",
"dependencies": {
"foo": "1.5.0"
}
}
4 changes: 4 additions & 0 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ export const FIXTURE_PATH_INCONSISTENT_LOCAL_PACKAGE_VERSION = join(
FIXTURE_PATH,
'inconsistent-local-package-version'
);
export const FIXTURE_PATH_INCREASABLE_RANGE = join(
FIXTURE_PATH,
'increasable-range'
);
59 changes: 59 additions & 0 deletions test/lib/dependency-versions-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,5 +684,64 @@ describe('Utils | dependency-versions', function () {
]);
});
});

describe('increasable range', function () {
beforeEach(function () {
// Create a mock workspace filesystem for temporary usage in this test because changes will be written to some files.
mockFs({
'package.json': JSON.stringify({
workspaces: ['*'],
}),
package1: {
'package.json': JSON.stringify({
name: 'package1',
dependencies: {
foo: '^1.0.0',
},
}),
},
package2: {
'package.json': JSON.stringify({
name: 'package2',
dependencies: {
foo: '1.5.0',
},
}),
},
});
});

afterEach(function () {
mockFs.restore();
});

it('increases the range', function () {
const packages = getPackagesHelper('.');
const mismatchingVersions = calculateMismatchingVersions(
calculateVersionsForEachDependency(packages)
);
fixMismatchingVersions(packages, mismatchingVersions);

// Read in package.json files.
const packageJson1Contents = readFileSync(
'package1/package.json',
'utf-8'
);
const packageJson2Contents = readFileSync(
'package2/package.json',
'utf-8'
);
const packageJson1: PackageJson = JSON.parse(packageJson1Contents);
const packageJson2: PackageJson = JSON.parse(packageJson2Contents);

expect(
packageJson1.dependencies && packageJson1.dependencies['foo']
).toStrictEqual('^1.5.0');

expect(
packageJson2.dependencies && packageJson2.dependencies['foo']
).toStrictEqual('^1.5.0');
});
});
});
});
13 changes: 13 additions & 0 deletions test/lib/output-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getPackages } from '../../lib/workspace.js';
import {
FIXTURE_PATH_TESTING_OUTPUT,
FIXTURE_PATH_NAMES_NOT_MATCHING_LOCATIONS,
FIXTURE_PATH_INCREASABLE_RANGE,
} from '../fixtures/index.js';

describe('Utils | output', function () {
Expand Down Expand Up @@ -123,6 +124,18 @@ describe('Utils | output', function () {
).toMatchInlineSnapshot('"Fixed versions for 1 dependency: bar@2.0.0"');
});

it('behaves correctly with an increasable range', function () {
expect(
mismatchingVersionsFixedToOutput(
calculateMismatchingVersions(
calculateVersionsForEachDependency(
getPackages(FIXTURE_PATH_INCREASABLE_RANGE, [], [], [], [])
)
).slice(0, 1)
)
).toMatchInlineSnapshot('"Fixed versions for 1 dependency: foo@^1.5.0"');
});

it('behaves correctly with empty input', function () {
expect(() =>
mismatchingVersionsFixedToOutput([])
Expand Down
55 changes: 55 additions & 0 deletions test/lib/semver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
versionRangeToRange,
getLatestVersion,
getHighestRangeType,
getIncreasedLatestVersion,
} from '../../lib/semver.js';

describe('Utils | semver', function () {
Expand Down Expand Up @@ -112,4 +113,58 @@ describe('Utils | semver', function () {
expect(getHighestRangeType(['^', '~'])).toStrictEqual('^');
});
});

describe('#getIncreasedLatestVersion', function () {
it('behaves correctly', function () {
// ^
expect(getIncreasedLatestVersion(['^1.0.0', '1.5.0'])).toStrictEqual(
'^1.5.0'
);
expect(getIncreasedLatestVersion(['1.5.0', '^1.0.0'])).toStrictEqual(
'^1.5.0'
);
expect(getIncreasedLatestVersion(['^0.4.0', '^0.4.5'])).toStrictEqual(
'^0.4.5'
);
expect(getIncreasedLatestVersion(['^0.4.0', '0.5.0'])).toStrictEqual(
'0.5.0'
);
expect(getIncreasedLatestVersion(['^1.0.0', '2.0.0'])).toStrictEqual(
'2.0.0'
);
expect(getIncreasedLatestVersion(['^1.0.0', '^1.0.0'])).toStrictEqual(
'^1.0.0'
);

// ^ ~
expect(getIncreasedLatestVersion(['~1.5.0', '^1.0.0'])).toStrictEqual(
'^1.5.0'
);
expect(getIncreasedLatestVersion(['~1.5.0', '^2.0.0'])).toStrictEqual(
'^2.0.0'
);
expect(getIncreasedLatestVersion(['~2.0.0', '^1.5.0'])).toStrictEqual(
'~2.0.0'
);

// ~
expect(getIncreasedLatestVersion(['~1.4.0', '1.4.5'])).toStrictEqual(
'~1.4.5'
);
expect(getIncreasedLatestVersion(['~1.4.0', '~1.5.0'])).toStrictEqual(
'~1.5.0'
);
expect(getIncreasedLatestVersion(['~1.0.0', '~1.0.0'])).toStrictEqual(
'~1.0.0'
);

// no range
expect(getIncreasedLatestVersion(['1.5.0', '1.0.0'])).toStrictEqual(
'1.5.0'
);
expect(getIncreasedLatestVersion(['1.0.0', '1.0.0'])).toStrictEqual(
'1.0.0'
);
});
});
});

0 comments on commit 33c92bb

Please sign in to comment.