diff --git a/.github/scripts/contract-test-mapping.json b/.github/scripts/contract-test-mapping.json new file mode 100644 index 0000000..0bd1e18 --- /dev/null +++ b/.github/scripts/contract-test-mapping.json @@ -0,0 +1,5 @@ +{ + "contracts/FeralFileAirdropV1.sol": ["test/feralfile_airdrop_v1.js"], + "contracts/FeralfileArtworkV3_3.sol": ["test/feralfile_exhibition_v3_3.js"], + "contracts/OwnerData.sol": ["test/owner_data.js"] +} \ No newline at end of file diff --git a/.github/scripts/detect_and_test_changes.sh b/.github/scripts/detect_and_test_changes.sh new file mode 100755 index 0000000..fe398d3 --- /dev/null +++ b/.github/scripts/detect_and_test_changes.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +echo "Changed files: $CHANGED_FILES" + +# Read the mapping file +declare -A MAPPING + +# Use jq to iterate over each key-value pair in the JSON object. +while IFS= read -r line; do + contract=$(jq -r '.contract' <<< "$line") + IFS=$'\n' read -r -d '' -a tests < <(jq -r '.tests[]' <<< "$line" && printf '\0') + + # Check if tests array is empty + if [ ${#tests[@]} -eq 0 ]; then + echo "Error: No tests found for contract $contract." + exit 1 + fi + + # Add each test to the mapping for the contract. + for test in "${tests[@]}"; do + MAPPING["$contract"]+="$test " + done +done < <(jq -c 'to_entries[] | {contract: .key, tests: .value}' .github/scripts/contract-test-mapping.json) + + +# Initialize a variable to hold test files to run +TEST_FILES="" + +# Loop through changed files and append relevant test files +for FILE in $CHANGED_FILES; do + if [[ -n "${MAPPING[$FILE]}" ]]; then + TEST_FILES+="${MAPPING[$FILE]}" + fi +done + +# Remove duplicate test files +TEST_FILES=$(echo "$TEST_FILES" | tr ' ' '\n' | sort -u | tr '\n' ' ') + +# Run Truffle tests if there are any test files to run +if [ -n "$TEST_FILES" ]; then + echo "Running tests for changed contracts:" + echo $TEST_FILES + truffle test $TEST_FILES +else + echo "No contract changes detected." +fi \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..566fb37 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,64 @@ +name: Solidity Test + +on: + pull_request: + branches: [ main ] + paths: + - '**/*.sol' + +jobs: + solidity-lint: + name: Test Solidity + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Node.js + uses: actions/setup-node@v1 + with: + node-version: '18' + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v42 + + - name: Install dependencies + env: + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + TARGET_BRANCH: ${{ github.base_ref }} + run: | + npm install + npm install -g truffle ganache + ganache -D -p 7545 + + - name: Write secret.json file + env: + MNEMONIC: ${{ secrets.MNEMONIC }} + MAINNET_ENDPOINT: ${{ secrets.MAINNET_ENDPOINT }} + GOERLI_ENDPOINT: ${{ secrets.GOERLI_ENDPOINT }} + BSCTEST_ENDPOINT: ${{ secrets.BSCTEST_ENDPOINT }} + BSC_ENDPOINT: ${{ secrets.BSC_ENDPOINT }} + ETHERSCAN_API: ${{ secrets.ETHERSCAN_API }} + BSC_API: ${{ secrets.BSC_API }} + run: | + cat < .secret.json + { + "mnemonic": "${MNEMONIC}", + "mainnet_endpoint": "${MAINNET_ENDPOINT}", + "goerli_endpoint": "${GOERLI_ENDPOINT}", + "bsctest_endpoint": "${BSCTEST_ENDPOINT}", + "bsc_endpoint": "${BSC_ENDPOINT}", + "etherscan_api": "${ETHERSCAN_API}", + "bsc_api": "${BSC_API}" + } + EOF + + - name: Test + env: + COINMARKETCAP_API_KEY: ${{ secrets.COINMARKETCAP_API_KEY }} + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + TARGET_BRANCH: ${{ github.base_ref }} + run: .github/scripts/detect_and_test_changes.sh \ No newline at end of file diff --git a/package.json b/package.json index b94e6ee..6dbfadd 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "@openzeppelin/test-helpers": "^0.5.16", "coveralls": "^3.1.1", + "eth-gas-reporter": "^0.2.27", "ganache-cli": "^6.12.2", "solidity-coverage": "^0.7.22", "truffle": "^5.9.2", diff --git a/truffle-config.js b/truffle-config.js index d5cbe72..9035a6e 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -106,6 +106,13 @@ module.exports = { // Set default mocha options here, use special reporters etc. mocha: { // timeout: 100000 + reporter: 'eth-gas-reporter', + reporterOptions : { + currency: 'USD', + gasPriceApi: 'https://api.etherscan.io/api?module=proxy&action=eth_gasPrice', + token: 'ETH', + coinmarketcap: process.env.COINMARKETCAP_API_KEY + } }, // Configure your compilers