Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Remove/obviate boilerplate files #1382

Closed
fulldecent opened this issue Oct 26, 2018 · 3 comments
Closed

Remove/obviate boilerplate files #1382

fulldecent opened this issue Oct 26, 2018 · 3 comments
Labels

Comments

@fulldecent
Copy link

fulldecent commented Oct 26, 2018

I loathed getting started with Truffle because there are so many files necessary to get hello world running. Then while I am learning it, I find out that these files deliver no value and are simply boilerplate.

Truffle should learn to work well without these files. Copy-pasting these files into my project do not make me a better programmer, they do not help me learn anything, and they are a distraction from writing tests or doing whatever else I want to do. Sure, maybe I will want to customize some Truffle behavior in the future, but the time to do that is not when just getting started.

truffle.js

npx truffle test
Could not find suitable configuration file.

Fix this error by placing the following file in truffle.js:

module.exports = {}

Clearly this file is not adding any value.

Perhaps the reason this file is needed is because otherwise Truffle doesn't know which folder to search for contracts. But of course the obvious default is to search the current folder and subdirectories for contracts.

migrations/1*.js

npx truffle test
Compiling ./contracts/a.sol...
Compiling ./contracts/b.sol...
Compiling truffle/Assert.sol...
Error: ENOENT: no such file or directory, stat '/Users/williamentriken/Developer/su-squares-ethereum-contract/migrations'

If you put this file into migrations/1-boilerplate.js the error goes away:

// This is a boilerplate file required by Truffle
var Migrations = artifacts.require('./Migrations.sol');

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};

I don't know what this file does and I don't care. It is the same file every project uses. I didn't wake up today wanting to write Javascript, I want to write Solidity.

If this file is missing, Truffle should gracefully default to not doing any migrations.

contracts/Migrations.sol

Obviously this is required because the boilerplate migration js file referenced it. So I won't bother to post the error message you get if the file is missing.

You need to put this file into contracts/Migrations.sol.

pragma solidity ^0.4.23;
// This is a boilerplate file required by Truffle

contract Migrations {
  address public owner;
  uint public last_completed_migration;

  constructor() public {
    owner = msg.sender;
  }

  modifier restricted() {
    if (msg.sender == owner) _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }

  function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
  }
}

At least this is written in Solidity. But it doesn't look like anything I care about. Most projects are just copy-pasting this.

updated, setCompleted and restricted don't look like functions I will call from any of my tests. So this is clearly a file I don't care about.

Since my test cases do not depend on the those functions, Truffle should gracefully proceed if I do not provide this file.

@fulldecent fulldecent changed the title Remove boilerplate files Remove/obviate boilerplate files Oct 26, 2018
@fulldecent
Copy link
Author

P.S. Here is the minimal project I can create that works with Truffle test.

./truffle.js:// Boilerplate required, https://github.com/trufflesuite/truffle/issues/1382
./migrations/1.js:// Boilerplate required, https://github.com/trufflesuite/truffle/issues/1382
./migrations/1.js:module.exports = ($)=>{};
./test/TestMetacoin.sol:pragma solidity ^0.4.2;
./test/TestMetacoin.sol:import "truffle/Assert.sol";
./test/TestMetacoin.sol:import "../contracts/MetaCoin.sol";
./test/TestMetacoin.sol:contract TestMetacoin {
./test/TestMetacoin.sol:  function testBal() public {
./test/TestMetacoin.sol:    MetaCoin meta = new MetaCoin();
./test/TestMetacoin.sol:    uint expected = 5;
./test/TestMetacoin.sol:    Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin iitially");
./test/TestMetacoin.sol:  }
./test/TestMetacoin.sol:}
./contracts/MetaCoin.sol:pragma solidity ^0.4.18;
./contracts/MetaCoin.sol:contract MetaCoin {
./contracts/MetaCoin.sol:	function getBalance(address) public pure returns(uint) {
./contracts/MetaCoin.sol:		return 5;
./contracts/MetaCoin.sol:	}
./contracts/MetaCoin.sol:}
./contracts/Migrations.sol:pragma solidity ^0.4.23;
./contracts/Migrations.sol:// Boilerplate required, https://github.com/trufflesuite/truffle/issues/1382
./contracts/Migrations.sol:contract Migrations {}

@stale
Copy link

stale bot commented Jan 4, 2019

Thank you for raising this issue! It has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If you would like to keep this issue open, please respond with information about the current state of this problem.

@stale stale bot added the stale label Jan 4, 2019
@fulldecent
Copy link
Author

Thank you, this issue is resolved now. Boilerplate is no longer necessary as of Truffle 5.0.1. Only a single truffle.js file, which can be empty, is needed. And this is reasonable.

Here is a minimal test case which demonstrates the new version works.

mkdir -p tmp/test tmp/contracts tmp/migrations

cat > tmp/truffle.js <<EOL
// Boilerplate required, https://github.com/trufflesuite/truffle/issues/1382
EOL

cat > tmp/test/TestMetacoin.sol <<EOL
pragma solidity ^0.5.0;
import "truffle/Assert.sol";
import "../contracts/MetaCoin.sol";
contract TestMetacoin {
  function testBal() public {
    MetaCoin meta = new MetaCoin();
    uint expected = 5;
    Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin iitially");
  }
}
EOL

cat > tmp/contracts/MetaCoin.sol <<EOL
pragma solidity ^0.5.0;
contract MetaCoin {
  function getBalance(address) public pure returns(uint) {
    return 5;
  }
}
EOL

(cd tmp && npx truffle test)
rm -rf tmp

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

No branches or pull requests

1 participant