Skip to content

Commit

Permalink
Add test with check of generated project.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok committed Feb 15, 2022
1 parent 0458af3 commit 51bea51
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/build
local.properties
.gradle
.idea
.idea
**yarn.lock
15 changes: 14 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ kotlin {
implementation(kotlin("test"))
}
}
val jvmMain by getting {
dependencies {
implementation("org.apache.commons:commons-compress:1.2")
}
}
val jsMain by getting {
dependencies {
implementation(compose.web.core)
Expand All @@ -43,4 +48,12 @@ kotlin {
}
}
}
}
}

task<Copy>("fixMissingResources") {
dependsOn("jvmProcessResources")
tasks.findByPath("jvmTest")?.dependsOn(this)

from("$buildDir/processedResources/jvm/main")
into("$buildDir/resources/")
}
64 changes: 64 additions & 0 deletions src/jvmMain/kotlin/org/jetbrains/webwiz/ProjectBurner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.jetbrains.webwiz

import org.apache.commons.compress.archivers.zip.UnixStat
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import org.jetbrains.webwiz.generator.files.GradleBat
import org.jetbrains.webwiz.generator.files.GradleWrapperJar
import org.jetbrains.webwiz.generator.files.Gradlew
import org.jetbrains.webwiz.generator.generate
import org.jetbrains.webwiz.models.ProjectInfo
import java.io.File

fun ProjectInfo.asDir(workingDir: File): File {
val dir = workingDir.resolve(projectName)
dir.deleteRecursively()
dir.mkdirs()

generate().forEach { projectFile ->
val f = dir.resolve(projectFile.path)
f.parentFile.mkdirs()
f.createNewFile()

if (projectFile is GradleWrapperJar) {
f.outputStream().use { out ->
javaClass.getResourceAsStream("/binaries/gradle-wrapper").use { it.copyTo(out) }
}
} else {
if (projectFile is Gradlew) f.setExecutable(true)
if (projectFile is GradleBat) f.setExecutable(true)

f.writeText(projectFile.content)
}
}
return dir
}

fun ProjectInfo.asZip(workingDir: File) =
asDir(workingDir).zip().also { it.deleteRecursively() }

fun File.zip(): File {
val dir = this
val resultZip = dir.parentFile.resolve("${dir.name}.zip")
ZipArchiveOutputStream(resultZip).use { output ->
dir.walk().forEach { file ->
val entry = ZipArchiveEntry(file.relativeTo(dir).path)

//Set unix mode to preserve execution rights for gradlew script
if (file.isDirectory) {
entry.unixMode = UnixStat.DIR_FLAG or UnixStat.DEFAULT_DIR_PERM
} else {
entry.unixMode = UnixStat.FILE_FLAG or
if (file.name == "gradlew") UnixStat.DEFAULT_DIR_PERM else UnixStat.DEFAULT_FILE_PERM
}

output.putArchiveEntry(entry)
if (!file.isDirectory) {
file.inputStream().use { it.copyTo(output) }
}
output.closeArchiveEntry()
}
}

return resultZip
}
68 changes: 68 additions & 0 deletions src/jvmTest/kotlin/org/jetbrains/webwiz/GeneratedProjectTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.jetbrains.webwiz

import org.jetbrains.webwiz.models.GradlePlugin
import org.jetbrains.webwiz.models.KmpLibrary
import org.jetbrains.webwiz.models.KotlinVersion
import org.jetbrains.webwiz.models.ProjectInfo
import org.jetbrains.webwiz.models.Target
import org.junit.AfterClass
import org.junit.BeforeClass
import org.junit.Test
import java.io.File
import java.lang.ProcessBuilder.*
import kotlin.io.path.createTempDirectory
import kotlin.test.assertEquals

class GeneratedProjectTest {

companion object {
private lateinit var workingDir: File

@BeforeClass
@JvmStatic
fun prepare() {
workingDir = createTempDirectory().toFile()
}

@AfterClass
@JvmStatic
fun release() {
workingDir.deleteRecursively()
}
}

@Test
fun testGeneratedProjectJvmLinux() {
checkProject(
ProjectInfo(
"wizard-sample",
"library",
"my.test.lib",
KotlinVersion.Stable,
setOf(Target.JVM, Target.LINUX),
setOf(KmpLibrary.KERMIT_LOGGER, KmpLibrary.COROUTINES, KmpLibrary.DATE_TIME),
emptySet(),
emptySet(),
setOf(GradlePlugin.PUBLISH),
true
)
)
}

private fun checkProject(projectInfo: ProjectInfo) {
val dir = projectInfo.asDir(workingDir)

println("Project dir: ${dir.absolutePath}")
println("============start of the build============")
val proc = ProcessBuilder("${dir.path}/gradlew", "check", "--info").apply {
directory(dir)
redirectOutput(Redirect.INHERIT)
redirectError(Redirect.INHERIT)
}.start()

proc.waitFor()
println("============end of the build============")
assertEquals(0, proc.exitValue(), "'./gradlew check --info' exit code")

}
}

0 comments on commit 51bea51

Please sign in to comment.