Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e0c7b3d

Browse files
bnoordhuiscjihrig
authored andcommittedNov 7, 2017
lib: shuffle v8_prof_polyfill.js for unit testing
Make it possible to test the versionCheck() function from that file in isolation. PR-URL: nodejs#16769 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c14030e commit e0c7b3d

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed
 

‎lib/internal/v8_prof_polyfill.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28+
module.exports = { versionCheck };
29+
30+
// Don't execute when required directly instead of being eval'd from
31+
// lib/internal/v8_prof_processor.js. This way we can test functions
32+
// from this file in isolation.
33+
if (module.id === 'internal/v8_prof_polyfill') return;
34+
2835
// Node polyfill
2936
const fs = require('fs');
3037
const cp = require('child_process');
@@ -64,7 +71,18 @@ const fd = fs.openSync(logFile, 'r');
6471
const buf = Buffer.allocUnsafe(4096);
6572
const dec = new (require('string_decoder').StringDecoder)('utf-8');
6673
var line = '';
67-
versionCheck();
74+
75+
{
76+
const message = versionCheck(peekline(), process.versions.v8);
77+
if (message) console.log(message);
78+
}
79+
80+
function peekline() {
81+
const s = readline();
82+
line = s + '\n' + line;
83+
return s;
84+
}
85+
6886
function readline() {
6987
while (true) {
7088
const lineBreak = line.indexOf('\n');
@@ -81,27 +99,21 @@ function readline() {
8199
}
82100
}
83101

84-
function versionCheck() {
102+
function versionCheck(firstLine, expected) {
85103
// v8-version looks like
86104
// "v8-version,$major,$minor,$build,$patch[,$embedder],$candidate"
87105
// whereas process.versions.v8 is either "$major.$minor.$build-$embedder" or
88106
// "$major.$minor.$build.$patch-$embedder".
89-
var firstLine = readline();
90-
line = firstLine + '\n' + line;
91107
firstLine = firstLine.split(',');
92-
const curVer = process.versions.v8.split(/[.\-]/);
108+
const curVer = expected.split(/[.\-]/);
93109
if (firstLine.length !== 6 && firstLine.length !== 7 ||
94110
firstLine[0] !== 'v8-version') {
95-
console.log('Unable to read v8-version from log file.');
96-
return;
111+
return 'Unable to read v8-version from log file.';
97112
}
98113
// Compare major, minor and build; ignore the patch and candidate fields.
99-
for (var i = 0; i < 3; i++) {
100-
if (curVer[i] !== firstLine[i + 1]) {
101-
console.log('Testing v8 version different from logging version');
102-
return;
103-
}
104-
}
114+
for (var i = 0; i < 3; i++)
115+
if (curVer[i] !== firstLine[i + 1])
116+
return 'Testing v8 version different from logging version';
105117
}
106118

107119
function macCppfiltNm(out) {

0 commit comments

Comments
 (0)
Please sign in to comment.