|
| 1 | +'use strict'; |
| 2 | +const os = require('os'); |
| 3 | +const path = require('path'); |
| 4 | +const sinon = require('sinon'); |
| 5 | +const Environment = require('yeoman-environment'); |
| 6 | +const assert = require('assert'); |
| 7 | +const helpers = require('yeoman-test'); |
| 8 | +const { TestAdapter } = require('yeoman-test/lib/adapter'); |
| 9 | + |
| 10 | +const Base = require('..'); |
| 11 | + |
| 12 | +const tmpdir = path.join(os.tmpdir(), 'yeoman-generator-environment'); |
| 13 | + |
| 14 | +describe('Generator with environment version', () => { |
| 15 | + before(helpers.setUpTestDirectory(tmpdir)); |
| 16 | + describe('mocked 3.0.0', () => { |
| 17 | + before(function() { |
| 18 | + this.timeout(100000); |
| 19 | + this.env = Environment.createEnv([], { 'skip-install': true }, new TestAdapter()); |
| 20 | + this.env.getVersion = this.env.getVersion || (() => {}); |
| 21 | + this.getVersionStub = sinon.stub(this.env, 'getVersion'); |
| 22 | + |
| 23 | + this.Dummy = class extends Base {}; |
| 24 | + this.dummy = new this.Dummy(['bar', 'baz', 'bom'], { |
| 25 | + foo: false, |
| 26 | + something: 'else', |
| 27 | + namespace: 'dummy', |
| 28 | + env: this.env, |
| 29 | + 'skip-install': true |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + after(function() { |
| 34 | + this.getVersionStub.restore(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('#checkEnvironmentVersion', () => { |
| 38 | + describe('without args', () => { |
| 39 | + it('returns true', function() { |
| 40 | + this.getVersionStub.returns('3.0.0'); |
| 41 | + assert.equal(this.dummy.checkEnvironmentVersion(), true); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('with required environment', () => { |
| 46 | + before(function() { |
| 47 | + this.getVersionStub.returns('3.0.1'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns true', function() { |
| 51 | + assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('with ignoreVersionCheck', () => { |
| 55 | + before(function() { |
| 56 | + this.dummy.options.ignoreVersionCheck = true; |
| 57 | + }); |
| 58 | + |
| 59 | + after(function() { |
| 60 | + this.dummy.options.ignoreVersionCheck = false; |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns true', function() { |
| 64 | + this.getVersionStub.returns('3.0.1'); |
| 65 | + assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('with greater than required environment', () => { |
| 71 | + it('returns true', function() { |
| 72 | + this.getVersionStub.returns('3.0.2'); |
| 73 | + assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), true); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('with less than required environment', () => { |
| 78 | + before(function() { |
| 79 | + this.getVersionStub.returns('3.0.0'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns true', function() { |
| 83 | + assert.throws( |
| 84 | + () => this.dummy.checkEnvironmentVersion('3.0.1'), |
| 85 | + /requires yeoman-environment at least 3.0.1, current version is 3.0.0$/ |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('with ignoreVersionCheck', () => { |
| 90 | + before(function() { |
| 91 | + this.dummy.options.ignoreVersionCheck = true; |
| 92 | + }); |
| 93 | + |
| 94 | + after(function() { |
| 95 | + this.dummy.options.ignoreVersionCheck = false; |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns false', function() { |
| 99 | + assert.equal(this.dummy.checkEnvironmentVersion('3.0.1'), false); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('with required inquirer', () => { |
| 105 | + it('returns true', function() { |
| 106 | + this.getVersionStub.withArgs('inquirer').returns('7.1.0'); |
| 107 | + assert.equal(this.dummy.checkEnvironmentVersion('inquirer', '7.1.0'), true); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('with greater than required inquirer', () => { |
| 112 | + it('returns true', function() { |
| 113 | + this.getVersionStub.withArgs('inquirer').returns('7.1.1'); |
| 114 | + assert.equal(this.dummy.checkEnvironmentVersion('inquirer', '7.1.0'), true); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('with less than required inquirer', () => { |
| 119 | + before(function() { |
| 120 | + this.getVersionStub.withArgs('inquirer').returns('7.1.0'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('throws exception', function() { |
| 124 | + assert.throws( |
| 125 | + () => this.dummy.checkEnvironmentVersion('inquirer', '7.1.1'), |
| 126 | + /requires inquirer at least 7.1.1, current version is 7.1.0$/ |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('with ignoreVersionCheck', () => { |
| 131 | + before(function() { |
| 132 | + this.dummy.options.ignoreVersionCheck = true; |
| 133 | + }); |
| 134 | + |
| 135 | + after(function() { |
| 136 | + this.dummy.options.ignoreVersionCheck = false; |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns false', function() { |
| 140 | + assert.equal(this.dummy.checkEnvironmentVersion('inquirer', '7.1.1'), false); |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('mocked 2.8.1', () => { |
| 148 | + before(function() { |
| 149 | + this.timeout(100000); |
| 150 | + this.env = Environment.createEnv([], { 'skip-install': true }, new TestAdapter()); |
| 151 | + this.getVersion = Environment.prototype.getVersion; |
| 152 | + delete Environment.prototype.getVersion; |
| 153 | + |
| 154 | + this.Dummy = class extends Base {}; |
| 155 | + this.dummy = new this.Dummy(['bar', 'baz', 'bom'], { |
| 156 | + foo: false, |
| 157 | + something: 'else', |
| 158 | + namespace: 'dummy', |
| 159 | + env: this.env, |
| 160 | + 'skip-install': true |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + after(function() { |
| 165 | + Environment.prototype.getVersion = this.getVersion; |
| 166 | + }); |
| 167 | + |
| 168 | + describe('#checkEnvironmentVersion', () => { |
| 169 | + describe('without args', () => { |
| 170 | + it('throws exception', function() { |
| 171 | + assert.throws( |
| 172 | + () => this.dummy.checkEnvironmentVersion(), |
| 173 | + /requires yeoman-environment at least 3.0.0, current version is less than 3.0.0$/ |
| 174 | + ); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('with ignoreVersionCheck', () => { |
| 179 | + before(function() { |
| 180 | + this.dummy.options.ignoreVersionCheck = true; |
| 181 | + }); |
| 182 | + |
| 183 | + after(function() { |
| 184 | + this.dummy.options.ignoreVersionCheck = false; |
| 185 | + }); |
| 186 | + |
| 187 | + describe('without args', () => { |
| 188 | + it('returns false', function() { |
| 189 | + assert.equal(this.dummy.checkEnvironmentVersion(), false); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + describe('without less then 3.0.0', () => { |
| 194 | + it('returns undefined', function() { |
| 195 | + assert.equal(this.dummy.checkEnvironmentVersion('2.9.0'), false); |
| 196 | + }); |
| 197 | + }); |
| 198 | + }); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments