-
Notifications
You must be signed in to change notification settings - Fork 1
/
testutil.js
95 lines (86 loc) · 2.73 KB
/
testutil.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
import core from "@actions/core"
import exec from "@actions/exec"
export const INPUT_TOKEN_ENV_VAR_NAME = "INPUT_GITHUB-PERSONAL-ACCESS-TOKEN"
// Run the `git config --list` command and return the output as a list of lines
export function gitConfigList() {
let output = ""
const options = {
silent: true,
listeners: {
stdout: (data) => {
output += data
},
stderr: (data) => {
output += data
},
},
}
return exec
.exec("git", ["config", "--global", "--list"], options)
.then(() => {
output = output.split("\n")
return output
})
}
// runRemove executes the post/remove task in a subshell. This does modify the underlying
// host's git config but there's no other way to test it.
export function runRemove() {
const removeJS = "remove.js"
let run = subprocess("node", [removeJS])
if (process.env.RUNNER_DEBUG) {
run = run.catch((err) => {
console.error(err)
throw err
})
}
return run
}
// runIndex executes main action in a subshell. This does modify the underlying
// git config.
export function runIndex() {
const indexJS = "index.js"
let run = subprocess("node", [indexJS])
if (process.env.RUNNER_DEBUG) {
run = run.catch((err) => {
console.error(err)
throw err
})
}
return run
}
// Consistent subprocess calling with output regardless of error
function subprocess(cmd, args, opts = { silent: !core.isDebug() }) {
let proc = {
stdout: "",
stderr: "",
err: null,
exitCode: 0,
}
// Always merge the passed environment on top of the process environment so
// we don't lose execution context
opts.env = opts.env || { ...process.env }
// This lets us inspect the process output, otherwise an error is thrown and
// it is lost
opts.ignoreReturnCode =
opts.ignoreReturnCode != undefined ? opts.ignoreReturnCode : true
return new Promise((resolve, reject) => {
exec.getExecOutput(cmd, args, opts)
.then((result) => {
if (result.exitCode > 0) {
let err = new Error(`Command failed: ${cmd}`)
err.exitCode = result.exitCode
err.stdout = result.stdout
err.stderr = result.stderr
reject(err)
return
}
proc.exitCode = result.exitCode
proc.stdout = result.stdout
proc.stderr = result.stderr
resolve(proc)
})
.catch((err) => {
reject(err)
})
})
}