Skip to content

Commit

Permalink
Generate specific gambit config for CI
Browse files Browse the repository at this point in the history
  • Loading branch information
gvladika committed Aug 21, 2024
1 parent fe530c2 commit cf405c8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 70 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/gambit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
chmod +x gambit
sudo mv gambit /usr/local/bin/
- name: Verify Gambit Installation
run: gambit --help

- name: Install packages
run: yarn

Expand All @@ -42,8 +39,8 @@ jobs:
solc-select install 0.8.16
solc-select use 0.8.16
- name: Set config file
run: cp ./test-mutation/config-templates/config-ci.json ./test-mutation/config.json
- name: Prepare config file
run: yarn prepare:mutation:ci-config

- name: Run mutation tests
run: |
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"test:e2e:local-env": "yarn hardhat test test-e2e/*",
"test:storage": "./scripts/storage_layout_test.bash",
"test:signatures": "./scripts/signatures_test.bash",
"prepare:mutation:ci-config": "ts-node test-mutation/generateGambitConfig.ts",
"test:mutation": "ts-node test-mutation/gambitTester.ts",
"test:unused:errors": "./test/unused-errors/find_unused_errors.sh",
"deploy:local:token-bridge": "ts-node ./scripts/local-deployment/deployCreatorAndCreateTokenBridge.ts",
Expand Down
65 changes: 0 additions & 65 deletions test-mutation/gambitTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import * as fs from 'fs'
import * as fsExtra from 'fs-extra'

const GAMBIT_OUT = 'gambit_out/'
const NUM_OF_MUTANTS_PER_FILE = 3
const CONFIG_FOR_CI_FILE = 'config_for_github_ci.json'

const TEST_ITEMS = [
'contracts',
Expand Down Expand Up @@ -46,8 +44,6 @@ runMutationTesting().catch(error => {
async function runMutationTesting() {
const startTime = Date.now()

await _generateConfigForGithubCI()

console.log('====== Generating mutants')
const mutants: Mutant[] = await _generateMutants()

Expand Down Expand Up @@ -191,64 +187,3 @@ function _printResults(results: TestResult[]) {
console.log(`Killed: ${killedCount} (${killedPercentage}%)`)
console.log(`Survived: ${survivedCount} (${survivedPercentage}%)`)
}

////////////////////////////////////////////////////////////////////

// GAMBIT CONFIG GENERATION

////////////////////////////////////////////////////////////////////

async function _generateConfigForGithubCI() {
const solidityDirs = [
path.join(__dirname, '../contracts/tokenbridge/ethereum'),
path.join(__dirname, '../contracts/tokenbridge/arbitrum'),
path.join(__dirname, '../contracts/tokenbridge/libraries'),
]
const solidityFiles = await _findSolidityFiles(solidityDirs)

// Construct the JSON array
const jsonConfig = solidityFiles.map(file => ({
filename: file,
sourceroot: '..',
solc_remappings: [
'@openzeppelin=../node_modules/@openzeppelin',
'@arbitrum=../node_modules/@arbitrum',
],
num_mutants: NUM_OF_MUTANTS_PER_FILE,
random_seed: true,
}))

// Write the result to a JSON file
const outputFilePath = path.join(__dirname, CONFIG_FOR_CI_FILE)
await fs.promises.writeFile(
outputFilePath,
JSON.stringify(jsonConfig, null, 2),
'utf-8'
)
console.log(`Generated JSON file: ${outputFilePath}`)
}

async function _findSolidityFiles(directories: string[]): Promise<string[]> {
const solidityFiles: string[] = []

async function exploreDirectory(dir: string): Promise<void> {
const entries = await fs.promises.readdir(dir, { withFileTypes: true })

for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
// Recursively explore subdirectory
await exploreDirectory(fullPath)
} else if (entry.isFile() && path.extname(fullPath) === '.sol') {
const relativePath = path.relative(__dirname, fullPath)
solidityFiles.push(relativePath)
}
}
}

for (const directory of directories) {
await exploreDirectory(directory)
}

return solidityFiles
}
75 changes: 75 additions & 0 deletions test-mutation/generateGambitConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as path from 'path'
import * as fs from 'fs'

const NUM_OF_MUTANTS_PER_FILE = 3
const CONFIG_FOR_CI_FILE = 'config_for_github_ci.json'

genGambitConfigForCI().catch(error => {
console.error('Error while generating Galbit config for CI:', error)
})

async function genGambitConfigForCI() {
// Generate the JSON config file to config_for_github_ci.json
await _generateConfigForGithubCI()

// Move config to config.json
const src = path.join(__dirname, CONFIG_FOR_CI_FILE)
const dest = path.join(__dirname, 'config.json')
await fs.promises.rename(src, dest)
console.log(`Moved ${src} to ${dest}`)
}

async function _generateConfigForGithubCI() {
const solidityDirs = [
path.join(__dirname, '../contracts/tokenbridge/ethereum'),
path.join(__dirname, '../contracts/tokenbridge/arbitrum'),
path.join(__dirname, '../contracts/tokenbridge/libraries'),
]
const solidityFiles = await _findSolidityFiles(solidityDirs)

// Construct the JSON array
const jsonConfig = solidityFiles.map(file => ({
filename: file,
sourceroot: '..',
solc_remappings: [
'@openzeppelin=../node_modules/@openzeppelin',
'@arbitrum=../node_modules/@arbitrum',
],
num_mutants: NUM_OF_MUTANTS_PER_FILE,
random_seed: true,
}))

// Write the result to a JSON file
const outputFilePath = path.join(__dirname, CONFIG_FOR_CI_FILE)
await fs.promises.writeFile(
outputFilePath,
JSON.stringify(jsonConfig, null, 2),
'utf-8'
)
console.log(`Generated JSON file: ${outputFilePath}`)
}

async function _findSolidityFiles(directories: string[]): Promise<string[]> {
const solidityFiles: string[] = []

async function exploreDirectory(dir: string): Promise<void> {
const entries = await fs.promises.readdir(dir, { withFileTypes: true })

for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
// Recursively explore subdirectory
await exploreDirectory(fullPath)
} else if (entry.isFile() && path.extname(fullPath) === '.sol') {
const relativePath = path.relative(__dirname, fullPath)
solidityFiles.push(relativePath)
}
}
}

for (const directory of directories) {
await exploreDirectory(directory)
}

return solidityFiles
}

0 comments on commit cf405c8

Please sign in to comment.