Skip to content

Commit

Permalink
Test that the specified contract does not have build info dir - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Aug 16, 2024
1 parent ec9c3ad commit 0bad622
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
23 changes: 22 additions & 1 deletion packages/core/src/cli/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,25 @@ test('validate - references other build info dir by annotation - bad', async t =
t.snapshot(expectation.join('\n'));
});

// TODO test that --contract does not have build info dir name
test('validate - contract must not have build info dir name', async t => {
const temp = await getTempDir(t);
const referenceDir = path.join(temp, 'build-info-v1');
await fs.mkdir(referenceDir);

const referenceBuildInfo = await artifacts.getBuildInfo(`contracts/test/cli/ValidateBuildInfoV1.sol:MyContract`);
await fs.writeFile(path.join(referenceDir, 'validate.json'), JSON.stringify(referenceBuildInfo));

const updatedDir = path.join(temp, 'build-info');
await fs.mkdir(updatedDir);

const updatedBuildInfo = await artifacts.getBuildInfo(`contracts/test/cli/ValidateBuildInfoV2_Ok.sol:MyContract`);
await fs.writeFile(path.join(updatedDir, 'validate.json'), JSON.stringify(updatedBuildInfo));

const error = await t.throwsAsync(
execAsync(
`${CLI} validate ${updatedDir} --referenceBuildInfoDirs ${referenceDir} --contract build-info:MyContract --reference build-info-v1:MyContract`,
),
);
const expectation: string[] = [`Stdout: ${(error as any).stdout}`, `Stderr: ${(error as any).stderr}`];
t.snapshot(expectation.join('\n'));
});
23 changes: 23 additions & 0 deletions packages/core/src/cli/cli.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,26 @@ Generated by [AVA](https://avajs.dev).
FAILED␊
Stderr: `

## validate - contract must not have build info dir name

> Snapshot 1
`Stdout: ␊
Stderr: /Users/eric/git/openzeppelin-upgrades/packages/core/dist/cli/validate/find-contract.js:39␊
throw new ReferenceContractNotFound(contractName, origin?.fullyQualifiedName, Object.keys(buildInfoDictionary));␊
^␊
ReferenceContractNotFound [Error]: Could not find contract build-info:MyContract.␊
at findContract (/Users/eric/git/openzeppelin-upgrades/packages/core/dist/cli/validate/find-contract.js:39:15)␊
at findSpecifiedContracts (/Users/eric/git/openzeppelin-upgrades/packages/core/dist/cli/validate/validate-upgrade-safety.js:53:56)␊
at validateUpgradeSafety (/Users/eric/git/openzeppelin-upgrades/packages/core/dist/cli/validate/validate-upgrade-safety.js:46:32)␊
at async main (/Users/eric/git/openzeppelin-upgrades/packages/core/dist/cli/validate.js:34:24)␊
at async run (/Users/eric/git/openzeppelin-upgrades/packages/core/dist/cli/cli.js:6:5) {␊
reference: 'build-info:MyContract',␊
origin: undefined,␊
buildInfoDirs: [ '', 'build-info', 'build-info-v1' ]␊
}␊
Node.js v18.18.2␊
`
Binary file modified packages/core/src/cli/cli.test.ts.snap
Binary file not shown.
23 changes: 14 additions & 9 deletions packages/core/src/cli/validate/find-contract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from '../../utils/assert';
import { ValidateCommandError } from './error';
import { BuildInfoDictionary } from './validate-upgrade-safety';
import { SourceContract } from './validations';
Expand All @@ -14,30 +15,31 @@ export class ReferenceContractNotFound extends Error {
readonly origin?: string;

/**
* Reference build info directories that were also searched, if any.
* Build info directories that were also searched.
*/
readonly referenceBuildInfoDirs?: string[];
readonly buildInfoDirs?: string[];

constructor(reference: string, origin?: string, referenceBuildInfoDirs?: string[]) {
constructor(reference: string, origin?: string, buildInfoDirs?: string[]) {
const msg =
origin !== undefined
? `Could not find contract ${reference} referenced in ${origin}.`
: `Could not find contract ${reference}.`;
super(msg);
this.reference = reference;
this.origin = origin;
this.referenceBuildInfoDirs = referenceBuildInfoDirs;
this.buildInfoDirs = buildInfoDirs;
}
}

export function findContract(
contractName: string,
origin: SourceContract | undefined,
buildInfoDictionary: BuildInfoDictionary,
searchMainBuildInfoDirOnly = false,
onlyMainBuildInfoDir = false,
) {
const foundContracts: SourceContract[] = [];
if (searchMainBuildInfoDirOnly) {
if (onlyMainBuildInfoDir) {
// TODO give error if specified contract has a build info dir name
foundContracts.push(...buildInfoDictionary[''].filter(c => isMatchFound(contractName, c, '')));
} else {
for (const [dir, contracts] of Object.entries(buildInfoDictionary)) {
Expand All @@ -63,9 +65,12 @@ export function findContract(
}

function isMatchFound(contractName: string, foundContract: SourceContract, buildInfoDirShortName: string): boolean {
const searchPrefix = buildInfoDirShortName === '' ? '' : `${buildInfoDirShortName}:`;
let prefix = '';
if (buildInfoDirShortName.length > 0) {
assert(foundContract.buildInfoDirShortName === buildInfoDirShortName);
prefix = `${buildInfoDirShortName}:`;
}
return (
`${searchPrefix}${foundContract.fullyQualifiedName}` === contractName ||
`${searchPrefix}${foundContract.name}` === contractName
`${prefix}${foundContract.fullyQualifiedName}` === contractName || `${prefix}${foundContract.name}` === contractName
);
}

0 comments on commit 0bad622

Please sign in to comment.