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

Add test task for ligo plugin #1255

Merged
merged 6 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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(result => [result]).then(sendJsonRes).catch(err =>
sendAsyncErr(err, false)
);
};

export default test;
2 changes: 2 additions & 0 deletions tests/e2e/data/help-contents/help-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Commands:
taq list-contracts List registered contracts
taq compile Provided by more than one plugin. The option -
-plugin is required.
taq test <sourceFile> Test a smart contract written in LIGO
taq create <template> Create files from pre-existing templates

Options:
Expand Down Expand Up @@ -96,6 +97,7 @@ Commands:
taq list-contracts List registered contracts
taq compile Provided by more than one plugin. The option -
-plugin is required.
taq test <sourceFile> Test a smart contract written in LIGO
taq create <template> Create files from pre-existing templates

Options:
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/data/help-contents/ligo-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Commands:
tax to Michelson code, along with its associat
ed storages and parameters files if they are f
ound [aliases: c, compile-ligo]
taq test <sourceFile> Test a smart contract written in LIGO
taq create <template> Create files from pre-existing templates

Options:
Expand Down