Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
Add slate test command (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Berthe authored May 10, 2017
1 parent 10682fe commit 4c4e8a4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"author": "Shopify Inc.",
"dependencies": {
"@shopify/theme-lint": "1.0.3",
"@shopify/themekit": "0.6.6",
"bluebird": "3.4.6",
"browser-sync": "2.18.2",
Expand Down
20 changes: 20 additions & 0 deletions src/commands/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import spawn from 'cross-spawn';
import debug from 'debug';
import config from '../config';

const logger = debug('slate-tools:test');

export default function(program) {
program
.command('test')
.description('Runs translation tests for a theme\'s locale files (<theme>/src/locales/).')
.action(() => {
logger(`--gulpfile ${config.gulpFile}`);
logger(`--cwd ${config.themeRoot}`);

spawn(config.gulp, ['test', '--gulpfile', config.gulpFile, '--cwd', config.themeRoot], {
detached: false,
stdio: 'inherit',
});
});
}
11 changes: 11 additions & 0 deletions src/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ gulp.task('build:zip', (done) => {
);
});

/**
* Runs translation tests on each file using @shopify/theme-lint
*
* @function test
* @memberof slate-cli.tasks
* @static
*/
gulp.task('test', (done) => {
runSequence('lint:locales', done);
});

/**
* Does a full clean/rebuild of your theme and creates a `.zip` compatible with
* shopify.
Expand Down
60 changes: 60 additions & 0 deletions src/tasks/includes/lint-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const gutil = require('gulp-util');
const _ = require('lodash');

/** Class representing a custom reporter for @shopify/theme-lint */
export default class Reporter {
constructor() {
this.successes = [];
this.failures = [];
}

/**
* Pushes a valid message onto successes.
*
* @param {String} message
* @param {String} file
*/
success(message, file = null, index = null) {
this.successes.push([message, file, index]);
}

/**
* Pushes an invalid message onto failures.
*
* @param {String} message
* @param {String} file
*/
failure(message, file = null, index = null) {
this.failures.push([message, file, index]);
}

/**
* Builds string output for translation tests
* depending on successes and failures.
*/
output() {
const testsRun = this.failures.length + this.successes.length;

if (this.failures.length === 0) {
gutil.log('Translation tests complete:',
gutil.colors.green(`Success (${testsRun} checks run)`),
);
} else {
gutil.log('Translation tests complete:',
gutil.colors.red(`Failed (${testsRun} checks run)`),
);

const failureGroups = _.groupBy(this.failures, (failure) => failure[1]);

_.forOwn(failureGroups, (failures, file) => {
gutil.log(gutil.colors.red(`${file}:`));

failures.map((failure) => {
return gutil.log(failure[0]);
});
});
}

this.successes = this.failures = [];
}
}
29 changes: 29 additions & 0 deletions src/tasks/lint-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const gulp = require('gulp');
const {runAll} = require('@shopify/theme-lint');

const config = require('./includes/config.js');
const Reporter = require('./includes/lint-reporter.js').default;

/**
* Runs all the translation tests and the reporter outputs
* the locale results once completed.
*
* @returns {String} Finalized linting output
* @private
*/
function lintLocales() {
return runAll(config.src.root, new Reporter()).then((reporter) => {
return reporter.output();
});
}

/**
* Runs translation tests using @shopify/theme-lint
*
* @function lint:locales
* @memberof slate-cli.tasks.lint
* @static
*/
gulp.task('lint:locales', () => {
return lintLocales();
});

0 comments on commit 4c4e8a4

Please sign in to comment.