Rewrite 'pull-os-versions' script in Javascript to add support for reporting os revis...
[WebKit.git] / Websites / perf.webkit.org / server-tests / resources / mock-subprocess.js
1 var MockSubprocess = {
2     execute: function (command)
3     {
4         const invocation = {command};
5         invocation.promise = new Promise((resolve, reject) => {
6             invocation.resolve = resolve;
7             invocation.reject = reject;
8         });
9
10         if (this._waitingInvocation) {
11             this._waitingInvocationResolver();
12             this._waitingInvocation = null;
13             this._waitingInvocationResolver = null;
14         }
15
16         this.invocations.push(invocation);
17         return invocation.promise;
18     },
19     waitingForInvocation: function ()
20     {
21         if (!this._waitingInvocation) {
22             this._waitingInvocation = new Promise(function (resolve, reject) {
23                 MockSubprocess._waitingInvocationResolver = resolve;
24             });
25         }
26         return this._waitingInvocation;
27     },
28     inject: function ()
29     {
30         const originalSubprocess = global.Subprocess;
31
32         beforeEach(function () {
33             MockSubprocess.reset();
34             originalSubprocess = global.Subprocess;
35             global.Subprocess = MockSubprocess;
36         });
37
38         afterEach(function () {
39             global.Subprocess = originalSubprocess;
40         });
41
42         return  MockSubprocess.invocations ;
43     },
44     reset: function ()
45     {
46         MockSubprocess.invocations = [];
47         MockSubprocess._waitingInvocation = null;
48         MockSubprocess._waitingInvocationResolver = null;
49     },
50
51     _waitingInvocation: null,
52     _waitingInvocationResolver: null,
53     invocations: [],
54 };
55
56 if (typeof module != 'undefined')
57     module.exports.MockSubprocess = MockSubprocess;