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

Commit

Permalink
Merge pull request #3461 from trufflesuite/add-compile-db-scenario
Browse files Browse the repository at this point in the history
Scenario Test for DB load on compile
  • Loading branch information
fainashalts authored and g. nicholas d'andrea committed Jan 7, 2021
2 parents e224bba + 443f7db commit 71a3881
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 13 deletions.
101 changes: 88 additions & 13 deletions packages/truffle/test/scenarios/compile/compile.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const MemoryLogger = require("../memorylogger");
const CommandRunner = require("../commandrunner");
const fs = require("fs");
const path = require("path");
const assert = require("assert");
const Server = require("../server");
const Reporter = require("../reporter");
const sandbox = require("../sandbox");
const log = console.log;
const fse = require("fs-extra");
const { connect } = require("@truffle/db");
const gql = require("graphql-tag");
const pascalCase = require("pascal-case");

describe("Repeated compilation of contracts with inheritance [ @standalone ]", function() {
describe("Repeated compilation of contracts with inheritance [ @standalone ]", function () {
let config,
sourcePaths,
artifactPaths,
Expand Down Expand Up @@ -44,34 +47,34 @@ describe("Repeated compilation of contracts with inheritance [ @standalone ]", f
}

function getSource(key) {
return fs.readFileSync(mapping[key].sourcePath);
return fse.readFileSync(mapping[key].sourcePath);
}

function getArtifactStats() {
const stats = {};
names.forEach(key => {
const mDate = fs.statSync(mapping[key].artifactPath).mtime.getTime();
const mDate = fse.statSync(mapping[key].artifactPath).mtime.getTime();
stats[key] = mDate;
});
return stats;
}

function touchSource(key) {
const source = getSource(key);
fs.writeFileSync(mapping[key].sourcePath, source);
fse.writeFileSync(mapping[key].sourcePath, source);
}

// ----------------------- Setup -----------------------------

before("set up the server", function(done) {
before("set up the server", function (done) {
Server.start(done);
});

after("stop server", function(done) {
after("stop server", function (done) {
Server.stop(done);
});

beforeEach("set up sandbox and do initial compile", async function() {
beforeEach("set up sandbox and do initial compile", async function () {
this.timeout(30000);

const conf = await sandbox.create(project);
Expand Down Expand Up @@ -119,7 +122,7 @@ describe("Repeated compilation of contracts with inheritance [ @standalone ]", f
// LeafB |
// ------------------------------------------------------------

it("Updates only Root when Root is touched", async function() {
it("Updates only Root when Root is touched", async function () {
this.timeout(30000);

touchSource("Root");
Expand Down Expand Up @@ -174,7 +177,7 @@ describe("Repeated compilation of contracts with inheritance [ @standalone ]", f
// LeafB |
// ------------------------------------------------------------

it("Updates Root and Library when Library is touched", async function() {
it("Updates Root and Library when Library is touched", async function () {
this.timeout(30000);

touchSource("LibraryA");
Expand Down Expand Up @@ -229,7 +232,7 @@ describe("Repeated compilation of contracts with inheritance [ @standalone ]", f
// LeafB |
// ------------------------------------------------------------

it("Updates Branch and Root when Branch is touched", async function() {
it("Updates Branch and Root when Branch is touched", async function () {
this.timeout(30000);

touchSource("Branch");
Expand Down Expand Up @@ -284,7 +287,7 @@ describe("Repeated compilation of contracts with inheritance [ @standalone ]", f
// LeafB |
// ------------------------------------------------------------

it("Updates LeafA, Branch and Root when LeafA is touched", async function() {
it("Updates LeafA, Branch and Root when LeafA is touched", async function () {
this.timeout(30000);

touchSource("LeafA");
Expand Down Expand Up @@ -339,7 +342,7 @@ describe("Repeated compilation of contracts with inheritance [ @standalone ]", f
// LeafB* |
// ------------------------------------------------------------

it("Updates everything except LibraryA when LeafC is touched", async function() {
it("Updates everything except LibraryA when LeafC is touched", async function () {
this.timeout(30000);

touchSource("LeafC");
Expand Down Expand Up @@ -385,3 +388,75 @@ describe("Repeated compilation of contracts with inheritance [ @standalone ]", f
}
});
});

describe("Compilation with db enabled", async () => {
let config, project;
const logger = new MemoryLogger();

function checkForDb(config) {
const dbPath = path.join(config.working_directory, ".db");

const dbExists = fse.pathExistsSync(dbPath);
return dbExists;
}

before("set up the server", function (done) {
Server.start(done);
});

after("stop server", function (done) {
Server.stop(done);
});

beforeEach("set up sandbox and do initial compile", async function () {
this.timeout(30000);

project = path.join(__dirname, "../../sources/db_enabled");
config = await sandbox.create(project);

try {
await CommandRunner.run("compile", config);
} catch (error) {
output = logger.contents();
log(output);
throw new Error(error);
}
});

it("creates a populated .db directory when db is enabled", async function () {
this.timeout(12000);

const dbExists = checkForDb(config);

assert(dbExists === true);
});

it("adds contracts to the db", async function () {
this.timeout(12000);

const GetAllContracts = gql`
query getAllContracts {
contracts {
name
}
}
`;

// connect to DB
const db = connect(config);
const results = await db.execute(GetAllContracts, {});

// number of contracts matches number of contracts in the project directory
// (plus one library in this one)
assert(results.data.contracts.length === 4);

// contract names in project exist in new .db contracts file
const resultsNames = results.data.contracts.map(a => a.name);

const contractNames = fse.readdirSync(path.join(project, "contracts"));
contractNames.map(name => {
const processedName = pascalCase(name.split(".")[0]);
assert(resultsNames.includes(processedName));
});
});
});
23 changes: 23 additions & 0 deletions packages/truffle/test/sources/db_enabled/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.5.0;

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);
}
}
11 changes: 11 additions & 0 deletions packages/truffle/test/sources/db_enabled/contracts/contract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma solidity ^0.5.0;

// This file defines a library that can be used as well.
library InnerLibrary {

}

// This name doesn't match its filename.
contract Contract {
uint public specialValue = 1337;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pragma solidity ^0.5.0;

import "./contract.sol";

contract RelativeImport is Contract {

}
14 changes: 14 additions & 0 deletions packages/truffle/test/sources/db_enabled/truffle-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*",
gas: 4700000,
gasPrice: 20000000000
}
},
db: {
enabled: true
}
};

0 comments on commit 71a3881

Please sign in to comment.