Skip to content
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 6 commits into from
Jul 2, 2024
Merged

Improve slither-mutate testing #2482

merged 6 commits into from
Jul 2, 2024

Conversation

DarkaMaul
Copy link
Contributor

@DarkaMaul DarkaMaul commented Jun 12, 2024

We noticed that slither-mutate was using Python 3.10 only syntax, but this was never caught in the CI tests.

This PR fixes this issue and add some test to exercise a better coverage of the tool.
It also uncovered a bug where directories with name ending in .sol were considered as files by the tool.

However, since it is by nature extremely slow,( compiling the code over and over), the main test is disabled and should be used with a debugger when modifying the tool.

The tests could be improved, but that is a nice start here.

Summary by CodeRabbit

  • New Features

    • Added a new Solidity smart contract Counter with functions to set and increment a public state variable.
    • Introduced a test contract CounterTest for testing the Counter contract.
    • Added configuration settings for Foundry in the foundry.toml file.
  • Bug Fixes

    • Improved file handling by ensuring only files are processed in the list of Solidity files.
  • Tests

    • Introduced comprehensive test functions for mutator-related functionality in the slither tool.

Copy link

coderabbitai bot commented Jun 12, 2024

Walkthrough

Walkthrough

The recent updates introduce improvements and new features to the slither tool's mutator functionality. Changes include enhanced type hinting and default parameter values, new Solidity smart contracts and test files, and additional configuration for Foundry. The updates also feature new test functions for various mutator-related operations and modifications to the test execution script to improve coverage reporting.

Changes

File(s) Change Summary
slither/tools/mutator/mutators/abstract_mutator.py Improved type hinting and adjusted return statements for clarity.
slither/tools/mutator/utils/file_handling.py Added a check to ensure files are valid before processing in get_sol_file_list.
slither/tools/mutator/utils/testing_generated_mutant.py Updated run_test_cmd to include default parameter values and enhanced type hinting.
tests/tools/mutator/test_data/test_source_unit/README.md Introduced Counter with initialization instructions.
tests/tools/mutator/test_data/test_source_unit/foundry.toml Added Foundry configuration settings specifying directories, libraries, and Solidity compiler version.
tests/tools/mutator/test_data/test_source_unit/script/Counter.s.sol Introduced CounterScript Solidity smart contract with setUp() and run() functions.
tests/tools/mutator/test_data/test_source_unit/src/Counter.sol Added Counter Solidity smart contract with public state variable and functions to update and increment it.
tests/tools/mutator/test_data/test_source_unit/test/Counter.t.sol Introduced CounterTest Solidity test contract to test the Counter contract's functionality.
tests/tools/mutator/test_mutator.py Added test functions for mutator functionalities, including obtaining mutators, running main functionality, backing up source files, getting Solidity file lists, and running tests with timeouts.
.github/scripts/tool_test_runner.sh Modified test execution to run pytest on a different directory structure, improving coverage reporting.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant TestRunner
    participant Mutator
    participant FileHandler
    participant SolidityContract
    participant Foundry

    User->>TestRunner: Run tests
    TestRunner->>Mutator: Execute main functionality
    Mutator->>FileHandler: Backup source files
    FileHandler->>Mutator: Return backup status
    Mutator->>SolidityContract: Process Solidity files
    SolidityContract->>Mutator: Return processing results
    Mutator->>TestRunner: Return test results
    TestRunner->>User: Display test results

    User->>Foundry: Configure settings
    Foundry-->>User: Confirm configuration
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range and nitpick comments (3)
slither/tools/mutator/utils/file_handling.py (1)

Line range hint 33-33: Remove unnecessary open mode parameters.

- with (mutation_dir / mutation_filename).open("w", encoding="utf8") as mutant_file:
+ with (mutation_dir / mutation_filename).open(encoding="utf8") as mutant_file:

This change aligns with the static analysis suggestion to remove unnecessary parameters and improve code clarity.

Also applies to: 88-88

slither/tools/mutator/utils/testing_generated_mutant.py (2)

Line range hint 21-21: Avoid using bare except.

Replace the bare except with a more specific exception to avoid catching unexpected errors.

- except:
+ except Exception as e:

Line range hint 48-55: Use capture_output instead of PIPE.

- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
+ capture_output=True,

This change simplifies the subprocess call and is in line with best practices.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 02df0dc and c3a5fb2.

Files selected for processing (9)
  • slither/tools/mutator/mutators/abstract_mutator.py (4 hunks)
  • slither/tools/mutator/utils/file_handling.py (1 hunks)
  • slither/tools/mutator/utils/testing_generated_mutant.py (1 hunks)
  • tests/tools/mutator/test_data/test_source_unit/README.md (1 hunks)
  • tests/tools/mutator/test_data/test_source_unit/foundry.toml (1 hunks)
  • tests/tools/mutator/test_data/test_source_unit/script/Counter.s.sol (1 hunks)
  • tests/tools/mutator/test_data/test_source_unit/src/Counter.sol (1 hunks)
  • tests/tools/mutator/test_data/test_source_unit/test/Counter.t.sol (1 hunks)
  • tests/tools/mutator/test_mutator.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/tools/mutator/test_data/test_source_unit/foundry.toml
Additional context used
Markdownlint
tests/tools/mutator/test_data/test_source_unit/README.md

21-21: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


27-27: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


33-33: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


39-39: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


45-45: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


51-51: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


57-57: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


63-63: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


64-64: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


65-65: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


14-14: null (MD034, no-bare-urls)
Bare URL used

Ruff
slither/tools/mutator/utils/file_handling.py

33-33: Unnecessary open mode parameters (UP015)

Remove open mode parameters


88-88: Unnecessary open mode parameters (UP015)

Remove open mode parameters

slither/tools/mutator/utils/testing_generated_mutant.py

21-21: Do not use bare except (E722)


48-55: Prefer capture_output over sending stdout and stderr to PIPE (UP022)

Replace with capture_output keyword argument


100-100: Unnecessary open mode parameters (UP015)

Remove open mode parameters

Additional comments not posted (12)
tests/tools/mutator/test_data/test_source_unit/script/Counter.s.sol (2)

7-7: The setUp function is currently empty. Ensure that this is intentional and consider documenting its intended use or future extensions.


9-11: The run function correctly uses vm.broadcast() for transaction broadcasting. Ensure that any transaction-specific logic that needs to be tested is included here in future updates.

tests/tools/mutator/test_data/test_source_unit/src/Counter.sol (2)

7-8: The setNumber function correctly updates the state variable number. Ensure that access controls are considered if needed in future iterations.


11-12: The increment function correctly increments the state variable number. Consider adding overflow checks if the contract will handle large numbers or critical operations.

tests/tools/mutator/test_data/test_source_unit/test/Counter.t.sol (3)

10-12: The setUp function correctly initializes the Counter contract and sets the initial state. This is crucial for ensuring consistent test conditions.


15-17: The test_Increment function correctly tests the increment functionality of the Counter contract. Good use of assertions to validate the expected state.


20-22: The testFuzz_SetNumber function effectively tests the setNumber functionality with fuzzed inputs. This is a good practice for ensuring robustness against a wide range of inputs.

slither/tools/mutator/utils/file_handling.py (1)

114-114: Ensure only files are processed in get_sol_file_list.

This change correctly implements the PR's objective to avoid directories being mistaken for files.

tests/tools/mutator/test_mutator.py (2)

59-59: Ensure slow tests are skipped.

The use of @pytest.mark.skip is appropriate here to avoid running slow tests automatically.


42-57: Mocking strategy for argparse.

Proper use of mock.patch to simulate command line arguments for testing. This ensures that the function can be tested in isolation.

slither/tools/mutator/mutators/abstract_mutator.py (2)

30-30: Use of Union type hint for solc_remappings.

This change correctly updates the type hint to be more explicit, which improves code readability and type safety.


84-84: Ensure correct handling of mutation results.

The logic to handle mutation results and update counts is correctly implemented. This ensures accurate tracking of mutation outcomes.

Also applies to: 149-149

Comment on lines 1 to 66
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README file provides a comprehensive overview of the Foundry toolkit. Consider showing example outputs for command line instructions to enhance clarity and user understanding. Also, consider hyperlinking the bare URL to improve readability and accessibility.

- https://book.getfoundry.sh/
+ [Foundry Documentation](https://book.getfoundry.sh/)
- $ forge build
+ $ forge build
+ # Example output: "Compiling..."
Tools
Markdownlint

21-21: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


27-27: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


33-33: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


39-39: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


45-45: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


51-51: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


57-57: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


63-63: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


64-64: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


65-65: null (MD014, commands-show-output)
Dollar signs used before commands without showing output


14-14: null (MD034, no-bare-urls)
Bare URL used

@DarkaMaul DarkaMaul force-pushed the features/test-mutator branch from 2af4c66 to 7b983e3 Compare June 12, 2024 09:15
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range and nitpick comments (2)
slither/tools/mutator/mutators/LIR.py (2)

Line range hint 41-41: Convert to not in to follow Python best practices.

- if not line_no[0] in self.dont_mutate_line:
+ if line_no[0] not in self.dont_mutate_line:

Line range hint 72-72: Convert to not in to follow Python best practices.

- if not line_no[0] in self.dont_mutate_line:
+ if line_no[0] not in self.dont_mutate_line:
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c3a5fb2 and 43301c7.

Files selected for processing (5)
  • .github/scripts/tool_test_runner.sh (1 hunks)
  • slither/tools/mutator/main.py (2 hunks)
  • slither/tools/mutator/mutators/LIR.py (2 hunks)
  • tests/tools/mutator/test_data/test_source_unit/README.md (1 hunks)
  • tests/tools/mutator/test_mutator.py (1 hunks)
Files not summarized due to errors (2)
  • slither/tools/mutator/main.py: Error: Server error. Please try again later.
  • slither/tools/mutator/mutators/LIR.py: Error: Server error. Please try again later.
Files skipped from review due to trivial changes (1)
  • tests/tools/mutator/test_data/test_source_unit/README.md
Files skipped from review as they are similar to previous changes (1)
  • tests/tools/mutator/test_mutator.py
Additional context used
Ruff
slither/tools/mutator/mutators/LIR.py

41-41: Test for membership should be not in (E713)

Convert to not in


72-72: Test for membership should be not in (E713)

Convert to not in

Additional comments not posted (5)
.github/scripts/tool_test_runner.sh (1)

5-5: Ensure the new test directory structure is correctly reflected in the CI pipeline.

slither/tools/mutator/mutators/LIR.py (2)

34-34: Ensure that appending "-1" for integer types is consistent with the intended functionality.


66-66: Ensure that appending "-1" for integer types is consistent with the intended functionality.

slither/tools/mutator/__main__.py (2)

9-9: Ensure the added imports are used effectively throughout the script.


119-119: Ensure that the function _get_mutators correctly filters and returns the list of mutators based on the provided list.

status_code=$?
python -m coverage report
else
pytest tests/tools/read-storage/test_read_storage.py
pytest tests/tools
Copy link

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.

  • Update CI configuration files to reflect the new test directory structure.
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:

#!/bin/bash
# Description: Verify the new test directory structure is correctly reflected in the CI pipeline.

# Test: Search for the test directory usage in CI configuration files. Expect: Only occurrences of the new structure.
rg --type yaml $'tests/tools' .github/workflows/

Length of output: 47



Script:

#!/bin/bash
# Description: Verify the new test directory structure is correctly reflected in the CI pipeline.

# Test: Search for any references to 'tests' in CI configuration files to ensure the new structure is being used.
rg --type yaml 'tests' .github/workflows/

Length of output: 898

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 43301c7 and 5c0eccf.

Files selected for processing (1)
  • tests/tools/mutator/test_mutator.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/tools/mutator/test_mutator.py

@0xalpharush 0xalpharush merged commit 7c7b71f into dev Jul 2, 2024
83 checks passed
@0xalpharush 0xalpharush deleted the features/test-mutator branch July 2, 2024 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants