<commit_before>/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package wooga.gradle.unity.tasks

import org.apache.maven.artifact.versioning.DefaultArtifactVersion
import org.gradle.api.Action
import org.gradle.api.internal.ClosureBackedAction
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.logging.Logging
import org.gradle.api.reporting.Reporting
import org.gradle.api.tasks.Console
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.TaskAction
import org.gradle.internal.reflect.Instantiator
import org.gradle.process.ExecSpec
import org.gradle.util.GUtil
import wooga.gradle.unity.batchMode.BatchModeFlags
import wooga.gradle.unity.batchMode.TestPlatform
import wooga.gradle.unity.testing.UnityTestTaskReport
import wooga.gradle.unity.testing.UnityTestTaskReportsImpl
import wooga.gradle.unity.utils.NUnitReportNormalizer

import javax.inject.Inject

class Test extends AbstractUnityTask implements Reporting<UnityTestTaskReport> {

    static org.gradle.api.logging.Logger logger = Logging.getLogger(Test)

    private final List<Object> filter = new ArrayList()
    private final List<Object> categories = new ArrayList()
    private final FileResolver fileResolver
    private TestPlatform testPlatform = TestPlatform.editmode
    private DefaultArtifactVersion unityVersion

    private final UnityTestTaskReport reports

    @Override
    AbstractUnityTask unityPath(File path) {
        unityVersion = null
        return super.unityPath(path)
    }

    @Override
    void setUnityPath(File path) {
        unityVersion = null
        super.setUnityPath(path)
    }

    @Input
    @Optional
    List<String> getCategories() {
        List<String> args = new ArrayList<String>()
        for (Object argument : categories) {
            args.add(argument.toString())
        }
        return args
    }

    Test categories(Object... var1) {
        if (var1 == null) {
            throw new IllegalArgumentException("categories == null!")
        } else {
            this.categories.addAll(Arrays.asList(var1))
            return this
        }
    }

    Test categories(Iterable<?> var1) {
        GUtil.addToCollection(categories, var1)
        return this
    }

    Test setCategories(Iterable<?> var1) {
        this.filter.clear()
        GUtil.addToCollection(categories, var1)
        return this
    }

    @Input
    @Optional
    List<String> getFilter() {
        List<String> args = new ArrayList<String>()
        for (Object argument : filter) {
            args.add(argument.toString())
        }
        return args
    }

    Test filter(Object... var1) {
        if (var1 == null) {
            throw new IllegalArgumentException("filter == null!")
        } else {
            this.filter.addAll(Arrays.asList(var1))
            return this
        }
    }

    Test filter(Iterable<?> var1) {
        GUtil.addToCollection(filter, var1)
        return this
    }

    Test setFilter(Iterable<?> var1) {
        this.filter.clear()
        GUtil.addToCollection(filter, var1)
        return this
    }

    @Input
    @Optional
    TestPlatform getTestPlatform() {
        return testPlatform
    }

    void setTestPlatform(TestPlatform value) {
        testPlatform = value
    }

    void setTestPlatform(String value) {
        testPlatform = TestPlatform.valueOf(value)
    }

    Test testPlatform(TestPlatform value) {
        setTestPlatform(value)
        return this
    }

    Test testPlatform(String value) {
        setTestPlatform(value)
        return this
    }

    @Console
    boolean verbose = true

    @Console
    boolean teamcity = false

    @Inject
    protected Instantiator getInstantiator() {
        throw new UnsupportedOperationException()
    }

    @Inject
    Test(FileResolver fileResolver) {
        super(Test.class)
        this.fileResolver = fileResolver
        description = "Executes Unity in batch mode and executes specified method"

        reports = instantiator.newInstance(UnityTestTaskReportsImpl.class, this)
        reports.xml.enabled = true
    }

    @Override
    UnityTestTaskReport getReports() {
        return reports
    }

    @Override
    UnityTestTaskReport reports(Closure closure) {
        return reports(new ClosureBackedAction<UnityTestTaskReport>(closure));
    }

    @Override
    UnityTestTaskReport reports(Action<? super UnityTestTaskReport> configureAction) {
        configureAction.execute(reports)
        return reports
    }

    @TaskAction
    @Override
    protected void exec() {
        DefaultArtifactVersion unityVersion = getUnityVersion(getUnityPath())
        logger.info("unity version major:${unityVersion.majorVersion} minor: ${unityVersion.minorVersion}")

        args(buildTestArguments(unityVersion))
        super.exec()

        //normalize test result file
        normalizeTestResult()
    }<commit_msg>Fix normalisation even when batchmode exec fails<commit_after>/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package wooga.gradle.unity.tasks

import org.apache.maven.artifact.versioning.DefaultArtifactVersion
import org.gradle.api.Action
import org.gradle.api.internal.ClosureBackedAction
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.logging.Logging
import org.gradle.api.reporting.Reporting
import org.gradle.api.tasks.Console
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.TaskAction
import org.gradle.internal.reflect.Instantiator
import org.gradle.process.ExecSpec
import org.gradle.util.GUtil
import wooga.gradle.unity.batchMode.BatchModeFlags
import wooga.gradle.unity.batchMode.TestPlatform
import wooga.gradle.unity.testing.UnityTestTaskReport
import wooga.gradle.unity.testing.UnityTestTaskReportsImpl
import wooga.gradle.unity.utils.NUnitReportNormalizer

import javax.inject.Inject

class Test extends AbstractUnityTask implements Reporting<UnityTestTaskReport> {

    static org.gradle.api.logging.Logger logger = Logging.getLogger(Test)

    private final List<Object> filter = new ArrayList()
    private final List<Object> categories = new ArrayList()
    private final FileResolver fileResolver
    private TestPlatform testPlatform = TestPlatform.editmode
    private DefaultArtifactVersion unityVersion

    private final UnityTestTaskReport reports

    @Override
    AbstractUnityTask unityPath(File path) {
        unityVersion = null
        return super.unityPath(path)
    }

    @Override
    void setUnityPath(File path) {
        unityVersion = null
        super.setUnityPath(path)
    }

    @Input
    @Optional
    List<String> getCategories() {
        List<String> args = new ArrayList<String>()
        for (Object argument : categories) {
            args.add(argument.toString())
        }
        return args
    }

    Test categories(Object... var1) {
        if (var1 == null) {
            throw new IllegalArgumentException("categories == null!")
        } else {
            this.categories.addAll(Arrays.asList(var1))
            return this
        }
    }

    Test categories(Iterable<?> var1) {
        GUtil.addToCollection(categories, var1)
        return this
    }

    Test setCategories(Iterable<?> var1) {
        this.filter.clear()
        GUtil.addToCollection(categories, var1)
        return this
    }

    @Input
    @Optional
    List<String> getFilter() {
        List<String> args = new ArrayList<String>()
        for (Object argument : filter) {
            args.add(argument.toString())
        }
        return args
    }

    Test filter(Object... var1) {
        if (var1 == null) {
            throw new IllegalArgumentException("filter == null!")
        } else {
            this.filter.addAll(Arrays.asList(var1))
            return this
        }
    }

    Test filter(Iterable<?> var1) {
        GUtil.addToCollection(filter, var1)
        return this
    }

    Test setFilter(Iterable<?> var1) {
        this.filter.clear()
        GUtil.addToCollection(filter, var1)
        return this
    }

    @Input
    @Optional
    TestPlatform getTestPlatform() {
        return testPlatform
    }

    void setTestPlatform(TestPlatform value) {
        testPlatform = value
    }

    void setTestPlatform(String value) {
        testPlatform = TestPlatform.valueOf(value)
    }

    Test testPlatform(TestPlatform value) {
        setTestPlatform(value)
        return this
    }

    Test testPlatform(String value) {
        setTestPlatform(value)
        return this
    }

    @Console
    boolean verbose = true

    @Console
    boolean teamcity = false

    @Inject
    protected Instantiator getInstantiator() {
        throw new UnsupportedOperationException()
    }

    @Inject
    Test(FileResolver fileResolver) {
        super(Test.class)
        this.fileResolver = fileResolver
        description = "Executes Unity in batch mode and executes specified method"

        reports = instantiator.newInstance(UnityTestTaskReportsImpl.class, this)
        reports.xml.enabled = true
    }

    @Override
    UnityTestTaskReport getReports() {
        return reports
    }

    @Override
    UnityTestTaskReport reports(Closure closure) {
        return reports(new ClosureBackedAction<UnityTestTaskReport>(closure));
    }

    @Override
    UnityTestTaskReport reports(Action<? super UnityTestTaskReport> configureAction) {
        configureAction.execute(reports)
        return reports
    }

    @TaskAction
    @Override
    protected void exec() {
        DefaultArtifactVersion unityVersion = getUnityVersion(getUnityPath())
        logger.info("unity version major:${unityVersion.majorVersion} minor: ${unityVersion.minorVersion}")

        args(buildTestArguments(unityVersion))

        try {
            super.exec()
        }
        finally {
            //normalize test result file
            normalizeTestResult()
        }
    }
{'fraction_non_alphanumeric': 0.08235610061767075, 'fraction_numerical': 0.0039387700295407755, 'mean_word_length': 2.816877348821319, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '2490586', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2926, 'n_words': 754}