forked from graphprotocol/graph-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
116 lines (97 loc) · 3.14 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const fs = require('fs-extra')
const path = require('path')
const spawn = require('spawn-command')
const stripAnsi = require('strip-ansi')
// Deletes folder if:
// - flag is true
// - folder exists
const deleteDir = (dir, flag) => {
if (flag && fs.existsSync(dir)) {
fs.removeSync(dir)
}
}
const resolvePath = p => path.join(__dirname, p)
const cliTest = (title, args, testPath, options = {}) => {
test(
title,
async () => {
try {
deleteDir(resolvePath(`./${testPath}`), options.deleteDir)
// Use the provided cwd if desired
let cwd =
options.cwd ? options.cwd : resolvePath(`./${testPath}`)
let [exitCode, stdout, stderr] = await runGraphCli(args, cwd)
let expectedExitCode = undefined
if (options.exitCode !== undefined) {
expectedExitCode = options.exitCode
}
let expectedStdout = undefined
try {
expectedStdout = fs.readFileSync(resolvePath(`./${testPath}.stdout`), 'utf-8')
} catch (e) {}
let expectedStderr = undefined
try {
expectedStderr = fs.readFileSync(resolvePath(`./${testPath}.stderr`), 'utf-8')
} catch (e) {}
if (expectedStderr !== undefined) {
// For some reason the error sometimes comes in stdout, then
// stderr comes empty.
//
// If that's the case, we should throw it so it's easier
// to debug the error.
//
// TODO: investigate why that happens (somewhere it should
// be using console.error or print.error for example) so this
// check can be removed.
if (stderr.length === 0 && stdout.length !== 0) {
throw new Error(stdout)
}
expect(stripAnsi(stderr)).toBe(expectedStderr)
}
if (expectedExitCode !== undefined) {
expect(exitCode).toBe(expectedExitCode)
}
if (expectedStdout !== undefined) {
expect(stripAnsi(stdout)).toBe(expectedStdout)
}
} finally {
deleteDir(resolvePath(`./${testPath}`), options.deleteDir)
}
},
options.timeout || undefined,
)
}
const runCommand = async (command, args = [], cwd = process.cwd()) => {
// Make sure to set an absolute working directory
cwd = cwd[0] !== '/' ? path.resolve(__dirname, cwd) : cwd
return new Promise((resolve, reject) => {
let stdout = ''
let stderr = ''
const child = spawn(`${command} ${args.join(' ')}`, { cwd })
child.on('error', error => {
reject(error)
})
child.stdout.on('data', data => {
stdout += data.toString()
})
child.stderr.on('data', data => {
stderr += data.toString()
})
child.on('exit', exitCode => {
resolve([exitCode, stdout, stderr])
})
})
}
const runGraphCli = async (args, cwd) => {
// Resolve the path to graph.js
let graphCli = path.join(__dirname, '..', '..', 'bin', 'graph')
return await runCommand(graphCli, args, cwd)
}
const npmLinkCli = () => runCommand('npm', ['link'])
const npmUnlinkCli = () => runCommand('npm', ['unlink'])
module.exports = {
cliTest,
npmLinkCli,
npmUnlinkCli,
runGraphCli,
}