diff --git a/README-dev.md b/README-dev.md index 055b0621..3e931043 100644 --- a/README-dev.md +++ b/README-dev.md @@ -38,11 +38,11 @@ If your IDE is reporting Typescript issues after compiling the plugin, you may w A test case is added by creating a directory in a subdirectory of a test group in the `test` directory. E.g. `declare-test` is a test case in the `general-tests` test group. A test case should contain: -- a `check.sh` script which does the testing logic +- a `check.ts` script which does the testing logic - a `network.json` file which specifies on which networks should the test case be run - a `hardhat.config.ts` file will be used -The main testing script is `scripts/test.sh`. It iterates over the test cases the test group specified by the `TEST_SUBDIR` environment variable. +The main testing script is `scripts/test.ts`. It iterates over the test cases the test group specified by the `TEST_SUBDIR` environment variable. ### Executing tests locally @@ -50,7 +50,7 @@ When running tests locally, you probably don't want to run the whole `test.sh` s - positioning yourself in your example repository - configuring the `hardhat.config.ts` -- executing the `check.sh` script (potentially modifying it to address path differences) +- executing the `check.ts` script (potentially modifying it to address path differences) To run all tests, you can use the `test-` scripts defined in `package.json`. For the tests to work, you may need to set the values from `config.json` as environment variables. You should also have the [`jq` CLI tool](https://stedolan.github.io/jq/) installed. diff --git a/package-lock.json b/package-lock.json index b9d5dd8b..650d1d3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "exit-hook": "2.2.1", "form-data": "^4.0.0", "glob": "^7.2.0", + "shelljs": "^0.8.5", "starknet": "^3.15.0" }, "devDependencies": { @@ -22,6 +23,7 @@ "@types/glob": "^7.2.0", "@types/mocha": "^9.0.0", "@types/node": "^16.11.26", + "@types/shelljs": "^0.8.11", "@typescript-eslint/eslint-plugin": "^5.8.1", "@typescript-eslint/parser": "^5.8.1", "chai": "^4.3.4", @@ -1058,6 +1060,16 @@ "@types/node": "*" } }, + "node_modules/@types/shelljs": { + "version": "0.8.11", + "resolved": "https://registry.npmjs.org/@types/shelljs/-/shelljs-0.8.11.tgz", + "integrity": "sha512-x9yaMvEh5BEaZKeVQC4vp3l+QoFj3BXcd4aYfuKSzIIyihjdVARAadYy3SMNIz0WCCdS2vB9JL/U6GQk5PaxQw==", + "dev": true, + "dependencies": { + "@types/glob": "*", + "@types/node": "*" + } + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "5.19.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.19.0.tgz", @@ -3527,6 +3539,14 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "node_modules/interpret": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.4.0.tgz", + "integrity": "sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/io-ts": { "version": "1.10.4", "resolved": "https://registry.npmjs.org/io-ts/-/io-ts-1.10.4.tgz", @@ -4662,8 +4682,7 @@ "node_modules/path-parse": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", - "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", - "dev": true + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" }, "node_modules/path-type": { "version": "4.0.0", @@ -5266,6 +5285,17 @@ "node": ">=8.10.0" } }, + "node_modules/rechoir": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", + "integrity": "sha512-HFM8rkZ+i3zrV+4LQjwQ0W+ez98pApMGM3HUrN04j3CqzPOzl9nmP15Y8YXNm8QHGv/eacOVEjqhmWpkRV0NAw==", + "dependencies": { + "resolve": "^1.1.6" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/regexpp": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/regexpp/-/regexpp-3.2.0.tgz", @@ -5312,7 +5342,6 @@ "version": "1.17.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz", "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==", - "dev": true, "dependencies": { "path-parse": "^1.0.6" }, @@ -5572,6 +5601,22 @@ "node": ">=8" } }, + "node_modules/shelljs": { + "version": "0.8.5", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.8.5.tgz", + "integrity": "sha512-TiwcRcrkhHvbrZbnRcFYMLl30Dfov3HKqzp5tO5b4pt6G/SezKcYhmDg15zXVBswHmctSAQKznqNW2LO5tTDow==", + "dependencies": { + "glob": "^7.0.0", + "interpret": "^1.0.0", + "rechoir": "^0.6.2" + }, + "bin": { + "shjs": "bin/shjs" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/side-channel": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", @@ -7094,6 +7139,16 @@ "@types/node": "*" } }, + "@types/shelljs": { + "version": "0.8.11", + "resolved": "https://registry.npmjs.org/@types/shelljs/-/shelljs-0.8.11.tgz", + "integrity": "sha512-x9yaMvEh5BEaZKeVQC4vp3l+QoFj3BXcd4aYfuKSzIIyihjdVARAadYy3SMNIz0WCCdS2vB9JL/U6GQk5PaxQw==", + "dev": true, + "requires": { + "@types/glob": "*", + "@types/node": "*" + } + }, "@typescript-eslint/eslint-plugin": { "version": "5.19.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.19.0.tgz", @@ -8955,6 +9010,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "interpret": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.4.0.tgz", + "integrity": "sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==" + }, "io-ts": { "version": "1.10.4", "resolved": "https://registry.npmjs.org/io-ts/-/io-ts-1.10.4.tgz", @@ -9831,8 +9891,7 @@ "path-parse": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", - "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", - "dev": true + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" }, "path-type": { "version": "4.0.0", @@ -10283,6 +10342,14 @@ "picomatch": "^2.2.1" } }, + "rechoir": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", + "integrity": "sha512-HFM8rkZ+i3zrV+4LQjwQ0W+ez98pApMGM3HUrN04j3CqzPOzl9nmP15Y8YXNm8QHGv/eacOVEjqhmWpkRV0NAw==", + "requires": { + "resolve": "^1.1.6" + } + }, "regexpp": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/regexpp/-/regexpp-3.2.0.tgz", @@ -10317,7 +10384,6 @@ "version": "1.17.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz", "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==", - "dev": true, "requires": { "path-parse": "^1.0.6" } @@ -10511,6 +10577,16 @@ "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", "dev": true }, + "shelljs": { + "version": "0.8.5", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.8.5.tgz", + "integrity": "sha512-TiwcRcrkhHvbrZbnRcFYMLl30Dfov3HKqzp5tO5b4pt6G/SezKcYhmDg15zXVBswHmctSAQKznqNW2LO5tTDow==", + "requires": { + "glob": "^7.0.0", + "interpret": "^1.0.0", + "rechoir": "^0.6.2" + } + }, "side-channel": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", diff --git a/package.json b/package.json index 4fc3dcb6..8a1e4701 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "exit-hook": "2.2.1", "form-data": "^4.0.0", "glob": "^7.2.0", + "shelljs": "^0.8.5", "starknet": "^3.15.0" }, "peerDependencies": { @@ -50,6 +51,7 @@ "@types/glob": "^7.2.0", "@types/mocha": "^9.0.0", "@types/node": "^16.11.26", + "@types/shelljs": "^0.8.11", "@typescript-eslint/eslint-plugin": "^5.8.1", "@typescript-eslint/parser": "^5.8.1", "chai": "^4.3.4", diff --git a/scripts/assert-contains.py b/scripts/assert-contains.py deleted file mode 100755 index e39a3328..00000000 --- a/scripts/assert-contains.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/python3 - -""" -Asserts that stdin contains the provided as CLI argument. -""" - -import sys - -if len(sys.argv) != 2: - sys.exit(sys.argv[0] + " ") - -pattern = sys.argv[1] -input_content = sys.stdin.read() - -if pattern in input_content: - sys.exit(0) - -print("Pattern not in input", file=sys.stderr) -print("Pattern:", pattern, file=sys.stderr) -print("Input:", input_content, file=sys.stderr) - -sys.exit(1) diff --git a/scripts/check-devnet-is-not-running.sh b/scripts/check-devnet-is-not-running.sh deleted file mode 100755 index 7174d16f..00000000 --- a/scripts/check-devnet-is-not-running.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -set -e - -check_devnet_is_not_running() { - local status - status=$(curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:5050/feeder_gateway/is_alive") || echo "Devnet is not running! $status" - - if [ "$status" != 000 ]; then - echo "Devnet is running and responded with status $status" - exit 1 - fi -} diff --git a/scripts/deploy-funded-cli-account.sh b/scripts/deploy-funded-cli-account.sh deleted file mode 100755 index 9a8d6849..00000000 --- a/scripts/deploy-funded-cli-account.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -set -eu - -if [[ "$NETWORK" != "devnet" ]]; then - echo "$0 only works with NETWORK set to devnet" - exit 1 -fi - -ACCOUNT_FILE="$ACCOUNT_DIR/starknet_open_zeppelin_accounts.json" - -npx hardhat starknet-new-account --starknet-network "$NETWORK" --wallet OpenZeppelin -ACCOUNT_ADDRESS=$(jq -r .$NETWORK.OpenZeppelin.address $ACCOUNT_FILE) - -echo "Funding $ACCOUNT_ADDRESS" -# assumes address, don't hardcode this in the future -curl 127.0.0.1:5050/mint \ - -H "Content-Type: application/json" \ - -d "{ \"address\": \"$ACCOUNT_ADDRESS\", \"amount\": 1000000000000000000, \"lite\": true }" - -npx hardhat starknet-deploy-account --starknet-network "$NETWORK" --wallet OpenZeppelin diff --git a/scripts/test.sh b/scripts/test.sh index dd67603f..36d37e8c 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -69,7 +69,11 @@ function iterate_dir() { [ "$network" == "devnet" ] && ../scripts/run-devnet.sh - NETWORK="$network" "$test_case/check.sh" && success=$((success + 1)) || echo "Test failed!" + # check if test_case/check.ts exists + if [ -f "$test_case/check.ts" ]; then + # run the test + NETWORK="$network" npx mocha -r ts-node/register "$test_case/check.ts" && success=$((success + 1)) || echo "Test failed!" + fi rm -rf starknet-artifacts git checkout --force diff --git a/test/configuration-tests/with-account-compilation-option/check.sh b/test/configuration-tests/with-account-compilation-option/check.sh deleted file mode 100755 index 894c6295..00000000 --- a/test/configuration-tests/with-account-compilation-option/check.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -set -e - -CONTRACT_NAME=dummy_account.cairo -CONTRACT_PATH="contracts/$CONTRACT_NAME" - -cp "$(dirname "$0")/$CONTRACT_NAME" "$CONTRACT_PATH" - -EXPECTED="Use the --account-contract flag to compile an account contract." - -echo "Testing rejection of compilation without the account flag" -npx hardhat starknet-compile "$CONTRACT_PATH" 2>&1 | - ../scripts/assert-contains.py "$EXPECTED" -echo "Success" - -npx hardhat starknet-compile "$CONTRACT_PATH" --account-contract diff --git a/test/configuration-tests/with-account-compilation-option/check.ts b/test/configuration-tests/with-account-compilation-option/check.ts new file mode 100644 index 00000000..cf5f64c4 --- /dev/null +++ b/test/configuration-tests/with-account-compilation-option/check.ts @@ -0,0 +1,16 @@ +import { copyFileSync } from "fs"; +import path from "path"; +import { hardhatStarknetCompile } from "../../utils/cli-functions"; +import { assertContains } from "../../utils/utils"; + +const contractName = "dummy_account.cairo"; +const contractPath = path.join("contracts", contractName); + +const expected = "Use the --account-contract flag to compile an account contract."; + +console.log("Testing rejection of compilation without the account flag"); +copyFileSync(path.join(__dirname, contractName), contractPath); +const execution = hardhatStarknetCompile(contractPath.split(" "), true); +assertContains(execution.stderr, expected); +console.log("Success"); +hardhatStarknetCompile(`${contractPath} --account-contract`.split(" ")); diff --git a/test/configuration-tests/with-artifacts-path/check.sh b/test/configuration-tests/with-artifacts-path/check.sh deleted file mode 100755 index 5af61b5a..00000000 --- a/test/configuration-tests/with-artifacts-path/check.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat starknet-deploy --starknet-network "$NETWORK" my-starknet-artifacts/contracts/contract.cairo/ --inputs 10 -rm -rf my-starknet-artifacts diff --git a/test/configuration-tests/with-artifacts-path/check.ts b/test/configuration-tests/with-artifacts-path/check.ts new file mode 100644 index 00000000..bceee3eb --- /dev/null +++ b/test/configuration-tests/with-artifacts-path/check.ts @@ -0,0 +1,12 @@ +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar, rmrfSync } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile("contracts/contract.cairo".split(" ")); +hardhatStarknetDeploy( + `--starknet-network ${network} my-starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) +); +rmrfSync("my-starknet-artifacts"); diff --git a/test/configuration-tests/with-cairo-version/check.sh b/test/configuration-tests/with-cairo-version/check.sh deleted file mode 100755 index 885cc5fd..00000000 --- a/test/configuration-tests/with-cairo-version/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat starknet-deploy contracts/contract.cairo --starknet-network "$NETWORK" --inputs 10 diff --git a/test/configuration-tests/with-cairo-version/check.ts b/test/configuration-tests/with-cairo-version/check.ts new file mode 100644 index 00000000..5b488721 --- /dev/null +++ b/test/configuration-tests/with-cairo-version/check.ts @@ -0,0 +1,11 @@ +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile("contracts/contract.cairo".split(" ")); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/contract.cairo --starknet-network ${network} --inputs 10`.split( + " " + ) +); diff --git a/test/configuration-tests/with-cli-network/check.sh b/test/configuration-tests/with-cli-network/check.sh deleted file mode 100755 index fbe422d2..00000000 --- a/test/configuration-tests/with-cli-network/check.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -set -e - -# Test how --starknet-network can be specified through CLI while at the same time -# overriding hardhat.config specification. -# It would be sufficient to run this test just once and not for both alpha and devnet. -# Only tests if --starknet-network is accepted, not if the correct network is targeted. - -npx hardhat run --no-compile scripts/compile-contract.ts -npx hardhat test --no-compile --starknet-network devnet test/quick-test.ts diff --git a/test/configuration-tests/with-cli-network/check.ts b/test/configuration-tests/with-cli-network/check.ts new file mode 100644 index 00000000..743d9775 --- /dev/null +++ b/test/configuration-tests/with-cli-network/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetRun, hardhatStarknetTest } from "../../utils/cli-functions"; + +// Test how --starknet-network can be specified through CLI while at the same time +// overriding hardhat.config specification. +// It would be sufficient to run this test just once and not for both alpha and devnet. +// Only tests if --starknet-network is accepted, not if the correct network is targeted. + +hardhatStarknetRun("--no-compile scripts/compile-contract.ts".split(" ")); +hardhatStarknetTest("--no-compile --starknet-network devnet test/quick-test.ts".split(" ")); diff --git a/test/configuration-tests/with-cli-paths/check.sh b/test/configuration-tests/with-cli-paths/check.sh deleted file mode 100755 index 14737fa4..00000000 --- a/test/configuration-tests/with-cli-paths/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10 diff --git a/test/configuration-tests/with-cli-paths/check.ts b/test/configuration-tests/with-cli-paths/check.ts new file mode 100644 index 00000000..9d98d53e --- /dev/null +++ b/test/configuration-tests/with-cli-paths/check.ts @@ -0,0 +1,11 @@ +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile("contracts/contract.cairo".split(" ")); +hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) +); diff --git a/test/configuration-tests/with-deploy-inputs/check.sh b/test/configuration-tests/with-deploy-inputs/check.sh deleted file mode 100755 index 02631b6d..00000000 --- a/test/configuration-tests/with-deploy-inputs/check.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat starknet-deploy starknet-artifacts/contracts/contract.cairo/ --inputs "10" --starknet-network "$NETWORK" -npx hardhat starknet-deploy starknet-artifacts/contracts/contract.cairo/ --inputs "10" --starknet-network "$NETWORK" --salt 0x10 diff --git a/test/configuration-tests/with-deploy-inputs/check.ts b/test/configuration-tests/with-deploy-inputs/check.ts new file mode 100644 index 00000000..92a293bd --- /dev/null +++ b/test/configuration-tests/with-deploy-inputs/check.ts @@ -0,0 +1,16 @@ +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile("contracts/contract.cairo".split(" ")); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/contract.cairo/ --inputs "10" --starknet-network ${network}`.split( + " " + ) +); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/contract.cairo/ --inputs "10" --starknet-network ${network} --salt 0x10`.split( + " " + ) +); diff --git a/test/configuration-tests/with-deploy-path/check.sh b/test/configuration-tests/with-deploy-path/check.sh deleted file mode 100755 index f6096540..00000000 --- a/test/configuration-tests/with-deploy-path/check.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat starknet-deploy starknet-artifacts/contracts/contract.cairo/ --inputs "10" --starknet-network "$NETWORK" -npx hardhat starknet-deploy contract --inputs "10" --starknet-network "$NETWORK" diff --git a/test/configuration-tests/with-deploy-path/check.ts b/test/configuration-tests/with-deploy-path/check.ts new file mode 100644 index 00000000..4e611b9a --- /dev/null +++ b/test/configuration-tests/with-deploy-path/check.ts @@ -0,0 +1,12 @@ +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile("contracts/contract.cairo".split(" ")); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/contract.cairo/ --inputs "10" --starknet-network ${network}`.split( + " " + ) +); +hardhatStarknetDeploy(`contract --inputs "10" --starknet-network ${network}`.split(" ")); diff --git a/test/configuration-tests/with-disable-hint-compilation-option/check.sh b/test/configuration-tests/with-disable-hint-compilation-option/check.sh deleted file mode 100755 index adfb030b..00000000 --- a/test/configuration-tests/with-disable-hint-compilation-option/check.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -set -e - -CONTRACT_NAME=contract_with_unwhitelisted_hints.cairo -CONTRACT_PATH="contracts/$CONTRACT_NAME" - -cp "$(dirname "$0")/$CONTRACT_NAME" "$CONTRACT_PATH" - -EXPECTED="Hint is not whitelisted. -This may indicate that this library function cannot be used in StarkNet contracts." - -echo "Testing rejection of compilation without the --disable-hint-validation flag" -npx hardhat starknet-compile "$CONTRACT_PATH" 2>&1 | - ../scripts/assert-contains.py "$EXPECTED" -echo "Success" - -npx hardhat starknet-compile "$CONTRACT_PATH" --disable-hint-validation diff --git a/test/configuration-tests/with-disable-hint-compilation-option/check.ts b/test/configuration-tests/with-disable-hint-compilation-option/check.ts new file mode 100644 index 00000000..12b7d689 --- /dev/null +++ b/test/configuration-tests/with-disable-hint-compilation-option/check.ts @@ -0,0 +1,19 @@ +import { copyFileSync } from "fs"; +import path from "path"; +import { hardhatStarknetCompile } from "../../utils/cli-functions"; +import { assertContains } from "../../utils/utils"; + +const contractName = "contract_with_unwhitelisted_hints.cairo"; +const contractPath = path.join("contracts", contractName); + +copyFileSync(path.join(__dirname, contractName), contractPath); + +const expected = `Hint is not whitelisted. +This may indicate that this library function cannot be used in StarkNet contracts.`; + +console.log("Testing rejection of compilation without the --disable-hint-validation flag"); +const execution = hardhatStarknetCompile([contractPath], true); +assertContains(execution.stderr, expected); +console.log("Success"); + +hardhatStarknetCompile(`${contractPath} --disable-hint-validation`.split(" ")); diff --git a/test/configuration-tests/with-networks/check.sh b/test/configuration-tests/with-networks/check.sh deleted file mode 100755 index 42ac64b9..00000000 --- a/test/configuration-tests/with-networks/check.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -ARTIFACT_PATH=starknet-artifacts/contracts/contract.cairo/ -INVALID_NETWORK="foo" -PREFIX=$(dirname "$0") - -echo "Testing no starknet network" -npx hardhat starknet-deploy "$ARTIFACT_PATH" --inputs 10 2>&1 \ - | tail -n +2 \ - | diff - "$PREFIX/without-starknet-network.txt" -echo "Success" - -echo "Testing invalid CLI network" -npx hardhat starknet-deploy --starknet-network "$INVALID_NETWORK" "$ARTIFACT_PATH" --inputs 10 2>&1 \ - | tail -n +2 \ - | diff - "$PREFIX/invalid-cli-network.txt" -echo "Success" - -echo "Testing no mocha network" -NETWORK="" npx hardhat test --no-compile test/contract-factory-test.ts -echo "Success" - -echo "Testing invalid config network" -NETWORK="$INVALID_NETWORK" npx hardhat test --no-compile test/contract-factory-test.ts 2>&1 \ - | tail -n +1 \ - | diff - "$PREFIX/invalid-config-network.txt" -echo "Success" diff --git a/test/configuration-tests/with-networks/check.ts b/test/configuration-tests/with-networks/check.ts new file mode 100644 index 00000000..5843c040 --- /dev/null +++ b/test/configuration-tests/with-networks/check.ts @@ -0,0 +1,45 @@ +import { assertContains } from "../../utils/utils"; +import path from "path"; +import { readFileSync } from "fs"; +import { + hardhatStarknetCompile, + hardhatStarknetDeploy, + hardhatStarknetTest +} from "../../utils/cli-functions"; + +hardhatStarknetCompile(["contracts/contract.cairo"]); +const artifactsPath = "starknet-artifacts/contracts/contract.cairo/"; +const invalidNetwork = "foo"; +const expected = + "Error in plugin Starknet: Invalid network provided in starknet.network in hardhat.config: foo."; +const prefix = path.join(__dirname); + +console.log("Testing no starknet network"); +let execution = hardhatStarknetDeploy(`${artifactsPath} --inputs 10`.split(" "), true); +assertContains( + execution.stderr, + readFileSync(path.join(prefix, "without-starknet-network.txt")).toString() +); +console.log("Success"); + +console.log("Testing invalid CLI network"); +execution = hardhatStarknetDeploy( + `--starknet-network ${invalidNetwork} ${artifactsPath} --inputs 10`.split(" "), + true +); +assertContains( + execution.stderr, + readFileSync(path.join(prefix, "invalid-cli-network.txt")).toString() +); +console.log("Success"); + +console.log("Testing no mocha network"); +process.env.NETWORK = ""; +hardhatStarknetTest("--no-compile test/contract-factory-test.ts".split(" ")); +console.log("Success"); + +console.log("Testing invalid config network"); +process.env.NETWORK = invalidNetwork; +execution = hardhatStarknetTest("--no-compile test/contract-factory-test.ts".split(" "), true); +assertContains(execution.stderr, expected); +console.log("Success"); diff --git a/test/configuration-tests/with-networks/invalid-cli-network.txt b/test/configuration-tests/with-networks/invalid-cli-network.txt index 0c683ad6..2c4b6baa 100644 --- a/test/configuration-tests/with-networks/invalid-cli-network.txt +++ b/test/configuration-tests/with-networks/invalid-cli-network.txt @@ -1,4 +1,2 @@ Error in plugin Starknet: Invalid network provided in starknet-network: foo. Valid hardhat networks: hardhat, localhost, devnet, alpha, alphaMainnet, integratedDevnet - -For more info run Hardhat with --show-stack-traces diff --git a/test/configuration-tests/with-networks/without-starknet-network.txt b/test/configuration-tests/with-networks/without-starknet-network.txt index 232c7046..997f7395 100644 --- a/test/configuration-tests/with-networks/without-starknet-network.txt +++ b/test/configuration-tests/with-networks/without-starknet-network.txt @@ -1,3 +1 @@ Error in plugin Starknet: No starknet-network or gateway-url provided. - -For more info run Hardhat with --show-stack-traces diff --git a/test/configuration-tests/with-sources-path/check.sh b/test/configuration-tests/with-sources-path/check.sh deleted file mode 100755 index 567fb17b..00000000 --- a/test/configuration-tests/with-sources-path/check.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -set -e - -mv contracts my-starknet-sources - -npx hardhat starknet-compile -npx hardhat starknet-deploy starknet-artifacts/my-starknet-sources/contract.cairo/ --starknet-network "$NETWORK" --inputs 10 diff --git a/test/configuration-tests/with-sources-path/check.ts b/test/configuration-tests/with-sources-path/check.ts new file mode 100644 index 00000000..a693d445 --- /dev/null +++ b/test/configuration-tests/with-sources-path/check.ts @@ -0,0 +1,14 @@ +import { renameSync } from "fs"; +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +renameSync("contracts", "my-starknet-sources"); + +hardhatStarknetCompile([]); +hardhatStarknetDeploy( + `starknet-artifacts/my-starknet-sources/contract.cairo/ --starknet-network ${network} --inputs 10`.split( + " " + ) +); diff --git a/test/configuration-tests/with-wait/check.sh b/test/configuration-tests/with-wait/check.sh deleted file mode 100755 index a6bab5db..00000000 --- a/test/configuration-tests/with-wait/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/util.cairo -npx hardhat starknet-deploy starknet-artifacts/contracts/util.cairo/ --starknet-network "$NETWORK" --wait diff --git a/test/configuration-tests/with-wait/check.ts b/test/configuration-tests/with-wait/check.ts new file mode 100644 index 00000000..e346d934 --- /dev/null +++ b/test/configuration-tests/with-wait/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile(["contracts/util.cairo"]); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/util.cairo/ --starknet-network ${network} --wait`.split(" ") +); diff --git a/test/constants/constants.ts b/test/constants/constants.ts new file mode 100644 index 00000000..1bbf3610 --- /dev/null +++ b/test/constants/constants.ts @@ -0,0 +1,4 @@ +export const HOST = "127.0.0.1"; +export const PORT = "5050"; +export const NODE_PORT = "8545"; +export const DEVNET_URL = `http://${HOST}:${PORT}`; diff --git a/test/general-tests/account-test/check.sh b/test/general-tests/account-test/check.sh deleted file mode 100755 index 0dc81e78..00000000 --- a/test/general-tests/account-test/check.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash -set -eu - -npx hardhat starknet-compile contracts/contract.cairo contracts/util.cairo - -npx hardhat test --no-compile test/oz-account-test.ts - -if [ "$NETWORK" == "devnet" ]; then - npx hardhat test --no-compile scripts/deploy-argent.ts - - TOKEN_ADDRESS="0x62230EA046A9A5FBC261AC77D03C8D41E5D442DB2284587570AB46455FD2488" \ - SENDER_ADDRESS=$OZ_ACCOUNT_ADDRESS \ - SENDER_PRIVATE_KEY=$OZ_ACCOUNT_PRIVATE_KEY \ - SENDER_IMPLEMENTATION="OpenZeppelin" \ - RECIPIENT_ADDRESS=$ARGENT_ACCOUNT_ADDRESS \ - TRANSFER_AMOUNT="1000000000000000000" \ - npx hardhat test --no-compile scripts/transfer-funds.ts -fi - -npx hardhat test --no-compile test/argent-account-test.ts diff --git a/test/general-tests/account-test/check.ts b/test/general-tests/account-test/check.ts new file mode 100644 index 00000000..e9b9e4f7 --- /dev/null +++ b/test/general-tests/account-test/check.ts @@ -0,0 +1,18 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile("contracts/contract.cairo contracts/util.cairo".split(" ")); +hardhatStarknetTest("--no-compile test/oz-account-test.ts".split(" ")); + +if (process.env.NETWORK === "devnet") { + hardhatStarknetTest("--no-compile scripts/deploy-argent.ts".split(" ")); + + process.env.TOKEN_ADDRESS = "0x62230EA046A9A5FBC261AC77D03C8D41E5D442DB2284587570AB46455FD2488"; + process.env.SENDER_ADDRESS = process.env.OZ_ACCOUNT_ADDRESS; + process.env.SENDER_PRIVATE_KEY = process.env.OZ_ACCOUNT_PRIVATE_KEY; + process.env.SENDER_IMPLEMENTATION = "OpenZeppelin"; + process.env.RECIPIENT_ADDRESS = process.env.ARGENT_ACCOUNT_ADDRESS; + process.env.TRANSFER_AMOUNT = "1000000000000000000"; + hardhatStarknetTest("--no-compile scripts/transfer-funds.ts".split(" ")); +} + +hardhatStarknetTest("--no-compile test/argent-account-test.ts".split(" ")); diff --git a/test/general-tests/cairo-migrate/check.sh b/test/general-tests/cairo-migrate/check.sh deleted file mode 100755 index fa2f35ca..00000000 --- a/test/general-tests/cairo-migrate/check.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash -set -e - -CONTRACT_NAME=old_contract.cairo -CONTRACT_PATH="contracts/$CONTRACT_NAME" -NEW_COMMENT="// Declare this file as a StarkNet contract." - -cp "$(dirname "$0")/$CONTRACT_NAME" "$CONTRACT_PATH" - -echo "Testing migration of old cairo contract to a new one" -# Migrate contract to new version. -npx hardhat migrate "$CONTRACT_PATH" 2>&1 | - ../scripts/assert-contains.py "$NEW_COMMENT" - -# Migrate contract to new version with change content in place option. -npx hardhat migrate "$CONTRACT_PATH" --inplace -cat "$CONTRACT_PATH" | ../scripts/assert-contains.py "$NEW_COMMENT" - -echo "Success" diff --git a/test/general-tests/cairo-migrate/check.ts b/test/general-tests/cairo-migrate/check.ts new file mode 100644 index 00000000..3db7faab --- /dev/null +++ b/test/general-tests/cairo-migrate/check.ts @@ -0,0 +1,21 @@ +import { copyFileSync, readFileSync } from "fs"; +import path from "path"; +import { hardhatStarknetMigrate } from "../../utils/cli-functions"; +import { assertContains } from "../../utils/utils"; + +const contractName = "old_contract.cairo"; +const contractPath = path.join("contracts", contractName); +const newComment = "// Declare this file as a StarkNet contract."; + +copyFileSync(path.join(__dirname, contractName), contractPath); + +console.log("Testing migration of old cairo contract to a new one"); +// Migrate contract to new version. +const execution = hardhatStarknetMigrate([contractPath], true); +assertContains(execution.stdout, newComment); + +// Migrate contract to new version with change content in place option. +hardhatStarknetMigrate(`${contractPath} --inplace`.split(" ")); +assertContains(readFileSync(contractPath).toString(), newComment); + +console.log("Success"); diff --git a/test/general-tests/constructor-test/check.sh b/test/general-tests/constructor-test/check.sh deleted file mode 100755 index e383f00f..00000000 --- a/test/general-tests/constructor-test/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo contracts/simple_storage.cairo contracts/empty_constructor.cairo -npx hardhat test --no-compile test/constructor.test.ts diff --git a/test/general-tests/constructor-test/check.ts b/test/general-tests/constructor-test/check.ts new file mode 100644 index 00000000..c9c3afb5 --- /dev/null +++ b/test/general-tests/constructor-test/check.ts @@ -0,0 +1,8 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile( + "contracts/contract.cairo contracts/simple_storage.cairo contracts/empty_constructor.cairo".split( + " " + ) +); +hardhatStarknetTest("--no-compile test/constructor.test.ts".split(" ")); diff --git a/test/general-tests/declare-test/check.sh b/test/general-tests/declare-test/check.sh deleted file mode 100755 index 579beb2d..00000000 --- a/test/general-tests/declare-test/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo contracts/deployer.cairo -npx hardhat test --no-compile test/declare-deploy.test.ts diff --git a/test/general-tests/declare-test/check.ts b/test/general-tests/declare-test/check.ts new file mode 100644 index 00000000..c4a2d487 --- /dev/null +++ b/test/general-tests/declare-test/check.ts @@ -0,0 +1,4 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile("contracts/contract.cairo contracts/deployer.cairo".split(" ")); +hardhatStarknetTest("--no-compile test/declare-deploy.test.ts".split(" ")); diff --git a/test/general-tests/decode-event-test/check.sh b/test/general-tests/decode-event-test/check.sh deleted file mode 100755 index bc918cee..00000000 --- a/test/general-tests/decode-event-test/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/events.cairo -npx hardhat test --no-compile test/decode-events.test.ts diff --git a/test/general-tests/decode-event-test/check.ts b/test/general-tests/decode-event-test/check.ts new file mode 100644 index 00000000..7ce8a571 --- /dev/null +++ b/test/general-tests/decode-event-test/check.ts @@ -0,0 +1,4 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile(["contracts/events.cairo"]); +hardhatStarknetTest("--no-compile test/decode-events.test.ts".split(" ")); diff --git a/test/general-tests/devnet-block-time/check.sh b/test/general-tests/devnet-block-time/check.sh deleted file mode 100755 index 8965add8..00000000 --- a/test/general-tests/devnet-block-time/check.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat test --no-compile test/devnet-time-test.ts diff --git a/test/general-tests/devnet-block-time/check.ts b/test/general-tests/devnet-block-time/check.ts new file mode 100644 index 00000000..cac485e5 --- /dev/null +++ b/test/general-tests/devnet-block-time/check.ts @@ -0,0 +1,3 @@ +import { hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetTest("--no-compile test/devnet-time-test.ts".split(" ")); diff --git a/test/general-tests/devnet-create-block/check.sh b/test/general-tests/devnet-create-block/check.sh deleted file mode 100755 index 894e4d0e..00000000 --- a/test/general-tests/devnet-create-block/check.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat test --no-compile test/devnet-create-block.test.ts diff --git a/test/general-tests/devnet-create-block/check.ts b/test/general-tests/devnet-create-block/check.ts new file mode 100644 index 00000000..b0ec60a0 --- /dev/null +++ b/test/general-tests/devnet-create-block/check.ts @@ -0,0 +1,3 @@ +import { hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetTest("--no-compile test/devnet-create-block.test.ts".split(" ")); diff --git a/test/general-tests/devnet-dump-and-load/check.sh b/test/general-tests/devnet-dump-and-load/check.sh deleted file mode 100755 index bfe19441..00000000 --- a/test/general-tests/devnet-dump-and-load/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/devnet-dump-and-load.test.ts diff --git a/test/general-tests/devnet-dump-and-load/check.ts b/test/general-tests/devnet-dump-and-load/check.ts new file mode 100644 index 00000000..049a0122 --- /dev/null +++ b/test/general-tests/devnet-dump-and-load/check.ts @@ -0,0 +1,4 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile(["contracts/contract.cairo"]); +hardhatStarknetTest("--no-compile test/devnet-dump-and-load.test.ts".split(" ")); diff --git a/test/general-tests/devnet-restart/check.sh b/test/general-tests/devnet-restart/check.sh deleted file mode 100755 index 576ec321..00000000 --- a/test/general-tests/devnet-restart/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/devnet-restart.test.ts diff --git a/test/general-tests/devnet-restart/check.ts b/test/general-tests/devnet-restart/check.ts new file mode 100644 index 00000000..4188b6ba --- /dev/null +++ b/test/general-tests/devnet-restart/check.ts @@ -0,0 +1,4 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile(["contracts/contract.cairo"]); +hardhatStarknetTest("--no-compile test/devnet-restart.test.ts".split(" ")); diff --git a/test/general-tests/expect-error-on-compile/check.sh b/test/general-tests/expect-error-on-compile/check.sh deleted file mode 100755 index 882b1220..00000000 --- a/test/general-tests/expect-error-on-compile/check.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -set -eu - -CONTRACT_NAME=invalid_contract.cairo -CONTRACT_PATH="contracts/$CONTRACT_NAME" - -cp "$(dirname "$0")/$CONTRACT_NAME" "$CONTRACT_PATH" - -echo "Testing rejection of compilation with correct message" -npx hardhat starknet-compile "$CONTRACT_PATH" 2>&1 | - ../scripts/assert-contains.py "Unknown identifier 'openzeppelin.token.erc721.library.ERC721.nonexistent_method'" -echo "Success" diff --git a/test/general-tests/expect-error-on-compile/check.ts b/test/general-tests/expect-error-on-compile/check.ts new file mode 100644 index 00000000..b4ff3c1c --- /dev/null +++ b/test/general-tests/expect-error-on-compile/check.ts @@ -0,0 +1,17 @@ +import { copyFileSync } from "fs"; +import path from "path"; +import { hardhatStarknetCompile } from "../../utils/cli-functions"; +import { assertContains } from "../../utils/utils"; + +const contractName = "invalid_contract.cairo"; +const contractPath = path.join("contracts", contractName); + +copyFileSync(path.join(__dirname, contractName), contractPath); + +console.log("Testing rejection of compilation with correct message"); +const compileResult = hardhatStarknetCompile([contractPath], true); +assertContains( + compileResult.stderr, + "Unknown identifier 'openzeppelin.token.erc721.library.ERC721.nonexistent_method'" +); +console.log("Success"); diff --git a/test/general-tests/function-argument-number/check.sh b/test/general-tests/function-argument-number/check.sh deleted file mode 100755 index cf4382bc..00000000 --- a/test/general-tests/function-argument-number/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/function-args-test.ts diff --git a/test/general-tests/function-argument-number/check.ts b/test/general-tests/function-argument-number/check.ts new file mode 100644 index 00000000..9cb60298 --- /dev/null +++ b/test/general-tests/function-argument-number/check.ts @@ -0,0 +1,4 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile(["contracts/contract.cairo"]); +hardhatStarknetTest("--no-compile test/function-args-test.ts".split(" ")); diff --git a/test/general-tests/getContractFactory/check.sh b/test/general-tests/getContractFactory/check.sh deleted file mode 100755 index c25f8295..00000000 --- a/test/general-tests/getContractFactory/check.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile -npx hardhat test --no-compile test/path-test.ts diff --git a/test/general-tests/getContractFactory/check.ts b/test/general-tests/getContractFactory/check.ts new file mode 100644 index 00000000..05eda6cf --- /dev/null +++ b/test/general-tests/getContractFactory/check.ts @@ -0,0 +1,4 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetCompile([]); +hardhatStarknetTest("--no-compile test/path-test.ts".split(" ")); diff --git a/test/general-tests/hardhat-run/check.sh b/test/general-tests/hardhat-run/check.sh deleted file mode 100755 index 4160f637..00000000 --- a/test/general-tests/hardhat-run/check.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -set -e - -echo "$0: Testing hh run with --starknet-network is currently disabled" - -# npx hardhat starknet-compile contracts/contract.cairo -# npx hardhat run --no-compile scripts/quick-script.ts --starknet-network "$NETWORK" diff --git a/test/general-tests/hardhat-run/check.ts b/test/general-tests/hardhat-run/check.ts new file mode 100644 index 00000000..84266d44 --- /dev/null +++ b/test/general-tests/hardhat-run/check.ts @@ -0,0 +1,8 @@ +// import { hardhatStarknetCompile, hardhatStarknetRun } from "../../utils/cli-functions"; +// import { ensureEnvVar } from "../../utils/utils"; + +console.log("Testing hh run with --starknet-network is currently disabled"); +// const network = ensureEnvVar("NETWORK"); + +// hardhatStarknetCompile(["contracts/contract.cairo"]); +// hardhatStarknetRun(`--no-compile scripts/quick-script.ts --starknet-network ${network}`.split(" ")); diff --git a/test/general-tests/plain/check.sh b/test/general-tests/plain/check.sh deleted file mode 100755 index baeeacc9..00000000 --- a/test/general-tests/plain/check.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -set -e - -INITIAL_VALUE=10 -PUBLIC_KEY=1628448741648245036800002906075225705100596136133912895015035902954123957052 - -npx hardhat starknet-compile -npx hardhat starknet-deploy starknet-artifacts/contracts/contract.cairo/ --starknet-network "$NETWORK" --inputs "$INITIAL_VALUE" -npx hardhat starknet-deploy starknet-artifacts/contracts/auth_contract.cairo/ --inputs "$PUBLIC_KEY $INITIAL_VALUE" --starknet-network "$NETWORK" -npx hardhat test --no-compile test/sample-test.ts diff --git a/test/general-tests/plain/check.ts b/test/general-tests/plain/check.ts new file mode 100644 index 00000000..b1d2ea07 --- /dev/null +++ b/test/general-tests/plain/check.ts @@ -0,0 +1,23 @@ +import { + hardhatStarknetCompile, + hardhatStarknetDeploy, + hardhatStarknetTest +} from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); +const initialValue = 10; +const publicKey = "1628448741648245036800002906075225705100596136133912895015035902954123957052"; + +hardhatStarknetCompile([]); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/contract.cairo/ --starknet-network ${network} --inputs ${initialValue}`.split( + " " + ) +); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/auth_contract.cairo/ --inputs "${publicKey} ${initialValue}" --starknet-network ${network}`.split( + " " + ) +); +hardhatStarknetTest("--no-compile test/sample-test.ts".split(" ")); diff --git a/test/general-tests/postman/check.sh b/test/general-tests/postman/check.sh deleted file mode 100755 index ff370c73..00000000 --- a/test/general-tests/postman/check.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -set -e - -trap 'kill $(jobs -p)' EXIT - -npx hardhat starknet-compile contracts/l1l2.cairo -npx hardhat node & -sleep 1 - -npx hardhat test --network localhost test/postman.test.ts diff --git a/test/general-tests/postman/check.ts b/test/general-tests/postman/check.ts new file mode 100644 index 00000000..79005f0a --- /dev/null +++ b/test/general-tests/postman/check.ts @@ -0,0 +1,12 @@ +import { exec } from "../../utils/utils"; +import { spawn } from "child_process"; +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { NODE_PORT } from "../../constants/constants"; + +hardhatStarknetCompile(["contracts/l1l2.cairo"]); + +spawn("npx", ["hardhat", "node", "--port", NODE_PORT], { detached: true }); +exec("sleep 1"); + +hardhatStarknetTest("--network localhost test/postman.test.ts".split(" ")); +exec(`kill -9 $(lsof -t -i:${NODE_PORT})`); diff --git a/test/general-tests/predeployed-accounts-test/check.sh b/test/general-tests/predeployed-accounts-test/check.sh deleted file mode 100755 index 0fe17130..00000000 --- a/test/general-tests/predeployed-accounts-test/check.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat test --no-compile test/get-predeployed-accounts.test.ts diff --git a/test/general-tests/predeployed-accounts-test/check.ts b/test/general-tests/predeployed-accounts-test/check.ts new file mode 100644 index 00000000..b88e9ecf --- /dev/null +++ b/test/general-tests/predeployed-accounts-test/check.ts @@ -0,0 +1,3 @@ +import { hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetTest("--no-compile test/get-predeployed-accounts.test.ts".split(" ")); diff --git a/test/general-tests/proxy-call/check.sh b/test/general-tests/proxy-call/check.sh deleted file mode 100755 index 89b61136..00000000 --- a/test/general-tests/proxy-call/check.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat test --no-compile scripts/delegate-proxy.ts diff --git a/test/general-tests/proxy-call/check.ts b/test/general-tests/proxy-call/check.ts new file mode 100644 index 00000000..240328c2 --- /dev/null +++ b/test/general-tests/proxy-call/check.ts @@ -0,0 +1,3 @@ +import { hardhatStarknetTest } from "../../utils/cli-functions"; + +hardhatStarknetTest("--no-compile scripts/delegate-proxy.ts".split(" ")); diff --git a/test/general-tests/relative-artifact-test/check.sh b/test/general-tests/relative-artifact-test/check.sh deleted file mode 100755 index 39aaf47c..00000000 --- a/test/general-tests/relative-artifact-test/check.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo contracts/util.cairo - -cp -a starknet-artifacts/contracts test/test-artifacts - -npx hardhat test --no-compile test/relative-artifacts.test.ts diff --git a/test/general-tests/relative-artifact-test/check.ts b/test/general-tests/relative-artifact-test/check.ts new file mode 100644 index 00000000..46680ce9 --- /dev/null +++ b/test/general-tests/relative-artifact-test/check.ts @@ -0,0 +1,8 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { exec } from "../../utils/utils"; + +hardhatStarknetCompile("contracts/contract.cairo contracts/util.cairo".split(" ")); + +exec("cp -a starknet-artifacts/contracts test/test-artifacts"); + +hardhatStarknetTest("--no-compile test/relative-artifacts.test.ts".split(" ")); diff --git a/test/general-tests/short-string-test/check.sh b/test/general-tests/short-string-test/check.sh deleted file mode 100755 index b7348958..00000000 --- a/test/general-tests/short-string-test/check.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e - -cd .. - -# The config file used for running mocha tests is the one in root - -npx hardhat test --no-compile test/general-tests/short-string-test/short-string-test.ts diff --git a/test/general-tests/short-string-test/check.ts b/test/general-tests/short-string-test/check.ts new file mode 100644 index 00000000..9720961f --- /dev/null +++ b/test/general-tests/short-string-test/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetTest } from "../../utils/cli-functions"; + +process.chdir(".."); + +// The config file used for running mocha tests is the one in root + +hardhatStarknetTest( + "--no-compile test/general-tests/short-string-test/short-string-test.ts".split(" ") +); diff --git a/test/general-tests/starknet-call/check.sh b/test/general-tests/starknet-call/check.sh deleted file mode 100755 index 46e59681..00000000 --- a/test/general-tests/starknet-call/check.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -output=$(npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10) -echo $output - -ADDRESS=$(echo $output | sed -r "s/.*Contract address: (\w*).*/\1/") -PREFIX=$(dirname "$0") - -echo "Testing no input argument" -npx hardhat starknet-call --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/no-inputs.txt" -echo "Success" - -echo "Testing too few input arguments" -npx hardhat starknet-call --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" --inputs "10 20 30" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/too-few-inputs.txt" -echo "Success" - -echo "Testing too many input arguments" -npx hardhat starknet-call --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" --inputs "10 20 30 40 50" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/too-many-inputs.txt" -echo "Success" - -echo "The success case of starknet-call test is temporarily disabled." -echo "To enable it back, uncomment the lines in its check.sh." -# echo "Testing success case" -# npx hardhat starknet-call --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" --inputs "10 20 30 40" 2>&1 \ -# | tail -n +2 \ -# | head -n -3 \ -# | diff - <(echo "40 60") -# echo "Success" diff --git a/test/general-tests/starknet-call/check.ts b/test/general-tests/starknet-call/check.ts new file mode 100644 index 00000000..31532efb --- /dev/null +++ b/test/general-tests/starknet-call/check.ts @@ -0,0 +1,59 @@ +import { readFileSync } from "fs"; +import path from "path"; +import { + hardhatStarknetCompile, + hardhatStarknetDeploy, + hardhatStarknetCall +} from "../../utils/cli-functions"; +import { assertContains, ensureEnvVar, extractAddress } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile(["contracts/contract.cairo"]); +const output = hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ), + true +); +console.log(output.stdout); + +const address = extractAddress(output.stdout, "Contract address: "); +const prefix = path.join(__dirname); + +console.log("Testing no input argument"); +let execution = hardhatStarknetCall( + `--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address}`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "no-inputs.txt")).toString()); +console.log("Success"); + +console.log("Testing too few input arguments"); +execution = hardhatStarknetCall( + `--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address} --inputs "10 20 30"`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "too-few-inputs.txt")).toString()); +console.log("Success"); + +console.log("Testing too many input arguments"); +execution = hardhatStarknetCall( + `--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address} --inputs "10 20 30 40 50"`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "too-many-inputs.txt")).toString()); +console.log("Success"); + +console.log("The success case of starknet-call test is temporarily disabled."); +console.log("To enable it back, uncomment the lines in its check.sh."); +// console.log("Testing success case"); +// execution = hardhatStarknetCall(`--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address} --inputs "10 20 30 40"`.split(" "), true); +// assertContains(execution.stdout, "40 60"); +// console.log("Success"); diff --git a/test/general-tests/starknet-call/no-inputs.txt b/test/general-tests/starknet-call/no-inputs.txt index 67093054..fb6b6ce5 100644 --- a/test/general-tests/starknet-call/no-inputs.txt +++ b/test/general-tests/starknet-call/no-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform call of sum_points_to_tuple: Error: AssertionError: Expected at least 4 inputs, got 0. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-call/too-few-inputs.txt b/test/general-tests/starknet-call/too-few-inputs.txt index 4c6be230..971373e1 100644 --- a/test/general-tests/starknet-call/too-few-inputs.txt +++ b/test/general-tests/starknet-call/too-few-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform call of sum_points_to_tuple: Error: AssertionError: Expected at least 4 inputs, got 3. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-call/too-many-inputs.txt b/test/general-tests/starknet-call/too-many-inputs.txt index 833a3546..bb147807 100644 --- a/test/general-tests/starknet-call/too-many-inputs.txt +++ b/test/general-tests/starknet-call/too-many-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform call of sum_points_to_tuple: Error: AssertionError: Wrong number of arguments. Expected 4, got 5. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-estimate-fee/check.sh b/test/general-tests/starknet-estimate-fee/check.sh deleted file mode 100755 index f8a4987a..00000000 --- a/test/general-tests/starknet-estimate-fee/check.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -output=$(npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10) -echo $output - -ADDRESS=$(echo $output | sed -r "s/.*Contract address: (\w*).*/\1/") -PREFIX=$(dirname "$0") - -echo "Testing no input argument" -npx hardhat starknet-estimate-fee --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/no-inputs.txt" -echo "Success" - -echo "Testing too few input arguments" -npx hardhat starknet-estimate-fee --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" --inputs "10 20 30" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/too-few-inputs.txt" -echo "Success" - -echo "Testing too many input arguments" -npx hardhat starknet-estimate-fee --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" --inputs "10 20 30 40 50" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/too-many-inputs.txt" -echo "Success" - -echo "The success case of starknet-estimate-fee test is temporarily disabled." -echo "To enable it back, uncomment the lines in its check.sh." -# echo "Testing success case" -# npx hardhat starknet-estimate-fee --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$ADDRESS" --inputs "10 20 30 40" 2>&1 \ -# | tail -n +2 \ -# | head -n -3 \ -# | diff - <(echo "40 60") -# echo "Success" diff --git a/test/general-tests/starknet-estimate-fee/check.ts b/test/general-tests/starknet-estimate-fee/check.ts new file mode 100644 index 00000000..25228a09 --- /dev/null +++ b/test/general-tests/starknet-estimate-fee/check.ts @@ -0,0 +1,57 @@ +import { readFileSync } from "fs"; +import path from "path"; +import { + hardhatStarknetCompile, + hardhatStarknetDeploy, + hardhatStarknetEstimateFee +} from "../../utils/cli-functions"; +import { assertContains, ensureEnvVar, extractAddress } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile(["contracts/contract.cairo"]); +const output = hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) +); + +const address = extractAddress(output.stdout, "Contract address: "); +const prefix = path.join(__dirname); + +console.log("Testing no input argument"); +let execution = hardhatStarknetEstimateFee( + `--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address}`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "no-inputs.txt")).toString()); +console.log("Success"); + +console.log("Testing too few input arguments"); +execution = hardhatStarknetEstimateFee( + `--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address} --inputs "10 20 30"`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "too-few-inputs.txt")).toString()); +console.log("Success"); + +console.log("Testing too many input arguments"); +execution = hardhatStarknetEstimateFee( + `--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address} --inputs "10 20 30 40 50"`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "too-many-inputs.txt")).toString()); +console.log("Success"); + +console.log("The success case of starknet-estimate-fee test is temporarily disabled."); +console.log("To enable it back, uncomment the lines in its check.sh."); +// console.log("Testing success case"); +// execution = hardhatStarknetEstimateFee(`--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address} --inputs "10 20 30 40"`.split(" "), false); +// assertContains(execution.stdout, "40 60") +// console.log("Success"); diff --git a/test/general-tests/starknet-estimate-fee/no-inputs.txt b/test/general-tests/starknet-estimate-fee/no-inputs.txt index 925b915d..a9e31505 100644 --- a/test/general-tests/starknet-estimate-fee/no-inputs.txt +++ b/test/general-tests/starknet-estimate-fee/no-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform estimateFee of sum_points_to_tuple: Error: AssertionError: Expected at least 4 inputs, got 0. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-estimate-fee/too-few-inputs.txt b/test/general-tests/starknet-estimate-fee/too-few-inputs.txt index e3fb1d05..802a4a3e 100644 --- a/test/general-tests/starknet-estimate-fee/too-few-inputs.txt +++ b/test/general-tests/starknet-estimate-fee/too-few-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform estimateFee of sum_points_to_tuple: Error: AssertionError: Expected at least 4 inputs, got 3. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-estimate-fee/too-many-inputs.txt b/test/general-tests/starknet-estimate-fee/too-many-inputs.txt index 937e8ea8..6e6d5332 100644 --- a/test/general-tests/starknet-estimate-fee/too-many-inputs.txt +++ b/test/general-tests/starknet-estimate-fee/too-many-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform estimateFee of sum_points_to_tuple: Error: AssertionError: Wrong number of arguments. Expected 4, got 5. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-invoke/check.sh b/test/general-tests/starknet-invoke/check.sh deleted file mode 100755 index c3045522..00000000 --- a/test/general-tests/starknet-invoke/check.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-compile contracts/contract.cairo -output=$(npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10) -echo $output - -ADDRESS=$(echo $output | sed -r "s/.*Contract address: (\w*).*/\1/") -PREFIX=$(dirname "$0") - -echo "Testing no input argument" -npx hardhat starknet-invoke --starknet-network "$NETWORK" --contract contract --function increase_balance --address "$ADDRESS" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/no-inputs.txt" -echo "Success" - -echo "Testing too few input arguments" -npx hardhat starknet-invoke --starknet-network "$NETWORK" --contract contract --function increase_balance --address "$ADDRESS" --inputs 10 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/too-few-inputs.txt" -echo "Success" - -echo "Testing too many input arguments" -npx hardhat starknet-invoke --starknet-network "$NETWORK" --contract contract --function increase_balance --address "$ADDRESS" --inputs "10 20 30" 2>&1 \ - | tail -n +6 \ - | diff - "$PREFIX/too-many-inputs.txt" -echo "Success" - -echo "The success case of starknet-invoke test is temporarily disabled." -echo "To enable it back, uncomment the lines in its check.sh." -# echo "Testing success case" -# npx hardhat starknet-invoke --starknet-network "$NETWORK" --contract contract --function increase_balance --address "$ADDRESS" --inputs "10 20" 2>&1 \ -# | tail -n +2\ -# | head -n -5\ -# | diff - <(echo "Invoke transaction was sent.") -# echo "Success" diff --git a/test/general-tests/starknet-invoke/check.ts b/test/general-tests/starknet-invoke/check.ts new file mode 100644 index 00000000..c67904a1 --- /dev/null +++ b/test/general-tests/starknet-invoke/check.ts @@ -0,0 +1,57 @@ +import { readFileSync } from "fs"; +import path from "path"; +import { + hardhatStarknetCompile, + hardhatStarknetDeploy, + hardhatStarknetInvoke +} from "../../utils/cli-functions"; +import { assertContains, ensureEnvVar, extractAddress } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile(["contracts/contract.cairo"]); +const output = hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) +); + +const address = extractAddress(output.stdout, "Contract address: "); +const prefix = path.join(__dirname); + +console.log("Testing no input argument"); +let execution = hardhatStarknetInvoke( + `--starknet-network ${network} --contract contract --function increase_balance --address ${address}`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "no-inputs.txt")).toString()); +console.log("Success"); + +console.log("Testing too few input arguments"); +execution = hardhatStarknetInvoke( + `--starknet-network ${network} --contract contract --function increase_balance --address ${address} --inputs 10`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "too-few-inputs.txt")).toString()); +console.log("Success"); + +console.log("Testing too many input arguments"); +execution = hardhatStarknetInvoke( + `--starknet-network ${network} --contract contract --function increase_balance --address ${address} --inputs "10 20 30"`.split( + " " + ), + true +); +assertContains(execution.stderr, readFileSync(path.join(prefix, "too-many-inputs.txt")).toString()); +console.log("Success"); + +console.log("The success case of starknet-invoke test is temporarily disabled."); +console.log("To enable it back, uncomment the lines in its check.sh."); +// console.log("Testing success case"); +// execution = hardhatStarknetInvoke(`--starknet-network ${network} --contract contract --function increase_balance --address ${address} --inputs "10 20"`.split(" "), true); +// assertContains(execution.stdout, "Invoke transaction was sent."); +// console.log("Success"); diff --git a/test/general-tests/starknet-invoke/no-inputs.txt b/test/general-tests/starknet-invoke/no-inputs.txt index c88332c8..b3c48942 100644 --- a/test/general-tests/starknet-invoke/no-inputs.txt +++ b/test/general-tests/starknet-invoke/no-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform invoke of increase_balance: Error: AssertionError: Expected at least 1 inputs, got 0. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-invoke/too-few-inputs.txt b/test/general-tests/starknet-invoke/too-few-inputs.txt index 89215f24..9539dddb 100644 --- a/test/general-tests/starknet-invoke/too-few-inputs.txt +++ b/test/general-tests/starknet-invoke/too-few-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform invoke of increase_balance: Error: AssertionError: Expected at least 2 inputs, got 1. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-invoke/too-many-inputs.txt b/test/general-tests/starknet-invoke/too-many-inputs.txt index 3cef593f..c4f11c6e 100644 --- a/test/general-tests/starknet-invoke/too-many-inputs.txt +++ b/test/general-tests/starknet-invoke/too-many-inputs.txt @@ -1,5 +1,2 @@ Error in plugin Starknet: Could not perform invoke of increase_balance: Error: AssertionError: Wrong number of arguments. Expected 2, got 3. - - -For more info run Hardhat with --show-stack-traces diff --git a/test/general-tests/starknet-new-account/check.sh b/test/general-tests/starknet-new-account/check.sh deleted file mode 100755 index 98e595ec..00000000 --- a/test/general-tests/starknet-new-account/check.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash - -set -eu - -export ACCOUNT_DIR="$HOME/.starknet_new_account_test" -ACCOUNT_FILE="$ACCOUNT_DIR/starknet_open_zeppelin_accounts.json" - -output=$(npx hardhat starknet-new-account --wallet OpenZeppelin --starknet-network "$NETWORK") -tail=${output#*$'\n'Account address: } -ACCOUNT_ADDRESS_FROM_STD=${tail%%$'\n'*} - -# Read newly created account and grab the address -ACCOUNT_ADDRESS_FROM_FILE=$(jq -r ."$NETWORK".OpenZeppelin.address $ACCOUNT_FILE) - -# Change hex to int -address_one=$(python -c "print(int('${ACCOUNT_ADDRESS_FROM_STD}', 16))") -address_two=$(python -c "print(int('${ACCOUNT_ADDRESS_FROM_FILE}', 16))") - -# If address_one and address_two are equal then success -if [ ${address_one} == ${address_two} ]; then - echo "Success" -else - echo "Failed" - exit 1 -fi diff --git a/test/general-tests/starknet-new-account/check.ts b/test/general-tests/starknet-new-account/check.ts new file mode 100644 index 00000000..2b7246ea --- /dev/null +++ b/test/general-tests/starknet-new-account/check.ts @@ -0,0 +1,27 @@ +import { readFileSync } from "fs"; +import path from "path"; +import { hardhatStarknetNewAccount } from "../../utils/cli-functions"; +import { assertEqual, ensureEnvVar, extractAddress } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); +const home = ensureEnvVar("HOME"); + +const accountDir = path.join(home, ".starknet_new_account_test"); +process.env.ACCOUNT_DIR = accountDir; +const accountFilePath = path.join(accountDir, "starknet_open_zeppelin_accounts.json"); + +const output = hardhatStarknetNewAccount( + `--wallet OpenZeppelin --starknet-network ${network}`.split(" ") +); +const accountAddressStd = extractAddress(output.stdout, "Account address: "); + +// Read newly created account and grab the address +const accountFile = readFileSync(accountFilePath); +const accountAddressFile = JSON.parse(accountFile.toString())[network].OpenZeppelin.address; + +// Change hex to int +const addressStd = parseInt(accountAddressStd, 16); +const addressFile = parseInt(accountAddressFile, 16); + +// If addressStd and addressFile are equal then success +assertEqual(addressStd, addressFile, "Account address mismatch"); diff --git a/test/general-tests/starknet-plugin-version/check.sh b/test/general-tests/starknet-plugin-version/check.sh deleted file mode 100755 index 92f72ae0..00000000 --- a/test/general-tests/starknet-plugin-version/check.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -npx hardhat starknet-plugin-version diff --git a/test/general-tests/starknet-plugin-version/check.ts b/test/general-tests/starknet-plugin-version/check.ts new file mode 100644 index 00000000..ad011791 --- /dev/null +++ b/test/general-tests/starknet-plugin-version/check.ts @@ -0,0 +1,3 @@ +import { hardhatStarknetPluginVersion } from "../../utils/cli-functions"; + +hardhatStarknetPluginVersion(); diff --git a/test/general-tests/starknet-verify/check.sh b/test/general-tests/starknet-verify/check.sh deleted file mode 100755 index 0202ab5c..00000000 --- a/test/general-tests/starknet-verify/check.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - - -set -e - -echo "The starknet-verify test is too flaky so it is temporarily suspended. Make sure it's working!" -exit 0 - -MAIN_CONTRACT=contracts/contract.cairo -UTIL_CONTRACT=contracts/util.cairo - -npx hardhat starknet-compile $MAIN_CONTRACT $UTIL_CONTRACT - -echo "Waiting for deployment to be accepted" -output=$(npx hardhat starknet-deploy --starknet-network "$NETWORK" contract --inputs 10 --wait) -address=$(echo $output | sed -r "s/.*Contract address: (\w*).*/\1/") -echo "Verifying contract at $address" - -echo "Sleeping to allow Voyager to index the deployment" -sleep 1m - -npx hardhat starknet-verify --starknet-network "$NETWORK" --path $MAIN_CONTRACT $UTIL_CONTRACT --address $address --compiler-version 0.9.0 --license "No License (None)" --account-contract false -echo "Sleeping to allow Voyager to register the verification" -sleep 15s - -is_verified=$(curl "https://goerli.voyager.online/api/contract/$address/code" | jq ".abiVerified") -if [ "$is_verified" == "true" ]; then - echo "Successfully verified!" -else - echo "$0: Error: Not verified!" - exit 1 -fi diff --git a/test/general-tests/starknet-verify/check.ts b/test/general-tests/starknet-verify/check.ts new file mode 100644 index 00000000..e878e007 --- /dev/null +++ b/test/general-tests/starknet-verify/check.ts @@ -0,0 +1,36 @@ +import { assertEqual, ensureEnvVar, exec, extractAddress } from "../../utils/utils"; +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import axios from "axios"; + +console.log( + "The starknet-verify test is too flaky so it is temporarily suspended. Make sure it's working!" +); +process.exit(0); + +const network = ensureEnvVar("NETWORK"); + +const mainContract = "contracts/contract.cairo"; +const utilContract = "contracts/util.cairo"; + +hardhatStarknetCompile(`${mainContract} ${utilContract}`.split(" ")); + +console.log("Waiting for deployment to be accepted"); +const output = hardhatStarknetDeploy( + `--starknet-network ${network} contract --inputs 10 --wait`.split(" ") +); +const address = extractAddress(output.stdout, "Contract address: "); +console.log("Verifying contract at $address"); + +console.log("Sleeping to allow Voyager to index the deployment"); +exec("sleep 1m"); + +exec( + `npx hardhat starknet-verify --starknet-network ${network} --path ${mainContract} ${utilContract} --address ${address} --compiler-version 0.9.0 --license "No License (None)" --account-contract false` +); +console.log("Sleeping to allow Voyager to register the verification"); +exec("sleep 15s"); + +(async () => { + const { data } = await axios.get(`https://goerli.voyager.online/api/contract/${address}/code`); + assertEqual(data.abiVerified, "true", "Contract is not verified"); +})(); diff --git a/test/general-tests/wallet-test/check.sh b/test/general-tests/wallet-test/check.sh deleted file mode 100755 index 14aa9838..00000000 --- a/test/general-tests/wallet-test/check.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -set -eu - -export ACCOUNT_DIR="$HOME/.starknet_accounts_wallet_test" - -npx hardhat starknet-compile contracts/contract.cairo - -../scripts/deploy-funded-cli-account.sh - -npx hardhat test --no-compile test/wallet-test.ts - -output=$(npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10) -echo $output -ADDRESS=$(echo $output | sed -r "s/.*Contract address: (\w*).*/\1/") - -npx hardhat starknet-call --contract contract --function get_balance --address "$ADDRESS" --wallet OpenZeppelin --starknet-network "$NETWORK" -npx hardhat starknet-invoke --contract contract --function increase_balance --inputs "10 20" --address "$ADDRESS" --wallet OpenZeppelin --starknet-network "$NETWORK" diff --git a/test/general-tests/wallet-test/check.ts b/test/general-tests/wallet-test/check.ts new file mode 100644 index 00000000..406d3965 --- /dev/null +++ b/test/general-tests/wallet-test/check.ts @@ -0,0 +1,41 @@ +import path from "path"; +import { + hardhatStarknetCompile, + hardhatStarknetTest, + hardhatStarknetDeploy, + hardhatStarknetCall, + hardhatStarknetInvoke +} from "../../utils/cli-functions"; +import { deployFundedAccount } from "../../utils/deploy-funded-account"; +import { ensureEnvVar, extractAddress } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); +const home = ensureEnvVar("HOME"); + +const accountDir = path.join(home, ".starknet_accounts_wallet_test"); +process.env.ACCOUNT_DIR = accountDir; + +hardhatStarknetCompile(["contracts/contract.cairo"]); + +(async () => { + await deployFundedAccount(); + hardhatStarknetTest("--no-compile test/wallet-test.ts".split(" ")); + + const output = hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) + ); + const address = extractAddress(output.stdout, "Contract address: "); + + hardhatStarknetCall( + `--contract contract --function get_balance --address ${address} --wallet OpenZeppelin --starknet-network ${network}`.split( + " " + ) + ); + hardhatStarknetInvoke( + `--contract contract --function increase_balance --inputs "10 20" --address ${address} --wallet OpenZeppelin --starknet-network ${network}`.split( + " " + ) + ); +})(); diff --git a/test/integrated-devnet-tests/with-active-venv-args/check.sh b/test/integrated-devnet-tests/with-active-venv-args/check.sh deleted file mode 100755 index 717b327a..00000000 --- a/test/integrated-devnet-tests/with-active-venv-args/check.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/integrated-devnet-args.test.ts -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-active-venv-args/check.ts b/test/integrated-devnet-tests/with-active-venv-args/check.ts new file mode 100644 index 00000000..05604214 --- /dev/null +++ b/test/integrated-devnet-tests/with-active-venv-args/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + hardhatStarknetTest("--no-compile test/integrated-devnet-args.test.ts".split(" ")); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-active-venv/check.sh b/test/integrated-devnet-tests/with-active-venv/check.sh deleted file mode 100755 index c12e2dbd..00000000 --- a/test/integrated-devnet-tests/with-active-venv/check.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -source ../my-venv/bin/activate - -check_devnet_is_not_running -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/integrated-devnet.test.ts -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-active-venv/check.ts b/test/integrated-devnet-tests/with-active-venv/check.ts new file mode 100644 index 00000000..7fbc39ec --- /dev/null +++ b/test/integrated-devnet-tests/with-active-venv/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + hardhatStarknetTest("--no-compile test/integrated-devnet.test.ts".split(" ")); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-default/check.sh b/test/integrated-devnet-tests/with-default/check.sh deleted file mode 100755 index b01520aa..00000000 --- a/test/integrated-devnet-tests/with-default/check.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/integrated-devnet.test.ts -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-default/check.ts b/test/integrated-devnet-tests/with-default/check.ts new file mode 100644 index 00000000..7fbc39ec --- /dev/null +++ b/test/integrated-devnet-tests/with-default/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + hardhatStarknetTest("--no-compile test/integrated-devnet.test.ts".split(" ")); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-docker-address-occupied/check.sh b/test/integrated-devnet-tests/with-docker-address-occupied/check.sh deleted file mode 100755 index a60a19fc..00000000 --- a/test/integrated-devnet-tests/with-docker-address-occupied/check.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -set -e - -trap 'kill $(jobs -p)' EXIT - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running - -# run devnet which will cause integrated-devnet to fail -starknet-devnet --host 127.0.0.1 --port 5050 --accounts 0 & - -npx hardhat starknet-compile contracts/contract.cairo - -npx hardhat test --no-compile test/integrated-devnet.test.ts 2>&1 | - ../scripts/assert-contains.py "127.0.0.1:5050 already occupied." diff --git a/test/integrated-devnet-tests/with-docker-address-occupied/check.ts b/test/integrated-devnet-tests/with-docker-address-occupied/check.ts new file mode 100644 index 00000000..e730d529 --- /dev/null +++ b/test/integrated-devnet-tests/with-docker-address-occupied/check.ts @@ -0,0 +1,23 @@ +import { spawn } from "child_process"; +import { checkDevnetIsNotRunning, assertContains } from "../../utils/utils"; +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +(async () => { + await checkDevnetIsNotRunning(); + // run devnet which will cause integrated-devnet to fail + const result = spawn( + "starknet-devnet", + "--host 127.0.0.1 --port 5050 --accounts 0".split(" "), + { detached: true } + ); + hardhatStarknetCompile(["contracts/contract.cairo"]); + + const execution = hardhatStarknetTest( + "--no-compile test/integrated-devnet.test.ts".split(" "), + true + ); + assertContains(execution.stderr, "127.0.0.1:5050 already occupied."); + result.kill(); + + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-docker-args/check.sh b/test/integrated-devnet-tests/with-docker-args/check.sh deleted file mode 100755 index 717b327a..00000000 --- a/test/integrated-devnet-tests/with-docker-args/check.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/integrated-devnet-args.test.ts -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-docker-args/check.ts b/test/integrated-devnet-tests/with-docker-args/check.ts new file mode 100644 index 00000000..05604214 --- /dev/null +++ b/test/integrated-devnet-tests/with-docker-args/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + hardhatStarknetTest("--no-compile test/integrated-devnet-args.test.ts".split(" ")); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-docker-wrong-cli/check.sh b/test/integrated-devnet-tests/with-docker-wrong-cli/check.sh deleted file mode 100755 index 8fb411a6..00000000 --- a/test/integrated-devnet-tests/with-docker-wrong-cli/check.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running - -npx hardhat starknet-compile contracts/contract.cairo - -npx hardhat test --no-compile test/integrated-devnet.test.ts 2>&1 | - ../scripts/assert-contains.py "starknet-devnet: error: --accounts must be a positive integer; got: invalid_value." diff --git a/test/integrated-devnet-tests/with-docker-wrong-cli/check.ts b/test/integrated-devnet-tests/with-docker-wrong-cli/check.ts new file mode 100644 index 00000000..32d84be0 --- /dev/null +++ b/test/integrated-devnet-tests/with-docker-wrong-cli/check.ts @@ -0,0 +1,16 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning, assertContains } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + const execution = hardhatStarknetTest( + "--no-compile test/integrated-devnet.test.ts".split(" "), + true + ); + assertContains( + execution.stderr, + "starknet-devnet: error: --accounts must be a positive integer; got: invalid_value." + ); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-docker/check.sh b/test/integrated-devnet-tests/with-docker/check.sh deleted file mode 100755 index b01520aa..00000000 --- a/test/integrated-devnet-tests/with-docker/check.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/integrated-devnet.test.ts -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-docker/check.ts b/test/integrated-devnet-tests/with-docker/check.ts new file mode 100644 index 00000000..7fbc39ec --- /dev/null +++ b/test/integrated-devnet-tests/with-docker/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + hardhatStarknetTest("--no-compile test/integrated-devnet.test.ts".split(" ")); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-stderr-to-STDERR/check.sh b/test/integrated-devnet-tests/with-stderr-to-STDERR/check.sh deleted file mode 100755 index 9d814039..00000000 --- a/test/integrated-devnet-tests/with-stderr-to-STDERR/check.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running - -EXPECTED_STDOUT="Account #0" -EXPECTED_WARNING="WARNING: Use these accounts and their keys ONLY for local testing. DO NOT use them on mainnet or other live networks because you will LOSE FUNDS." - -npx hardhat starknet-compile contracts/contract.cairo - -npx hardhat test --no-compile test/integrated-devnet.test.ts 2>&1 | - ../scripts/assert-contains.py "$EXPECTED_WARNING" - - -# Checks if file logs/stdout.log exists and contains the expected warning string -if [ -f logs/stdout.log ]; then - if ! grep -q "$EXPECTED_STDOUT" logs/stdout.log; then - echo "Expected warning to contain '$EXPECTED_STDOUT'" - exit 1 - fi -else - echo "Expected logs/stdout.log to exist" - exit 1 -fi - -echo "Success" -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-stderr-to-STDERR/check.ts b/test/integrated-devnet-tests/with-stderr-to-STDERR/check.ts new file mode 100644 index 00000000..66262672 --- /dev/null +++ b/test/integrated-devnet-tests/with-stderr-to-STDERR/check.ts @@ -0,0 +1,26 @@ +import { readFileSync } from "fs"; +import { assertExists, checkDevnetIsNotRunning, assertContains } from "../../utils/utils"; +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +(async () => { + await checkDevnetIsNotRunning(); + + const expectedStdout = "Account #0"; + const expectedWarning = + "WARNING: Use these accounts and their keys ONLY for local testing. DO NOT use them on mainnet or other live networks because you will LOSE FUNDS."; + + hardhatStarknetCompile(["contracts/contract.cairo"]); + const execution = hardhatStarknetTest( + "--no-compile test/integrated-devnet.test.ts".split(" "), + true + ); + assertContains(execution.stderr, expectedWarning); + + // Checks if file logs/stderr.log exists and contains the expected warning string + assertExists("logs/stdout.log", "Expected logs/stdout.log to exist"); + const stdout = readFileSync("logs/stdout.log", "utf-8"); + assertContains(stdout, expectedStdout); + + console.log("Success"); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-stderr-to-STDOUT/check.sh b/test/integrated-devnet-tests/with-stderr-to-STDOUT/check.sh deleted file mode 100755 index 8fb411a6..00000000 --- a/test/integrated-devnet-tests/with-stderr-to-STDOUT/check.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running - -npx hardhat starknet-compile contracts/contract.cairo - -npx hardhat test --no-compile test/integrated-devnet.test.ts 2>&1 | - ../scripts/assert-contains.py "starknet-devnet: error: --accounts must be a positive integer; got: invalid_value." diff --git a/test/integrated-devnet-tests/with-stderr-to-STDOUT/check.ts b/test/integrated-devnet-tests/with-stderr-to-STDOUT/check.ts new file mode 100644 index 00000000..2b5b6182 --- /dev/null +++ b/test/integrated-devnet-tests/with-stderr-to-STDOUT/check.ts @@ -0,0 +1,17 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning, assertContains } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + const execution = hardhatStarknetTest( + "--no-compile test/integrated-devnet.test.ts".split(" "), + true + ); + assertContains( + execution.stderr, + "starknet-devnet: error: --accounts must be a positive integer; got: invalid_value." + ); + + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-stderr-to-file/check.sh b/test/integrated-devnet-tests/with-stderr-to-file/check.sh deleted file mode 100755 index 587d517b..00000000 --- a/test/integrated-devnet-tests/with-stderr-to-file/check.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running -npx hardhat starknet-compile contracts/contract.cairo - -EXPECTED_STDOUT="Account #0" -EXPECTED_WARNING="WARNING: Use these accounts and their keys ONLY for local testing. DO NOT use them on mainnet or other live networks because you will LOSE FUNDS." - -output=$(npx hardhat test --no-compile test/integrated-devnet.test.ts) -# Checks if output contains the expected string from stdout -if ! echo "$output" | grep -q "$EXPECTED_STDOUT"; then - echo "Expected output to contain '$EXPECTED_STDOUT'" - exit 1 -fi - - -# Checks if file logs/stderr.log exists and contains the expected warning string -if [ -f logs/stderr.log ]; then - if ! grep -q "$EXPECTED_WARNING" logs/stderr.log; then - echo "Expected warning to contain '$EXPECTED_WARNING'" - exit 1 - fi -else - echo "Expected logs/stderr.log to exist" - exit 1 -fi - -echo "Success" -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-stderr-to-file/check.ts b/test/integrated-devnet-tests/with-stderr-to-file/check.ts new file mode 100644 index 00000000..827daf77 --- /dev/null +++ b/test/integrated-devnet-tests/with-stderr-to-file/check.ts @@ -0,0 +1,24 @@ +import { readFileSync } from "fs"; +import { assertExists, checkDevnetIsNotRunning, assertContains } from "../../utils/utils"; +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + + const expectedStdout = "Account #0"; + const expectedWarning = + "WARNING: Use these accounts and their keys ONLY for local testing. DO NOT use them on mainnet or other live networks because you will LOSE FUNDS."; + + const output = hardhatStarknetTest("--no-compile test/integrated-devnet.test.ts".split(" ")); + // Checks if output contains the expected string from stdout + assertContains(output.stdout, expectedStdout); + + // Checks if file logs/stderr.log exists and contains the expected warning string + assertExists("logs/stderr.log", "Expected logs/stderr.log to exist"); + const stderr = readFileSync("logs/stderr.log", "utf-8"); + assertContains(stderr, expectedWarning); + + console.log("Success"); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-venv-address-occupied/check.sh b/test/integrated-devnet-tests/with-venv-address-occupied/check.sh deleted file mode 100755 index 54e376dd..00000000 --- a/test/integrated-devnet-tests/with-venv-address-occupied/check.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -set -e - -trap 'kill $(jobs -p)' EXIT - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running -starknet-devnet --host 127.0.0.1 --port 5050 --accounts 0 & - -npx hardhat starknet-compile contracts/contract.cairo - -npx hardhat test --no-compile test/integrated-devnet.test.ts 2>&1 | - ../scripts/assert-contains.py "127.0.0.1:5050 already occupied." diff --git a/test/integrated-devnet-tests/with-venv-address-occupied/check.ts b/test/integrated-devnet-tests/with-venv-address-occupied/check.ts new file mode 100644 index 00000000..ae83a92b --- /dev/null +++ b/test/integrated-devnet-tests/with-venv-address-occupied/check.ts @@ -0,0 +1,22 @@ +import { spawn } from "child_process"; +import { checkDevnetIsNotRunning, assertContains } from "../../utils/utils"; +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; + +(async () => { + await checkDevnetIsNotRunning(); + + const result = spawn( + "starknet-devnet", + "--host 127.0.0.1 --port 5050 --accounts 0".split(" "), + { detached: true } + ); + hardhatStarknetCompile(["contracts/contract.cairo"]); + const execution = hardhatStarknetTest( + "--no-compile test/integrated-devnet.test.ts".split(" "), + true + ); + assertContains(execution.stderr, "127.0.0.1:5050 already occupied."); + result.kill(); + + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-venv-wrong-cli/check.sh b/test/integrated-devnet-tests/with-venv-wrong-cli/check.sh deleted file mode 100755 index 8fb411a6..00000000 --- a/test/integrated-devnet-tests/with-venv-wrong-cli/check.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running - -npx hardhat starknet-compile contracts/contract.cairo - -npx hardhat test --no-compile test/integrated-devnet.test.ts 2>&1 | - ../scripts/assert-contains.py "starknet-devnet: error: --accounts must be a positive integer; got: invalid_value." diff --git a/test/integrated-devnet-tests/with-venv-wrong-cli/check.ts b/test/integrated-devnet-tests/with-venv-wrong-cli/check.ts new file mode 100644 index 00000000..69ec938f --- /dev/null +++ b/test/integrated-devnet-tests/with-venv-wrong-cli/check.ts @@ -0,0 +1,18 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning, assertContains } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + + hardhatStarknetCompile(["contracts/contract.cairo"]); + const execution = hardhatStarknetTest( + "--no-compile test/integrated-devnet.test.ts".split(" "), + true + ); + assertContains( + execution.stderr, + "starknet-devnet: error: --accounts must be a positive integer; got: invalid_value." + ); + + await checkDevnetIsNotRunning(); +})(); diff --git a/test/integrated-devnet-tests/with-venv/check.sh b/test/integrated-devnet-tests/with-venv/check.sh deleted file mode 100755 index b01520aa..00000000 --- a/test/integrated-devnet-tests/with-venv/check.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e - -source ../scripts/check-devnet-is-not-running.sh - -check_devnet_is_not_running -npx hardhat starknet-compile contracts/contract.cairo -npx hardhat test --no-compile test/integrated-devnet.test.ts -check_devnet_is_not_running diff --git a/test/integrated-devnet-tests/with-venv/check.ts b/test/integrated-devnet-tests/with-venv/check.ts new file mode 100644 index 00000000..7fbc39ec --- /dev/null +++ b/test/integrated-devnet-tests/with-venv/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions"; +import { checkDevnetIsNotRunning } from "../../utils/utils"; + +(async () => { + await checkDevnetIsNotRunning(); + hardhatStarknetCompile(["contracts/contract.cairo"]); + hardhatStarknetTest("--no-compile test/integrated-devnet.test.ts".split(" ")); + await checkDevnetIsNotRunning(); +})(); diff --git a/test/recompilation-tests/hardhat-run/check.sh b/test/recompilation-tests/hardhat-run/check.sh deleted file mode 100755 index 489f04f5..00000000 --- a/test/recompilation-tests/hardhat-run/check.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -set -e - -# Hardhat run command -echo "Testing Recompilation with deleted artifact on hardhat run" -rm -rf starknet-artifacts/contracts/contract.cairo -npx hardhat run --no-compile scripts/deploy.ts - -echo "Testing Recompilation with cache file deleted on hardhat run" -rm -rf cache/cairo-files-cache.json -npx hardhat run --no-compile scripts/deploy.ts diff --git a/test/recompilation-tests/hardhat-run/check.ts b/test/recompilation-tests/hardhat-run/check.ts new file mode 100644 index 00000000..1aa83398 --- /dev/null +++ b/test/recompilation-tests/hardhat-run/check.ts @@ -0,0 +1,11 @@ +import { hardhatStarknetRun } from "../../utils/cli-functions"; +import { rmrfSync } from "../../utils/utils"; + +// Hardhat run command +console.log("should recompile with deleted artifact on hardhat run"); +rmrfSync("starknet-artifacts/contracts/contract.cairo"); +hardhatStarknetRun("--no-compile scripts/deploy.ts".split(" ")); + +console.log("should recompile with cache file deleted on hardhat run"); +rmrfSync("cache/cairo-files-cache.json"); +hardhatStarknetRun("--no-compile scripts/deploy.ts".split(" ")); diff --git a/test/recompilation-tests/hardhat-test/check.sh b/test/recompilation-tests/hardhat-test/check.sh deleted file mode 100755 index 3e789826..00000000 --- a/test/recompilation-tests/hardhat-test/check.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash -set -e - -CONTRACT_NAME=contract_test_cache.cairo -CONTRACT_PATH="contracts/${CONTRACT_NAME}" - -DEPENDENCY_NAME=dependency.cairo -DEPENDENCY_PATH="contracts/${DEPENDENCY_NAME}" - -rm -rf $DEPENDENCY_PATH $CONTRACT_PATH -# Hardhat test command -echo "Testing Recompilation with new contract added" -cp "$(dirname "$0")/$CONTRACT_NAME" "$CONTRACT_PATH" -cp "$(dirname "$0")/$DEPENDENCY_NAME" "$DEPENDENCY_PATH" -npx hardhat test --no-compile test/recompilation/recompilation-main-test.ts - -echo "Testing Recompilation with artifacts deleted" -rm -rf starknet-artifacts/contracts/contract.cairo -npx hardhat test --no-compile test/recompilation/recompilation-main-test.ts - -echo "Testing Recompilation with updated contract" -# Appending a new function to the contract -cat "$(dirname "$0")/get_balance.cairo" >> contracts/contract_test_cache.cairo -npx hardhat test --no-compile test/recompilation/recompilation-update-test.ts - -echo "Testing Recompilation with cache file deleted" -rm -rf cache/cairo-files-cache.json -npx hardhat test --no-compile test/recompilation/recompilation-main-test.ts - -echo "Testing Recompilation with dependency changed" -echo "//" >> "$DEPENDENCY_PATH" -npx hardhat test --no-compile test/recompilation/recompilation-dependency-test.ts - -echo "Testing Recompilation with source deleted" -cp cache/cairo-files-cache.json cache-content-before.json -rm -rf contracts/contract_test_cache.cairo -npx hardhat test --no-compile test/recompilation/recompilation-main-test.ts -# Check that the cache file was updated using diff -if diff -q cache-content-before.json cache/cairo-files-cache.json; then - echo "Cache file was not updated" - exit 1 -fi - -echo "Testing Recompilation one contract added another deleted" -rm -f contracts/contract_test_cache.cairo -rm -f contracts/dependency.cairo - -CONTRACT_WITH_NO_DEPENDENCY=contract_test_cache_no_dependency.cairo -cp "$(dirname "$0")/$CONTRACT_WITH_NO_DEPENDENCY" "$CONTRACT_PATH" - -npx hardhat test --no-compile test/recompilation/recompilation-main-test.ts diff --git a/test/recompilation-tests/hardhat-test/check.ts b/test/recompilation-tests/hardhat-test/check.ts new file mode 100644 index 00000000..5e2c612a --- /dev/null +++ b/test/recompilation-tests/hardhat-test/check.ts @@ -0,0 +1,58 @@ +import { copyFileSync, readFileSync, appendFileSync } from "fs"; +import path from "path"; +import { hardhatStarknetTest } from "../../utils/cli-functions"; +import { assertNotEqual, rmrfSync } from "../../utils/utils"; + +const prefix = path.join(__dirname); +const contractName = "contract_test_cache.cairo"; +const contractPath = path.join("contracts", contractName); + +const dependencyName = "dependency.cairo"; +const dependencyPath = path.join("contracts", dependencyName); + +rmrfSync(dependencyPath); +rmrfSync(contractPath); +// Hardhat test command +console.log("Testing Recompilation with new contract added"); +copyFileSync(path.join(prefix, contractName), contractPath); +copyFileSync(path.join(prefix, dependencyName), dependencyPath); + +hardhatStarknetTest("--no-compile test/recompilation/recompilation-main-test.ts".split(" ")); + +console.log("Testing Recompilation with artifacts deleted"); +rmrfSync("starknet-artifacts/contracts/contract.cairo"); +hardhatStarknetTest("--no-compile test/recompilation/recompilation-main-test.ts".split(" ")); + +console.log("Testing Recompilation with updated contract"); +// Appending a new function to the contract +appendFileSync( + "contracts/contract_test_cache.cairo", + readFileSync(path.join(prefix, "get_balance.cairo")).toString() +); +hardhatStarknetTest("--no-compile test/recompilation/recompilation-update-test.ts".split(" ")); + +console.log("Testing Recompilation with cache file deleted"); +rmrfSync("cache/cairo-files-cache.json"); +hardhatStarknetTest("--no-compile test/recompilation/recompilation-main-test.ts".split(" ")); + +console.log("Testing Recompilation with dependency changed"); +appendFileSync(dependencyPath, "//"); +hardhatStarknetTest("--no-compile test/recompilation/recompilation-dependency-test.ts".split(" ")); + +console.log("Testing Recompilation with source deleted"); +copyFileSync("cache/cairo-files-cache.json", "cache-content-before.json"); +rmrfSync("contracts/contract_test_cache.cairo"); +hardhatStarknetTest("--no-compile test/recompilation/recompilation-main-test.ts".split(" ")); +// Check that the cache file was updated using diff +const cacheContentAfter = readFileSync("cache/cairo-files-cache.json"); +const cacheContentBefore = readFileSync("cache-content-before.json"); +assertNotEqual(cacheContentAfter, cacheContentBefore, "Cache file was not updated."); + +console.log("Testing Recompilation one contract added another deleted"); +rmrfSync("contracts/contract_test_cache.cairo"); +rmrfSync("contracts/dependency.cairo"); + +const contractWithNoDependency = "contract_test_cache_no_dependency.cairo"; +copyFileSync(path.join(prefix, contractWithNoDependency), contractPath); + +hardhatStarknetTest("--no-compile test/recompilation/recompilation-main-test.ts".split(" ")); diff --git a/test/recompilation-tests/recompiler-default/check.sh b/test/recompilation-tests/recompiler-default/check.sh deleted file mode 100755 index d25e085f..00000000 --- a/test/recompilation-tests/recompiler-default/check.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -set -e - -EXPECTED='StarknetPluginError: Artifact expected to be at' - -echo "Testing with deleted artifact on recompiler option set to default (off)" -rm -rf starknet-artifacts/contracts/contract.cairo/ -npx hardhat run --no-compile scripts/deploy.ts 2>&1 | - ../scripts/assert-contains.py "$EXPECTED" -echo "Success" diff --git a/test/recompilation-tests/recompiler-default/check.ts b/test/recompilation-tests/recompiler-default/check.ts new file mode 100644 index 00000000..1389613b --- /dev/null +++ b/test/recompilation-tests/recompiler-default/check.ts @@ -0,0 +1,9 @@ +import { hardhatStarknetRun } from "../../utils/cli-functions"; +import { assertContains, rmrfSync } from "../../utils/utils"; + +const expected = "StarknetPluginError: Artifact expected to be at"; + +console.log("Testing with deleted artifact on recompiler option set to default (off)"); +rmrfSync("starknet-artifacts/contracts/contract.cairo"); +const execution = hardhatStarknetRun("--no-compile scripts/deploy.ts".split(" "), true); +assertContains(execution.stderr, expected); diff --git a/test/recompilation-tests/recompiler-off/check.sh b/test/recompilation-tests/recompiler-off/check.sh deleted file mode 100755 index d18160b4..00000000 --- a/test/recompilation-tests/recompiler-off/check.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -set -e - -EXPECTED='StarknetPluginError: Artifact expected to be at' - -echo "Testing with deleted artifact on recompiler option set to false" -rm -rf starknet-artifacts/contracts/contract.cairo/ -npx hardhat run --no-compile scripts/deploy.ts 2>&1 | - ../scripts/assert-contains.py "$EXPECTED" -echo "Success" diff --git a/test/recompilation-tests/recompiler-off/check.ts b/test/recompilation-tests/recompiler-off/check.ts new file mode 100644 index 00000000..355b3f58 --- /dev/null +++ b/test/recompilation-tests/recompiler-off/check.ts @@ -0,0 +1,10 @@ +import { hardhatStarknetRun } from "../../utils/cli-functions"; +import { assertContains, rmrfSync } from "../../utils/utils"; + +const expected = "StarknetPluginError: Artifact expected to be at"; + +console.log("Testing with deleted artifact on recompiler option set to default (off)"); +rmrfSync("starknet-artifacts/contracts/contract.cairo"); +const execution = hardhatStarknetRun("--no-compile scripts/deploy.ts".split(" "), true); +assertContains(execution.stderr, expected); +console.log("Success"); diff --git a/test/recompilation-tests/starknet-call/check.sh b/test/recompilation-tests/starknet-call/check.sh deleted file mode 100755 index f3097299..00000000 --- a/test/recompilation-tests/starknet-call/check.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -set -e - -echo "Testing Recompilation with deleted artifact on starknet-call" -output=$(npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10) - -tail=${output#*$'\n'Contract address: } -address=${tail%%$'\n'*} - -rm -rf starknet-artifacts/contracts/contract.cairo -npx hardhat starknet-call --starknet-network "$NETWORK" --contract contract --function sum_points_to_tuple --address "$address" --inputs "10 20 30 40" diff --git a/test/recompilation-tests/starknet-call/check.ts b/test/recompilation-tests/starknet-call/check.ts new file mode 100644 index 00000000..d26ad033 --- /dev/null +++ b/test/recompilation-tests/starknet-call/check.ts @@ -0,0 +1,19 @@ +import { hardhatStarknetDeploy, hardhatStarknetCall } from "../../utils/cli-functions"; +import { extractAddress, ensureEnvVar, rmrfSync } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +console.log("Testing Recompilation with deleted artifact on starknet-call"); +const output = hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) +); +const address = extractAddress(output.stdout, "Contract address: "); + +rmrfSync("starknet-artifacts/contracts/contract.cairo"); +hardhatStarknetCall( + `--starknet-network ${network} --contract contract --function sum_points_to_tuple --address ${address} --inputs "10 20 30 40"`.split( + " " + ) +); diff --git a/test/recompilation-tests/starknet-deploy/check.sh b/test/recompilation-tests/starknet-deploy/check.sh deleted file mode 100755 index 572c6bf3..00000000 --- a/test/recompilation-tests/starknet-deploy/check.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -set -e - -# Hardhat starknet-deploy command -echo "Testing Recompilation with deleted artifact on starknet-deploy" -rm -rf starknet-artifacts/contracts/contract.cairo -npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10 diff --git a/test/recompilation-tests/starknet-deploy/check.ts b/test/recompilation-tests/starknet-deploy/check.ts new file mode 100644 index 00000000..db397d17 --- /dev/null +++ b/test/recompilation-tests/starknet-deploy/check.ts @@ -0,0 +1,13 @@ +import { hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar, rmrfSync } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +// Hardhat starknet-deploy command +console.log("Testing Recompilation with deleted artifact on starknet-deploy"); +rmrfSync("starknet-artifacts/contracts/contract.cairo"); +hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) +); diff --git a/test/recompilation-tests/starknet-invoke/check.sh b/test/recompilation-tests/starknet-invoke/check.sh deleted file mode 100755 index 42bcbdc2..00000000 --- a/test/recompilation-tests/starknet-invoke/check.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -set -e - -# Hardhat starknet-invoke command -echo "Testing Recompilation with deleted artifact on hardhat starknet-invoke" -output=$(npx hardhat starknet-deploy --starknet-network "$NETWORK" starknet-artifacts/contracts/contract.cairo/ --inputs 10) -# Grab the output Contract address to a variable using parameter expansion -tail=${output#*$'\n'Contract address: } -address=${tail%%$'\n'*} - -# Remove artifact contract to force recompilation -rm -rf starknet-artifacts/contracts/contract.cairo - -export ACCOUNT_DIR="$HOME/.starknet_accounts_recompile_test" -../scripts/deploy-funded-cli-account.sh - -npx hardhat starknet-invoke \ - --starknet-network "$NETWORK" \ - --contract contract \ - --function increase_balance \ - --address "$address" \ - --inputs "10 20" \ - --wallet OpenZeppelin diff --git a/test/recompilation-tests/starknet-invoke/check.ts b/test/recompilation-tests/starknet-invoke/check.ts new file mode 100644 index 00000000..4d950534 --- /dev/null +++ b/test/recompilation-tests/starknet-invoke/check.ts @@ -0,0 +1,26 @@ +import path from "path"; +import { hardhatStarknetDeploy, hardhatStarknetInvoke } from "../../utils/cli-functions"; +import { deployFundedAccount } from "../../utils/deploy-funded-account"; +import { ensureEnvVar, extractAddress, rmrfSync } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); +// Hardhat starknet-invoke command +console.log("Testing Recompilation with deleted artifact on hardhat starknet-invoke"); +const output = hardhatStarknetDeploy( + `--starknet-network ${network} starknet-artifacts/contracts/contract.cairo/ --inputs 10`.split( + " " + ) +); +// Grab the output Contract address to a variable using parameter expansion +const address = extractAddress(output.stdout, "Contract address: "); +// Remove artifact contract to force recompilation +rmrfSync("starknet-artifacts/contracts/contract.cairo"); +const home = ensureEnvVar("HOME"); +const accountDir = path.join(home, ".starknet_accounts_recompile_test"); +process.env.ACCOUNT_DIR = accountDir; + +(async () => { + await deployFundedAccount(); + const args = `--starknet-network ${network} --contract contract --address ${address} --function increase_balance --inputs "10 20" --wallet OpenZeppelin`; + hardhatStarknetInvoke(args.split(" ")); +})(); diff --git a/test/utils/cli-functions.ts b/test/utils/cli-functions.ts new file mode 100644 index 00000000..0d64ec51 --- /dev/null +++ b/test/utils/cli-functions.ts @@ -0,0 +1,50 @@ +import shell from "shelljs"; + +const exec = (cmd: string, silent?: boolean) => { + silent = silent || false; + return shell.exec(cmd, { silent: silent }); +}; + +export const hardhatStarknetCompile = (args: Array, silent?: boolean) => { + return exec(`npx hardhat starknet-compile ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetDeploy = (args: Array, silent?: boolean) => { + return exec(`npx hardhat starknet-deploy ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetInvoke = (args: Array, silent?: boolean) => { + return exec(`npx hardhat starknet-invoke ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetCall = (args: Array, silent?: boolean) => { + return exec(`npx hardhat starknet-call ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetEstimateFee = (args: Array, silent?: boolean) => { + return exec(`npx hardhat starknet-estimate-fee ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetNewAccount = (args: Array, silent?: boolean) => { + return exec(`npx hardhat starknet-new-account ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetDeployAccount = (args: Array, silent?: boolean) => { + return exec(`npx hardhat starknet-deploy-account ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetRun = (args: Array, silent?: boolean) => { + return exec(`npx hardhat run ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetTest = (args: Array, silent?: boolean) => { + return exec(`npx hardhat test ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetMigrate = (args: Array, silent?: boolean) => { + return exec(`npx hardhat migrate ${args.join(" ")}`, silent); +}; + +export const hardhatStarknetPluginVersion = () => { + return exec("npx hardhat starknet-plugin-version"); +}; diff --git a/test/utils/deploy-funded-account.ts b/test/utils/deploy-funded-account.ts new file mode 100644 index 00000000..4b7d02ec --- /dev/null +++ b/test/utils/deploy-funded-account.ts @@ -0,0 +1,32 @@ +import axios from "axios"; +import { readFileSync } from "fs"; +import path from "path"; +import { DEVNET_URL } from "../constants/constants"; +import { hardhatStarknetDeployAccount, hardhatStarknetNewAccount } from "./cli-functions"; +import { assertEqual, ensureEnvVar } from "./utils"; + +export async function deployFundedAccount(url = DEVNET_URL) { + const network = ensureEnvVar("NETWORK"); + const accountDir = ensureEnvVar("ACCOUNT_DIR"); + + assertEqual(network, "devnet", "only works with NETWORK set to devnet"); + + const args = ["--starknet-network", network, "--wallet", "OpenZeppelin"]; + + // Creates new account + hardhatStarknetNewAccount(args); + + const accountFile = path.join(accountDir, "starknet_open_zeppelin_accounts.json"); + const accountAddress = JSON.parse(readFileSync(accountFile, "utf-8"))[network].OpenZeppelin + .address; + + console.log(`Funding account ${accountAddress} on ${network}.`); + await axios.post(`${url}/mint`, { + address: accountAddress, + amount: 1000000000000000000n, + lite: true + }); + + // Deploying funded account on the network + hardhatStarknetDeployAccount(args); +} diff --git a/test/utils/utils.ts b/test/utils/utils.ts new file mode 100644 index 00000000..8a77eb99 --- /dev/null +++ b/test/utils/utils.ts @@ -0,0 +1,66 @@ +import axios from "axios"; +import assert, { AssertionError } from "assert"; +import { existsSync, rmSync } from "fs"; +import shell from "shelljs"; +import { DEVNET_URL } from "../constants/constants"; + +export function exec(cmd: string) { + const result = shell.exec(cmd); + assertEqual(result.code, 0, `Command ${cmd} failed.\n${result.stderr}`); + + return result; +} + +export function assertContains(output: string, pattern: string) { + if (!output.includes(pattern)) { + throw new AssertionError({ + message: `Pattern not in input\nPattern: ${pattern}\nInput: ${output}` + }); + } +} + +export function extractAddress(source: string, pattern: string) { + // Replaces all line breaks with a space + source = source.replace(/(\r\n|\n|\r)/gm, " "); + // Take the first word in source after the pattern + const res = source.split(pattern)[1].split(" ")[0]; + return res; +} + +export async function checkDevnetIsNotRunning(url = DEVNET_URL): Promise { + try { + const res = await axios.get(`${url}/is_alive`); + throw new AssertionError({ + message: `Devnet is running and responded with status ${res.status}` + }); + } catch (err) { + console.log("Devnet is not running!"); + } +} + +export function ensureEnvVar(varName: string): string { + if (!process.env[varName]) { + throw new Error(`Env var ${varName} not set or empty`); + } + return process.env[varName] as string; +} + +export function rmrfSync(path: string) { + rmSync(path, { recursive: true, force: true }); +} + +export function assertEqual(val1: unknown, val2: unknown, msg?: string) { + assert.equal(val1, val2, msg); +} + +export function assertNotEqual(val1: unknown, val2: unknown, msg?: string) { + assert.notEqual(val1, val2, msg); +} + +export function assertExists(path: string, msg?: string) { + if (!existsSync(path)) { + throw new AssertionError({ + message: msg + }); + } +} diff --git a/test/venv-tests/with-venv-active/check.sh b/test/venv-tests/with-venv-active/check.sh deleted file mode 100755 index 699cf021..00000000 --- a/test/venv-tests/with-venv-active/check.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -set -e - -source ../my-venv/bin/activate - -INITIAL_VALUE=10 - -npx hardhat starknet-compile -npx hardhat starknet-deploy starknet-artifacts/contracts/contract.cairo/ --starknet-network "$NETWORK" --inputs "$INITIAL_VALUE" diff --git a/test/venv-tests/with-venv-active/check.ts b/test/venv-tests/with-venv-active/check.ts new file mode 100644 index 00000000..de24dc78 --- /dev/null +++ b/test/venv-tests/with-venv-active/check.ts @@ -0,0 +1,13 @@ +import { hardhatStarknetCompile, hardhatStarknetDeploy } from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const network = ensureEnvVar("NETWORK"); + +const initialValue = 10; + +hardhatStarknetCompile([]); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/contract.cairo/ --starknet-network ${network} --inputs ${initialValue}`.split( + " " + ) +); diff --git a/test/venv-tests/with-venv/check.sh b/test/venv-tests/with-venv/check.sh deleted file mode 100755 index d6e02c22..00000000 --- a/test/venv-tests/with-venv/check.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e - -INITIAL_VALUE=10 - -npx hardhat starknet-compile -npx hardhat starknet-deploy starknet-artifacts/contracts/contract.cairo/ --starknet-network "$NETWORK" --inputs "$INITIAL_VALUE" -npx hardhat test --no-compile test/quick-test.ts diff --git a/test/venv-tests/with-venv/check.ts b/test/venv-tests/with-venv/check.ts new file mode 100644 index 00000000..ff0d9061 --- /dev/null +++ b/test/venv-tests/with-venv/check.ts @@ -0,0 +1,17 @@ +import { + hardhatStarknetCompile, + hardhatStarknetDeploy, + hardhatStarknetTest +} from "../../utils/cli-functions"; +import { ensureEnvVar } from "../../utils/utils"; + +const initialValue = 10; +const network = ensureEnvVar("NETWORK"); + +hardhatStarknetCompile([]); +hardhatStarknetDeploy( + `starknet-artifacts/contracts/contract.cairo/ --starknet-network ${network} --inputs ${initialValue}`.split( + " " + ) +); +hardhatStarknetTest("--no-compile test/quick-test.ts".split(" "));