Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for upgrade #532

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@ import { normalizePinnedPackages, loadPlebConfig } from '../utils/config.js';

const { gt, coerce } = semver;

type RequiredRegistryApi = Pick<NpmRegistry, 'fetchDistTags' | 'dispose'>;

export interface UpgradeOptions {
directoryPath: string;
dryRun?: boolean;
registryUrl?: string;
createRegistry?: (url: string, token?: string) => RequiredRegistryApi;
log?: (message: unknown) => void;
logError?: (message: unknown) => void;
}

function createDefaultRegistry(url: string, token?: string) {
return new NpmRegistry(url, token);
}

export async function upgrade({
directoryPath,
registryUrl,
dryRun,
createRegistry = createDefaultRegistry,
log = console.log,
logError = console.error,
}: UpgradeOptions): Promise<void> {
Expand All @@ -33,7 +41,7 @@ export async function upgrade({
const npmConfig = await loadEnvNpmConfig({ basePath: directoryPath });
const resolvedRegistryUrl = registryUrl ?? npmConfig['registry'] ?? officialNpmRegistryUrl;
const token = npmConfig[`${uriToIdentifier(resolvedRegistryUrl)}:_authToken`];
const registry = new NpmRegistry(resolvedRegistryUrl, token);
const registry = createRegistry(resolvedRegistryUrl, token);

const internalPackageNames = new Set<string>(packages.map(({ packageJson }) => packageJson.name!));

Expand Down Expand Up @@ -148,7 +156,7 @@ export async function upgrade({
}

export interface IFetchLatestPackageVersionsOptions {
registry: NpmRegistry;
registry: RequiredRegistryApi;
packageNames: Set<string>;
logError: (message: unknown) => void;
}
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/dependencies-exact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pleb-dependencies-exact",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"package-a": "1.0.0"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/dependencies-file-ref/inner-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pleb-dependencies-file-ref-inner",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"package-b": "^1.0.0"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/dependencies-file-ref/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pleb-dependencies-file-ref",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"package-a": "file:./inner-dep"
}
}
9 changes: 9 additions & 0 deletions test/fixtures/dependencies-pinned/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "pleb-dependencies",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"package-a": "~1.0.0",
"package-b": "~1.0.0"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/dependencies-pinned/pleb.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
pinnedPackages: [{ name: 'package-b', reason: 'broken stuff' }],
};
10 changes: 10 additions & 0 deletions test/fixtures/dependencies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "pleb-dependencies",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"package-a": "~1.0.0",
"package-b": "~1.0.0",
"package-c": "^1.0.0"
}
}
3 changes: 2 additions & 1 deletion test/fixtures/lerna-workspace/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"scripts": {},
"dependencies": {
"lerna-workspace-b": "^1.0.0"
"lerna-workspace-b": "^1.0.0",
"external-a": "^1.0.0"
}
}
5 changes: 4 additions & 1 deletion test/fixtures/lerna-workspace/packages/b/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "lerna-workspace-b",
"version": "1.0.0",
"scripts": {}
"scripts": {},
"dependencies": {
"external-b": "^1.0.0"
}
}
3 changes: 2 additions & 1 deletion test/fixtures/yarn-workspace/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"prepack": "echo \"prepack yarn-workspace-a\""
},
"dependencies": {
"yarn-workspace-b": "^1.0.0"
"yarn-workspace-b": "^1.0.0",
"external-a": "^1.0.0"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/yarn-workspace/packages/b/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"version": "1.0.0",
"scripts": {
"prepack": "echo \"prepack yarn-workspace-b\""
},
"dependencies": {
"external-b": "^1.0.0"
}
}
5 changes: 3 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "../dist/test",
"types": ["node", "mocha"]
}
"types": ["node", "mocha"],
},
"references": [{ "path": "../src" }]
}
233 changes: 233 additions & 0 deletions test/upgrade.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { join } from 'path';
import { fileURLToPath } from 'url';
import { expect } from 'chai';
import { upgrade } from 'pleb';

const fixturesRoot = fileURLToPath(new URL('../../test/fixtures', import.meta.url));

describe('pleb upgrade', () => {
function createMockRegistry(packagesData: Record<string, { latest: string; [tag: string]: string | undefined }>) {
return {
async fetchDistTags(packageName: string) {
return Promise.resolve(packagesData[packageName]!);
},
dispose() {
/**/
},
};
}

function createMockOutput() {
const output: { type: 'log' | 'error'; message: unknown }[] = [];
return {
output,
log(this: void, message: unknown) {
output.push({ type: 'log', message });
},
logError(this: void, message: unknown) {
output.push({ type: 'error', message });
},
};
}

it('should report nothing to upgrade for an empty project', async () => {
const newPackagePath = join(fixturesRoot, 'new-package');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () => createMockRegistry({}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 0 dependencies...' },
{ type: 'log', message: 'Nothing to upgrade.' },
]);
});

it('should report nothing to upgrade when everything is up to date', async () => {
const newPackagePath = join(fixturesRoot, 'dependencies');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () =>
createMockRegistry({
'package-a': { latest: '1.0.0' },
'package-b': { latest: '1.0.0' },
'package-c': { latest: '1.0.0' },
}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 3 dependencies...' },
{ type: 'log', message: 'Nothing to upgrade.' },
]);
});

it('should report on dependencies with newer versions', async () => {
const newPackagePath = join(fixturesRoot, 'dependencies');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () =>
createMockRegistry({
'package-a': { latest: '1.0.0' },
'package-b': { latest: '2.0.0' },
'package-c': { latest: '2.0.0' },
}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 3 dependencies...' },
{ type: 'log', message: 'Changes:' },
{ type: 'log', message: ' package-b ~1.0.0 -> ~2.0.0' },
{ type: 'log', message: ' package-c ^1.0.0 -> ^2.0.0' },
]);
});

it('should force exact version to caret (opinionated)', async () => {
const newPackagePath = join(fixturesRoot, 'dependencies-exact');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
AviVahl marked this conversation as resolved.
Show resolved Hide resolved
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () =>
createMockRegistry({
'package-a': { latest: '1.0.0' },
}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 1 dependencies...' },
{ type: 'log', message: 'Changes:' },
{ type: 'log', message: ' package-a 1.0.0 -> ^1.0.0' },
]);
});

it('should not update file reference', async () => {
const newPackagePath = join(fixturesRoot, 'dependencies-file-ref');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () =>
createMockRegistry({
'package-a': { latest: '2.0.0' },
'package-b': { latest: '1.0.0' },
}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 1 dependencies...' },
{ type: 'log', message: 'Nothing to upgrade.' },
]);
});

it('should report from multiple yarn workspaces (only on external packages)', async () => {
const newPackagePath = join(fixturesRoot, 'yarn-workspace');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () =>
createMockRegistry({
'yarn-workspace-b': { latest: '2.0.0' },
'external-a': { latest: '2.0.0' },
'external-b': { latest: '2.0.0' },
}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 2 dependencies...' },
{ type: 'log', message: 'Changes:' },
{ type: 'log', message: ' external-b ^1.0.0 -> ^2.0.0' },
{ type: 'log', message: ' external-a ^1.0.0 -> ^2.0.0' },
]);
});

it('should report from multiple lerna workspaces (only on external packages)', async () => {
const newPackagePath = join(fixturesRoot, 'lerna-workspace');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () =>
createMockRegistry({
'lerna-workspace-b': { latest: '2.0.0' },
'external-a': { latest: '2.0.0' },
'external-b': { latest: '2.0.0' },
}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 2 dependencies...' },
{ type: 'log', message: 'Changes:' },
{ type: 'log', message: ' external-b ^1.0.0 -> ^2.0.0' },
{ type: 'log', message: ' external-a ^1.0.0 -> ^2.0.0' },
]);
});

it('should skip pinned packages', async () => {
const newPackagePath = join(fixturesRoot, 'dependencies-pinned');
const { log, logError, output } = createMockOutput();

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await upgrade({
dryRun: true,
directoryPath: newPackagePath,
registryUrl: '',
createRegistry: () =>
createMockRegistry({
'package-a': { latest: '2.0.0' },
'package-b': { latest: '2.0.0' },
}),
log,
logError,
});

expect(output).to.eql([
{ type: 'log', message: 'Getting "latest" version for 2 dependencies...' },
{ type: 'log', message: 'Changes:' },
{ type: 'log', message: ' package-a ~1.0.0 -> ~2.0.0' },
{ type: 'log', message: 'Skipped:' },
{ type: 'log', message: ' package-b ~1.0.0 -> ~2.0.0 (broken stuff)' },
]);
});
});