<commit_before>package fr.inria.spirals.repairnator.process.step.paths;

import ch.qos.logback.classic.Level;
import fr.inria.jtravis.entities.Build;
import fr.inria.spirals.repairnator.BuildToBeInspected;
import fr.inria.spirals.repairnator.utils.Utils;
import fr.inria.spirals.repairnator.config.RepairnatorConfig;
import fr.inria.spirals.repairnator.process.files.FileHelper;
import fr.inria.spirals.repairnator.process.inspectors.JobStatus;
import fr.inria.spirals.repairnator.process.inspectors.ProjectInspector;
import fr.inria.spirals.repairnator.process.step.StepStatus;
import fr.inria.spirals.repairnator.process.step.CloneRepository;
import fr.inria.spirals.repairnator.process.step.checkoutrepository.CheckoutBuggyBuild;
import fr.inria.spirals.repairnator.process.step.checkoutrepository.CheckoutType;
import fr.inria.spirals.repairnator.process.utils4tests.ProjectInspectorMocker;
import fr.inria.spirals.repairnator.states.ScannedBuildStatus;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * Created by urli on 07/03/2017.
 */
public class TestComputeTestDir {

    private File tmpDir;

    @Before
    public void setup() {
        Utils.setLoggersLevel(Level.ERROR);
    }

    @After
    public void tearDown() throws IOException {
        RepairnatorConfig.deleteInstance();
        FileHelper.deleteFile(tmpDir);
    }

    @Test
    public void testComputeTestDirWithMultiModuleProject() throws IOException {
        long buggyBuildCandidateId = 386332218; // https://travis-ci.org/fermadeiral/test-repairnator-bears/builds/386332218

        Build buggyBuildCandidate = this.checkBuildAndReturn(buggyBuildCandidateId, false);

        BuildToBeInspected buildToBeInspected = new BuildToBeInspected(buggyBuildCandidate, null, ScannedBuildStatus.ONLY_FAIL, "test");

        tmpDir = Files.createTempDirectory("computetestdir").toFile();

        File repoDir = new File(tmpDir, "repo");

        JobStatus jobStatus = new JobStatus(tmpDir.getAbsolutePath()+"/repo");

        ProjectInspector inspector = ProjectInspectorMocker.mockProjectInspector(jobStatus, tmpDir, buildToBeInspected, CheckoutType.CHECKOUT_BUGGY_BUILD);

        CloneRepository cloneStep = new CloneRepository(inspector);
        ComputeTestDir computeTestDir = new ComputeTestDir(inspector, true);

        cloneStep.addNextStep(new CheckoutBuggyBuild(inspector, true)).addNextStep(computeTestDir);
        cloneStep.execute();

        assertThat(computeTestDir.isShouldStop(), is(false));
        List<StepStatus> stepStatusList = jobStatus.getStepStatuses();
        assertThat(stepStatusList.size(), is(3));
        StepStatus computeTestDirStatus = stepStatusList.get(2);
        assertThat(computeTestDirStatus.getStep(), is(computeTestDir));

        for (StepStatus stepStatus : stepStatusList) {
            assertThat(stepStatus.isSuccess(), is(true));
        }

        assertThat(jobStatus.getTestDir(), is(new File[] {
                new File(repoDir.getAbsolutePath()+"/test-repairnator-bears-core/src/test/java").getCanonicalFile(),
                new File(repoDir.getAbsolutePath()+"/test-repairnator-bears-patchstats/src/test/java").getCanonicalFile()
        }));
        assertThat(jobStatus.getProperties().getProjectMetrics().getNumberTestFiles(), is(3));
    }

    @Test
    public void testComputeTestDirWithReflexiveReferences() throws IOException {
        long buildId = 345990212;

        Build build = this.checkBuildAndReturn(buildId, true);

        tmpDir = Files.createTempDirectory("computetestdir").toFile();

        File repoDir = new File(tmpDir, "repo");
        BuildToBeInspected toBeInspected = new BuildToBeInspected(build, null, ScannedBuildStatus.ONLY_FAIL, "");


        JobStatus jobStatus = new JobStatus(tmpDir.getAbsolutePath()+"/repo");
        jobStatus.setFailingModulePath(repoDir.getAbsolutePath());

        ProjectInspector inspector = ProjectInspectorMocker.mockProjectInspector(jobStatus, tmpDir, toBeInspected);

        CloneRepository cloneStep = new CloneRepository(inspector);
        ComputeTestDir computeTestDir = new ComputeTestDir(inspector, true);

        cloneStep.addNextStep(new CheckoutBuggyBuild(inspector, true)).addNextStep(computeTestDir);
        cloneStep.execute();

        assertThat(computeTestDir.isShouldStop(), is(true));
        List<StepStatus> stepStatusList = jobStatus.getStepStatuses();
        assertThat(stepStatusList.size(), is(3));
        StepStatus computeTestDirStatus = stepStatusList.get(2);
        assertThat(computeTestDirStatus.getStep(), is(computeTestDir));

        for (StepStatus stepStatus : stepStatusList) {
            if (stepStatus.getStep() != computeTestDir) {<commit_msg>chore: fix broken CI (#981)<commit_after>package fr.inria.spirals.repairnator.process.step.paths;

import ch.qos.logback.classic.Level;
import fr.inria.jtravis.entities.Build;
import fr.inria.spirals.repairnator.BuildToBeInspected;
import fr.inria.spirals.repairnator.utils.Utils;
import fr.inria.spirals.repairnator.config.RepairnatorConfig;
import fr.inria.spirals.repairnator.process.files.FileHelper;
import fr.inria.spirals.repairnator.process.inspectors.JobStatus;
import fr.inria.spirals.repairnator.process.inspectors.ProjectInspector;
import fr.inria.spirals.repairnator.process.step.StepStatus;
import fr.inria.spirals.repairnator.process.step.CloneRepository;
import fr.inria.spirals.repairnator.process.step.checkoutrepository.CheckoutBuggyBuild;
import fr.inria.spirals.repairnator.process.step.checkoutrepository.CheckoutType;
import fr.inria.spirals.repairnator.process.utils4tests.ProjectInspectorMocker;
import fr.inria.spirals.repairnator.states.ScannedBuildStatus;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * Created by urli on 07/03/2017.
 */
public class TestComputeTestDir {

    private File tmpDir;

    @Before
    public void setup() {
        Utils.setLoggersLevel(Level.ERROR);
    }

    @After
    public void tearDown() throws IOException {
        RepairnatorConfig.deleteInstance();
        FileHelper.deleteFile(tmpDir);
    }

    @Test
    public void testComputeTestDirWithMultiModuleProject() throws IOException {
        long buggyBuildCandidateId = 386332218; // https://travis-ci.org/fermadeiral/test-repairnator-bears/builds/386332218

        Build buggyBuildCandidate = this.checkBuildAndReturn(buggyBuildCandidateId, false);

        BuildToBeInspected buildToBeInspected = new BuildToBeInspected(buggyBuildCandidate, null, ScannedBuildStatus.ONLY_FAIL, "test");

        tmpDir = Files.createTempDirectory("computetestdir").toFile();

        File repoDir = new File(tmpDir, "repo");

        JobStatus jobStatus = new JobStatus(tmpDir.getAbsolutePath()+"/repo");

        ProjectInspector inspector = ProjectInspectorMocker.mockProjectInspector(jobStatus, tmpDir, buildToBeInspected, CheckoutType.CHECKOUT_BUGGY_BUILD);

        CloneRepository cloneStep = new CloneRepository(inspector);
        ComputeTestDir computeTestDir = new ComputeTestDir(inspector, true);

        cloneStep.addNextStep(new CheckoutBuggyBuild(inspector, true)).addNextStep(computeTestDir);
        cloneStep.execute();

        assertThat(computeTestDir.isShouldStop(), is(false));
        List<StepStatus> stepStatusList = jobStatus.getStepStatuses();
        assertThat(stepStatusList.size(), is(3));
        StepStatus computeTestDirStatus = stepStatusList.get(2);
        assertThat(computeTestDirStatus.getStep(), is(computeTestDir));

        for (StepStatus stepStatus : stepStatusList) {
            assertThat(stepStatus.isSuccess(), is(true));
        }

        assertThat(jobStatus.getTestDir(), is(new File[] {
                new File(repoDir.getAbsolutePath()+"/test-repairnator-bears-core/src/test/java").getCanonicalFile(),
                new File(repoDir.getAbsolutePath()+"/test-repairnator-bears-patchstats/src/test/java").getCanonicalFile()
        }));
        assertThat(jobStatus.getProperties().getProjectMetrics().getNumberTestFiles(), is(3));
    }

    @Test
    @Ignore // this test is ignored because it depends on an external repo / PR, whose state has changed (no HEAD ref available anymore, probably because the branch has been removed since then)
    public void testComputeTestDirWithReflexiveReferences() throws IOException {
        // https://api.travis-ci.org/v3/build/345990212
        // https://github.com/vitruv-tools/Vitruv-Applications-ComponentBasedSystems/pull/18
        long buildId = 345990212;

        Build build = this.checkBuildAndReturn(buildId, true);

        tmpDir = Files.createTempDirectory("computetestdir").toFile();

        File repoDir = new File(tmpDir, "repo");
        BuildToBeInspected toBeInspected = new BuildToBeInspected(build, null, ScannedBuildStatus.ONLY_FAIL, "");


        JobStatus jobStatus = new JobStatus(tmpDir.getAbsolutePath()+"/repo");
        jobStatus.setFailingModulePath(repoDir.getAbsolutePath());

        ProjectInspector inspector = ProjectInspectorMocker.mockProjectInspector(jobStatus, tmpDir, toBeInspected);

        CloneRepository cloneStep = new CloneRepository(inspector);
        ComputeTestDir computeTestDir = new ComputeTestDir(inspector, true);

        cloneStep.addNextStep(new CheckoutBuggyBuild(inspector, true)).addNextStep(computeTestDir);
        cloneStep.execute();

        assertThat(computeTestDir.isShouldStop(), is(true));
        List<StepStatus> stepStatusList = jobStatus.getStepStatuses();
        assertThat(stepStatusList.size(), is(3));
        StepStatus computeTestDirStatus = stepStatusList.get(2);
        assertThat(computeTestDirStatus.getStep(), is(computeTestDir));

        for (StepStatus stepStatus : stepStatusList) {
            if (stepStatus.getStep() != computeTestDir) {
{'fraction_non_alphanumeric': 0.10534861509073544, 'fraction_numerical': 0.009264565425023878, 'mean_word_length': 4.328753180661578, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5273381', 'n_tokens_mistral': 3215, 'n_tokens_neox': 3108, 'n_words': 497}