Skip to content

Sync upstream test-buffer-alloc #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 16, 2018
Merged

Sync upstream test-buffer-alloc #178

merged 21 commits into from
Feb 16, 2018

Conversation

vmx
Copy link
Contributor

@vmx vmx commented Jan 21, 2018

This pull request updates the test-buffer-alloc.js from Node.js to the current upstream version.

I've used very granular commits which hopefully shows my workflow. I hope my approach is sound and the work was worth it.

This is part of #177.

vmx added 21 commits January 18, 2018 22:55
This commits updates the `test-buffer-alloc.js`. Tests currently
not passing are commented out and will be fixed in a subsequent
commit.
Node.js treats an equal sign as the end of a Base64 encoded string.
Hence `Buffer.from('=bad')` results in an empty string.
If a hex string has an odd number of bytes, the last byte is just
ignored in Node.js.
The error message changed from

    "encoding" must be a valid string encoding

to

    Unknown encoding: <the-given-encoding>

To make the test work, parts of Node.js's `common` module need to
be used. Add the parts that are needed with minor modifications (the
check for the return code were removed, as vanilla JS errors don't
have an error code, only Node.js errors have).
We don't want to print anything, hence `printSkipMessage()` is just
a no-op to make the tests without changes pass.
Make `toLocaleString()` to point to the same implementation as `toString()`.
This fixed is based on Node.js' commit d4f00fe1098b0d7b8444565e741d8b457fd9c091
nodejs/node@d4f00fe
Some tests from test-buffer.js were moved into test-buffer-alloc.js.
Remove those tests as they got updated (and fixed) in
test-buffer-alloc.js.
Some post-processing of the downloaded tests is no longer needed.
Use `const` and `let` instead of `var` and also import a trimmed
down version of the `common` test helper.
This commit uses the `test-buffer-alloc.js` from upstream Node.js
as of 2018-01-20.

This is part of feross#177.
index.js Outdated
@@ -53,6 +53,16 @@ function typedArraySupport () {
}
}

Object.defineProperty(Buffer.prototype, 'offset', {
enumerable: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be enumerable: false, since we don't want these properties to show up in Object.keys(). Here's what Node.js does:

Object.keys(Buffer.from([1, 2,3]))
// [ '0', '1', '2' ]

@@ -53,6 +53,16 @@ function typedArraySupport () {
}
}

Object.defineProperty(Buffer.prototype, 'parent', {
enumerable: true,
Copy link
Owner

@feross feross Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this so the default value of enumerable: false is used. We don't want Object.keys(Buffer.from([1, 2, 3])) to return [1, 2, 3, 'offset', 'parent'].

@@ -1560,9 +1560,11 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) {
// HELPER FUNCTIONS
// ================

var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_=]/g
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this line needs to be changed. In the new line you added below, you're already finding the first = and discarding it and everything that follows. There should be no = left in the string to remove.

@@ -1445,6 +1445,9 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert

// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
Buffer.prototype.copy = function copy (target, targetStart, start, end) {
if (target === undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to check if the argument is not actually a Buffer to catch all possible bad values instead of just undefined


Object.defineProperty(Buffer.prototype, 'offset', {
enumerable: true,
get () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type of function declaration breaks IE11, fortunately we can just use get: function () {


const failed = mustCallChecks.filter(function(context) {
if ('minimum' in context) {
context.messageSegment = `at least ${context.minimum}`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template literal syntax breaks IE11.

return _mustCallInner(fn, exact, 'exact');
};

function _mustCallInner(fn, criteria = 1, field) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default argument values break IE11

@feross feross merged commit d1bbce0 into feross:master Feb 16, 2018
@feross
Copy link
Owner

feross commented Feb 16, 2018

I figured out the reason that IE11 was unhappy -- test/common.js belongs in test/node/common.js. The files that match test/* run in both ES5 and ES6+ browsers, while test/node/* only run in ES6+ browsers. Moving the file into node/ fixes the IE11 errors.

@feross
Copy link
Owner

feross commented Feb 16, 2018

Fixed the other issues I mentioned and merged this. Thanks for doing this work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants