Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
113 changes: 113 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict';

const Fs = require('fs');
const Nock = require('nock');
const Path = require('path');
const SimpleGit = require('simple-git/promise');
const Sinon = require('sinon');
const Tmp = require('tmp');

const Utils = require('../../lib/utils');


module.exports = class TestContext {

constructor() {

this._cleanup = [];
this.stubs = {};

Sinon.useFakeTimers({
now: +new Date('2020-02-02T20:00:02Z'),
toFake: ['Date']
});

this._mockSimpleGit();
this._mockNetwork();
}

cleanup() {

Sinon.restore();

this._cleanup.forEach((cleanup) => cleanup());

this._cleanup = [];
}

_mockSimpleGit() {

this.stubs.listRemote = Sinon.stub().throws();

Sinon.stub(Utils, 'simpleGit').callsFake((...args) => {

const simpleGit = SimpleGit(...args);

Sinon.stub(simpleGit, 'listRemote').callsFake(this.stubs.listRemote);

return simpleGit;
});
}

_mockNetwork() {

if (!Nock.isActive()) {
Nock.activate();
}

Nock.disableNetConnect();

Nock('https://raw.githubusercontent.com')
.persist()
.get('/nodejs/Release/master/schedule.json')
.reply(200, Fs.readFileSync(Path.join(__dirname, 'node-release-schedule.json')));

Nock('https://nodejs.org')
.persist()
.get('/dist/index.json')
.reply(200, Fs.readFileSync(Path.join(__dirname, 'node-release-dist.json')));

this._cleanup.push(() => {

Nock.restore();
Nock.cleanAll();
Nock.enableNetConnect();
});
}

async setupRepoFolder({ travisYml, packageJson, npmShrinkwrapJson, packageLockJson, git = true } = {}) {

const tmpObj = Tmp.dirSync({ unsafeCleanup: true });

this.path = tmpObj.name;

this._cleanup.push(() => tmpObj.removeCallback());

if (travisYml) {
Fs.copyFileSync(Path.join(__dirname, 'travis-ymls', travisYml), Path.join(this.path, '.travis.yml'));
}

if (packageJson !== false) {
Fs.writeFileSync(Path.join(this.path, 'package.json'), JSON.stringify(packageJson || {
name: 'test-module',
version: '0.0.0-development'
}));
}

if (npmShrinkwrapJson) {
Fs.copyFileSync(Path.join(__dirname, npmShrinkwrapJson), Path.join(this.path, 'npm-shrinkwrap.json'));
}

if (packageLockJson) {
Fs.copyFileSync(Path.join(__dirname, packageLockJson), Path.join(this.path, 'package-lock.json'));
}

if (git) {
const simpleGit = SimpleGit(this.path);
await simpleGit.init();
await simpleGit.add('./*');
await simpleGit.commit('initial commit', ['--no-gpg-sign']);
}
}

};
Loading