use dedicated variable for naming mutant files

pull/2302/head
bohendo 10 months ago
parent fcbd327842
commit 5d43f9e28e
No known key found for this signature in database
  1. 7
      slither/tools/mutator/mutators/abstract_mutator.py
  2. 12
      slither/tools/mutator/utils/file_handling.py
  3. 3
      slither/tools/mutator/utils/testing_generated_mutant.py

@ -19,7 +19,6 @@ class AbstractMutator(
): # pylint: disable=too-few-public-methods,too-many-instance-attributes
NAME = ""
HELP = ""
INVALID_MUTANTS_COUNT = 0
VALID_MUTANTS_COUNT = 0
VALID_RR_MUTANTS_COUNT = 0
VALID_CR_MUTANTS_COUNT = 0
@ -82,9 +81,8 @@ class AbstractMutator(
(all_patches) = self._mutate()
if "patches" not in all_patches:
logger.debug("No patches found by %s", self.NAME)
return ([0,0,0], [0,0,0], self.dont_mutate_line)
for file in all_patches["patches"]:
return ([0, 0, 0], [0, 0, 0], self.dont_mutate_line)
for file in all_patches["patches"]: # Note: This should only loop over a single file
original_txt = self.slither.source_code[file].encode("utf8")
patches = all_patches["patches"][file]
patches.sort(key=lambda x: x["start"])
@ -96,7 +94,6 @@ class AbstractMutator(
file,
patch,
self.test_command,
self.VALID_MUTANTS_COUNT,
self.NAME,
self.timeout,
self.solc_remappings,

@ -1,4 +1,5 @@
import os
import traceback
from typing import Dict, List
import logging
@ -46,9 +47,13 @@ def transfer_and_delete(files_dict: Dict) -> None:
logger.error(f"Error transferring content: {e}")
def create_mutant_file(file: str, count: int, rule: str) -> None:
global_counter = {}
def create_mutant_file(file: str, rule: str) -> None:
"""function to create new mutant file"""
try:
if rule not in global_counter:
global_counter[rule] = 0
_, filename = os.path.split(file)
# Read content from the duplicated file
with open(file, "r", encoding="utf8") as source_file:
@ -67,12 +72,13 @@ def create_mutant_file(file: str, count: int, rule: str) -> None:
+ "_"
+ rule
+ "_"
+ str(count)
+ str(global_counter[rule])
+ ".sol",
"w",
encoding="utf8",
) as mutant_file:
mutant_file.write(content)
global_counter[rule] += 1
# reset the file
with open(duplicated_files[file], "r", encoding="utf8") as duplicated_file:
@ -83,6 +89,8 @@ def create_mutant_file(file: str, count: int, rule: str) -> None:
except Exception as e: # pylint: disable=broad-except
logger.error(f"Error creating mutant: {e}")
traceback_str = traceback.format_exc()
logger.error(traceback_str) # Log the stack trace
def reset_file(file: str) -> None:

@ -63,7 +63,6 @@ def test_patch( # pylint: disable=too-many-arguments
file: str,
patch: Dict,
command: str,
index: int,
generator_name: str,
timeout: int,
mappings: str | None,
@ -82,7 +81,7 @@ def test_patch( # pylint: disable=too-many-arguments
filepath.write(replaced_content)
if compile_generated_mutant(file, mappings):
if run_test_cmd(command, file, timeout):
create_mutant_file(file, index, generator_name)
create_mutant_file(file, generator_name)
logger.info(
red(
f"[{generator_name}] Line {patch['line_number']}: '{patch['old_string']}' ==> '{patch['new_string']}' --> VALID"

Loading…
Cancel
Save