Skip to content

Commit 2474216

Browse files
committed
refactor: centralize coverage detection with type-safe helper
- Replace manual environment variable management with hre.__SOLIDITY_COVERAGE_RUNNING - Add centralized isRunningUnderCoverage() utility function - Simplify test coverage detection using clean if statements - Remove duplicate .solcover.js file (keep only test/.solcover.js) - Update coverage configuration for better reporting
1 parent 373e2e9 commit 2474216

File tree

10 files changed

+36
-90
lines changed

10 files changed

+36
-90
lines changed

packages/contracts/.solcover.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
],
2929
"scripts": {
3030
"prepack": "pnpm build",
31-
"clean": "rm -rf artifacts/ cache/ types/ abis/ build/ dist/",
31+
"clean": "rm -rf artifacts/ cache/ types/ abis/ build/ dist/ coverage/",
3232
"build": "pnpm build:self",
3333
"build:self": "pnpm compile",
3434
"compile": "hardhat compile",

packages/contracts/test/.solcover.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@ module.exports = {
66
network_id: 1337,
77
},
88
skipFiles,
9-
// Use default istanbulFolder: './coverage'
9+
istanbulFolder: '../coverage',
1010
// Remove 'html' reporter to avoid duplicates, keep lcov for lcov.info
1111
istanbulReporter: ['lcov', 'text', 'json'],
1212
configureYulOptimizer: true,
1313
mocha: {
1414
grep: '@skip-on-coverage',
1515
invert: true,
1616
},
17-
onCompileComplete: async function (/* config */) {
18-
// Set environment variable to indicate we're running under coverage
19-
process.env.SOLIDITY_COVERAGE = 'true'
20-
},
21-
onIstanbulComplete: async function (/* config */) {
22-
// Clean up environment variable
23-
delete process.env.SOLIDITY_COVERAGE
24-
},
2517
}

packages/contracts/test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"scripts": {
5858
"postinstall": "scripts/setup-symlinks",
59-
"clean": "rm -rf artifacts/ cache/ reports/ types/",
59+
"clean": "rm -rf artifacts/ cache/ types/",
6060
"build": "pnpm build:dep",
6161
"build:dep": "pnpm --filter '@graphprotocol/contracts-tests^...' run build:self",
6262
"test": "pnpm build && pnpm test:self",

packages/contracts/test/scripts/coverage

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ set -eo pipefail
55
echo {} > addresses-local.json
66

77
DISABLE_SECURE_ACCOUNTS=true \
8-
SOLIDITY_COVERAGE=true \
98
L1_GRAPH_CONFIG=config/graph.hardhat.yml \
109
L2_GRAPH_CONFIG=config/graph.arbitrum-hardhat.yml \
1110
ADDRESS_BOOK=addresses-local.json \

packages/contracts/test/tests/unit/gns.test.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { BigNumber, ContractTransaction, ethers, Event } from 'ethers'
2727
import { defaultAbiCoder } from 'ethers/lib/utils'
2828
import hre from 'hardhat'
2929

30+
import { isRunningUnderCoverage } from '../../utils/coverage'
3031
import { NetworkFixture } from './lib/fixtures'
3132
import {
3233
AccountDefaultName,
@@ -787,16 +788,9 @@ describe.skip('L1GNS @skip-on-coverage', () => {
787788
const tx = gns.connect(me).multicall([bogusPayload, tx2.data])
788789

789790
// Under coverage, the error message may be different due to instrumentation
790-
const isRunningUnderCoverage =
791-
hre.network.name === 'coverage' ||
792-
process.env.SOLIDITY_COVERAGE === 'true' ||
793-
process.env.npm_lifecycle_event === 'test:coverage'
794-
795-
if (isRunningUnderCoverage) {
796-
// Under coverage, the transaction should still revert, but the message might be empty
791+
if (isRunningUnderCoverage()) {
797792
await expect(tx).to.be.reverted
798793
} else {
799-
// Normal test run should have the specific error message
800794
await expect(tx).revertedWith("function selector was not recognized and there's no fallback function")
801795
}
802796
})
@@ -1295,14 +1289,8 @@ describe.skip('L1GNS @skip-on-coverage', () => {
12951289
await expect(tx2).revertedWith('NO_SIGNAL')
12961290
})
12971291
it('sets the curator signal to zero so they cannot withdraw', async function () {
1298-
// Check if we're running under coverage
1299-
const isRunningUnderCoverage =
1300-
hre.network.name === 'coverage' ||
1301-
process.env.SOLIDITY_COVERAGE === 'true' ||
1302-
process.env.npm_lifecycle_event === 'test:coverage'
1303-
1304-
if (isRunningUnderCoverage) {
1305-
// Under coverage, skip this test as it has issues with BigNumber values
1292+
// Under coverage, skip this test as it has issues with BigNumber values
1293+
if (isRunningUnderCoverage()) {
13061294
this.skip()
13071295
return
13081296
}
@@ -1328,14 +1316,8 @@ describe.skip('L1GNS @skip-on-coverage', () => {
13281316
await expect(tx).revertedWith('GNS: No signal to withdraw GRT')
13291317
})
13301318
it('gives each curator an amount of tokens proportional to their nSignal', async function () {
1331-
// Check if we're running under coverage
1332-
const isRunningUnderCoverage =
1333-
hre.network.name === 'coverage' ||
1334-
process.env.SOLIDITY_COVERAGE === 'true' ||
1335-
process.env.npm_lifecycle_event === 'test:coverage'
1336-
1337-
if (isRunningUnderCoverage) {
1338-
// Under coverage, skip this test as it has issues with BigNumber values
1319+
// Under coverage, skip this test as it has issues with BigNumber values
1320+
if (isRunningUnderCoverage()) {
13391321
this.skip()
13401322
return
13411323
}

packages/contracts/test/tests/unit/l2/l2GraphTokenGateway.test.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { expect, use } from 'chai'
66
import { constants, ContractTransaction, Signer, utils, Wallet } from 'ethers'
77
import hre from 'hardhat'
88

9+
import { isRunningUnderCoverage } from '../../../utils/coverage'
910
import { NetworkFixture } from '../lib/fixtures'
1011

1112
use(smock.matchers)
@@ -80,12 +81,7 @@ describe('L2GraphTokenGateway', () => {
8081
await fixture.setUp()
8182
// Thanks to Livepeer: https://github.com/livepeer/arbitrum-lpt-bridge/blob/main/test/unit/L2/l2LPTGateway.test.ts#L86
8283
// Skip smock setup when running under coverage due to provider compatibility issues
83-
const isRunningUnderCoverage =
84-
hre.network.name === 'coverage' ||
85-
process.env.SOLIDITY_COVERAGE === 'true' ||
86-
process.env.npm_lifecycle_event === 'test:coverage'
87-
88-
if (!isRunningUnderCoverage) {
84+
if (!isRunningUnderCoverage()) {
8985
arbSysMock = await smock.fake('ArbSys', {
9086
address: '0x0000000000000000000000000000000000000064',
9187
})
@@ -237,12 +233,7 @@ describe('L2GraphTokenGateway', () => {
237233
// expect(arbSysMock.sendTxToL1).to.have.been.calledOnce
238234

239235
// Only check smock expectations when not running under coverage
240-
const isRunningUnderCoverage =
241-
hre.network.name === 'coverage' ||
242-
process.env.SOLIDITY_COVERAGE === 'true' ||
243-
process.env.npm_lifecycle_event === 'test:coverage'
244-
245-
if (!isRunningUnderCoverage && arbSysMock) {
236+
if (!isRunningUnderCoverage() && arbSysMock) {
246237
expect(arbSysMock.sendTxToL1).to.have.been.calledWith(l1GRTGatewayMock.address, calldata)
247238
}
248239
const senderBalance = await grt.balanceOf(tokenSender.address)
@@ -271,14 +262,8 @@ describe('L2GraphTokenGateway', () => {
271262
await expect(tx).revertedWith('TOKEN_NOT_GRT')
272263
})
273264
it('burns tokens and triggers an L1 call', async function () {
274-
// Check if we're running under coverage
275-
const isRunningUnderCoverage =
276-
hre.network.name === 'coverage' ||
277-
process.env.SOLIDITY_COVERAGE === 'true' ||
278-
process.env.npm_lifecycle_event === 'test:coverage'
279-
280-
if (isRunningUnderCoverage) {
281-
// Skip this test under coverage due to complex instrumentation issues
265+
// Skip this test under coverage due to complex instrumentation issues
266+
if (isRunningUnderCoverage()) {
282267
this.skip()
283268
return
284269
}
@@ -287,14 +272,8 @@ describe('L2GraphTokenGateway', () => {
287272
await testValidOutboundTransfer(tokenSender, defaultData)
288273
})
289274
it('decodes the sender address from messages sent by the router', async function () {
290-
// Check if we're running under coverage
291-
const isRunningUnderCoverage =
292-
hre.network.name === 'coverage' ||
293-
process.env.SOLIDITY_COVERAGE === 'true' ||
294-
process.env.npm_lifecycle_event === 'test:coverage'
295-
296-
if (isRunningUnderCoverage) {
297-
// Skip this test under coverage due to complex instrumentation issues
275+
// Skip this test under coverage due to complex instrumentation issues
276+
if (isRunningUnderCoverage()) {
298277
this.skip()
299278
return
300279
}

packages/contracts/test/tests/unit/lib/fixtures.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
2929
import { providers, Wallet } from 'ethers'
3030

31+
import { isRunningUnderCoverage } from '../../../utils/coverage'
32+
3133
export interface L1FixtureContracts {
3234
controller: Controller
3335
disputeManager: DisputeManager
@@ -72,7 +74,7 @@ export class NetworkFixture {
7274

7375
async load(deployer: SignerWithAddress, l2Deploy?: boolean): Promise<GraphNetworkContracts> {
7476
// Use instrumented artifacts when running coverage tests, otherwise use local artifacts
75-
const artifactsDir = process.env.SOLIDITY_COVERAGE ? './artifacts' : '../artifacts'
77+
const artifactsDir = isRunningUnderCoverage() ? './artifacts' : '../artifacts'
7678

7779
const contracts = await deployGraphNetwork(
7880
'addresses-local.json',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import hre from 'hardhat'
2+
3+
/**
4+
* Utility functions for detecting and handling coverage test execution
5+
*/
6+
7+
/**
8+
* Checks if tests are currently running under solidity-coverage instrumentation
9+
* @returns true if running under coverage, false otherwise
10+
*/
11+
export function isRunningUnderCoverage(): boolean {
12+
return hre.__SOLIDITY_COVERAGE_RUNNING === true
13+
}

packages/horizon/test/unit/staking/slash/slash.t.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ contract HorizonStakingSlashTest is HorizonStakingTest {
172172
vm.assume(delegationTokensToSlash <= delegationTokens);
173173
vm.assume(delegationTokensToUndelegate <= delegationTokens);
174174
vm.assume(delegationTokensToUndelegate > 0);
175-
// Ensure that after undelegating, either we undelegate everything or leave at least MIN_DELEGATION
176-
vm.assume(delegationTokensToUndelegate == delegationTokens || delegationTokens - delegationTokensToUndelegate >= MIN_DELEGATION);
175+
vm.assume(
176+
delegationTokensToUndelegate == delegationTokens ||
177+
MIN_DELEGATION <= delegationTokens - delegationTokensToUndelegate
178+
);
177179

178180
resetPrank(users.delegator);
179181
_delegate(users.indexer, subgraphDataServiceAddress, delegationTokens, 0);

0 commit comments

Comments
 (0)