1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 1 1 1 1 27 27 2 2 2 2 25 25 27 27 1 4 4 4 5 5 1 4 4 4 4 4 8 8 8 4 4 4 4 1 10 10 10 12 12 12 12 12 1 4 4 4 5 5 5 5 5 1 1 34 17 1 11 138 11 139 11 | 'use strict'; var fs = require('fs'); var _ = require('lodash'); // Extend the native assert module var assert = module.exports = require('assert'); /** * Assert that a file exists * @param {String} file - path to a file * @example * assert.file('templates/user.hbs'); * * @also * * Assert that each of an array of files exists * @param {Array} pairs - an array of paths to files * @example * assert.file(['templates/user.hbs', 'templates/user/edit.hbs']); * * @also * * Assert that a file's content matches a regex * @deprecated * @param {String} file - path to a file * @param {Regex} reg - regex that will be used to search the file * @example * assert.file('models/user.js', /App\.User = DS\.Model\.extend/); */ assert.file = function () { var args = _.toArray(arguments); if (_.last(args) instanceof RegExp) { // DEPRECATED CASE var depMsg = 'assert.file(String, RegExp) DEPRECATED; use '; depMsg += 'assert.fileContent(String, RegExp) instead.'; console.log(depMsg); assert.fileContent(args[0], args[1]); } else { args = _.isString(args[0]) ? args : args[0]; args.forEach(function (file) { var here = fs.existsSync(file); assert.ok(here, file + ', no such file or directory'); }); } }; /** * Assert that a file doesn't exist * @param {String} file - path to a file * @example * assert.noFile('templates/user.hbs'); * * @also * * Assert that each of an array of files doesn't exist * @param {Array} pairs - an array of paths to files * @example * assert.noFile(['templates/user.hbs', 'templates/user/edit.hbs']); */ assert.noFile = function () { var args = _.toArray(arguments); args = _.isString(args[0]) ? args : args[0]; args.forEach(function (file) { var here = fs.existsSync(file); assert.ok(!here, file + ' exists'); }); }; /** * Assert that each of an array of files exists. If an item is an array with * the first element a filepath and the second element a regex, check to see * that the file content matches the regex * @deprecated * @param {Array} pairs - an array of paths to files or file/regex subarrays * @example * file(['templates/user.hbs', 'templates/user/edit.hbs']); * @example * files(['foo.js', 'bar.js', ['baz.js', /function baz/]]); */ assert.files = function (files) { var depMsg = 'assert.files deprecated. Use '; depMsg += 'assert.file([String, String, ...]) or '; depMsg += 'assert.file([[String, RegExp], [String, RegExp]...]) instead.'; console.log(depMsg); files.forEach(function (item) { var file = item; var rx; if (item instanceof Array) { file = item[0]; rx = item[1]; assert.fileContent(file, rx); } else { assert.file(file); } }); }; /** * Assert that a file's content matches a regex * @param {String} file - path to a file * @param {Regex} reg - regex that will be used to search the file * @example * assert.fileContent('models/user.js', /App\.User = DS\.Model\.extend/); * * @also * * Assert that each file in an array of file-regex pairs matches its corresponding regex * @param {Array} pairs - an array of arrays, where each subarray is a [String, RegExp] pair * @example * var arg = [ * [ 'models/user.js', /App\.User \ DS\.Model\.extend/ ], * [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ] * ] * assert.fileContent(arg); */ assert.fileContent = function () { var args = _.toArray(arguments); var pairs = _.isString(args[0]) ? [args] : args[0]; pairs.forEach(function (pair) { var file = pair[0]; var regex = pair[1]; assert.file(file); var body = fs.readFileSync(file, 'utf8'); assert.ok(regex.test(body), file + ' did not match \'' + regex + '\'.'); }); }; /** * Assert that a file's content does not match a regex * @param {String} file - path to a file * @param {Regex} reg - regex that will be used to search the file * @example * assert.noFileContent('models/user.js', /App\.User = DS\.Model\.extend/); * * @also * * Assert that each file in an array of file-regex pairs does not match its corresponding regex * @param {Array} pairs - an array of arrays, where each subarray is a [String, RegExp] pair * var arg = [ * [ 'models/user.js', /App\.User \ DS\.Model\.extend/ ], * [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ] * ] * assert.noFileContent(arg); */ assert.noFileContent = function (file, reg) { var args = _.toArray(arguments); var pairs = _.isString(args[0]) ? [args] : args[0]; pairs.forEach(function (pair) { var file = pair[0]; var regex = pair[1]; assert.file(file); var body = fs.readFileSync(file, 'utf8'); assert.ok(!regex.test(body), file + ' did not match \'' + regex + '\'.'); }); }; /** * Assert that two strings are equal after standardization of newlines * @param {String} value - a string * @param {String} expected - the expected value of the string * @example * assert.textEqual('I have a yellow cat', 'I have a yellow cat'); */ assert.textEqual = function (value, expected) { function eol(str) { return str.replace(/\r\n/g, '\n'); } assert.equal(eol(value), eol(expected)); }; /** * Assert an Object implements an interface * @param {Object} subject - subject implementing the façade * @param {Object|Array} methods - a façace, hash or array of keys to be implemented */ assert.implement = function (subject, methods) { methods = _.isArray(methods) ? methods : Object.keys(methods).filter(function (method) { return _.isFunction(methods[method]); }); var pass = methods.filter(function (method) { return !_.isFunction(subject[method]); }); assert.ok(pass.length === 0, 'expected object to implement the complete interface'); }; |