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 #3418 from trufflesuite/respect-compilations
Browse files Browse the repository at this point in the history
Internal improvement: Separate multiple compilations in testing & factor doing so
  • Loading branch information
haltman-at authored Oct 7, 2020
2 parents 1aa9f2c + 38c2fc4 commit ad69387
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 57 deletions.
64 changes: 50 additions & 14 deletions packages/codec/lib/compilations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,54 @@ const debug = debugModule("codec:compilations:utils");
import * as Ast from "@truffle/codec/ast";
import * as Compiler from "@truffle/codec/compiler";
import { ContractObject as Artifact } from "@truffle/contract-schema/spec";
import { CompiledContract } from "@truffle/compile-common";
import {
CompiledContract,
Compilation as CompilerCompilation
} from "@truffle/compile-common";
import { Compilation, Contract, Source } from "./types";

export function shimCompilations(
inputCompilations: CompilerCompilation[],
shimmedCompilationIdPrefix = "shimmedcompilation",
externalSolidity = false
): Compilation[] {
return inputCompilations.map(
({ contracts, sourceIndexes }, compilationIndex) =>
shimContracts(
contracts,
sourceIndexes,
`${shimmedCompilationIdPrefix}Number(${compilationIndex})`,
externalSolidity
)
);
}

/**
* wrapper around shimArtifactsToCompilation that just returns
* the result in a one-element array (keeping the old name
* shimArtifacts for compatibility)
*/
export function shimArtifacts(
artifacts: (Artifact | CompiledContract)[],
files?: string[],
shimmedCompilationId = "shimmedcompilation",
externalSolidity = false
shimmedCompilationId: string = "shimmedcompilation",
externalSolidity: boolean = false
): Compilation[] {
//note: always returns a one-element array (a single fictional compilation)
return [
shimContracts(artifacts, files, shimmedCompilationId, externalSolidity)
];
}

/**
* shims a bunch of contracts ("artifacts", though not necessarily)
* to a compilation. usually used via one of the above two functions.
*/
export function shimContracts(
artifacts: (Artifact | CompiledContract)[],
files?: string[],
shimmedCompilationId: string = "shimmedcompilation",
externalSolidity: boolean = false
): Compilation {
let contracts: Contract[] = [];
let sources: Source[] = [];
let unreliableSourceOrder: boolean = false;
Expand Down Expand Up @@ -130,16 +168,14 @@ export function shimArtifacts(
compiler = contracts[0].compiler;
}

return [
{
id: shimmedCompilationId,
unreliableSourceOrder,
sources,
contracts,
compiler,
externalSolidity
}
];
return {
id: shimmedCompilationId,
unreliableSourceOrder,
sources,
contracts,
compiler,
externalSolidity
};
}

function sourceIndexForAst(ast: Ast.AstNode): number | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/compile-common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Abi, ImmutableReferences } from "@truffle/contract-schema/spec";

export type Compilation = {
sourceIndexes: string[];
sourceIndexes: string[]; //note: doesnot include internal sources
contracts: CompiledContract[];
compiler: {
name: string | undefined;
Expand Down
10 changes: 1 addition & 9 deletions packages/core/lib/debug/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,7 @@ class CLIDebugger {

compileSpinner.succeed();

return [].concat(
...compilationResult.map((compilation, index) =>
Codec.Compilations.Utils.shimArtifacts(
compilation.contracts,
compilation.sourceIndexes,
`shimmedCompilationNumber(${index})`
)
)
);
return Codec.Compilations.Utils.shimCompilations(compilationResult);
}

async startDebugger(compilations) {
Expand Down
16 changes: 5 additions & 11 deletions packages/core/lib/debug/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,13 @@ class DebugExternalHandler {
sources
);
//shim the result
const compilationIdBase = `externalFor(${address})Via(${fetcher.fetcherName})`;
const newCompilations = [].concat(
...compilations.map((compilation, index) =>
Codec.Compilations.Utils.shimArtifacts(
compilation.contracts,
compilation.sourceIndexes,
`${compilationIdBase}Number(${index})`,
true //mark compilation as external Solidity
)
)
const shimmedCompilations = Codec.Compilations.Utils.shimCompilations(
compilations,
`externalFor(${address})Via(${fetcher.fetcherName})`,
true //mark compilations as external Solidity
);
//add it!
await this.bugger.addExternalCompilations(newCompilations);
await this.bugger.addExternalCompilations(shimmedCompilations);
//check: did this actually help?
debug("checking result");
if (!getUnknownAddresses(this.bugger).includes(address)) {
Expand Down
32 changes: 10 additions & 22 deletions packages/core/lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,16 @@ const Test = {

await this.performInitialDeploy(config, testResolver);

const solidityCompilationOutput = compilations.reduce(
(a, compilation) => {
if (compilation.compiler && compilation.compiler.name === "solc") {
a.sourceIndexes = a.sourceIndexes.concat(compilation.sourceIndexes);
a.contracts = a.contracts.concat(compilation.contracts);
}
return a;
},
{ sourceIndexes: [], contracts: [] }
);
const sourcePaths = []
.concat(
...compilations.map(compilation => compilation.sourceIndexes) //we don't need the indices here, just the paths
)
.filter(path => path); //make sure we don't pass in any undefined

await this.defineSolidityTests(
mocha,
testContracts,
solidityCompilationOutput.sourceIndexes,
runner
);
await this.defineSolidityTests(mocha, testContracts, sourcePaths, runner);

const debuggerCompilations = Codec.Compilations.Utils.shimArtifacts(
solidityCompilationOutput.contracts,
solidityCompilationOutput.sourceIndexes
const debuggerCompilations = Codec.Compilations.Utils.shimCompilations(
compilations
);

//for stack traces, we'll need to set up a light-mode debugger...
Expand Down Expand Up @@ -213,9 +202,8 @@ const Test = {
const updated =
(await Profiler.updated(config.with({ resolver: testResolver }))) || [];

const compiler = (config.compileNone || config["--compile-none"])
? "none"
: config.compiler;
const compiler =
config.compileNone || config["--compile-none"] ? "none" : config.compiler;

let compileConfig = config.with({
all: config.compileAll === true,
Expand Down

0 comments on commit ad69387

Please sign in to comment.