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

[Bug] The injected codes make the Promise(bluebird) can not work. #56

Open
snowyu opened this issue Jul 30, 2015 · 7 comments
Open

[Bug] The injected codes make the Promise(bluebird) can not work. #56

snowyu opened this issue Jul 30, 2015 · 7 comments

Comments

@snowyu
Copy link

snowyu commented Jul 30, 2015

reproduce:

Q   = require 'bluebird'

readFile = Q.promisify (aPath, done)->
  if aPath == 'c'
    data = 'ok'
  else
    err = new Error('no such file')
  done(err, data)

Q.reduce ['a', 'b', 'c', 'd'], (content, file)->
  console.log file, content
  if !content
    content = readFile(file).caught(->)
  return content
, null
.then (content)->
  console.log 'content=', content

the result should be:

a null
b undefined
c undefined
d ok
content= ok

But after injected:

a null
b 0
c 1
d 1
content= 1
@jwalton
Copy link
Contributor

jwalton commented Jul 30, 2015

I think I know what's going on here. As a workaround, if you change:

content = readFile(file).caught(->)

to

content = readFile(file).caught(-> undefined)

I bet it will work. I suspect what's going on here is we're instrumenting the empty function, adding a lineCount++ at the start, which means instead of returning undefined, it ends up returning the count.

@snowyu
Copy link
Author

snowyu commented Jul 30, 2015

why should inject the code to empty function and return a value?

  • It will make the high level function get error result like this.
  • It will make the check whether is an empty function get wrong result.

@jwalton
Copy link
Contributor

jwalton commented Jul 30, 2015

Yeah, it's a bug for sure. The way coffee-coverage works is to instrument the original coffee-script before compilation, so we change:

->

to something like:

__coverageVar['filename.coffee].s[0]++
->
    __coverageVar['filename.coffee].f[0]++
    __coverageVar['filename.coffee].s[1]++

At the end of the test, we know how many times the 0th function and 1st statement were run. But, of course, this is wrong; it should be generating:

__coverageVar['filename.coffee].s[0]++
->
    __coverageVar['filename.coffee].f[0]++
    __coverageVar['filename.coffee].s[1]++
    return undefined

@snowyu
Copy link
Author

snowyu commented Jul 30, 2015

It will solve the first problem in above list if so. But It still make the checker(isEmptyFunction) error.

The test whether empty function(second problem in the list) is important for my case too, My createObject function in the inherits-ex depends on it. this make it easier to write a class.

inherits = require 'inherits-ex'
createObject = require 'inherits-ex/lib/createObject'

class A
  constructor: ->console.log 'A'

class B
  inherits B, A

b = new B # the original way
b = createObject B # the constructor of A will be executed because the B's constructor is empty.

I have to work around the coverage's bug in my isEmptyFunction function temporarily.

@jwalton
Copy link
Contributor

jwalton commented Jul 30, 2015

Hmm, interesting corner case. You can make a truly empty function with something like:

### !pragma no-coverage-next ###
x = ->

(We could add a ### !pragma no-coverage-block ### too, which would be a bit finer-grained.) But I'm not sure this is really going to help your use case. We could not instrument empty functions, but then you wouldn't know if your empty function was being called or not.

@snowyu
Copy link
Author

snowyu commented Jul 31, 2015

class B
  inherits B, A

The empty constructor is generated by coffee-script compiler. So unless modify the compiler...

@snowyu
Copy link
Author

snowyu commented Jul 31, 2015

maybe add a switcher to enable the measuring the calls of empty function.

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

No branches or pull requests

2 participants