-
Notifications
You must be signed in to change notification settings - Fork 979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve slither-mutate testing #2482
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c3a5fb2
Add tests to slither-mutate and fix some Python 3.8 compatibility iss…
DarkaMaul 7b983e3
Fix typo (thanks Gustavo)
DarkaMaul f1df3ea
Run tests only if forge is available.
DarkaMaul 94fc82c
Run tools tests in CI.
DarkaMaul 43301c7
Remove last wrongly typed annotation
DarkaMaul 5c0eccf
Fix test in CI.
DarkaMaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Counter | ||
|
||
Init using : | ||
|
||
```shell | ||
forge install --no-commit --no-git . | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[profile.default] | ||
src = 'src' | ||
out = 'out' | ||
libs = ['lib'] | ||
solc = "0.8.15" | ||
|
||
# See more config options https://github.com/foundry-rs/foundry/tree/master/config |
12 changes: 12 additions & 0 deletions
12
tests/tools/mutator/test_data/test_source_unit/script/Counter.s.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.13; | ||
|
||
import {Script, console} from "forge-std/Script.sol"; | ||
|
||
contract CounterScript is Script { | ||
function setUp() public {} | ||
|
||
function run() public { | ||
vm.broadcast(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/tools/mutator/test_data/test_source_unit/src/Counter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.15; | ||
|
||
contract Counter { | ||
uint256 public number; | ||
|
||
function setNumber(uint256 newNumber) public { | ||
number = newNumber; | ||
} | ||
|
||
function increment() public { | ||
number++; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/tools/mutator/test_data/test_source_unit/test/Counter.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.15; | ||
|
||
import {Test, console} from "forge-std/Test.sol"; | ||
import {Counter} from "../src/Counter.sol"; | ||
|
||
contract CounterTest is Test { | ||
Counter public counter; | ||
|
||
function setUp() public { | ||
counter = new Counter(); | ||
counter.setNumber(0); | ||
} | ||
|
||
function test_Increment() public { | ||
counter.increment(); | ||
assertEq(counter.number(), 1); | ||
} | ||
|
||
function testFuzz_SetNumber(uint256 x) public { | ||
counter.setNumber(x); | ||
assertEq(counter.number(), x); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import argparse | ||
from contextlib import contextmanager | ||
import os | ||
from pathlib import Path | ||
import shutil | ||
import subprocess | ||
import tempfile | ||
from unittest import mock | ||
|
||
import pytest | ||
from slither import Slither | ||
from slither.tools.mutator.__main__ import _get_mutators, main | ||
from slither.tools.mutator.utils.testing_generated_mutant import run_test_cmd | ||
from slither.tools.mutator.utils.file_handling import get_sol_file_list, backup_source_file | ||
|
||
|
||
TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data" | ||
|
||
foundry_available = shutil.which("forge") is not None | ||
project_ready = Path(TEST_DATA_DIR, "test_source_unit/lib/forge-std").exists() | ||
|
||
|
||
@contextmanager | ||
def change_directory(new_dir): | ||
original_dir = os.getcwd() | ||
os.chdir(new_dir) | ||
try: | ||
yield | ||
finally: | ||
os.chdir(original_dir) | ||
|
||
|
||
def test_get_mutators(): | ||
|
||
mutators = _get_mutators(None) | ||
assert mutators | ||
|
||
mutators = _get_mutators(["ASOR"]) | ||
assert len(mutators) == 1 | ||
assert mutators[0].NAME == "ASOR" | ||
|
||
mutators = _get_mutators(["ASOR", "NotExisiting"]) | ||
assert len(mutators) == 1 | ||
|
||
|
||
@mock.patch( | ||
"argparse.ArgumentParser.parse_args", | ||
return_value=argparse.Namespace( | ||
test_cmd="forge test", | ||
test_dir=None, | ||
ignore_dirs="lib,mutation_campaign", | ||
output_dir=None, | ||
timeout=None, | ||
solc_remaps="forge-std=./lib/forge-std", | ||
verbose=None, | ||
very_verbose=None, | ||
mutators_to_run=None, | ||
comprehensive=None, | ||
codebase=(TEST_DATA_DIR / "test_source_unit" / "src" / "Counter.sol").as_posix(), | ||
contract_names="Counter", | ||
), | ||
) | ||
@pytest.mark.skip(reason="Slow test") | ||
def test_mutator(mock_args, solc_binary_path): # pylint: disable=unused-argument | ||
|
||
with change_directory(TEST_DATA_DIR / "test_source_unit"): | ||
main() | ||
|
||
|
||
def test_backup_source_file(solc_binary_path): | ||
solc_path = solc_binary_path("0.8.15") | ||
|
||
file_path = (TEST_DATA_DIR / "test_source_unit" / "src" / "Counter.sol").as_posix() | ||
sl = Slither(file_path, solc=solc_path) | ||
|
||
with tempfile.TemporaryDirectory() as directory: | ||
files_dict = backup_source_file(sl.source_code, Path(directory)) | ||
|
||
assert len(files_dict) == 1 | ||
assert Path(files_dict[file_path]).exists() | ||
|
||
|
||
@pytest.mark.skipif( | ||
not foundry_available or not project_ready, reason="requires Foundry and project setup" | ||
) | ||
def test_get_sol_file_list(): | ||
|
||
project_directory = TEST_DATA_DIR / "test_source_unit" | ||
|
||
files = get_sol_file_list(project_directory, None) | ||
|
||
assert len(files) == 46 | ||
|
||
files = get_sol_file_list(project_directory, ["lib"]) | ||
assert len(files) == 3 | ||
|
||
files = get_sol_file_list(project_directory, ["lib", "script"]) | ||
assert len(files) == 2 | ||
|
||
files = get_sol_file_list(project_directory / "src" / "Counter.sol", None) | ||
assert len(files) == 1 | ||
|
||
(project_directory / "test.sol").mkdir() | ||
files = get_sol_file_list(project_directory, None) | ||
assert all("test.sol" not in file for file in files) | ||
(project_directory / "test.sol").rmdir() | ||
|
||
|
||
@pytest.mark.skipif( | ||
not foundry_available or not project_ready, reason="requires Foundry and project setup" | ||
) | ||
def test_run_test(caplog): | ||
with change_directory(TEST_DATA_DIR / "test_source_unit"): | ||
result = run_test_cmd("forge test", timeout=None, target_file=None, verbose=True) | ||
assert result | ||
assert not caplog.records | ||
|
||
# Failed command | ||
result = run_test_cmd("forge non-test", timeout=None, target_file=None, verbose=True) | ||
assert not result | ||
assert caplog.records | ||
|
||
|
||
def test_run_tests_timeout(caplog, monkeypatch): | ||
def mock_run(*args, **kwargs): | ||
raise subprocess.TimeoutExpired(cmd=args[0], timeout=kwargs.get("timeout")) | ||
|
||
monkeypatch.setattr(subprocess, "run", mock_run) | ||
|
||
with change_directory(TEST_DATA_DIR / "test_source_unit"): | ||
result = run_test_cmd("forge test", timeout=1) | ||
assert not result | ||
assert "Tests took too long" in caplog.messages[0] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
The search results indicate multiple references to 'tests' in the CI configuration files, but none specifically mention 'tests/tools'. This suggests that the new test directory structure might not be fully reflected in the CI pipeline.
.github/workflows/test.yml
: Contains several references to 'tests', but none to 'tests/tools'..github/workflows/pylint.yml
,.github/workflows/linter.yml
,.github/workflows/ci.yml
,.github/workflows/black.yml
: Also contain references to 'tests', but not 'tests/tools'.Please ensure that the new test directory structure 'tests/tools' is correctly referenced in the CI configuration files.
Analysis chain
Ensure the new test directory structure is correctly reflected in the CI pipeline.
Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 47
Script:
Length of output: 898