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

@ -1,4 +1,5 @@
import os import os
import traceback
from typing import Dict, List from typing import Dict, List
import logging import logging
@ -46,9 +47,13 @@ def transfer_and_delete(files_dict: Dict) -> None:
logger.error(f"Error transferring content: {e}") 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""" """function to create new mutant file"""
try: try:
if rule not in global_counter:
global_counter[rule] = 0
_, filename = os.path.split(file) _, filename = os.path.split(file)
# Read content from the duplicated file # Read content from the duplicated file
with open(file, "r", encoding="utf8") as source_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 + rule
+ "_" + "_"
+ str(count) + str(global_counter[rule])
+ ".sol", + ".sol",
"w", "w",
encoding="utf8", encoding="utf8",
) as mutant_file: ) as mutant_file:
mutant_file.write(content) mutant_file.write(content)
global_counter[rule] += 1
# reset the file # reset the file
with open(duplicated_files[file], "r", encoding="utf8") as duplicated_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 except Exception as e: # pylint: disable=broad-except
logger.error(f"Error creating mutant: {e}") 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: def reset_file(file: str) -> None:

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

Loading…
Cancel
Save