Skip to content

Commit

Permalink
feat(release): update lockfile after version command (nrwl#21107)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Jan 23, 2024
1 parent 6164481 commit 096cefb
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 57 deletions.
39 changes: 37 additions & 2 deletions e2e/release/src/independent-projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { joinPathFragments } from '@nx/devkit';
import {
cleanupProject,
exists,
getSelectedPackageManager,
newProject,
readFile,
runCLI,
Expand Down Expand Up @@ -34,6 +35,16 @@ expect.addSnapshotSerializer({
.replaceAll(/\d*\.\d*\s?kB/g, 'XXX.XXX kb')
// Normalize the version title date
.replaceAll(/\(\d{4}-\d{2}-\d{2}\)/g, '(YYYY-MM-DD)')
.replaceAll('package-lock.json', '{lock-file}')
.replaceAll('yarn.lock', '{lock-file}')
.replaceAll('pnpm-lock.yaml', '{lock-file}')
.replaceAll('npm install --package-lock-only', '{lock-file-command}')
.replaceAll(
'yarn install --mode update-lockfile',
'{lock-file-command}'
)
.replaceAll('pnpm install --lockfile-only', '{lock-file-command}')
.replaceAll(getSelectedPackageManager(), '{package-manager}')
// We trim each line to reduce the chances of snapshot flakiness
.split('\n')
.map((r) => r.trim())
Expand Down Expand Up @@ -127,6 +138,9 @@ describe('nx release - independent projects', () => {
"scripts": {
> NX Updating {package-manager} lock file
> NX Staging changed files with git
Expand Down Expand Up @@ -159,6 +173,9 @@ describe('nx release - independent projects', () => {
+
> NX Updating {package-manager} lock file
> NX Staging changed files with git
Expand Down Expand Up @@ -198,6 +215,9 @@ describe('nx release - independent projects', () => {
}
> NX Updating {package-manager} lock file
> NX Staging changed files with git
Expand Down Expand Up @@ -237,10 +257,15 @@ describe('nx release - independent projects', () => {
"scripts": {
> NX Updating {package-manager} lock file
Updating {lock-file} with the following command:
{lock-file-command}
> NX Committing changes with git
Staging files in git with the following command:
git add {project-name}/package.json
git add {project-name}/package.json {lock-file}
Committing files in git with the following command:
git commit --message chore(release): publish --message - project: {project-name} 999.9.9-version-git-operations-test.2
Expand Down Expand Up @@ -340,10 +365,20 @@ describe('nx release - independent projects', () => {
"scripts": {
> NX Updating {package-manager} lock file
Updating {lock-file} with the following command:
{lock-file-command}
> NX Updating {package-manager} lock file
Updating {lock-file} with the following command:
{lock-file-command}
> NX Committing changes with git
Staging files in git with the following command:
git add {project-name}/package.json {project-name}/package.json {project-name}/package.json
git add {project-name}/package.json {project-name}/package.json {project-name}/package.json {lock-file}
Committing files in git with the following command:
git commit --message chore(release): publish --message - project: {project-name} 999.9.9-version-git-operations-test.3 --message - project: {project-name} 999.9.9-version-git-operations-test.3 --message - release-group: fixed 999.9.9-version-git-operations-test.3
Expand Down
187 changes: 187 additions & 0 deletions e2e/release/src/lock-file-updates.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import {
cleanupProject,
newProject,
runCLI,
runCommand,
uniq,
updateFile,
updateJson,
} from '@nx/e2e/utils';

expect.addSnapshotSerializer({
serialize(str: string) {
return (
str
// Remove all output unique to specific projects to ensure deterministic snapshots
.replaceAll(/my-pkg-\d+/g, '{project-name}')
.replaceAll(
/integrity:\s*.*/g,
'integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
)
.replaceAll(/\b[0-9a-f]{40}\b/g, '{SHASUM}')
.replaceAll(/\d*B index\.js/g, 'XXB index.js')
.replaceAll(/\d*B project\.json/g, 'XXB project.json')
.replaceAll(/\d*B package\.json/g, 'XXXB package.json')
.replaceAll(/size:\s*\d*\s?B/g, 'size: XXXB')
.replaceAll(/\d*\.\d*\s?kB/g, 'XXX.XXX kb')
.replaceAll(/[a-fA-F0-9]{7}/g, '{COMMIT_SHA}')
.replaceAll(/Test @[\w\d]+/g, 'Test @{COMMIT_AUTHOR}')
// Normalize the version title date.
.replaceAll(/\(\d{4}-\d{2}-\d{2}\)/g, '(YYYY-MM-DD)')
// We trim each line to reduce the chances of snapshot flakiness
.split('\n')
.map((r) => r.trim())
.join('\n')
);
},
test(val: string) {
return val != null && typeof val === 'string';
},
});

describe('nx release lock file updates', () => {
let pkg1: string;
let pkg2: string;
let pkg3: string;
let previousPackageManager: string;
let previousYarnEnableImmutableInstalls: string;
let previousNodeOptions: string;

beforeAll(() => {
previousPackageManager = process.env.SELECTED_PM;
previousYarnEnableImmutableInstalls =
process.env.YARN_ENABLE_IMMUTABLE_INSTALLS;
previousNodeOptions = process.env.NODE_OPTIONS;
});

// project will be created by each test individually
// in order to test different package managers
const initializeProject = (packageManager: 'npm' | 'yarn' | 'pnpm') => {
process.env.SELECTED_PM = packageManager;

newProject({
unsetProjectNameAndRootFormat: false,
packages: ['@nx/js'],
packageManager,
});

pkg1 = uniq('my-pkg-1');
runCLI(`generate @nx/workspace:npm-package ${pkg1}`);

pkg2 = uniq('my-pkg-2');
runCLI(`generate @nx/workspace:npm-package ${pkg2}`);

pkg3 = uniq('my-pkg-3');
runCLI(`generate @nx/workspace:npm-package ${pkg3}`);

// Update pkg2 to depend on pkg1
updateJson(`${pkg2}/package.json`, (json) => {
json.dependencies ??= {};
json.dependencies[`@proj/${pkg1}`] = '0.0.0';
return json;
});
};

afterEach(() => {
cleanupProject();
});

afterAll(() => {
process.env.SELECTED_PM = previousPackageManager;
process.env.YARN_ENABLE_IMMUTABLE_INSTALLS =
previousYarnEnableImmutableInstalls;
process.env.NODE_OPTIONS = previousNodeOptions;
});

it('should update package-lock.json when package manager is npm', async () => {
initializeProject('npm');

runCommand(`npm install`);

// workaround for NXC-143
runCLI('reset');

runCommand(`git add .`);
runCommand(`git commit -m "chore: initial commit"`);

const versionOutput = runCLI(`release version 999.9.9`);

expect(versionOutput.match(/NX Updating npm lock file/g).length).toBe(1);

const filesChanges = runCommand('git diff --name-only HEAD');

expect(filesChanges).toMatchInlineSnapshot(`
{project-name}/package.json
{project-name}/package.json
{project-name}/package.json
package-lock.json
`);
});

it.skip('should update yarn.lock when package manager is yarn', async () => {
process.env.YARN_ENABLE_IMMUTABLE_INSTALLS = 'false';
process.env.NODE_OPTIONS = '--no-enable-network-family-autoselection';

initializeProject('yarn');

updateJson('package.json', (json) => {
json.workspaces = [pkg1, pkg2, pkg3];
return json;
});

runCommand(`yarn install`);

// workaround for NXC-143
runCLI('reset');

runCommand(`git add .`);
runCommand(`git commit -m "chore: initial commit"`);

const versionOutput = runCLI(`release version 999.9.9`);

expect(versionOutput.match(/NX Updating yarn lock file/g).length).toBe(1);

const filesChanges = runCommand('git diff --name-only HEAD');

expect(filesChanges).toMatchInlineSnapshot(`
.yarn/install-state.gz
{project-name}/package.json
{project-name}/package.json
{project-name}/package.json
yarn.lock
`);
});

it('should update pnpm-lock.yaml when package manager is pnpm', async () => {
initializeProject('pnpm');

updateFile(
'pnpm-workspace.yaml',
`packages:\n - ${pkg1}\n - ${pkg2}\n - ${pkg3}\n`
);

// workaround for NXC-143
runCLI('reset');

runCommand(`pnpm install`);

runCommand(`git add .`);
runCommand(`git commit -m "chore: initial commit"`);

const versionOutput = runCLI(`release version 999.9.9`);

expect(versionOutput.match(/NX Updating pnpm lock file/g).length).toBe(1);

const filesChanges = runCommand('git diff --name-only HEAD');

expect(filesChanges).toMatchInlineSnapshot(`
{project-name}/package.json
{project-name}/package.json
{project-name}/package.json
pnpm-lock.yaml
`);
});
});
19 changes: 8 additions & 11 deletions e2e/release/src/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,17 +1146,14 @@ ${JSON.stringify(
silenceError: true,
});

expect(releaseOutput6a).toMatchInlineSnapshot(`
> NX Running release version for project: {project-name}
{project-name} 🔍 Reading data for package "@proj/{project-name}" from {project-name}/package.json
> NX Unable to resolve the current version from the registry ${e2eRegistryUrl}. Please ensure that the package exists in the registry in order to use the "registry" currentVersionResolver. Alternatively, you can set the "version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when the registry lookup fails.
- Resolving the current version for tag "other" on ${e2eRegistryUrl}
`);
expect(
releaseOutput6a.match(
new RegExp(
`> NX Unable to resolve the current version from the registry ${e2eRegistryUrl}. Please ensure that the package exists in the registry in order to use the "registry" currentVersionResolver. Alternatively, you can set the "version.generatorOptions.fallbackCurrentVersionResolver" to "disk" in order to fallback to the version on disk when the registry lookup fails.`,
'g'
)
).length
).toEqual(1);

const releaseOutput6b = runCLI(
`release patch --skip-publish --first-release`,
Expand Down
57 changes: 30 additions & 27 deletions packages/js/src/generators/release-version/release-version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,36 @@ describe('release-version', () => {
})
).toMatchInlineSnapshot(`
{
"my-lib": {
"currentVersion": "0.0.1",
"dependentProjects": [
{
"dependencyCollection": "dependencies",
"source": "project-with-dependency-on-my-pkg",
"target": "my-lib",
"type": "static",
},
{
"dependencyCollection": "devDependencies",
"source": "project-with-devDependency-on-my-pkg",
"target": "my-lib",
"type": "static",
},
],
"newVersion": "1.0.0",
},
"project-with-dependency-on-my-pkg": {
"currentVersion": "0.0.1",
"dependentProjects": [],
"newVersion": "1.0.0",
},
"project-with-devDependency-on-my-pkg": {
"currentVersion": "0.0.1",
"dependentProjects": [],
"newVersion": "1.0.0",
"callback": [Function],
"data": {
"my-lib": {
"currentVersion": "0.0.1",
"dependentProjects": [
{
"dependencyCollection": "dependencies",
"source": "project-with-dependency-on-my-pkg",
"target": "my-lib",
"type": "static",
},
{
"dependencyCollection": "devDependencies",
"source": "project-with-devDependency-on-my-pkg",
"target": "my-lib",
"type": "static",
},
],
"newVersion": "1.0.0",
},
"project-with-dependency-on-my-pkg": {
"currentVersion": "0.0.1",
"dependentProjects": [],
"newVersion": "1.0.0",
},
"project-with-devDependency-on-my-pkg": {
"currentVersion": "0.0.1",
"dependentProjects": [],
"newVersion": "1.0.0",
},
},
}
`);
Expand Down
Loading

0 comments on commit 096cefb

Please sign in to comment.