Skip to content

Commit

Permalink
Add test task for ligo plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy Chen Chen committed Sep 19, 2022
1 parent 8ee18c8 commit 6fbd701
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
3 changes: 1 addition & 2 deletions taqueria-plugin-ligo/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const compileExprs = (parsedArgs: Opts, sourceFile: string, exprKind: ExprKind):
})
.then(mergeArtifactsOutput(sourceFile));

const compileContractWithStorageAndParameter = async (parsedArgs: Opts, sourceFile: string) => {
const compileContractWithStorageAndParameter = async (parsedArgs: Opts, sourceFile: string): Promise<TableRow[]> => {
const contractCompileResult = await compileContract(parsedArgs, sourceFile);
if (contractCompileResult.artifact === COMPILE_ERR_MSG) return [contractCompileResult];

Expand Down Expand Up @@ -226,7 +226,6 @@ const mergeArtifactsOutput = (sourceFile: string) =>

export const compile = (parsedArgs: Opts): Promise<void> => {
const sourceFile = parsedArgs.sourceFile;
if (!sourceFile) return sendAsyncErr('No source file specified.');
let p: Promise<TableRow[]>;
if (isStoragesFile(sourceFile)) p = compileExprs(parsedArgs, sourceFile, 'storage');
else if (isParametersFile(sourceFile)) p = compileExprs(parsedArgs, sourceFile, 'parameter');
Expand Down
7 changes: 7 additions & 0 deletions taqueria-plugin-ligo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ Plugin.create(i18n => ({
handler: 'proxy',
encoding: 'json',
}),
Task.create({
task: 'test',
command: 'test <sourceFile>',
description: 'Test a smart contract written in LIGO',
handler: 'proxy',
encoding: 'json',
}),
],
templates: [
Template.create({
Expand Down
5 changes: 3 additions & 2 deletions taqueria-plugin-ligo/ligo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sendAsyncErr } from '@taqueria/node-sdk';
import { RequestArgs } from '@taqueria/node-sdk/types';
import compile from './compile';
import { test } from './test';

interface Opts extends RequestArgs.ProxyRequestArgs {
sourceFile: string;
Expand All @@ -10,8 +11,8 @@ export const ligo = (parsedArgs: Opts): Promise<void> => {
switch (parsedArgs.task) {
case 'compile':
return compile(parsedArgs);
// case 'test':
// return test(parsedArgs); // TODO: to be implemented in the future
case 'test':
return test(parsedArgs);
default:
return sendAsyncErr(`${parsedArgs.task} is not an understood task by the LIGO plugin`);
}
Expand Down
53 changes: 53 additions & 0 deletions taqueria-plugin-ligo/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { execCmd, getArch, sendAsyncErr, sendErr, sendJsonRes, sendWarn } from '@taqueria/node-sdk';
import { RequestArgs } from '@taqueria/node-sdk/types';
import { access, readFile, writeFile } from 'fs/promises';
import { basename, extname, join } from 'path';

interface Opts extends RequestArgs.t {
sourceFile: string;
}

type TableRow = { contract: string; testResults: string };

const getInputFilename = (parsedArgs: Opts, sourceFile: string): string =>
join(parsedArgs.config.contractsDir, sourceFile);

const getTestContractCmd = (parsedArgs: Opts, sourceFile: string): string => {
const projectDir = process.env.PROJECT_DIR ?? parsedArgs.projectDir;
if (!projectDir) throw `No project directory provided`;
const baseCmd =
`DOCKER_DEFAULT_PLATFORM=linux/amd64 docker run --rm -v \"${projectDir}\":/project -w /project -u $(id -u):$(id -g) ligolang/ligo:next run test`;
const inputFile = getInputFilename(parsedArgs, sourceFile);
const cmd = `${baseCmd} ${inputFile}`;
return cmd;
};

const testContract = (parsedArgs: Opts, sourceFile: string): Promise<TableRow> =>
getArch()
.then(() => getTestContractCmd(parsedArgs, sourceFile))
.then(execCmd)
.then(({ stdout, stderr }) => {
if (stderr.length > 0) sendWarn(stderr);
const result = '🎉 All tests passed 🎉';
return {
contract: sourceFile,
testResults: stdout.length > 0 ? `${stdout}\n${result}` : result,
};
})
.catch(err => {
sendErr(`\n=== For ${sourceFile} ===`);
sendErr(err.message.replace(/Command failed.+?\n/, ''));
return {
contract: sourceFile,
testResults: 'Some tests failed :(',
};
});

export const test = (parsedArgs: Opts): Promise<void> => {
const sourceFile = parsedArgs.sourceFile;
return testContract(parsedArgs, sourceFile).then(results => [results]).then(sendJsonRes).catch(err =>
sendAsyncErr(err, false)
);
};

export default test;

0 comments on commit 6fbd701

Please sign in to comment.