<commit_before>/* Copyright 2009 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 geb.spock

import geb.report.ReportState
import geb.report.Reporter
import geb.report.ReportingListener
import geb.test.CallbackHttpServer
import org.opentest4j.TestAbortedException
import org.opentest4j.TestSkippedException
import org.spockframework.runtime.ConditionNotSatisfiedError
import spock.lang.*
import spock.util.EmbeddedSpecRunner
import spock.util.EmbeddedSpecRunner.SummarizedEngineExecutionResults

class GebReportingSpecSpec extends Specification {

    final static String REPORTING_SPEC_NAME = "GebReportingExtendingSpec"

    EmbeddedSpecRunner specRunner = new EmbeddedSpecRunner(throwFailure: false)

    @TempDir
    File temporaryDir

    @Shared
    @AutoCleanup("stop")
    def server = new CallbackHttpServer()

    def setupSpec() {
        server.start()
        server.html {
            body {
                div "Test page"
            }
        }
    }

    def setup() {
        specRunner.addClassImport(ConfigModifyingGebReportingSpec)
        specRunner.addClassImport(Unroll)
        specRunner.addClassImport(Reporter)
        specRunner.addClassImport(ReportingListener)
        specRunner.addClassImport(ReportState)
        specRunner.addClassImport(Shared)
        specRunner.addClassImport(TestAbortedException)
        specRunner.addClassImport(TestSkippedException)
    }

    File getReportDir() {
        new File(temporaryDir, "reports")
    }

    File getReportGroupDir() {
        new File(reportDir, "apackage/${REPORTING_SPEC_NAME}")
    }

    File reportFile(String name) {
        new File(reportGroupDir, name)
    }

    def "report named after the test and which contains response text is made at the end of a test"() {
        when:
        runReportingSpec """
            def "a request is made"() {
                given:
                config.reportOnTestFailureOnly = false
                go "/"
            }
        """

        then:
        reportFile("001-001-a request is made-end.html").text.startsWith("<?xml")
    }

    def "report is written after each test if reporting on failure only is disabled"() {
        when:
        runReportingSpec "config.reportOnTestFailureOnly = false", """
            def "first test"() {
                given:
                go "/"
            }

            def "second test"() {
                given:
                go "/"
            }
        """

        then:
        reportFile("001-001-first test-end.html").exists()
        reportFile("002-001-second test-end.html").exists()
    }

    def "report is not written after a successful test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            def "passing test"() {
                given:
                go "/"

                expect:
                true
            }
        """

        then:
        !reportGroupDir.listFiles()
    }

    def "report is written after a failing test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            def "failing test"() {
                given:
                go "/"

                expect:
                false
            }
        """

        then:
        reportFile("001-001-failing test-failure.html").exists()
    }

    def "report is written after a failing unrolled test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            @Unroll
            def "failing test"() {
                given:
                go "/"

                expect:
                false

                where:
                parameter << [0]
            }
        """

        then:
        reportFile("001-001-failing test _parameter_ 0_ _0_-failure.html").exists()
    }

    def "report is not written after a skipping test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            def "skipping test via TestSkippedException"() {
                given:
                throw new TestSkippedException('Skipping test')

                and:
                go "/"

                expect:
                false
            }

            def "skipping test via TestAbortedException"() {
                given:
                throw new TestAbortedException('Aborting test')

                and:
                go "/"

                expect:
                false
            }
        """

        then:
        !reportGroupDir.listFiles()
    }

    def "failure when writing a report does not overwrite the original test failure"() {
        when:
        def result = runReportingSpec """
            def "failing test"() {
                given:
                config.reporter = new Reporter() {
                    void writeReport(ReportState reportState) {<commit_msg>Simplify test, use on of Spock native ways to throw a TestAbortedException<commit_after>/* Copyright 2009 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 geb.spock

import geb.report.ReportState
import geb.report.Reporter
import geb.report.ReportingListener
import geb.test.CallbackHttpServer
import org.opentest4j.TestSkippedException
import org.spockframework.runtime.ConditionNotSatisfiedError
import spock.lang.*
import spock.util.EmbeddedSpecRunner
import spock.util.EmbeddedSpecRunner.SummarizedEngineExecutionResults

class GebReportingSpecSpec extends Specification {

    final static String REPORTING_SPEC_NAME = "GebReportingExtendingSpec"

    EmbeddedSpecRunner specRunner = new EmbeddedSpecRunner(throwFailure: false)

    @TempDir
    File temporaryDir

    @Shared
    @AutoCleanup("stop")
    def server = new CallbackHttpServer()

    def setupSpec() {
        server.start()
        server.html {
            body {
                div "Test page"
            }
        }
    }

    def setup() {
        specRunner.addClassImport(ConfigModifyingGebReportingSpec)
        specRunner.addClassImport(Unroll)
        specRunner.addClassImport(Reporter)
        specRunner.addClassImport(ReportingListener)
        specRunner.addClassImport(ReportState)
        specRunner.addClassImport(Shared)
        specRunner.addClassImport(PendingFeature)
        specRunner.addClassImport(TestSkippedException)
    }

    File getReportDir() {
        new File(temporaryDir, "reports")
    }

    File getReportGroupDir() {
        new File(reportDir, "apackage/${REPORTING_SPEC_NAME}")
    }

    File reportFile(String name) {
        new File(reportGroupDir, name)
    }

    def "report named after the test and which contains response text is made at the end of a test"() {
        when:
        runReportingSpec """
            def "a request is made"() {
                given:
                config.reportOnTestFailureOnly = false
                go "/"
            }
        """

        then:
        reportFile("001-001-a request is made-end.html").text.startsWith("<?xml")
    }

    def "report is written after each test if reporting on failure only is disabled"() {
        when:
        runReportingSpec "config.reportOnTestFailureOnly = false", """
            def "first test"() {
                given:
                go "/"
            }

            def "second test"() {
                given:
                go "/"
            }
        """

        then:
        reportFile("001-001-first test-end.html").exists()
        reportFile("002-001-second test-end.html").exists()
    }

    def "report is not written after a successful test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            def "passing test"() {
                given:
                go "/"

                expect:
                true
            }
        """

        then:
        !reportGroupDir.listFiles()
    }

    def "report is written after a failing test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            def "failing test"() {
                given:
                go "/"

                expect:
                false
            }
        """

        then:
        reportFile("001-001-failing test-failure.html").exists()
    }

    def "report is written after a failing unrolled test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            @Unroll
            def "failing test"() {
                given:
                go "/"

                expect:
                false

                where:
                parameter << [0]
            }
        """

        then:
        reportFile("001-001-failing test _parameter_ 0_ _0_-failure.html").exists()
    }

    def "report is not written after a skipped or aborted test when reporting on failure only is enabled"() {
        when:
        runReportingSpec """
            @PendingFeature
            def "aborted test"() {
                expect:
                false
            }

            def "skipped test"() {
                given:
                throw new TestSkippedException()

                expect:
                false
            }
        """

        then:
        !reportGroupDir.listFiles()
    }

    def "failure when writing a report does not overwrite the original test failure"() {
        when:
        def result = runReportingSpec """
            def "failing test"() {
                given:
                config.reporter = new Reporter() {
                    void writeReport(ReportState reportState) {
{'fraction_non_alphanumeric': 0.08152327221438646, 'fraction_numerical': 0.007992477668077104, 'mean_word_length': 1.7842931937172775, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '4265309', 'n_tokens_mistral': 2830, 'n_tokens_neox': 2641, 'n_words': 792}