Skip to content

Commit

Permalink
Migrate diktat smoke tests to SAVE-cli mechanism (#1388)
Browse files Browse the repository at this point in the history
* Migrate diktat smoke tests to SAVE-cli mechanism

### What's done:
* migrated diktat smoke tests to SAVE-cli mechanism
Closes #1383

Co-authored-by: Andrey Kuleshov <andrewkuleshov7@gmail.com>
  • Loading branch information
Cheshiriks and orchestr7 authored Jul 15, 2022
1 parent 7582190 commit 54c8cce
Show file tree
Hide file tree
Showing 23 changed files with 623 additions and 280 deletions.
20 changes: 17 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
restore-keys: |
maven-build-
- name: Maven Install
run: mvn -B clean install
run: mvn -B clean install -DskipTests
- name: Maven run test
run: mvn test
- name: Code coverage report
uses: codecov/codecov-action@v3
with:
Expand Down Expand Up @@ -141,13 +143,25 @@ jobs:
- name: Maven Install
if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
run: |
mvn -B -T1C clean install
mvn -B -T1C clean install -DskipTests
shell: bash

- name: Maven run test
if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
run: |
mvn test
shell: bash

- name: Maven Install on windows
if: runner.os == 'Windows'
run: |
mvn -B -T1C clean install
mvn -B -T1C clean install -DskipTests
shell: cmd

- name: Maven run test
if: runner.os == 'Windows'
run: |
mvn test
shell: cmd

# This step needs a Git repository, so it's impossible to extract it
Expand Down
6 changes: 6 additions & 0 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<apache.httpclient.version>4.5.13</apache.httpclient.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -93,6 +94,11 @@
<groupId>com.bpodgursky</groupId>
<artifactId>jbool_expressions</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.httpclient.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.cqfn.diktat.ruleset.smoke

import org.cqfn.diktat.util.SAVE_VERSION
import org.apache.commons.io.FileUtils
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.condition.DisabledOnOs
import org.junit.jupiter.api.condition.OS
import java.io.File
import java.io.FileOutputStream
import java.nio.file.Paths
import kotlin.io.path.exists
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.name
import kotlin.io.path.pathString

@DisabledOnOs(OS.MAC)
class DiktatSaveSmokeTest : DiktatSmokeTestBase() {
override val isLintErrors = false
override fun fixAndCompare(
config: String,
expected: String,
test: String,
) {
saveSmokeTest(config, test)
}

/**
* @param testPath path to file with code that will be transformed by formatter, relative to [resourceFilePath]
* @param configFilePath path of diktat-analysis file
*/
@Suppress("TOO_LONG_FUNCTION")
private fun saveSmokeTest(
configFilePath: String,
testPath: String
) {
val processBuilder = createProcessBuilderWithCmd(testPath)

val file = File("src/test/resources/test/smoke/tmpSave.txt")
val configFile = File("src/test/resources/test/smoke/diktat-analysis.yml")
val configFileFrom = File(configFilePath)

configFile.createNewFile()
file.createNewFile()

FileUtils.copyFile(configFileFrom, configFile)

processBuilder.redirectErrorStream(true)
processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(file))

val process = processBuilder.start()
process.waitFor()

val output = file.readLines()
val saveOutput = output.joinToString("\n")

file.delete()

Assertions.assertTrue(
saveOutput.contains("SUCCESS")
)
}

companion object {
private const val KTLINT_VERSION = "0.46.1"

private fun getSaveForCurrentOs() = when {
System.getProperty("os.name").startsWith("Linux", ignoreCase = true) -> "save-$SAVE_VERSION-linuxX64.kexe"
System.getProperty("os.name").startsWith("Mac", ignoreCase = true) -> "save-$SAVE_VERSION-macosX64.kexe"
System.getProperty("os.name").startsWith("Windows", ignoreCase = true) -> "save-$SAVE_VERSION-mingwX64.exe"
else -> ""
}

private fun downloadFile(url: String, file: File) {
val httpClient = HttpClients.createDefault()
val request = HttpGet(url)
httpClient.use {
val response: CloseableHttpResponse = httpClient.execute(request)
response.use {
val fileSave = response.entity
fileSave?.let {
FileOutputStream(file).use { outstream -> fileSave.writeTo(outstream) }
}
}
}
}

@BeforeAll
@JvmStatic
internal fun beforeAll() {
val diktatDir: String =
Paths.get("../diktat-ruleset/target")
.takeIf { it.exists() }
?.listDirectoryEntries()
?.single { it.name.contains("diktat") }
?.pathString ?: ""

val diktat = File("src/test/resources/test/smoke/diktat.jar")
val diktatFrom = File(diktatDir)
val save = File("src/test/resources/test/smoke/${getSaveForCurrentOs()}")
val ktlint = File("src/test/resources/test/smoke/ktlint")

ktlint.createNewFile()
save.createNewFile()
diktat.createNewFile()

downloadFile("https://github.com/saveourtool/save-cli/releases/download/v$SAVE_VERSION/${getSaveForCurrentOs()}", save)
downloadFile("https://github.com/pinterest/ktlint/releases/download/$KTLINT_VERSION/ktlint", ktlint)

FileUtils.copyFile(diktatFrom, diktat)
}

@AfterAll
@JvmStatic
internal fun afterAll() {
val diktat = File("src/test/resources/test/smoke/diktat.jar")
val configFile = File("src/test/resources/test/smoke/diktat-analysis.yml")
val save = File("src/test/resources/test/smoke/${getSaveForCurrentOs()}")
val ktlint = File("src/test/resources/test/smoke/ktlint")

diktat.delete()
configFile.delete()
ktlint.delete()
save.delete()
}
}
}
Loading

0 comments on commit 54c8cce

Please sign in to comment.