UNPKG

1.41 kBPlain TextView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5const mock = require('mock-require')
6const crypto = require('crypto');
7const childProcess = require('child_process');
8const carmi = require('../../')
9
10const getHash = input => crypto.createHash('md5').update(input).digest('hex').slice(-7);
11
12const getLsTreeResultLine = (filename, withRandomHash) => {
13 const hashInput = withRandomHash && withRandomHash !== 'undefined' ? Date.now().toString() : filename;
14 return `123456 blob ${getHash(hashInput)} ${filename}`;
15};
16
17// Mock cache directory to allow tests working with own one.
18if (process.env.CACHE_DIR) {
19 mock('find-cache-dir', () => process.env.CACHE_DIR)
20}
21
22// Mock git ls-tree call to check git-hash test scenario
23mock('child_process', {
24 ...childProcess,
25 execSync(cmd, ...args) {
26 if (cmd.includes('git ls-tree')) {
27 if (process.env.ERROR_STAGE === 'git-hash') {
28 throw new Error('Oops');
29 }
30 const withRandomHash = process.env.RANDOM_GIT_HASH;
31 return [getLsTreeResultLine('test.carmi.js', withRandomHash), getLsTreeResultLine('test2.carmi.js', withRandomHash)].join('\n');
32 }
33 return childProcess.execSync(cmd, ...args);
34 }
35});
36
37// Mock carmi to send compilation call message to the parrent process.
38mock('../..', {
39 ...carmi,
40 compile() {
41 process.send('carmi:compile')
42 return ''
43 }
44})
45
46// Require original carmi binary after all mocks installed
47require('../carmi')