Skip to content

Commit

Permalink
fix e2e tests by adding --force option into commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomontero committed Dec 21, 2023
1 parent 6e06ca5 commit d96e200
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 59 deletions.
3 changes: 3 additions & 0 deletions src/cli/logic-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module.exports = ({ commandProcessor, root }) => {
'name': {
description: 'Name of the logic function'
},
'description': {
description: 'Description of the logic function'
},
'force': {
boolean: true,
default: true,
Expand Down
112 changes: 64 additions & 48 deletions src/cmd/logic-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
`Refer to ${this.ui.chalk.yellow('particle logic-function execute')} and ${this.ui.chalk.yellow('particle logic-function deploy')} for more information.${os.EOL}`);
}

async create({ org, name, description, params : { filepath } } = { params: { } }) {
async create({ org, name, description, force, params : { filepath } } = { params: { } }) {
this._setOrg(org);
const {
name: logicFunctionName,
description: logicFunctionDescription
} = await this._promptLogicFunctionInput({ _name: name, _description: description });
} = await this._promptLogicFunctionInput({ _name: name, _description: description, force });
this.ui.stdout.write(`${os.EOL}`);
this.ui.stdout.write(`Creating Logic Function ${this.ui.chalk.cyan(logicFunctionName)} for ${getOrgName(this.org)}...${os.EOL}`);

Expand All @@ -107,6 +107,7 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {

await this._confirmOverwriteIfNeeded({
filePaths: [logicFunction.configurationPath, logicFunction.sourcePath],
force
});

await logicFunction.saveToDisk();
Expand All @@ -120,8 +121,14 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
this._printCreateHelperOutput();
}

async _promptLogicFunctionInput({ _name, _description }) {
async _promptLogicFunctionInput({ _name, _description, force }) {
let name = _name, description = _description;
if (force) {
return {
name: name? name.trim() : '',
description: description ? description.trim() : ''
};
}
if (!_name) {
const result = await this._prompt({
type: 'input',
Expand Down Expand Up @@ -150,17 +157,17 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
}

_printCreateOutput({ logicFunctionName, basePath, jsonPath, jsPath }) {
this.ui.stdout.write(`Successfully created ${this.ui.chalk.cyan(logicFunctionName)} locally in ${this.ui.chalk.bold(basePath)}${os.EOL}`);
this.ui.stdout.write(`Successfully created ${this.ui.chalk.cyan(logicFunctionName)} locally in ${this.ui.chalk.bold(basePath)}`);
this.ui.stdout.write(`${os.EOL}`);
this.ui.stdout.write(`Files created:${os.EOL}`);
this.ui.stdout.write(` - ${path.basename(jsonPath)}${os.EOL}`);
this.ui.stdout.write(` - ${path.basename(jsPath)}${os.EOL}`);
this.ui.stdout.write(`- ${path.basename(jsPath)}${os.EOL}`);
this.ui.stdout.write(`- ${path.basename(jsonPath)}${os.EOL}`);
this.ui.stdout.write(`${os.EOL}`);
}

_printCreateHelperOutput() {
this.ui.stdout.write(`${os.EOL}`);
this.ui.stdout.write(`Guidelines for creating your Logic Function can be found here https://docs.particle.io/getting-started/cloud/logic/ ${os.EOL}`);
this.ui.stdout.write(`Guidelines for creating your Logic Function can be found here https://docs.particle.io/getting-started/cloud/logic/${os.EOL}`);
this.ui.stdout.write(`Once you have written your Logic Function, run${os.EOL}`);
this.ui.stdout.write('- ' + this.ui.chalk.yellow('\'particle logic-function execute\'') + ` to run your Function${os.EOL}`);
this.ui.stdout.write('- ' + this.ui.chalk.yellow('\'particle logic-function deploy\'') + ` to deploy your new changes${os.EOL}`);
Expand Down Expand Up @@ -197,7 +204,10 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {

// Prompts the user to overwrite if any files exist
// If user says no, we exit the process
async _confirmOverwriteIfNeeded({ filePaths, _exit = () => process.exit(0) }) {
async _confirmOverwriteIfNeeded({ force, filePaths, _exit = () => process.exit(0) }) {
if (force) {
return;
}
let exists = false;
const pathsToCheck = filePaths;
for (const p of pathsToCheck) {
Expand Down Expand Up @@ -382,23 +392,26 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
return { fileName, content: fileBuffer.toString() };
}

async deploy({ org, data, dataPath, params: { filepath } }) {
async deploy({ org, data, force, dataPath, params: { filepath } }) {
this._setOrg(org);

await this._getLogicFunctionList();

const confirm = await this._prompt({
type: 'confirm',
name: 'proceed',
message: `Deploying to ${getOrgName(this.org)}. Proceed?`,
choices: Boolean
});
if (!force) {
const confirm = await this._prompt({
type: 'confirm',
name: 'proceed',
message: `Deploying to ${getOrgName(this.org)}. Proceed?`,
choices: Boolean
});

if (!confirm.proceed) {
this.ui.stdout.write(`Aborted.${os.EOL}`);
return;
if (!confirm.proceed) {
this.ui.stdout.write(`Aborted.${os.EOL}`);
return;
}
}


const { logicConfigContent, logicCodeContent } = await this.execute({ org, data, dataPath, params: { filepath } });
const name = logicConfigContent.logic_function.name;
logicConfigContent.logic_function.enabled = true;
Expand All @@ -407,18 +420,19 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
const logicFuncNameDeployed = await this._validateLFName({ name });
if (logicFuncNameDeployed) {
try {
const confirm = await this._prompt({
type: 'confirm',
name: 'proceed',
message: `A Logic Function with name ${name} is already available in the cloud ${getOrgName(this.org)}.${os.EOL}Proceed and overwrite with the new content?`,
choices: Boolean
});

if (!confirm.proceed) {
this.ui.stdout.write(`Aborted.${os.EOL}`);
return;
if (!force) {
const confirm = await this._prompt({
type: 'confirm',
name: 'proceed',
message: `A Logic Function with name ${name} is already available in the cloud ${getOrgName(this.org)}.${os.EOL}Proceed and overwrite with the new content?`,
choices: Boolean
});

if (!confirm.proceed) {
this.ui.stdout.write(`Aborted.${os.EOL}`);
return;
}
}

const { id } = await this._getLogicFunctionIdAndName(name);
await this.api.updateLogicFunction({ org, id, logicFunctionData: logicConfigContent.logic_function });
this._printDeployOutput(name, id);
Expand Down Expand Up @@ -481,7 +495,8 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
async _overwriteIfLFExistsLocally(name, id) {
const { jsonPath, jsPath } = this._getLocalLFPathNames(name);

const exist = await this._confirmOverwriteIfNeeded({ filePaths: { jsonPath, jsPath } });
const exist = await this._confirmOverwriteIfNeeded(
{ filePaths: [jsonPath, jsPath] });

if (!exist) {
return;
Expand All @@ -497,11 +512,11 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
}

_printDisableOutput(name, id) {
this.ui.stdout.write(`Logic Function ${name}(${id}) is now disabled.${os.EOL}`);
this.ui.stdout.write(`Logic Function ${name} (${id}) is now disabled.${os.EOL}`);
}

_printEnableOutput(name, id) {
this.ui.stdout.write(`Logic Function ${name}(${id}) is now enabled.${os.EOL}`);
this.ui.stdout.write(`Logic Function ${name} (${id}) is now enabled.${os.EOL}`);
}

_printDisableNewFilesOutput({ jsonPath, jsPath }) {
Expand All @@ -512,29 +527,30 @@ module.exports = class LogicFunctionsCommand extends CLICommandBase {
this.ui.stdout.write(`${os.EOL}`);
}

async delete({ org, name, id }) {
async delete({ org, name, id, force }) {
this._setOrg(org);

await this._getLogicFunctionList();

({ name, id } = await this._getLogicFunctionIdAndName(name, id));

const confirm = await this._prompt({
type: 'confirm',
name: 'delete',
message: `Are you sure you want to delete Logic Function ${name}? This action cannot be undone.`,
choices: Boolean
});

if (confirm.delete) {
try {
await this.api.deleteLogicFunction({ org: this.org, id });
this.ui.stdout.write(`Logic Function ${name}(${id}) has been successfully deleted.${os.EOL}`);
} catch (err) {
throw new Error(`Error deleting Logic Function ${name}: ${err.message}`);
if (!force) {
const confirm = await this._prompt({
type: 'confirm',
name: 'delete',
message: `Are you sure you want to delete Logic Function ${name}? This action cannot be undone.`,
choices: Boolean
});
if (!confirm.delete) {
this.ui.stdout.write(`Aborted.${os.EOL}`);
return;
}
} else {
this.ui.stdout.write(`Aborted.${os.EOL}`);
}
try {
await this.api.deleteLogicFunction({ org: this.org, id });
this.ui.stdout.write(`Logic Function ${name}(${id}) has been successfully deleted.${os.EOL}`);
} catch (err) {
throw new Error(`Error deleting Logic Function ${name}: ${err.message}`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/logic-function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ describe('LogicFunctionCommands', () => {
expect(initFromTemplateStub.calledOnce).to.be.true;
expect(saveToDiskStub.calledOnce).to.be.true;
expect(logicFunctionCommands.ui.stdout.write.secondCall.args[0]).to.equal(`Creating Logic Function logic func 1 for your Sandbox...${os.EOL}`);
expect(logicFunctionCommands.ui.stdout.write.thirdCall.args[0]).to.equal(`Successfully created logic func 1 locally in ${PATH_TMP_DIR}${os.EOL}`);
expect(logicFunctionCommands.ui.stdout.write.thirdCall.args[0]).to.equal(`Successfully created logic func 1 locally in ${PATH_TMP_DIR}`);
});

it('ask to overwrite if files already exist', async () => {
Expand All @@ -338,7 +338,7 @@ describe('LogicFunctionCommands', () => {
expect(saveToDiskStub.calledOnce).to.be.true;
expect(logicFunctionCommands.ui.prompt.callCount).to.equal(3);
expect(logicFunctionCommands.ui.stdout.write.secondCall.args[0]).to.equal(`Creating Logic Function logic func 1 for your Sandbox...${os.EOL}`);
expect(logicFunctionCommands.ui.stdout.write.thirdCall.args[0]).to.equal(`Successfully created logic func 1 locally in ${PATH_TMP_DIR}${os.EOL}`);
expect(logicFunctionCommands.ui.stdout.write.thirdCall.args[0]).to.equal(`Successfully created logic func 1 locally in ${PATH_TMP_DIR}`);
});

});
Expand Down
19 changes: 10 additions & 9 deletions test/e2e/logic-function.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ describe('Logic Function Commands', () => {
];

const createOutput = [
'',
'Creating Logic Function newLF for cyberdyne-systems...',
'',
'Successfully created newLF locally in .',
`Successfully created newLF locally in ${PATH_TMP_DIR}`,
'',
'Files created:',
'- newlf/@types/particle_core.d.ts',
'- newlf/@types/particle_encoding.d.ts',
'- newlf/newlf.js',
'- newlf/newlf.logic.json',
'- newlf.js',
'- newlf.logic.json',
'',
'Guidelines for creating your Logic Function can be found here <TBD>.',
'Guidelines for creating your Logic Function can be found here https://docs.particle.io/getting-started/cloud/logic/',
'Once you have written your Logic Function, run',
'- \'particle logic-function execute\' to run your Function',
'- \'particle logic-function deploy\' to deploy your new changes',
''
];

const executeOutput = [
Expand Down Expand Up @@ -124,7 +124,6 @@ describe('Logic Function Commands', () => {

it('Lists Logic Functions', async () => {
const { stdout, stderr, exitCode } = await cli.run(['lf', 'list', '--org', 'cyberdyne-systems']);

// FIXME: This would pass even if listOutput was empty
expect(stdout.split('\n')).to.include.members(listOutput);
expect(stderr).to.equal('');
Expand Down Expand Up @@ -171,6 +170,10 @@ describe('Logic Function Commands', () => {
});

it('Re-deploys a Logic Function', async () => {
if (!fs.existsSync(path.join(PATH_TMP_DIR, 'newlf', 'newlf.js'))) {
await fs.copy(path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf3_proj', 'config.json'), path.join(PATH_TMP_DIR, 'newlf', 'newlf.logic.json'));
await fs.copy(path.join(PATH_FIXTURES_LOGIC_FUNCTIONS, 'lf3_proj', 'code.js'), path.join(PATH_TMP_DIR, 'newlf', 'newlf.js'));
}
const { stdout, stderr, exitCode } = await cli.run(['lf', 'deploy', '--org', 'cyberdyne-systems', '--data', '1234', '--force'], { cwd: path.join(PATH_TMP_DIR, 'newlf') });

stdout.split('\n').forEach((line) => {
Expand All @@ -187,8 +190,6 @@ describe('Logic Function Commands', () => {

it('Disables a Logic Function', async () => {
const { stdout, stderr, exitCode } = await cli.run(['lf', 'disable', '--org', 'cyberdyne-systems', '--id', id]);


expect(stdout).to.equal(`Logic Function newlf (${id}) is now disabled.`);
expect(stderr).to.equal('');
expect(exitCode).to.equal(0);
Expand Down

0 comments on commit d96e200

Please sign in to comment.