Giter VIP home page Giter VIP logo

Comments (13)

thlorenz avatar thlorenz commented on July 29, 2024

I have no idea how this path 'V:GitHubTestRunnerode_modulesproxyquireproxyquire.js' was munged like that.

I doubt however that proxyquire had any part in it.

What version of nodejs are you using?

If you want to troubleshoot this, I'd suggest setting the temp directory to something obvious (i.e. c:\tmp as explained here and commenting this line in proxyquire inside your node_modules/proxyquire/proxyquire.js.

You should then have a mocha.js@x file in your temp directory that proxyquire generated. You can compare that to the original to figure out if proxyquire caused problems.

BTW, I hope you are not actually mocking out the mocha test runner that is running your tests? I don't understand how that would be useful.

from proxyquire.

pghalliday avatar pghalliday commented on July 29, 2024

Thanks, i'll try and debug some more. The code works fine on Linux and OSx.

And lol, I am mocking the test runner but not while it runs my tests. The module I am writing (actually I wrote a while back but wanted to get under test) is a plugin for grunt that runs tests using mocha. So in the tests for my plugin I have mocked, grunt, mocha and the module cache so they don't interfere with the currently running tests (which are also using mocha and in fact the plugin under test)... um, not at all confusing, but hey, like i say, it's working everywhere but windows.

from proxyquire.

thlorenz avatar thlorenz commented on July 29, 2024

Ok, let me know if you find a solution or need more help.

from proxyquire.

pghalliday avatar pghalliday commented on July 29, 2024

Ah ha, I can see the problem. It could be the resolution of __dirname on windows:

var __dirname = "V:\GitHub\grunt-mocha-test\tasks"; var __filename = "V:\GitHub\grunt-mocha-test\tasks\mocha.js"; function require(mdl) { return module.require("V:\GitHub\grunt-mocha-test\node_modules\proxyquire\proxyquire.js")._require(mdl, "C:\Users\PHALLI~1\AppData\Local\Temp\mocha.js@0", __dirname); }

as you can see this contains single backslashes, hence the munging due to escaping (wondered where the newline came from). I can probablty fix this externally for __dirname but i can see that there may be a problem with the temp file location too (at the end of that code snippet). So it may also be necessary to override the temp directory on windows. I will continue to investigate

from proxyquire.

pghalliday avatar pghalliday commented on July 29, 2024

Ok i think the best solution is to escape the backslashes within proxyquire. I'm currently testing this locally and will likely make a pull request shortly

from proxyquire.

thlorenz avatar thlorenz commented on July 29, 2024

Looks like you are on the right track.

Something along the lines of:

'var __dirname = "' + path.dirname(resolvedFile).replace(/\\/g, '\\\\') + '"; '
[..]

should work.

from proxyquire.

pghalliday avatar pghalliday commented on July 29, 2024

Yeah I have this currently but can't figure out the best way to get it under test

    [ 'var __dirname = "' + path.dirname(resolvedFile).replace(/\\/g, '\\\\') + '"; '
    , 'var __filename = "' + resolvedFile.replace(/\\/g, '\\\\') + '"; '
    , 'function require(mdl) { '
    , 'return module'
    ,   '.require("' , __filename.replace(/\\/g, '\\\\'), '")'
    ,   '._require(mdl, "' + resolvedProxy.replace(/\\/g, '\\\\') + '", __dirname); '
    , '} '

as it was though only this test fails which I find odd (I thought more would)

describe('Multiple requires of same module don\'t affect each other', function () {
  describe('Given I require foo stubbed with bar1 as foo1 and foo stubbed with bar2 as foo2', function () {

anyway with my change it now passes but this can only be verified on windows (ie. Travis won't tell you if it breaks will it?)

from proxyquire.

thlorenz avatar thlorenz commented on July 29, 2024

It won't fail on travis if it is only windows related.

Some suggestions:

Ideally you'd put all that duplicate code into function.

You don't need __filename.replace(/\\/g, '\\\\') since you fixed __filename above.

Make sure that all tests pass and put a comment on the test that only will fail on windows if you remove your fixes (I'm assuming you wrote a test to expose the problem first).

from proxyquire.

pghalliday avatar pghalliday commented on July 29, 2024

Yup i think you're right about the duplicate code - i'll change that.

Actually i didn't write a new test in the end as the test i mentioned already failed on windows. I'll see if i can split it into a separate test though that only checks this issue and comment that one as only failing on windows.

I was surprised that only that test failed though as all the tests use the resolve function at some point.

Not sure i understand why you think this is not needed:

__filename.replace(/\\/g, '\\\\')

It's not really fixed above if you mean here:

, 'var __filename = "' + resolvedFile.replace(/\\/g, '\\\\') + '"; '

as that bit of code doesn't get evaluated till later and is not the same __filename variable

from proxyquire.

thlorenz avatar thlorenz commented on July 29, 2024

Unless I'm missing something, __filename should have been escaped since resolvedFile was. No need to escape it again (it's the same variable).
Same reason why you don't have to escape __dirname again, which I see you didn't.

Try without it and see what you get in the generated file.

Also if the problem was exposed with an existing test and now passes, that is fine. If you want to add tests for other potential problems that are now fixed, please do so.

from proxyquire.

pghalliday avatar pghalliday commented on July 29, 2024

i think it's clearer if i add code colouring

 [ 'var __dirname = "' + path.dirname(resolvedFile).replace(/\\/g, '\\\\') + '"; '
    , 'var __filename = "' + resolvedFile.replace(/\\/g, '\\\\') + '"; '
    , 'function require(mdl) { '
    , 'return module'
    ,   '.require("' , __filename.replace(/\\/g, '\\\\'), '")'
    ,   '._require(mdl, "' + resolvedProxy.replace(/\\/g, '\\\\') + '", __dirname); '
    , '} '

I have to escape the stuff before it gets added to the literal - the stuff in the literal doesn't need to be escaped.

Anyway working on some tests that will prove each change independently (I hope)

from proxyquire.

thlorenz avatar thlorenz commented on July 29, 2024

ok see it now. thanks.

go ahead and make a pull request once the duplicate code is cleaned up, I'll merge it in tonight and publish a new version.

Thanks for contributing!

from proxyquire.

pghalliday avatar pghalliday commented on July 29, 2024

Your welcome :) Cleaning up now

from proxyquire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.