-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidator.js
More file actions
177 lines (161 loc) · 5.43 KB
/
validator.js
File metadata and controls
177 lines (161 loc) · 5.43 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import path from 'path';
import { filename } from 'dirname-filename-esm';
import { assert } from './assert.js';
import * as utils from './utils.js';
/**
* add a validate fn to chains
*
* @param {Function<Context>} fn - async ctx => ctx.assert(ctx.result.stdout.includes('hi'));
* @throws {AssertionError}
*/
export function expect(fn) {
const buildError = new Error('only for stack');
return this._addChain(async ctx => {
try {
await fn.call(this, ctx);
} catch (err) {
throw mergeError(buildError, err);
}
});
}
const extractPathRegex = /\s+at.*[(\s](.*):\d+:\d+\)?/;
const currentFileName = types.isObject(meta) ? filename(import.meta) : __filename;
function mergeError(buildError, runError) {
buildError.message = runError.message;
buildError.actual = runError.actual;
buildError.expected = runError.expected;
buildError.operator = runError.operator;
buildError.stackStartFn = runError.stackStartFn;
buildError.cause = runError;
buildError.stack = buildError.stack
.split('\n')
.filter(line => {
/* istanbul ignore if */
if (line.trim() === '') return false;
const pathMatches = line.match(extractPathRegex);
if (pathMatches === null || !pathMatches[1]) return true;
if (pathMatches[ 1 ] === currentFileName) return false;
return true;
})
.join('\n');
return buildError;
}
/**
* validate file
*
* - `file('/path/to/file')`: check whether file exists
* - `file('/path/to/file', /\w+/)`: check whether file match regexp
* - `file('/path/to/file', 'usage')`: check whether file includes specified string
* - `file('/path/to/file', { version: '1.0.0' })`: checke whether file content partial includes specified JSON
*
* @param {String} filePath - target path to validate, could be relative path
* @param {String|RegExp|Object} [expected] - rule to validate
* @throws {AssertionError}
*/
export function file(filePath, expected) {
assert(filePath, '`filePath` is required');
return this.expect(async function file({ cwd, assert }) {
const fullPath = path.resolve(cwd, filePath);
await assert.matchFile(fullPath, expected);
});
}
/**
* validate file with opposite rule
*
* - `notFile('/path/to/file')`: check whether file don't exists
* - `notFile('/path/to/file', /\w+/)`: check whether file don't match regex
* - `notFile('/path/to/file', 'usage')`: check whether file don't includes specified string
* - `notFile('/path/to/file', { version: '1.0.0' })`: checke whether file content don't partial includes specified JSON
*
* @param {String} filePath - target path to validate, could be relative path
* @param {String|RegExp|Object} [expected] - rule to validate
* @throws {AssertionError}
*/
export function notFile(filePath, expected) {
assert(filePath, '`filePath` is required');
return this.expect(async function notFile({ cwd, assert }) {
const fullPath = path.resolve(cwd, filePath);
await assert.doesNotMatchFile(fullPath, expected);
});
}
/**
* validate stdout
*
* - `stdout(/\w+/)`: check whether stdout match regex
* - `stdout('server started')`: check whether stdout includes some string
*
* @param {String|RegExp} expected - rule to validate
* @throws {AssertionError}
*/
export function stdout(expected) {
assert(expected, '`expected` is required');
return this.expect(async function stdout({ result, assert }) {
assert.matchRule(result.stdout, expected);
});
}
/**
* validate stdout with opposite rule
*
* - `notStdout(/\w+/)`: check whether stdout don't match regex
* - `notStdout('server started')`: check whether stdout don't includes some string
*
* @param {String|RegExp} unexpected - rule to validate
* @throws {AssertionError}
*/
export function notStdout(unexpected) {
assert(unexpected, '`unexpected` is required');
return this.expect(async function notStdout({ result, assert }) {
assert.doesNotMatchRule(result.stdout, unexpected);
});
}
/**
* validate stderr
*
* - `stderr(/\w+/)`: check whether stderr match regex
* - `stderr('server started')`: check whether stderr includes some string
*
* @param {String|RegExp} expected - rule to validate
* @throws {AssertionError}
*/
export function stderr(expected) {
assert(expected, '`expected` is required');
return this.expect(async function stderr({ result, assert }) {
assert.matchRule(result.stderr, expected);
});
}
/**
* validate stderr with opposite rule
*
* - `notStderr(/\w+/)`: check whether stderr don't match regex
* - `notStderr('server started')`: check whether stderr don't includes some string
*
* @param {String|RegExp} unexpected - rule to validate
* @throws {AssertionError}
*/
export function notStderr(unexpected) {
assert(unexpected, '`unexpected` is required');
return this.expect(async function notStderr({ result, assert }) {
assert.doesNotMatchRule(result.stderr, unexpected);
});
}
/**
* validate process exitCode
*
* @param {Number|Function} n - value to compare
* @throws {AssertionError}
*/
export function code(n) {
this._expectedExitCode = n;
const fn = utils.types.isFunction(n) ? n : code => assert.equal(code, n, `Expected exitCode to be ${n} but got ${code}`);
this.expect(function code({ result }) {
// when using `.wait()`, it could maybe not exit at this time, so skip and will double check it later
if (result.code !== undefined) {
fn(result.code);
}
});
// double check
this._addChain(function code({ result }) {
fn(result.code);
}, 'end');
return this;
}