Skip to content

Commit

Permalink
Fix ZkProgram path to use basename if located outside the source dire…
Browse files Browse the repository at this point in the history
…ctory
  • Loading branch information
Shigoto-dev19 committed Aug 20, 2024
1 parent 7596e94 commit 6080182
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,13 +792,18 @@ async function findZkProgramFile(buildPath, zkProgramNameArg) {
// eslint-disable-next-line no-unused-vars
const [_, zkProgramVarName, nameArg] = match;

const buildSrcPath = buildPath.replace('**/*.js', 'src');
const relativePath = path.relative(buildSrcPath, file);

const isNested =
!relativePath.startsWith('..') && !path.isAbsolute(relativePath);

const zkProgramFile = isNested ? relativePath : path.basename(file);

if (nameArg === zkProgramNameArg) {
return {
zkProgramVarName,
zkProgramFile: path.relative(
buildPath.replace('**/*.js', 'src'),
file
),
zkProgramFile,
};
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/lib/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ jest.unstable_mockModule('node:path', () => ({
resolve: jest.fn(),
dirname: jest.fn(),
sep: '/',
relative: jest.fn().mockImplementation((from, to) => {
const fromParts = from.split('/');
const toParts = to.split('/');

let commonLength = 0;
for (let i = 0; i < Math.min(fromParts.length, toParts.length); i++) {
if (fromParts[i] !== toParts[i]) break;
commonLength++;
}

const up = fromParts.slice(commonLength).map(() => '..');
const down = toParts.slice(commonLength);

return [...up, ...down].join('/') || '.';
}),
},
}));

Expand Down Expand Up @@ -1075,6 +1090,31 @@ describe('deploy.js', () => {
);
});

it('should return the ZkProgram when found in nested folders', async () => {
const projectRoot = '/some/path/';
const zkProgramNameArg = 'myZkProgram';
const zkProgramFile = 'proofs/file1.js';
const zkProgramMock = { name: 'myZkProgram' };
glob.mockResolvedValue(['/some/path/file1.js']);
fs.readFileSync.mockReturnValue(
`
const myVar = ZkProgram({
name: 'myZkProgram'
});
`
);
jest.spyOn(path, 'basename').mockReturnValue('proofs/file1.js');
dynamicImport.mockResolvedValue({ myVar: zkProgramMock });
const { getZkProgram } = await import('./deploy.js');

const result = await getZkProgram(projectRoot, zkProgramNameArg);

expect(result).toBe(zkProgramMock);
expect(dynamicImport).toHaveBeenCalledWith(
`${projectRoot}/build/src/${zkProgramFile}`
);
});

it('should handle Windows paths correctly', async () => {
const originalPlatform = process.platform;
try {
Expand Down

0 comments on commit 6080182

Please sign in to comment.