apply plugin: 'com.blogspot.toomuchcoding.testprofiler'

configurations {
    smokeTestCompile.extendsFrom testCompile
    smokeTestRuntime.extendsFrom testRuntime
}

sourceSets {
    smokeTest {
        groovy.srcDir file('src/test-smoke/groovy')
        resources.srcDir file('src/test-smoke/resources')
        compileClasspath = sourceSets.main.output + sourceSets.test.output + configurations.smokeTestCompile
        runtimeClasspath = output + compileClasspath + configurations.smokeTestRuntime
    }
}

test {
    jvmArgs = project.gradle.startParameter.systemPropertiesArgs.entrySet().collect {"-D${it.key}=${it.value}"} + ['-Xmx512m']
    testLogging {
        exceptionFormat = 'full'
    }
    exclude '**/*UISpec.*'
}

task guiTest(type: Test, group: 'Verification') {
    description = 'Runs the acceptance GUI tests with Geb.'
    jvmArgs = project.gradle.startParameter.systemPropertiesArgs.entrySet().collect { "-D${it.key}=${it.value}" }
    testLogging {
        exceptionFormat = 'full'
    }
    include '**/*UISpec.*'
    reports.html.setDestination(file("${reporting.baseDir}/guiTests"))
}

task smokeTest(type: Test, group: 'Verification') {
    description = 'Executes smoke tests on real application instance (-DsmokeTestAppUrl=http://<app-host>:<port>)'
    testClassesDirs = sourceSets.smokeTest.output.classesDirs
    classpath = sourceSets.smokeTest.runtimeClasspath
    outputs.upToDateWhen { false }
    systemProperties['smokeTestAppUrl'] = System.properties['smokeTestAppUrl'] ?: "http://localhost:${findProperty('server.port') ?: defaultApplicationPort}"
    reports.html.setDestination(file("${reporting.baseDir}/smokeTest"))
    reports.junitXml.setDestination(file("${testResultsDir}/smokeTest"))
}

if (project.hasProperty('coverage')) {
    apply plugin: 'codenarc'
    apply plugin: 'jacoco'
    apply plugin: 'org.sonarqube'
    apply plugin: 'com.github.kt3k.coveralls'

    jacocoTestReport {
        reports {
            xml.enabled = true // coveralls plugin depends on xml format report
            html.enabled = true
        }
    }

    codenarc {
        configFile = file('gradle/config/codenarc/rules.groovy')
        maxPriority1Violations = 0
        maxPriority2Violations = 6
        maxPriority3Violations = 10
        reportFormat = 'xml'
        ignoreFailures = true
    }

    test {
        jacoco {
            excludes = ['*Configuration']
        }
    }

    sonarqube {
        properties {
            property 'sonar.groovy.jacoco.reportPath', "${project.buildDir}/jacoco/test.exec" //only for projects using Groovy in production code
        }
    }
}

testprofiler {
    minTestThreshold = 100

    buildBreaker {
        maxTestThreshold = project.findProperty('maxTestThreshold') ?: 30_000
        ifTestsExceedMaxThreshold {
            displayWarning()
        }
    }
}
