From 7aa35bffafc1759781d12411a23ce9b5e4157f6d Mon Sep 17 00:00:00 2001 From: Henning Date: Thu, 27 Oct 2016 23:34:39 +0200 Subject: [PATCH 1/3] Fix to make it work in unit-test mode --- lib/mailer.js | 2 +- lib/utils.js | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/mailer.js b/lib/mailer.js index 07e3802..ec6d8ec 100644 --- a/lib/mailer.js +++ b/lib/mailer.js @@ -187,7 +187,7 @@ const factory = (options) => { // `preview` prop in the data context to apply to the layout. if (tmpl.__helpers.has('preview')) { preview = tmpl.__helpers.get('preview'); - } else if (data.preview) { + } else if (data && data.preview) { // data is optional preview = data.preview; } diff --git a/lib/utils.js b/lib/utils.js index 71fdd8f..86fb2b9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -154,7 +154,21 @@ Utils = { }, readFile(relativePathFromApp) { - const file = Path.join(ROOT, relativePathFromApp); + let file + if (Meteor.isAppTest + || Meteor.isTest) { + // note: + // * this DOES WORK with Meteor.isTest (=unit test mode) + // * Meteor.isAppTest is NOT TESTED yet (=in-app test mode) + // + // background-info: we had NO luck with "Assets.absoluteFilePath(relativePathFromApp)", + // so lets build the path ourselves. + // see discussion https://github.com/lookback/meteor-emails/issues/76 + file = Path.join(process.cwd(), 'assets', 'app', relativePathFromApp) + } else { + // = standard mode (development || production) + file = Path.join(ROOT, relativePathFromApp); + } try { return fs.readFileSync(file, { From be673deeb5fa7a9f9333eefa3acf5a50dd76b52f Mon Sep 17 00:00:00 2001 From: Henning Date: Fri, 28 Oct 2016 00:32:09 +0200 Subject: [PATCH 2/3] Added support for scss in unit-test mode --- lib/utils.js | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 86fb2b9..9463e5e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -22,6 +22,21 @@ const sass = (function() { return null; })(); +/** + * Check if file exists + * @param {String} filePath + * @return {Boolean} + */ +const fileExists = function (filePath) { + try { + return fs.statSync(filePath).isFile(); + } + catch (err) + { + return false; + } +} + const TAG = 'mailer-utils'; // This package assumes that assets (templates, SCSS, CSS ..) are @@ -37,7 +52,6 @@ const TAG = 'mailer-utils'; // /var/www/app/bundle // // For Modulus, you need to use the `APP_DIR` variable, which you do NOT need to set. - const developmentPrivateDir = () => { if (!isDevEnv) { return ''; @@ -164,7 +178,7 @@ Utils = { // background-info: we had NO luck with "Assets.absoluteFilePath(relativePathFromApp)", // so lets build the path ourselves. // see discussion https://github.com/lookback/meteor-emails/issues/76 - file = Path.join(process.cwd(), 'assets', 'app', relativePathFromApp) + file = Path.join(process.cwd(), 'assets', 'app', relativePathFromApp); } else { // = standard mode (development || production) file = Path.join(ROOT, relativePathFromApp); @@ -192,17 +206,30 @@ ${packageToRecommend}`, TAG); return Utils.readFile(scss); } - const file = Path.join(ROOT, scss); + let file + if (Meteor.isAppTest + || Meteor.isTest) { + // note: + // * this DOES WORK with Meteor.isTest (=unit test mode) + // * Meteor.isAppTest is NOT TESTED yet (=in-app test mode) + file = Path.join(process.cwd(), 'assets', 'app', scss); + } else { + // = standard mode (development || production) + file = Path.join(ROOT, scss); + } - try { - return sass.renderSync({ - file: file, - sourceMap: false - }).css.toString(); - } catch (ex) { - console.error(`Sass failed to compile: ${ex.message}`); - console.error(`In ${ex.file || scss} at line ${ex.line}, column ${ex.column}`); - return ''; + if (fileExists(file)) { + console.log('SCSS EXISTS') + try { + return sass.renderSync({ + file: file, + sourceMap: false + }).css.toString(); + } catch (ex) { + console.error(`Sass failed to compile: ${ex.message}`); + console.error(`In ${ex.file || scss} at line ${ex.line}, column ${ex.column}`); + } } + return ''; // fallback: on error, or if file does NOT exist } }; From 764dc2f344698729383f393905abd9f786587cb1 Mon Sep 17 00:00:00 2001 From: Henning Date: Fri, 28 Oct 2016 00:36:00 +0200 Subject: [PATCH 3/3] removed log statement --- lib/utils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 9463e5e..b0bf80d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -219,7 +219,6 @@ ${packageToRecommend}`, TAG); } if (fileExists(file)) { - console.log('SCSS EXISTS') try { return sass.renderSync({ file: file,