hcoles / pitest

State of the art mutation testing system for the JVM
Apache License 2.0
1.7k stars 357 forks source link

Trying to create custom mutation Inteceptor #1276

Closed reshma-0014 closed 1 year ago

reshma-0014 commented 1 year ago

I am trying to create my own logic for mutation Inteceptor but only found the docs related to it. There is no example project for it. Can anyone give me the example projects so that I will use it a reference.

hcoles commented 1 year ago

If you search for implementations of MutationInterceptor in the pitest codebase, you should find lots of examples.

The projects under the pitest organisation

https://github.com/pitest

Give examples of how to package up a plugin.

reshma-0014 commented 1 year ago

Thanks for those examples, it really helps

reshma-0014 commented 11 months ago

I am trying to filter mutants according to the method name but I am unable to find any example related to this filtering. Could anyone help me do this or provide some example project. All the examples mentioned in this are based on report type. (https://github.com/pitest)

hcoles commented 11 months ago

There are about 30 implementations of MutationInterceptor in the pitest codebase. One of the simplest is org.pitest.mutationtest.build.intercept.kotlin.KotlinFilter which filters mutants based on their file extension. The others demonstrate how to perform more complex static analysis.

reshma-0014 commented 11 months ago

Using Kotlin filter I am trying to filter according to a certain method and also created a class extending MutationFilterFactory. But it doesn't seem to filter in my actual project.

public class MutantExportInterceptor implements MutationFilter {

  @Override
  public Collection<MutationDetails> filter(
      Collection<MutationDetails> mutations) {
    return FCollection.filter(mutations, isMethodToFilter());
  }

  private F<MutationDetails, Boolean> isMethodToFilter() {
    return new F<MutationDetails, Boolean>() {
      @Override
      public Boolean apply(MutationDetails mutation) {
        // Modify this condition based on your method name filtering criteria
        return mutation.getMethod().name().equals("MethodName");
      }
    };
  }

}

hcoles commented 11 months ago

Have you packaged it up into a plugin like the examples at https://github.com/pitest/?

Have you installed the packaged plugin into your project?

reshma-0014 commented 11 months ago

Yes I have turn packaged it into a jar and added it in my project

hcoles commented 11 months ago

If you can post the complete project somewhere I can take a look. I'm afriad it isn't really possible to debug without access to the source.

reshma-0014 commented 11 months ago
Screenshot 2023-11-22 at 11 37 54 AM

When I tried including this plugin (https://github.com/pitest/export-plugin) using the instruction in the readme file I am stuck with this error, could you help me with this.

hcoles commented 11 months ago

The project is archived and looks to be being built against a snapshot version of pitest that is no longer available in maven central.

If you wish to use if you'll need to update the project and build it against pitest 1.15.3.

(note that the export functionality is now included in pitest without the need for a plugin, which is why this project was archived).

reshma-0014 commented 11 months ago

Thanks for the clarification

reshma-0014 commented 11 months ago

testImplementation "org.pitest:pitest:1.10.3" testImplementation 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.1.11' Should we have to include these lines in dependency in gradle project or the jar generated after building the export plugin project is enough?

reshma-0014 commented 11 months ago

I am not sure what exactly should I need to let you know about the project.... but I have attached the build.gradle file in here. Could you please take a look at it and help

plugins { id 'java-library' id 'jacoco' id 'java' id 'info.solidsoft.pitest' version '1.15.0' }

compileJava { options.encoding = "UTF-8" }

dependencies { testCompileOnly fileTree('libs') { include '/*.jar' } testRuntimeOnly fileTree('libs') { include '*/.jar' } implementation fileTree('libs') { include '/*.jar' }

    implementation files('pitest-export-plugin-0.1-SNAPSHOT.jar')

    testImplementation 'junit:junit:4.13'
    testImplementation 'org.junit.platform:junit-platform-launcher:1.8.1'
    testImplementation 'org.junit.platform:junit-platform-runner:1.8.1'
    testImplementation 'org.junit.platform:junit-platform-engine:1.8.1'
    testImplementation 'org.junit.platform:junit-platform-suite-api:1.8.1'
    testImplementation 'org.junit.platform:junit-platform-suite:1.8.1'
    testImplementation 'org.junit.platform:junit-platform-suite-commons:1.8.1'
    testImplementation 'org.junit.platform:junit-platform-suite-engine:1.8.1'
    testImplementation 'org.junit.platform:junit-platform-commons:1.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
    testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

    implementation 'junit:junit:4.13'
    implementation 'org.junit.platform:junit-platform-launcher:1.8.1'
    implementation 'org.junit.platform:junit-platform-runner:1.8.1'
    implementation 'org.junit.platform:junit-platform-engine:1.8.1'
    implementation 'org.junit.platform:junit-platform-suite:1.8.1'
    implementation 'org.junit.platform:junit-platform-suite-api:1.8.1'
    implementation 'org.junit.platform:junit-platform-suite-commons:1.8.1'
    implementation 'org.junit.platform:junit-platform-suite-engine:1.8.1'
    implementation 'org.junit.platform:junit-platform-commons:1.8.1'
    implementation 'org.junit.jupiter:junit-jupiter:5.8.1'
    implementation 'org.junit.vintage:junit-vintage-engine:5.8.1'
    implementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    implementation 'org.junit.jupiter:junit-jupiter-params:5.8.1'
    implementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

}

repositories { mavenCentral() // maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } }

test { if("$task" == "pitest") { println "Mutation Report generation started" useJUnitPlatform() pitest { targetClasses = ["com.”] targetTests = ["com."] features =["+EXPORT"] junit5PluginVersion = "1.1.1" jvmArgs = ["-Xmx5120m"] threads = 5 outputFormats = ['HTML','XML'] timestampedReports = false exportLineCoverage = true failWhenNoMutations=false } } }

}

hcoles commented 11 months ago

I am not sure what you are showing me here.

Is this a project you are trying to analyse using pitest and the export plugin? If so, then then plugin needs to be a dependency of the pitest task, not an implementation dependency. But, as already explained, the export functionality is already built into pitest in recent versions.

Or is this meant to be the build file for your project that creates a plugin?

reshma-0014 commented 11 months ago

Thanks for your inputs. yes it is the gradle script of the project that I am trying to generate pitest report using the plugin(the filter which I mentioned earlier in this thread not the existing export plugin).

Now I have also tried adding it as dependency in the pitest task.

test { if("$task" == "pitest") { println "Mutation Report generation started" useJUnitPlatform() pitest { dependencies { classpath files('pitest-export-plugin-0.1-SNAPSHOT.jar')

        }   
      targetClasses = ["com.*"]
        targetTests = ["com.*”]
        // pitestVersion = "1.2.1-SNAPSHOT"
        junit5PluginVersion = "1.1.1"
        pitestVersion = "1.10.3"
        jvmArgs = ["-Xmx5120m"]
        threads = 5
        outputFormats = ['HTML','XML']
        timestampedReports = false
        exportLineCoverage = true
        failWhenNoMutations=false
    }

}

}

I am not getting any filtered mutants generated for my project not even getting the logs which I added in the filter plugin.

hcoles commented 11 months ago

As stated earlier, I am not able to help you debug your plugin project unless you share it's complete code.

reshma-0014 commented 11 months ago

I have here attached the plugin code. https://github.com/reshma-0014/PitestPluginFilter

hcoles commented 11 months ago

The main issue here looks to be that you have implemented MutationFilterFactory but the service loader file is for MutationInterceptorFactory.

The project is also building against a very old verison of pitest (1.2.2).

hcoles commented 11 months ago

Incidentally, if all you wish to do is filter mutants based on method name, this functionality is available out of the box via the excludedMethods paramemter.

reshma-0014 commented 11 months ago

I have now updated those files.... ( https://github.com/reshma-0014/PitestPluginFilter ) I understand I can use excludedMethods parameter.... but more than that I would like to add other logics depending on the method definition too. Eg. Avoid like to remove the mutants depending on the variables used and its types.

Statement stmt = conn.createStatement(); stmt.executeQuery(strSelect) //any mutant in this line has to be eliminated

Bw could you please help me with the syntax of excludedMethods... I would like to give it a try.