-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check translation process for downloading strings (#96)
- Loading branch information
Showing
6 changed files
with
289 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/ioki/lokalise/gradle/plugin/tasks/CheckEverythingTranslatedTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ioki.lokalise.gradle.plugin.tasks | ||
|
||
import com.ioki.lokalise.gradle.plugin.DownloadStringsConfig | ||
import com.ioki.lokalise.gradle.plugin.LokaliseApiFactory | ||
import com.ioki.lokalise.gradle.plugin.LokaliseProjectApi | ||
import kotlinx.coroutines.runBlocking | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.TaskContainer | ||
import org.gradle.api.tasks.TaskProvider | ||
|
||
abstract class CheckEverythingTranslatedTask : DefaultTask() { | ||
|
||
@get:Input | ||
abstract val lokaliseApiFactory: Property<() -> LokaliseProjectApi> | ||
|
||
@TaskAction | ||
fun f() { | ||
val project = runBlocking { lokaliseApiFactory.get().invoke().getProject() } | ||
if (project.statistics.progressTotal != 100) { | ||
throw GradleException("Not all keys are translated") | ||
} | ||
} | ||
} | ||
|
||
internal fun TaskContainer.registerCheckEverythingTranslatedTask( | ||
lokaliseApiFactory: LokaliseApiFactory, | ||
config: DownloadStringsConfig, | ||
): TaskProvider<CheckEverythingTranslatedTask> = register( | ||
"checkEverythingIsTranslatedFor${config.name.replaceFirstChar { it.titlecase() }}", | ||
CheckEverythingTranslatedTask::class.java | ||
) { | ||
it.lokaliseApiFactory.set(lokaliseApiFactory::createProjectApi) | ||
it.onlyIf { config.checkTranslationProcess.getOrElse(false) } | ||
} |
194 changes: 194 additions & 0 deletions
194
src/test/kotlin/com/ioki/lokalise/gradle/plugin/unit/CheckEverythingTranslatedTaskTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package com.ioki.lokalise.gradle.plugin.unit | ||
|
||
import org.gradle.testkit.runner.GradleRunner | ||
import org.gradle.testkit.runner.TaskOutcome | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.io.TempDir | ||
import strikt.api.expectThat | ||
import strikt.assertions.contains | ||
import strikt.assertions.isEqualTo | ||
import strikt.assertions.isNotNull | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import kotlin.io.path.createFile | ||
import kotlin.io.path.writeText | ||
|
||
class CheckEverythingTranslatedTaskTest { | ||
|
||
@TempDir | ||
lateinit var tempDir: Path | ||
|
||
@BeforeEach | ||
fun `setup lokalise test project dir`() { | ||
Paths.get(tempDir.toString(), "settings.gradle").createFile() | ||
val buildGradle = Paths.get(tempDir.toString(), "build.gradle.kts") | ||
|
||
buildGradle.writeText( | ||
""" | ||
plugins { | ||
id("com.ioki.lokalise") | ||
} | ||
lokalise { | ||
apiToken.set("AWESOM3-AP1-T0KEN") | ||
projectId.set("AW3S0ME-PR0J3C7-1D") | ||
downloadStringsConfigs { | ||
register("library") { | ||
checkTranslationProcess = true | ||
} | ||
register("flavor") | ||
} | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@Test | ||
fun `running downloadTranslationsForLibrary will first run checkEverythingIsTranslatedForLibrary`() { | ||
val result = GradleRunner.create() | ||
.withProjectDir(tempDir.toFile()) | ||
.withPluginClasspath() | ||
.withArguments("downloadTranslationsForLibrary") | ||
.buildAndFail() | ||
|
||
expectThat(result.output).contains(":checkEverythingIsTranslatedForLibrary") | ||
expectThat(result.task(":checkEverythingIsTranslatedForLibrary")) | ||
.isNotNull() | ||
.get { outcome } | ||
.isEqualTo(TaskOutcome.FAILED) // Failed because of API call, but not skipped | ||
} | ||
|
||
@Test | ||
fun `running downloadTranslationsForFlavor will skip checkEverythingIsTranslatedForFlavor`() { | ||
val result = GradleRunner.create() | ||
.withProjectDir(tempDir.toFile()) | ||
.withPluginClasspath() | ||
.withArguments("downloadTranslationsForFlavor") | ||
.buildAndFail() | ||
|
||
expectThat(result.task(":checkEverythingIsTranslatedForFlavor")) | ||
.isNotNull() | ||
.get { outcome } | ||
.isEqualTo(TaskOutcome.SKIPPED) | ||
} | ||
|
||
@Test | ||
fun `running checkEverythingIsTranslatedForLibrary if project is not translated will throw`() { | ||
val buildGradle = Paths.get(tempDir.toString(), "build.gradle.kts") | ||
buildGradle.writeText( | ||
""" | ||
import com.ioki.lokalise.gradle.plugin.tasks.CheckEverythingTranslatedTask | ||
import com.ioki.lokalise.gradle.plugin.* | ||
import com.ioki.lokalise.api.models.Project as LokaliseProject | ||
plugins { | ||
id("com.ioki.lokalise") | ||
} | ||
val fakeLokaliseApi: LokaliseProjectApi = object : LokaliseProjectApi { | ||
override suspend fun getProject(): LokaliseProject { | ||
return ${lokaliseProject(10)} | ||
} | ||
} | ||
tasks.register<CheckEverythingTranslatedTask>("testCheckEverythingTranslatedTask") { | ||
lokaliseApiFactory.set({ fakeLokaliseApi }) | ||
} | ||
""".trimIndent() | ||
) | ||
val result = GradleRunner.create() | ||
.withProjectDir(tempDir.toFile()) | ||
.withPluginClasspath() | ||
.withArguments("testCheckEverythingTranslatedTask", "--info") | ||
.buildAndFail() | ||
|
||
expectThat(result.output.contains("Not all keys are translated")) | ||
} | ||
|
||
@Test | ||
fun `running checkEverythingIsTranslatedForLibrary if project is translated will succeed`() { | ||
val buildGradle = Paths.get(tempDir.toString(), "build.gradle.kts") | ||
buildGradle.writeText( | ||
""" | ||
import com.ioki.lokalise.gradle.plugin.tasks.CheckEverythingTranslatedTask | ||
import com.ioki.lokalise.gradle.plugin.* | ||
import com.ioki.lokalise.api.models.Project as LokaliseProject | ||
plugins { | ||
id("com.ioki.lokalise") | ||
} | ||
val fakeLokaliseApi: LokaliseProjectApi = object : LokaliseProjectApi { | ||
override suspend fun getProject(): LokaliseProject { | ||
return ${lokaliseProject(100)} | ||
} | ||
tasks.register<CheckEverythingTranslatedTask>("testCheckEverythingTranslatedTask") { | ||
lokaliseApiFactory.set({ fakeLokaliseApi }) | ||
} | ||
""".trimIndent() | ||
) | ||
val result = GradleRunner.create() | ||
.withProjectDir(tempDir.toFile()) | ||
.withPluginClasspath() | ||
.withArguments("testCheckEverythingTranslatedTask", "--info") | ||
.buildAndFail() | ||
|
||
expectThat(result.output.contains("Not all keys are translated")) | ||
} | ||
} | ||
|
||
private fun lokaliseProject(totalProgress: Int): String = """ | ||
LokaliseProject( | ||
projectId = "", | ||
projectType = "", | ||
name = "", | ||
description = "", | ||
createdAt = "", | ||
createdAtTimestamp = 0, | ||
createdBy = 0, | ||
createdByEmail = "", | ||
teamId = 0, | ||
baseLanguageId = 0, | ||
baseLanguageIso = "", | ||
settings = LokaliseProject.Settings( | ||
perPlatformKeyNames = false, | ||
reviewing = false, | ||
autoToggleUnverified = false, | ||
offlineTranslation = false, | ||
keyEditing = false, | ||
inlineMachineTranslations = false, | ||
branching = false, | ||
segmentation = false, | ||
customTranslationStatuses = false, | ||
customTranslationStatusesAllowMultiple = false, | ||
contributorPreviewDownloadEnabled = false | ||
), | ||
statistics = LokaliseProject.Statistics( | ||
progressTotal = $totalProgress, | ||
keysTotal = 0, | ||
team = 0, | ||
baseWords = 0, | ||
qaIssuesTotal = 0, | ||
qaIssues = LokaliseProject.Statistics.QaIssues( | ||
notReviewed = 0, | ||
unverified = 0, | ||
spellingGrammar = 0, | ||
inconsistentPlaceholders = 0, | ||
inconsistentHtml = 0, | ||
differentNumberOfUrls = 0, | ||
differentUrls = 0, | ||
leadingWhitespace = 0, | ||
trailingWhitespace = 0, | ||
differentNumberOfEmailAddress = 0, | ||
differentEmailAddress = 0, | ||
differentBrackets = 0, | ||
differentNumbers = 0, | ||
doubleSpace = 0, | ||
specialPlaceholder = 0, | ||
unbalancedBrackets = 0 | ||
), | ||
languages = emptyList() | ||
) | ||
) | ||
""".trimIndent() |