Skip to content
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

Refuse to replace already replaced values #1785

Merged
merged 1 commit into from May 7, 2018

Conversation

mantoni
Copy link
Member

@mantoni mantoni commented May 6, 2018

Fix for issue #1779

When values where replaced multiple times, restore didn't restore the original value. This change causes sandbox.replace to throw if the given object / property combination was already used.

How to verify - mandatory

  1. Check out this branch
  2. npm install
  3. npm test

or

sinon.replace(console, "log", sinon.fake());
sinon.replace(console, "log", sinon.fake()); // now throws

Versioning

While this fixes an issue where sandbox.restore() fails to restore values that where replaced multiple times, this might also cause existing tests to fail. However, I would argue that we're fixing a broken behavior in the new sandbox.replace API to bring it in line with sandbox.stub which also throws in this case. Therefore I'd vote for semver patch.

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

@mantoni mantoni requested a review from mroderick May 6, 2018 13:30
var orig = functionStub;
functionStub = spy.create(functionStub, stubLength);
functionStub.func = orig;
functionStub.id = "stub#" + uuid++;
Copy link
Member Author

Choose a reason for hiding this comment

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

Uh, sorry. Commited this by accident. I noticed that the generated stub#${uuid} id property is replaced by spy.create with spy#{uuid}. This change fixes it, but it's not really part of this PR. Shall I revert or just leave it?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just put it into it's own commit?

@mroderick mroderick added the semver:patch changes will cause a new patch version label May 6, 2018
Copy link
Member

@mroderick mroderick left a comment

Choose a reason for hiding this comment

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

Looks great! Just a couple of very minor comments from me 👍

}

function verifyNotReplaced(object, property) {
for (var i = 0; i < fakeRestorers.length; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

Since we're targeting ES5.1, it's fine to use the ES5 Array Extras... it is much harder to create off-by-one errors, when you don't need to maintain an index 😉

function verifyNotReplaced(object, property) {
    fakeRestorers.forEach(function(fakeRestorer) {
        if (fakeRestorer.object === object && fakeRestorer.property === property) {
            throw new TypeError("Attempted to replace " + property + " which is already replaced");
        }
    });
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, you're right. And even forEach would give us an index as the second argument, if needed.

var orig = functionStub;
functionStub = spy.create(functionStub, stubLength);
functionStub.func = orig;
functionStub.id = "stub#" + uuid++;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just put it into it's own commit?

When values where replaced multiple times, `restore` didn't restore the
original value. This change causes `sandbox.replace` to throw if the
given object / property combination was already used.

Fixes #1779
@mantoni mantoni force-pushed the sandbox-replace-only-once branch from ebabc59 to 00432ae Compare May 6, 2018 14:53
@mroderick mroderick merged commit e5b43de into master May 7, 2018
@mroderick mroderick deleted the sandbox-replace-only-once branch May 7, 2018 05:49
@mroderick
Copy link
Member

This has been published as sinon@5.0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver:patch changes will cause a new patch version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants