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 27, 2021
2 parents d77a700 + 886845d commit f07fcc2
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 4 deletions.
83 changes: 79 additions & 4 deletions packages/truffle/test/scenarios/compile/compile.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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 () {
let config, artifactPaths, initialTimes, finalTimes, output;
Expand Down Expand Up @@ -39,21 +42,21 @@ 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);
}

function hasBeenUpdated(fileName) {
Expand Down Expand Up @@ -320,3 +323,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
}
};
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,36 @@
utf8 "^3.0.0"
web3-utils "1.2.9"

"@truffle/db@^0.2.0-1":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@truffle/db/-/db-0.2.0.tgz#e60ea32e2dba6fd7a30bb25631db878cd2c0172f"
integrity sha512-0s6tobOkHWCJbQFz6C1p1ex87RB+ltyriZGWgT3YqQjhtlu87snIWENQhCSZc6W6NDQO/h3XaTIoqmOsDuwVMA==
dependencies:
"@truffle/abi-utils" "^0.1.1"
"@truffle/code-utils" "^1.2.22"
"@truffle/compile-common" "^0.5.0"
"@truffle/config" "^1.2.33"
"@truffle/workflow-compile" "^3.1.2"
apollo-server "^2.18.2"
debug "^4.2.0"
fse "^4.0.1"
graphql "^15.3.0"
graphql-tag "^2.11.0"
graphql-tools "^6.2.4"
jsondown "^1.0.0"
leveldown "^5.2.0"
module-alias "^2.1.0"
pascal-case "^2.0.1"
pluralize "^8.0.0"
pouchdb "7.1.1"
pouchdb-adapter-memory "^7.1.1"
pouchdb-adapter-node-websql "^7.0.0"
pouchdb-debug "^7.1.1"
pouchdb-find "^7.0.0"
source-map-support "^0.5.9"
web3 "1.2.9"
web3-utils "1.2.9"

"@trufflesuite/chromafi@^2.2.2":
version "2.2.2"
resolved "https://registry.yarnpkg.com/@trufflesuite/chromafi/-/chromafi-2.2.2.tgz#d3fc507aa8504faffc50fb892cedcfe98ff57f77"
Expand Down

0 comments on commit f07fcc2

Please sign in to comment.