|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var ng = require('../helpers/ng'); |
| 4 | +var expect = require('chai').expect; |
| 5 | +var walkSync = require('walk-sync'); |
| 6 | +var glob = require('glob'); |
| 7 | +var Blueprint = require('ember-cli/lib/models/blueprint'); |
| 8 | +var path = require('path'); |
| 9 | +var tmp = require('ember-cli/tests/helpers/tmp'); |
| 10 | +var root = process.cwd(); |
| 11 | +var util = require('util'); |
| 12 | +var conf = require('ember-cli/tests/helpers/conf'); |
| 13 | +var minimatch = require('minimatch'); |
| 14 | +var intersect = require('lodash/array/intersection'); |
| 15 | +var remove = require('lodash/array/remove'); |
| 16 | +var forEach = require('lodash/collection/forEach'); |
| 17 | +var any = require('lodash/collection/some'); |
| 18 | +var EOL = require('os').EOL; |
| 19 | + |
| 20 | +var defaultIgnoredFiles = Blueprint.ignoredFiles; |
| 21 | + |
| 22 | +describe('Acceptance: ng init', function() { |
| 23 | + this.timeout(20000); |
| 24 | + |
| 25 | + before(function() { |
| 26 | + conf.setup(); |
| 27 | + }); |
| 28 | + |
| 29 | + after(function() { |
| 30 | + conf.restore(); |
| 31 | + }); |
| 32 | + |
| 33 | + beforeEach(function() { |
| 34 | + Blueprint.ignoredFiles = defaultIgnoredFiles; |
| 35 | + |
| 36 | + return tmp.setup('./tmp') |
| 37 | + .then(function() { |
| 38 | + process.chdir('./tmp'); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(function() { |
| 43 | + return tmp.teardown('./tmp'); |
| 44 | + }); |
| 45 | + |
| 46 | + function confirmBlueprinted() { |
| 47 | + var blueprintPath = path.join(root, 'addon', 'ng2', 'blueprints', 'ng2', 'files'); |
| 48 | + var expected = walkSync(blueprintPath).sort(); |
| 49 | + var actual = walkSync('.').sort(); |
| 50 | + |
| 51 | + forEach(Blueprint.renamedFiles, function(destFile, srcFile) { |
| 52 | + expected[expected.indexOf(srcFile)] = destFile; |
| 53 | + }); |
| 54 | + |
| 55 | + expected.forEach(function (file, index) { |
| 56 | + expected[index] = file.replace(/__name__/g, 'angular-cli'); |
| 57 | + }); |
| 58 | + |
| 59 | + removeIgnored(expected); |
| 60 | + removeIgnored(actual); |
| 61 | + |
| 62 | + expected.sort(); |
| 63 | + |
| 64 | + expect(expected).to.deep.equal(actual, EOL + ' expected: ' + util.inspect(expected) + |
| 65 | + EOL + ' but got: ' + util.inspect(actual)); |
| 66 | + } |
| 67 | + |
| 68 | + function confirmGlobBlueprinted(pattern) { |
| 69 | + var blueprintPath = path.join(root, 'addon', 'ng2', 'blueprints', 'ng2', 'files'); |
| 70 | + var actual = pickSync('.', pattern); |
| 71 | + var expected = intersect(pickSync(blueprintPath, pattern), actual); |
| 72 | + |
| 73 | + removeIgnored(expected); |
| 74 | + removeIgnored(actual); |
| 75 | + |
| 76 | + expected.sort(); |
| 77 | + |
| 78 | + expect(expected).to.deep.equal(actual, EOL + ' expected: ' + util.inspect(expected) + |
| 79 | + EOL + ' but got: ' + util.inspect(actual)); |
| 80 | + } |
| 81 | + |
| 82 | + function pickSync(filePath, pattern) { |
| 83 | + return glob.sync(path.join('**', pattern), { |
| 84 | + cwd: filePath, |
| 85 | + dot: true, |
| 86 | + mark: true, |
| 87 | + strict: true |
| 88 | + }).sort(); |
| 89 | + } |
| 90 | + function removeIgnored(array) { |
| 91 | + remove(array, function(fn) { |
| 92 | + return any(Blueprint.ignoredFiles, function(ignoredFile) { |
| 93 | + return minimatch(fn, ignoredFile, { |
| 94 | + matchBase: true |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + it('ng init', function() { |
| 101 | + return ng([ |
| 102 | + 'init', |
| 103 | + '--skip-npm', |
| 104 | + '--skip-bower', |
| 105 | + ]).then(confirmBlueprinted); |
| 106 | + }); |
| 107 | + |
| 108 | + it('ng init can run in created folder', function() { |
| 109 | + return tmp.setup('./tmp/foo') |
| 110 | + .then(function() { |
| 111 | + process.chdir('./tmp/foo'); |
| 112 | + }) |
| 113 | + .then(function() { |
| 114 | + return ng([ |
| 115 | + 'init', |
| 116 | + '--skip-npm', |
| 117 | + '--skip-bower' |
| 118 | + ]); |
| 119 | + }) |
| 120 | + .then(confirmBlueprinted) |
| 121 | + .then(function() { |
| 122 | + return tmp.teardown('./tmp/foo'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('init an already init\'d folder', function() { |
| 127 | + return ng([ |
| 128 | + 'init', |
| 129 | + '--skip-npm', |
| 130 | + '--skip-bower' |
| 131 | + ]) |
| 132 | + .then(function() { |
| 133 | + return ng([ |
| 134 | + 'init', |
| 135 | + '--skip-npm', |
| 136 | + '--skip-bower' |
| 137 | + ]); |
| 138 | + }) |
| 139 | + .then(confirmBlueprinted); |
| 140 | + }); |
| 141 | + |
| 142 | + it('init a single file', function() { |
| 143 | + return ng([ |
| 144 | + 'init', |
| 145 | + 'package.json', |
| 146 | + '--skip-npm', |
| 147 | + '--skip-bower' |
| 148 | + ]) |
| 149 | + .then(function() { return 'package.json'; }) |
| 150 | + .then(confirmGlobBlueprinted); |
| 151 | + }); |
| 152 | + |
| 153 | + it('init a single file on already init\'d folder', function() { |
| 154 | + return ng([ |
| 155 | + 'init', |
| 156 | + '--skip-npm', |
| 157 | + '--skip-bower' |
| 158 | + ]) |
| 159 | + .then(function() { |
| 160 | + return ng([ |
| 161 | + 'init', |
| 162 | + 'package.json', |
| 163 | + '--skip-npm', |
| 164 | + '--skip-bower' |
| 165 | + ]); |
| 166 | + }) |
| 167 | + .then(confirmBlueprinted); |
| 168 | + }); |
| 169 | + |
| 170 | + it('init multiple files by glob pattern', function() { |
| 171 | + return ng([ |
| 172 | + 'init', |
| 173 | + 'src/**', |
| 174 | + '--skip-npm', |
| 175 | + '--skip-bower' |
| 176 | + ]) |
| 177 | + .then(function() { return 'src/**'; }) |
| 178 | + .then(confirmGlobBlueprinted); |
| 179 | + }); |
| 180 | + |
| 181 | + it('init multiple files by glob pattern on already init\'d folder', function() { |
| 182 | + return ng([ |
| 183 | + 'init', |
| 184 | + '--skip-npm', |
| 185 | + '--skip-bower' |
| 186 | + ]) |
| 187 | + .then(function() { |
| 188 | + return ng([ |
| 189 | + 'init', |
| 190 | + 'src/**', |
| 191 | + '--skip-npm', |
| 192 | + '--skip-bower' |
| 193 | + ]); |
| 194 | + }) |
| 195 | + .then(confirmBlueprinted); |
| 196 | + }); |
| 197 | + |
| 198 | + it('init multiple files by glob patterns', function() { |
| 199 | + return ng([ |
| 200 | + 'init', |
| 201 | + 'src/**', |
| 202 | + 'package.json', |
| 203 | + '--skip-npm', |
| 204 | + '--skip-bower' |
| 205 | + ]) |
| 206 | + .then(function() { return '{src/**,package.json}'; }) |
| 207 | + .then(confirmGlobBlueprinted); |
| 208 | + }); |
| 209 | + |
| 210 | + it('init multiple files by glob patterns on already init\'d folder', function() { |
| 211 | + return ng([ |
| 212 | + 'init', |
| 213 | + '--skip-npm', |
| 214 | + '--skip-bower' |
| 215 | + ]) |
| 216 | + .then(function() { |
| 217 | + return ng([ |
| 218 | + 'init', |
| 219 | + 'src/**', |
| 220 | + 'package.json', |
| 221 | + '--skip-npm', |
| 222 | + '--skip-bower' |
| 223 | + ]); |
| 224 | + }) |
| 225 | + .then(confirmBlueprinted); |
| 226 | + }); |
| 227 | + |
| 228 | +}); |
0 commit comments