Skip to content
This repository was archived by the owner on Jun 14, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions lib/commands/compile.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,94 @@
var format = JSON.stringify;

var command = {
command: 'compile',
description: 'Compile contract source files',
builder: {
all: {
type: "boolean",
default: false
},
list: {
type: "string",
},
help: {
type: "boolean",
default: "false"
}
},

run: function (options, done) {
var Config = require("truffle-config");
var Contracts = require("truffle-workflow-compile");
var CompilerProvider = require("truffle-compile").CompilerProvider;
var provider = new CompilerProvider();

var config = Config.detect(options);
Contracts.compile(config, done);
}

(config.list !== undefined)
? command.listVersions(provider, config, done)
: Contracts.compile(config, done);
},

listVersions: function(provider, options, done){
const log = options.logger.log;
options.list = (options.list.length) ? options.list : "releases";

// Help
if (options.list && options.help){
log(command.help);
return done();
}

// Docker tags
if (options.list === 'docker'){
return provider
.getDockerTags()
.then(tags => {
tags.push('See more at: hub.docker.com/r/ethereum/solc/tags/')
log(format(tags));
done();
})
.catch(done);
}

// Solcjs releases
provider
.getReleases()
.then(releases => {
const shortener = options.all ? null : command.shortener;
const list = format(releases[options.list], shortener, ' ');
log(list);
done();
})
.catch(done);
},

shortener: function(key, val){
const defaultLength = 10;

if (Array.isArray(val) && val.length > defaultLength){
const length = val.length;
const remaining = length - defaultLength;
const more = '.. and ' + remaining + ' more. Use `--all` to see full list.';
val.length = defaultLength;
val.push(more);
}

return val;
},

help: "\n" +
"See available solc versions. (Default: solcjs stable releases)\n\n" +

"USAGE:\n" +
" --list [option] [--all]\n\n" +

"OPTIONS:\n" +
" `docker` recently published docker tags\n" +
" `releases` solcjs stable releases\n" +
" `prereleases` solcjs nightly builds\n" +
" `latestRelease` solcjs latest\n\n",
}

module.exports = command;
1 change: 1 addition & 0 deletions lib/testing/soliditytest.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ var SolidityTest = {

compile.with_dependencies(runner.config.with({
paths: [
"truffle/Assert.sol",
"truffle/DeployedAddresses.sol",
path.join(__dirname, "SafeSend.sol")
],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
],
"devDependencies": {
"ipfsd-ctl": "^0.21.0",
"memorystream": "^0.3.1",
"truffle-blockchain-utils": "^0.0.4"
}
}
120 changes: 98 additions & 22 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ var Box = require("truffle-box");
var Contracts = require("truffle-workflow-compile");
var Artifactor = require("truffle-artifactor");
var Resolver = require("truffle-resolver");
var MemoryStream = require('memorystream');
var command = require('../lib/commands/compile');
var path = require("path");
var fs = require("fs");

describe("compile", function() {
var config;
var output = '';
var memStream;

before("Create a sandbox", function(done) {
this.timeout(10000);

Box.sandbox(function(err, result) {
if (err) return done(err);
config = result;
Expand All @@ -25,10 +30,13 @@ describe("compile", function() {
}
};
config.network = "default";
config.logger = {log: (val) => val && memStream.write(val)};
done();
});
});

afterEach("Clear MemoryStream", () => output = '');

it('compiles all initial contracts', function(done) {
this.timeout(10000);

Expand Down Expand Up @@ -57,10 +65,10 @@ describe("compile", function() {
});
});

it('compiles updated contract and descendents', function(done) {
it('compiles updated contract and its ancestors', function(done) {
this.timeout(10000);

var file_to_update = path.resolve(path.join(config.contracts_directory, "MetaCoin.sol"));
var file_to_update = path.resolve(path.join(config.contracts_directory, "ConvertLib.sol"));
var stat = fs.statSync(file_to_update);

// Update the modification time to simulate an edit.
Expand All @@ -82,36 +90,104 @@ describe("compile", function() {
});
});

it('compiles updated contract and its ancestors', function(done) {
this.timeout(10000);
it("compiling shouldn't create any network artifacts", function() {
var contract = config.resolver.require("MetaCoin.sol");
assert.equal(Object.keys(contract.networks).length, 0, "Expected the contract to be managing zero networks");
});

var file_to_update = path.resolve(path.join(config.contracts_directory, "ConvertLib.sol"));
var stat = fs.statSync(file_to_update);
describe('solc listing options', function(){

// Update the modification time to simulate an edit.
var newTime = new Date().getTime();
fs.utimesSync(file_to_update, newTime, newTime);
beforeEach(() => {
memStream = new MemoryStream();
memStream.on('data', function(data){ output += data.toString()});
})

Contracts.compile(config.with({
all: false,
quiet: true
}), function(err, contracts) {
if (err) return done(err);
it("prints a truncated list of solcjs versions", function(done){
this.timeout(5000);

assert.equal(Object.keys(contracts).length, 2, "Expected MetaCoin and ConvertLib to be compiled");
const options = {
list: ""
};

// reset time
fs.utimesSync(file_to_update, stat.atime, stat.mtime);
command.run(config.with(options), (err, result) => {
if(err) return done(err);

done();
memStream.on('end', function() {
const arr = JSON.parse(output);
assert(arr.length === 11);
done();
});

memStream.end('');
})
})

it("prints a list of docker tags", function(done){
this.timeout(5000);

const options = {
list: "docker"
};

command.run(config.with(options), (err, result) => {
if(err) return done(err);

memStream.on('end', function() {
const arr = JSON.parse(output);
assert(arr.length === 11);
assert(typeof arr[0] === 'string');
done();
});

memStream.end('');
})
})

it("prints a full list of releases when --all is set", function(done){
this.timeout(5000);

const options = {
list: "releases",
all: true
};

command.run(config.with(options), (err, result) => {
if(err) return done(err);

memStream.on('end', function() {
const arr = JSON.parse(output);
assert(arr.length > 11);
assert(typeof arr[0] === 'string');
done();
});

memStream.end('');
})
});
});

it("compiling shouldn't create any network artifacts", function() {
var contract = config.resolver.require("MetaCoin.sol");
assert.equal(Object.keys(contract.networks).length, 0, "Expected the contract to be managing zero networks");
it("prints a help when list and help are set", function(done){
this.timeout(5000);

const options = {
list: "releases",
help: true
};

command.run(config.with(options), (err, result) => {
if(err) return done(err);

memStream.on('end', function() {
assert(output.includes('USAGE'));
done();
});

memStream.end('');
})
})
});



// TODO: Kept this as a comment because I'm confused if it applies.
// Since the binary and abi are updated with every compile, and they're not within
// the networks object anymore, it may not matter when that specific network changed.
Expand Down
2 changes: 2 additions & 0 deletions test/ethpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ describe('EthPM integration', function() {
assert.isNotNull(contracts["transferable"]);

fs.readdir(config.contracts_build_directory, function(err, files) {
if (err) return done(err);

var found = [false, false];
var search = ["owned", "transferable"];

Expand Down
2 changes: 1 addition & 1 deletion test/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('NPM integration', function() {
var parentContractSource = "pragma solidity ^0.4.2; import 'fake_source/contracts/Module.sol'; contract Parent {}";

before("Create a sandbox", function(done) {
this.timeout(10000);
this.timeout(15000);
Box.sandbox(function(err, result) {
if (err) return done(err);
config = result;
Expand Down
7 changes: 4 additions & 3 deletions test/profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ describe('profiler', function() {

it('profiles example project successfully', function(done) {
Profiler.required_sources(config.with({
paths: ["./MetaCoin.sol"],
paths: ["./ConvertLib.sol"],
base_path: config.contracts_directory
}), function(err, result) {
}), function(err, allSources, compilationTargets) {
if (err) return done(err);

assert.equal(Object.keys(result).length, 2);
assert.equal(Object.keys(allSources).length, 3);
assert.equal(compilationTargets.length, 2);
done();
});
});
Expand Down