|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Fs = require('fs'); |
| 4 | +const Nock = require('nock'); |
| 5 | +const Path = require('path'); |
| 6 | +const SimpleGit = require('simple-git/promise'); |
| 7 | +const Sinon = require('sinon'); |
| 8 | +const Tmp = require('tmp'); |
| 9 | + |
| 10 | +const Utils = require('../../lib/utils'); |
| 11 | + |
| 12 | + |
| 13 | +module.exports = class TestContext { |
| 14 | + |
| 15 | + constructor() { |
| 16 | + |
| 17 | + this._cleanup = []; |
| 18 | + this.stubs = {}; |
| 19 | + |
| 20 | + Sinon.useFakeTimers({ |
| 21 | + now: +new Date('2020-02-02T20:00:02Z'), |
| 22 | + toFake: ['Date'] |
| 23 | + }); |
| 24 | + |
| 25 | + this._mockSimpleGit(); |
| 26 | + this._mockNetwork(); |
| 27 | + } |
| 28 | + |
| 29 | + cleanup() { |
| 30 | + |
| 31 | + Sinon.restore(); |
| 32 | + |
| 33 | + this._cleanup.forEach((cleanup) => cleanup()); |
| 34 | + |
| 35 | + this._cleanup = []; |
| 36 | + } |
| 37 | + |
| 38 | + _mockSimpleGit() { |
| 39 | + |
| 40 | + this.stubs.listRemote = Sinon.stub().throws(); |
| 41 | + |
| 42 | + Sinon.stub(Utils, 'simpleGit').callsFake((...args) => { |
| 43 | + |
| 44 | + const simpleGit = SimpleGit(...args); |
| 45 | + |
| 46 | + Sinon.stub(simpleGit, 'listRemote').callsFake(this.stubs.listRemote); |
| 47 | + |
| 48 | + return simpleGit; |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + _mockNetwork() { |
| 53 | + |
| 54 | + if (!Nock.isActive()) { |
| 55 | + Nock.activate(); |
| 56 | + } |
| 57 | + |
| 58 | + Nock.disableNetConnect(); |
| 59 | + |
| 60 | + Nock('https://raw.githubusercontent.com') |
| 61 | + .persist() |
| 62 | + .get('/nodejs/Release/master/schedule.json') |
| 63 | + .reply(200, Fs.readFileSync(Path.join(__dirname, 'node-release-schedule.json'))); |
| 64 | + |
| 65 | + Nock('https://nodejs.org') |
| 66 | + .persist() |
| 67 | + .get('/dist/index.json') |
| 68 | + .reply(200, Fs.readFileSync(Path.join(__dirname, 'node-release-dist.json'))); |
| 69 | + |
| 70 | + this._cleanup.push(() => { |
| 71 | + |
| 72 | + Nock.restore(); |
| 73 | + Nock.cleanAll(); |
| 74 | + Nock.enableNetConnect(); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + async setupRepoFolder({ travisYml, packageJson, npmShrinkwrapJson, packageLockJson, git = true } = {}) { |
| 79 | + |
| 80 | + const tmpObj = Tmp.dirSync({ unsafeCleanup: true }); |
| 81 | + |
| 82 | + this.path = tmpObj.name; |
| 83 | + |
| 84 | + this._cleanup.push(() => tmpObj.removeCallback()); |
| 85 | + |
| 86 | + if (travisYml) { |
| 87 | + Fs.copyFileSync(Path.join(__dirname, 'travis-ymls', travisYml), Path.join(this.path, '.travis.yml')); |
| 88 | + } |
| 89 | + |
| 90 | + if (packageJson !== false) { |
| 91 | + Fs.writeFileSync(Path.join(this.path, 'package.json'), JSON.stringify(packageJson || { |
| 92 | + name: 'test-module', |
| 93 | + version: '0.0.0-development' |
| 94 | + })); |
| 95 | + } |
| 96 | + |
| 97 | + if (npmShrinkwrapJson) { |
| 98 | + Fs.copyFileSync(Path.join(__dirname, npmShrinkwrapJson), Path.join(this.path, 'npm-shrinkwrap.json')); |
| 99 | + } |
| 100 | + |
| 101 | + if (packageLockJson) { |
| 102 | + Fs.copyFileSync(Path.join(__dirname, packageLockJson), Path.join(this.path, 'package-lock.json')); |
| 103 | + } |
| 104 | + |
| 105 | + if (git) { |
| 106 | + const simpleGit = SimpleGit(this.path); |
| 107 | + await simpleGit.init(); |
| 108 | + await simpleGit.add('./*'); |
| 109 | + await simpleGit.commit('initial commit', ['--no-gpg-sign']); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +}; |
0 commit comments