diff --git a/src/apps.js b/src/apps.js index 80bdaf8c6..848a804f6 100644 --- a/src/apps.js +++ b/src/apps.js @@ -275,7 +275,7 @@ function parseCrontab(crontab) { for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); if (!line || line.startsWith('#')) continue; - const parts = /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/.exec(line); + const parts = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/.exec(line); if (!parts) throw new BoxError(BoxError.BAD_FIELD, `Invalid cron configuration at line ${i+1}`); const schedule = parts.slice(1, 6).join(' '); const command = parts[6]; @@ -286,7 +286,7 @@ function parseCrontab(crontab) { throw new BoxError(BoxError.BAD_FIELD, `Invalid cron pattern at line ${i+1}`); } - if (command.length === 0) throw new BoxError(BoxError.BAD_FIELD, `Invalid cron pattern. Command must not be empty at line ${i+1}`); + if (command.length === 0) throw new BoxError(BoxError.BAD_FIELD, `Invalid cron pattern. Command must not be empty at line ${i+1}`); // not possible with the regexp we have result.push({ schedule, command }); } diff --git a/src/test/apps-test.js b/src/test/apps-test.js index 47e6fc695..4d751bee7 100644 --- a/src/test/apps-test.js +++ b/src/test/apps-test.js @@ -317,4 +317,40 @@ describe('Apps', function () { expect(result[2].installationState).to.be(apps.ISTATE_PENDING_INSTALL); }); }); + + describe('parseCrontab', function () { + it('succeeds for null crontob', function () { + expect(apps._parseCrontab(null)).to.eql([]); + }); + + it('succeeds for empty crontob', function () { + expect(apps._parseCrontab('')).to.eql([]); + }); + + it('throws for bad crontab', function () { + safe(() => apps._parseCrontab('# some comment\n*/1 * * * ')); // incomplete pattern + expect(safe.error.message).to.be('Invalid cron configuration at line 2'); + + safe(() => apps._parseCrontab('* * * * 13 command')); // bad pattern + expect(safe.error.message).to.be('Invalid cron pattern at line 1'); + + safe(() => apps._parseCrontab('*/1 * * *\t* ')); // no command + expect(safe.error.message).to.be('Invalid cron configuration at line 1'); + }); + + it('succeeds for crontab', function () { + const result = apps._parseCrontab( + '# this is a custom cron job\n' + + ' */1 * * *\t* echo "==> This is a custom cron task running every minute"\n\n' + + '00 */1 * * * echo "==> This is a custom cron task running every hour" '); // trailing spaces are trimmed + + expect(result.length).to.be(2); + expect(result[0].schedule).to.be('*/1 * * * *'); + expect(result[0].command).to.be('echo "==> This is a custom cron task running every minute"'); + + expect(result[1].schedule).to.be('00 */1 * * *'); + expect(result[1].command).to.be('echo "==> This is a custom cron task running every hour"'); + + }); + }); });