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

fix(cli): stop the publishing process before sending an empty transaction #1194

Merged
merged 8 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
105 changes: 74 additions & 31 deletions packages/cli/src/commands/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
DeploymentInfo,
InMemoryRegistry,
IPFSLoader,
OnChainRegistry,
preparePublishPackage,
OnChainRegistry,
} from '@usecannon/builder';
import * as builder from '@usecannon/builder';
import fs from 'fs-extra';
import _ from 'lodash';
import path from 'path';
Expand Down Expand Up @@ -136,37 +137,75 @@ describe('publish command', () => {
jest.spyOn(OnChainRegistry.prototype, 'publish').mockResolvedValue([]);
});

it('should publish the package to the registry', async () => {
// jest spy on fs readdir which return string[] of package.json
await publish({
packageRef: fullPackageRef,
cliSettings: resolveCliSettings(),
onChainRegistry,
tags,
chainId,
quiet: true,
includeProvisioned: false,
skipConfirm: true,
describe('publish', () => {
it('should not publish if there are no new packages', async () => {
jest.spyOn(builder, 'preparePublishPackage').mockResolvedValue([]);

await expect(
publish({
packageRef: fullPackageRef,
cliSettings: resolveCliSettings(),
onChainRegistry,
tags,
chainId,
quiet: true,
skipConfirm: true,
includeProvisioned: false,
})
).rejects.toThrow("There isn't anything new to publish.");
});

expect(await onChainRegistry.getUrl(fullPackageRef, chainId)).toEqual(testPkgDataNewIpfsUrl);
expect(await onChainRegistry.getUrl(`package:tag0@${preset}`, chainId)).toEqual(testPkgDataNewIpfsUrl);
});
it('should publish the package to the registry', async () => {
// jest spy on fs readdir which return string[] of package.json
jest.spyOn(builder, 'preparePublishPackage').mockResolvedValue([
{
packagesNames: [fullPackageRef, `${fullPackageRef}:tag0@${preset}`],
chainId,
url: 'ipfs://test-ipfs-new-url',
metaUrl: '',
},
]);

it('should publish the package to the registry with no tags', async () => {
tags = [];
await publish({
packageRef: fullPackageRef,
cliSettings: resolveCliSettings(),
onChainRegistry,
tags,
chainId,
quiet: true,
skipConfirm: true,
includeProvisioned: true,
await publish({
packageRef: fullPackageRef,
cliSettings: resolveCliSettings(),
onChainRegistry,
tags,
chainId,
quiet: true,
skipConfirm: true,
includeProvisioned: false,
});

expect(await onChainRegistry.getUrl(fullPackageRef, chainId)).toEqual(testPkgDataNewIpfsUrl);
expect(await onChainRegistry.getUrl(`${fullPackageRef}:tag0@${preset}`, chainId)).toEqual(testPkgDataNewIpfsUrl);
});

expect(await onChainRegistry.getUrl(fullPackageRef, chainId)).toEqual(testPkgDataNewIpfsUrl);
it('should publish the package to the registry with no tags', async () => {
tags = [];

jest.spyOn(builder, 'preparePublishPackage').mockResolvedValue([
{
packagesNames: [fullPackageRef],
chainId,
url: 'ipfs://test-ipfs-new-url',
metaUrl: '',
},
]);

await publish({
packageRef: fullPackageRef,
cliSettings: resolveCliSettings(),
onChainRegistry,
tags,
chainId,
quiet: true,
skipConfirm: true,
includeProvisioned: true,
});

expect(await onChainRegistry.getUrl(fullPackageRef, chainId)).toEqual(testPkgDataNewIpfsUrl);
});
});

describe('scanDeploys', () => {
Expand All @@ -179,10 +218,14 @@ describe('publish command', () => {
// @ts-ignore
jest.spyOn(fs, 'readdir').mockResolvedValue(_deployDataLocalFileNames);

const builder = await import('@usecannon/builder');
jest.spyOn(builder, 'preparePublishPackage').mockImplementation(async () => {
return [];
});
jest.spyOn(builder, 'preparePublishPackage').mockResolvedValue([
{
packagesNames: [fullPackageRef],
chainId,
url: 'ipfs://test-ipfs-new-url',
metaUrl: '',
},
]);
});

it('should only find single deploy file on chainId and preset set', async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ export async function publish({
publishCalls.push(...calls);
}

if (!publishCalls.length) {
throw new Error("There isn't anything new to publish.");
}

if (!skipConfirm) {
for (const publishCall of publishCalls) {
const packageName = new PackageReference(publishCall.packagesNames[0]).name;
Expand All @@ -179,7 +183,7 @@ export async function publish({
totalFees > 0n &&
totalFees >= (await onChainRegistry.provider!.getBalance({ address: onChainRegistry.signer!.address }))
) {
throw new Error('you do not appear to have enough ETH in your wallet to publish');
throw new Error('You do not appear to have enough ETH in your wallet to publish');
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/util/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const hideApiKey = (providerUrl: string) => {
parsedUrl.pathname = pathParts.join('/');
return parsedUrl.toString();
} catch (error) {
debug('error processing url:', error);
return providerUrl; // return original URL if parsing fails
}
};
Expand Down
Loading