JoinColony / solcover

Code coverage for solidity
MIT License
64 stars 8 forks source link

solcover fail to generate test coverage with my simple add contract #17

Closed maiffany closed 7 years ago

maiffany commented 7 years ago

I want to use solcover to generate test coverage of my contract. I test it with the simple one:

contract Add {
    function Add() {
    }
    function sum(uint x, uint y) returns (uint) {
        return (x+y);
    }
}

And here is my test code:

contract('Add', function(accounts) {
  it("should return 5 when add 2 and 3", function() {
    var add = Add.deployed();
    return add.sum.call(2,3).then(function(res){
      assert.equal(res.valueOf(), 5, "add result is 5");
    });   
  });
});

But there are some errs when compile it.

Add.sol:2:1: Warning: Source file does not specify required compiler version!Consider adding "pragma solidity ^0.4.6
contract Add {
^
Spanning multiple lines.
Compilation failed. See above.
fs.js:549

Then i add compile version like this:

pragma solidity ^0.4.6;
contract Add {

    function Add() {
    }

    function sum(uint x, uint y) returns (uint) {
        return (x+y);
    }
}

And now it pass the compile. But this time I got the error:

cp: no such file or directory: ./../originalContracts/Migrations.sol
rm: no such file or directory: ./allFiredEvents
Compiling Add.sol...

  Contract: Add
    ✓ should return 5 when add 2 and 3

  1 passing (15s)

fs.js:549
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open './allFiredEvents'
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.readFileSync (fs.js:397:15)
    at Object.<anonymous> (/Users/maiffany/graduate/mytest/solcover/runCoveredTests.js:60:13)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)

And in you runCoveredTests.js i found this:

image

I guess maybe it is this command that generate file "allFiredEvents". So i try this in my command line:

image

But i still can't find the file "allFiredEvents".

So where did i get wrong?

area commented 7 years ago

It's the modified testrpc command (which this tool provides itself and runs itself) that generates allFiredEvents, so my best guess is that you have another instance of testrpc running.

If that's true, I'll make it clearer in the README that you should not have testrpc, geth or similar running already when running runCoveredTests.js.

maiffany commented 7 years ago

I saw in your README that I should have my port:8545 free. But When i didn't run my localhost ethereum, it showed errors:

cp: no such file or directory: ./../originalContracts/Migrations.sol
rm: no such file or directory: ./allFiredEvents
Compiling Add.sol...
Error: Invalid JSON RPC response: "Error: connect ECONNREFUSED 127.0.0.1:8545\n    at Object.exports._errnoException (util.js:870:11)\n    at exports._exceptionWithHostPort (util.js:893:20)\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)"
    at Object.module.exports.InvalidResponse (/usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/errors.js:35:16)
    at null.request.onreadystatechange (/usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/httpprovider.js:111:32)
    at null.dispatchEvent (/usr/local/lib/node_modules/truffle/node_modules/web3/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:591:25)
    at setState (/usr/local/lib/node_modules/truffle/node_modules/web3/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:610:14)
    at null.handleError (/usr/local/lib/node_modules/truffle/node_modules/web3/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:532:5)
    at ClientRequest.errorHandler (/usr/local/lib/node_modules/truffle/node_modules/web3/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:459:14)
    at emitOne (events.js:77:13)
    at ClientRequest.emit (events.js:169:7)
    at Socket.socketErrorListener (_http_client.js:256:9)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at emitErrorNT (net.js:1253:8)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickDomainCallback (node.js:397:17)
fs.js:549
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open './allFiredEvents'
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.readFileSync (fs.js:397:15)
    at Object.<anonymous> (/Users/maiffany/graduate/mytest/solcover/runCoveredTests.js:60:13)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
area commented 7 years ago

From the solcover directory, run

./node_modules/ethereumjs-testrpc/bin/testrpc

and

node -v

(and put the output here)

maiffany commented 7 years ago

=_= I found there is no ethereumjs-testrpc in my node_modules directory. Maybe my node version is too old. I'll try to update my node later when my network works fine.


-bash: ./node_modules/ethereumjs-testrpc/bin/testrpc: No such file or directory
MINAHU-MC0:solcover maiffany$ node -v
v4.4.4```
area commented 7 years ago

Be sure you've run npm install from the solcover directory. You'll need to update your node version soon anyway when #19 gets merged, though.

maiffany commented 7 years ago

Thank you for your time and help! But so weird, I've use the latest version of node and testrpc works fine. But it still cannot find the file allFiredEvents. My node version is v7.3.0. v6.9.2 doesn't work either.

./node_modules/ethereumjs-testrpc/bin/testrpc: No such file or directory


Available Accounts
==================
(0) 0x0e4ee105d47be78df913df71a5ae1e8974d73232
(1) 0xba4deed46f17c2c441ba1f7158e274cb6b610a09
(2) 0xcf247271a9c1a858a46fe8c3a7e6b853c2b83b6f
(3) 0x433afd579d24b9140297d11fbe1617c1570edfd5
(4) 0xf6cbf07e69bd9f3265c831a5a6727afa9e58fcd2
(5) 0xfb72080de20a94cd7c9d390737ff9eaf939edf2d
(6) 0xbc92f3d3b084c9edc712d806a41717f391324261
(7) 0xfa0caf3ac1de00cb64e8cef4664bcdabe1781d1f
(8) 0x4feca5c06fee845e5c9fa76725eb1c640e85944d
(9) 0x4a5e52d5c7f81c66a15e0efcf860fb33c89ec8d2

Private Keys
==================

node ./runCoveredTests.js


cp: no such file or directory: ./../originalContracts/Migrations.sol
rm: no such file or directory: ./allFiredEvents
Compiling Add.sol...

  Contract: Add
    ✓ should return 5 when add 2 and 3 (56ms)

  1 passing (426ms)

fs.js:557
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open './allFiredEvents'
    at Object.fs.openSync (fs.js:557:18)
    at Object.fs.readFileSync (fs.js:467:33)
    at Object.<anonymous> (/Users/maiffany/testcoverage/solcover/runCoveredTests.js:60:13)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)```
cgewecke commented 7 years ago

@maiffany That error happens when no coverage events are generated. At the moment there is an issue with solCover capturing events when methods use call because a code patch broke (see #19 ). Could you try putting a nonsense statement in the body of your contract constructor: e.g

function Add() {
    uint z = 1;
}

and seeing if solcover generates a basic report?

maiffany commented 7 years ago

Finally!! it succeed! Thank you so much! It seems like the contract method sum invoke failed by the test.js. I'll check how to write the right unit test myself.

image

cgewecke commented 7 years ago

Oh good!

area commented 7 years ago

I've now merged #19, so a git pull, rm -r ./node_modules, npm install from the solcover directory should get you running!

~That said, I'm a little suspicious of the claim line 5 is being run there - if that's still the case after the above update, I'll try and investigate.~ It's the constructor for the contract, of course it's being run. That'll teach me to try and be clever before breakfast.

Sorry for the problems, and thanks for the patch, @cgewecke.

maiffany commented 7 years ago

I didn't write the test cast in the wrong way. I'm sure the method sum has been invoked. But the coverage didn't show it.

my test.js. I modified 5 to 6, so the assert is false.

  it("should return 5 when add 2 and 3", function() {
    var add = Add.deployed();
    return add.sum.call().then(function(res){
      assert.equal(res.valueOf(), 6, "add result is 5");
    });   
  });
});

The test result is:

image

This means that the sum has been invoke. But the the coverage result shows it didn't.

image

area commented 7 years ago

That's the case even updating solcover, and running the commands in my previous reply? Can you upload your whole test project to a repo on github so I can try myself?

maiffany commented 7 years ago

Here is my test project: https://github.com/maiffany/mytest

I'll try your commands in your previous reply.

area commented 7 years ago

So running

git clone https://github.com/maiffany/mytest
cd mytest
git clone https://github.com/JoinColony/solcover/
cd solcover
npm install 
node ./runCoveredTests.js

I get screen shot 2016-12-21 at 09 20 46

maiffany commented 7 years ago

Yes! After I update the code, it works! Thanks!

area commented 7 years ago

Great, sorry again for the problems. Do make sure to raise any other issues you encounter using it though - it's still very new and a bit / a lot hacked together, so bug reports are very valuable for improving the code, which I definitely want to do!

adamlemmon-pc commented 7 years ago

Hi guys,

Sorry to chime in a while after this seems to have been resolved but I am getting very similar errors and have been debugging with no luck for a fair while now.

Any help is greatly appreciated, I really want to use this :)

Finally after no luck I resorted to the steps that worked as you mentioned working above: git clone https://github.com/maiffany/mytest cd mytest git clone https://github.com/JoinColony/solcover/ cd solcover npm install node ./runCoveredTests.js

and get the following output: Unknown network "test". See your Truffle configuration file for available networks. image

As the command in runCoveredTests.js is noted as "shell.exec('truffle test --network test');"

I then updated mytest/truffle.js to the following, adding a network: module.exports = { build: { "index.html": "index.html", "app.js": [ "javascripts/app.js" ], "app.css": [ "stylesheets/app.css" ], "images/": "images/" }, rpc: { host: "localhost", port: 8545, gasPrice: 20e9, gas: 0xfffffff, }, networks: { test: {} } };

Rerun resulting in the following error: _Error: You must specify a networkid in your 'test' configuration in order to use this network. image

Add a network_id: module.exports = { build: { "index.html": "index.html", "app.js": [ "javascripts/app.js" ], "app.css": [ "stylesheets/app.css" ], "images/": "images/" }, rpc: { host: "localhost", port: 8545, gasPrice: 20e9, gas: 0xfffffff, }, networks: { test: { *network_id: "",** } } };

Resulting in the following error: Error: Invalid JSON RPC response: "" image

_./nodemodules/ethereumjs-testrpc/bin/testrpc: image

adam@adam:~/Desktop/mytest/solcover$ node -v v7.6.0

onigiri-x commented 6 years ago

Also getting this issue no matter what steps I take.