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

Little bug in spy.printf "%*" formatter #1332

Closed
eauc opened this issue Mar 15, 2017 · 1 comment
Closed

Little bug in spy.printf "%*" formatter #1332

eauc opened this issue Mar 15, 2017 · 1 comment

Comments

@eauc
Copy link

eauc commented Mar 15, 2017

First of all, many thanks for all your hard work on sinon ;)

  • Sinon version : v2.0.0
  • Environment : all ? found in node v4
  • Example URL : n/a
  • Other libraries you are using: sinon-chai, chai

What did you expect to happen?
In the spy.printf(format, args...) function,
when using the "%*" formatter, and passing a list of strings as arguments,
I'd expect all the string to be unquoted.

What actually happens
the first string is unquoted,
all the others ones are quoted.

How to reproduce
npm install sinon@2.0.0
$ node

const sinon = require("sinon")
const spy = sinon.spy()
spy.printf("%*", "a","b","c")
a, "b", "c"

The Bug

In lib/sinon/util/core/format.js:

var formatter = formatio.configure({
    quoteStrings: false,
    limitChildrenCount: 250
});

module.exports = function format() {
    return formatter.ascii.apply(formatter, arguments);
};

you're calling formatio.ascii with all the arguments of your sinonFormat function.

In lib/sinon/spy-formatters.js

module.exports = {
    ....
    "*": function (spyInstance, args) {
        return args.map(sinonFormat).join(", ");
    }
};

You're calling sinonFormat directly with map.

  • sinonFormat is called with (value, index, collection)
  • formatio.ascii is thus called with (value, index, collection)
  • formatio.ascii expect (value, processedFlad, indentValue)

the index masquerade as the processedFlag, resulting in the first string being correctly unquoted (index 0 == false) and all the others being quoted (index>0 == true)

Context

I tried to make the sinon-chai module work with sinon@2.0.0.
I ran the tests for sinon-chai, and the only error is in the format of the expectation messages.
I traked it down to this little bug in sinon.

@fatso83
Copy link
Contributor

fatso83 commented Mar 15, 2017

Thanks for your detailed investigation. That helps a lot! Do you have any chance of fixing this with a small PR?

eauc pushed a commit to eauc/sinon that referenced this issue Mar 15, 2017
…atter.

When used with a list of strings, only the first one is correctly non-quoted :
spy.printf("%*", "a", "b", "c") -> a, "b", "c"

The formatio.ascii function was called with all the arguments provided by map (value, index, collection).
Just added a wrapper in the map() call to forward only the first argument (value).

The unit test shows that the error is a little more serious:
in case the arguments are not simple values, but array/object, formatio throws an exception.
fatso83 added a commit that referenced this issue Mar 15, 2017
Fix issue #1332 : little bug in spy.printf "%*" formatter.
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

No branches or pull requests

2 participants