|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs-extra'); |
| 4 | +var ng = require('../helpers/ng'); |
| 5 | +var existsSync = require('exists-sync'); |
| 6 | +var expect = require('chai').expect; |
| 7 | +var path = require('path'); |
| 8 | +var tmp = require('../helpers/tmp'); |
| 9 | +var root = process.cwd(); |
| 10 | +var Promise = require('@angular/cli/ember-cli/lib/ext/promise'); |
| 11 | +var SilentError = require('silent-error'); |
| 12 | +const denodeify = require('denodeify'); |
| 13 | + |
| 14 | +const readFile = denodeify(fs.readFile); |
| 15 | + |
| 16 | +describe('Acceptance: ng generate guard', function () { |
| 17 | + beforeEach(function () { |
| 18 | + return tmp.setup('./tmp').then(function () { |
| 19 | + process.chdir('./tmp'); |
| 20 | + }).then(function () { |
| 21 | + return ng(['new', 'foo', '--skip-install']); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(function () { |
| 26 | + this.timeout(10000); |
| 27 | + |
| 28 | + return tmp.teardown('./tmp'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('ng generate guard my-guard', function () { |
| 32 | + const appRoot = path.join(root, 'tmp/foo'); |
| 33 | + const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts'); |
| 34 | + const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts'); |
| 35 | + const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); |
| 36 | + |
| 37 | + return ng(['generate', 'guard', 'my-guard']) |
| 38 | + .then(() => { |
| 39 | + expect(existsSync(testPath)).to.equal(true); |
| 40 | + expect(existsSync(testSpecPath)).to.equal(true); |
| 41 | + }) |
| 42 | + .then(() => readFile(appModulePath, 'utf-8')) |
| 43 | + .then(content => { |
| 44 | + expect(content).not.to.matches(/import.*\MyGuardGuard\b.*from '.\/my-guard.guard';/); |
| 45 | + expect(content).not.to.matches(/providers:\s*\[MyGuardGuard\]/m); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('ng generate guard my-guard --no-spec', function () { |
| 50 | + const appRoot = path.join(root, 'tmp/foo'); |
| 51 | + const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts'); |
| 52 | + const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts'); |
| 53 | + const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); |
| 54 | + |
| 55 | + return ng(['generate', 'guard', 'my-guard', '--no-spec']) |
| 56 | + .then(() => { |
| 57 | + expect(existsSync(testPath)).to.equal(true); |
| 58 | + expect(existsSync(testSpecPath)).to.equal(false); |
| 59 | + }) |
| 60 | + .then(() => readFile(appModulePath, 'utf-8')) |
| 61 | + .then(content => { |
| 62 | + expect(content).not.to.matches(/import.*\MyGuardGuard\b.*from '.\/my-guard.guard';/); |
| 63 | + expect(content).not.to.matches(/providers:\s*\[MyGuardGuard\]/m); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('ng generate guard my-guard --module=app.module.ts', function () { |
| 68 | + const appRoot = path.join(root, 'tmp/foo'); |
| 69 | + const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts'); |
| 70 | + const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts'); |
| 71 | + const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); |
| 72 | + |
| 73 | + return ng(['generate', 'guard', 'my-guard', '--module', 'app.module.ts']) |
| 74 | + .then(() => { |
| 75 | + expect(existsSync(testPath)).to.equal(true); |
| 76 | + expect(existsSync(testSpecPath)).to.equal(true); |
| 77 | + }) |
| 78 | + .then(() => readFile(appModulePath, 'utf-8')) |
| 79 | + .then(content => { |
| 80 | + expect(content).to.matches(/import.*\MyGuardGuard\b.*from '.\/my-guard.guard';/); |
| 81 | + expect(content).to.matches(/providers:\s*\[MyGuardGuard\]/m); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('ng generate guard test' + path.sep + 'my-guard', function () { |
| 86 | + fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', 'test')); |
| 87 | + return ng(['generate', 'guard', 'test' + path.sep + 'my-guard']).then(() => { |
| 88 | + var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'test', 'my-guard.guard.ts'); |
| 89 | + expect(existsSync(testPath)).to.equal(true); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('ng generate guard test' + path.sep + '..' + path.sep + 'my-guard', function () { |
| 94 | + return ng(['generate', 'guard', 'test' + path.sep + '..' + path.sep + 'my-guard']).then(() => { |
| 95 | + var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-guard.guard.ts'); |
| 96 | + expect(existsSync(testPath)).to.equal(true); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('ng generate guard my-guard from a child dir', () => { |
| 101 | + fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1')); |
| 102 | + return new Promise(function (resolve) { |
| 103 | + process.chdir('./src'); |
| 104 | + resolve(); |
| 105 | + }) |
| 106 | + .then(() => process.chdir('./app')) |
| 107 | + .then(() => process.chdir('./1')) |
| 108 | + .then(() => { |
| 109 | + process.env.CWD = process.cwd(); |
| 110 | + return ng(['generate', 'guard', 'my-guard']) |
| 111 | + }) |
| 112 | + .then(() => { |
| 113 | + var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-guard.guard.ts'); |
| 114 | + expect(existsSync(testPath)).to.equal(true); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('ng generate guard child-dir' + path.sep + 'my-guard from a child dir', () => { |
| 119 | + fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'child-dir')); |
| 120 | + return new Promise(function (resolve) { |
| 121 | + process.chdir('./src'); |
| 122 | + resolve(); |
| 123 | + }) |
| 124 | + .then(() => process.chdir('./app')) |
| 125 | + .then(() => process.chdir('./1')) |
| 126 | + .then(() => { |
| 127 | + process.env.CWD = process.cwd(); |
| 128 | + return ng(['generate', 'guard', 'child-dir' + path.sep + 'my-guard']) |
| 129 | + }) |
| 130 | + .then(() => { |
| 131 | + var testPath = path.join( |
| 132 | + root, 'tmp', 'foo', 'src', 'app', '1', 'child-dir', 'my-guard.guard.ts'); |
| 133 | + expect(existsSync(testPath)).to.equal(true); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('ng generate guard child-dir' + path.sep + '..' + path.sep + 'my-guard from a child dir', |
| 138 | + () => { |
| 139 | + fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1')); |
| 140 | + return new Promise(function (resolve) { |
| 141 | + process.chdir('./src'); |
| 142 | + resolve(); |
| 143 | + }) |
| 144 | + .then(() => process.chdir('./app')) |
| 145 | + .then(() => process.chdir('./1')) |
| 146 | + .then(() => { |
| 147 | + process.env.CWD = process.cwd(); |
| 148 | + return ng( |
| 149 | + ['generate', 'guard', 'child-dir' + path.sep + '..' + path.sep + 'my-guard']) |
| 150 | + }) |
| 151 | + .then(() => { |
| 152 | + var testPath = |
| 153 | + path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-guard.guard.ts'); |
| 154 | + expect(existsSync(testPath)).to.equal(true); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('ng generate guard ' + path.sep + 'my-guard from a child dir, gens under ' + |
| 159 | + path.join('src', 'app'), |
| 160 | + () => { |
| 161 | + fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1')); |
| 162 | + return new Promise(function (resolve) { |
| 163 | + process.chdir('./src'); |
| 164 | + resolve(); |
| 165 | + }) |
| 166 | + .then(() => process.chdir('./app')) |
| 167 | + .then(() => process.chdir('./1')) |
| 168 | + .then(() => { |
| 169 | + process.env.CWD = process.cwd(); |
| 170 | + return ng(['generate', 'guard', path.sep + 'my-guard']) |
| 171 | + }) |
| 172 | + .then(() => { |
| 173 | + var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-guard.guard.ts'); |
| 174 | + expect(existsSync(testPath)).to.equal(true); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('ng generate guard ..' + path.sep + 'my-guard from root dir will fail', () => { |
| 179 | + return ng(['generate', 'guard', '..' + path.sep + 'my-guard']).then(() => { |
| 180 | + throw new SilentError(`ng generate guard ..${path.sep}my-guard from root dir should fail.`); |
| 181 | + }, (err) => { |
| 182 | + expect(err).to.equal(`Invalid path: "..${path.sep}my-guard" cannot be above the "src${path.sep}app" directory`); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments