Update: Since this tutorial was published, we have released Ganache a personal blockchain and a replacement to the TestRPC. We have left this tutorial unaltered, but we highly recommend checking out our Working with Ganache page.
This post was originally published by David Burela on his blog Burela's House-o-blog. Big thanks to David for allowing us publish it here!
I have been working on automating the compilation and testing of Ethereum solidity contracts, via the use of Truffle. I’ve got the test results being published back into the portal, allowing me to see on each commit if my code still compiles and passes my tests.
I’m assuming you already have a Truffle project locally that you want to automate the continuous builds & testing on. Follow the tutorial on installing Truffle & TestRPC on Windows.
My final system will allow you to run “truffle test” locally to see standard test output, but will modify the test runner on the server to output it as JUnit format.
The system uses the Visual Studio Team Services (VSTS) build engine to automate this. You can sign up for free, and get unlimited private Git repos.
You can have the code hosted on any Git provider. So either within VSTS itself, or GitHub, BitBucket, etc.
A pre-step is to define the test section in the truffle.js file
mocha: {
reporter: “spec”,
reporterOptions: {
mochaFile: ‘junitresults.xml’
}
}
VSTS does provide hosted build agents, which are generic and can build standard .Net projects, Xamarin, etc. But because we are going to use npm packages installed globally on the box to handle the Truffle builds
$ choco install git -y
$ choco install nodejs.install –y
$ npm install -g npm
$ npm install -g –production windows-build-tools
$ npm install -g ethereumjs-testrpc
$ npm install -g truffle
$ npm install -g mocha
$ npm install -g mocha-junit-reporter
Create a new variable with the path to where the npm global path is, for the user you installed the npm packages on above:
npm.path
C:\Users\<user>\AppData\Roaming\npm
#Setting environment paths
$ENV:Path = $ENV:Path + “;” + $env:npm_path
npm config set prefix $env:npm_path #only needs to be set once, will update for user
#DEBUG
#$env:path
#npm list -g –depth=0
#Display system information
Write-Host “System version information”
Write-Host -nonewline “node version: ” ; node -v
Write-Host -nonewline “npm version: “; npm -v
Write-Host -nonewline “npm prefix: “; npm prefix -g
Write-Host -nonewline “truffle: ” ; truffle version
# remove old test results
rm .\junitresults.xml -ea SilentlyContinue
# Modify the Truffle test runner to use the JUnit reporter
Rename-Item .\truffle.js .\truffle_temp.js
cat .\truffle_temp.js | % { $_ -replace ‘reporter: “spec”‘, ‘reporter: “mocha-junit-reporter”‘ } | Out-File -Encoding ASCII .\truffle.js
rm .\truffle_temp.js
#Setting environment paths
$ENV:Path = $ENV:Path + “;” + $env:npm_path
#Truffle build
truffle compile
#Setting environment paths
$ENV:Path = $ENV:Path + “;” + $env:npm_path
# launch the process
echo “launching TestRPC”
$testrpcProcess = Start-Process testrpc -passthru
# persist the PID to disk and display in logs
$testrpcProcess.Id | Export-CliXml testrpcPID.xml
cat testrpcPID.xml
#Setting environment paths
$ENV:Path = $ENV:Path + “;” + $env:npm_path
# Run the tests
truffle test
#Setting environment paths
$ENV:Path = $ENV:Path + “;” + $env:npm_path
# retrieve the PID and kill the entire processs tree
cat testrpcPID.xml
$testrpcPID = Import-CliXml testrpcPID.xml
taskkill /pid $testrpcPID /F /T
junitresults.xml
Things that I would like to add in the future:
truffle migrate
to push to a Bletchley test environment