Skip to content

Commit 5995f3b

Browse files
committed
windows test fails
1 parent e548679 commit 5995f3b

File tree

6 files changed

+127
-73
lines changed

6 files changed

+127
-73
lines changed

src/main/kotlin/components/Execution.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
package components
22

3+
import models.ExecutionResult
34
import org.slf4j.Logger
45
import org.slf4j.LoggerFactory
56
import java.io.IOException
67

7-
fun executeWith(runtime: Runtime, command: String): String = execute(runtime, command)
8+
fun executeWith(runtime: Runtime, command: String): ExecutionResult = execute(runtime, command)
89

9-
fun execute(command: String): String = execute(Runtime.getRuntime(), command)
10+
fun execute(command: String): ExecutionResult = execute(Runtime.getRuntime(), command)
1011

11-
private fun execute(runtime: Runtime, command: String): String {
12+
private fun execute(runtime: Runtime, command: String): ExecutionResult {
1213
return try {
1314
when (getOs()) {
1415
Os.WINDOWS -> {
15-
val com = mutableListOf<String>()
16-
command.replace("\n", " ").split(" ").forEach {
17-
if (it.trim().isNotEmpty()) {
18-
com.add(it)
19-
}
20-
}
21-
runtime.exec(arrayOf("cmd", "/c", com.joinToString(" ").replace("&&", "&"))).outputString()
16+
ExecutionResult(
17+
command.normalizeCommand(),
18+
runtime.exec(
19+
arrayOf(
20+
"cmd",
21+
"/c",
22+
command.normalizeCommand()
23+
)
24+
).outputString()
25+
)
2226
}
2327
Os.OSX -> {
24-
runtime.exec(
25-
arrayOf(
26-
"/bin/bash",
27-
"-c",
28-
command
29-
)
30-
).outputString()
28+
ExecutionResult(
29+
command,
30+
runtime.exec(
31+
arrayOf(
32+
"/bin/bash",
33+
"-c",
34+
command
35+
)
36+
).outputString()
37+
)
3138
}
3239
}
3340
} catch (e: IOException) {
34-
""
41+
ExecutionResult(command.normalizeCommand(), "")
3542
}
3643
}
3744

src/main/kotlin/components/Extensions.kt

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ import javax.xml.transform.stream.StreamResult
1717

1818
fun String.runCommand(runner: (command: String, result: String) -> Unit = { _, _ -> }): String {
1919
val result = execute(this)
20-
runner(this, result)
21-
return result
20+
runner(result.command, result.result)
21+
return result.result
22+
}
23+
24+
fun String.normalizeCommand(): String {
25+
val com = mutableListOf<String>()
26+
this.replace("\n", " ").split(" ").forEach {
27+
if (it.trim().isNotEmpty()) {
28+
com.add(it)
29+
}
30+
}
31+
return com.joinToString(" ")
2232
}
2333

2434
fun String.escape(): String = Regex.escape(this)
@@ -36,13 +46,18 @@ fun String.androidTreatment(): String {
3646
}
3747

3848
fun File.validForConfiguration(configuration: Configuration): Boolean {
39-
var valid = this.absolutePath.contains("/${configuration.name}/")
40-
&& !this.absolutePath.contains("/$resourceBackup/")
49+
var valid = this.absolutePath.contains("${File.separator}${configuration.name}${File.separator}")
50+
&& !this.absolutePath.contains("${File.separator}$resourceBackup${File.separator}")
4151
if (valid) {
4252
valid = false
4353
configuration.srcFolders.forEach { folder ->
44-
if (this.absolutePath.contains("/$folder/".replace("//", "/"))
45-
&& !this.absolutePath.contains("/$resourceBackup/")
54+
if (this.absolutePath.contains(
55+
"${File.separator}$folder${File.separator}".replace(
56+
"${File.separator}${File.separator}",
57+
"${File.separator}"
58+
)
59+
)
60+
&& !this.absolutePath.contains("${File.separator}$resourceBackup${File.separator}")
4661
) {
4762
valid = true
4863
}
@@ -51,7 +66,13 @@ fun File.validForConfiguration(configuration: Configuration): Boolean {
5166
if (valid) {
5267
valid = false
5368
configuration.stringFiles.forEach { file ->
54-
if (this.absolutePath.contains("/$file".replace("//", "/"))) {
69+
if (this.absolutePath.contains(
70+
"${File.separator}$file".replace(
71+
"${File.separator}${File.separator}",
72+
"${File.separator}"
73+
)
74+
)
75+
) {
5576
valid = true
5677
}
5778
}
@@ -64,8 +85,13 @@ fun File.resourceFile(configuration: Configuration): ResourceFile? {
6485
var validFile: File? = null
6586
var valid = false
6687
configuration.srcFolders.forEach { folder ->
67-
if (this.absolutePath.contains("/$folder/".replace("//", "/"))
68-
&& !this.absolutePath.contains("/$resourceBackup/")
88+
if (this.absolutePath.contains(
89+
"${File.separator}$folder${File.separator}".replace(
90+
"${File.separator}${File.separator}",
91+
"${File.separator}"
92+
)
93+
)
94+
&& !this.absolutePath.contains("${File.separator}$resourceBackup${File.separator}")
6995
) {
7096
sourceFolder = folder
7197
valid = true
@@ -74,7 +100,13 @@ fun File.resourceFile(configuration: Configuration): ResourceFile? {
74100
if (valid) {
75101
valid = false
76102
configuration.stringFiles.forEach { file ->
77-
if (this.absolutePath.contains("/$file".replace("//", "/"))) {
103+
if (this.absolutePath.contains(
104+
"${File.separator}$file".replace(
105+
"${File.separator}${File.separator}",
106+
"${File.separator}"
107+
)
108+
)
109+
) {
78110
valid = true
79111
validFile = this
80112
}
@@ -97,7 +129,10 @@ fun Project.createExtension(): Extension {
97129
fun Process.outputString(): String {
98130
val input = this.inputStream.bufferedReader().use { it.readText() }
99131
val error = this.errorStream.bufferedReader().use { it.readText() }
100-
return if (input.isNotEmpty()) input else error
132+
return (when {
133+
input.isNotEmpty() -> input
134+
else -> error
135+
}).replace("\r", "")
101136
}
102137

103138
fun defaultConfig(): Configuration {
@@ -122,6 +157,9 @@ fun File.restore(projectPath: String): File {
122157
.replace("${File.separator}${File.separator}", File.separator)
123158

124159
val restore = File(cleanPath)
160+
if (restore.exists()) {
161+
restore.delete()
162+
}
125163
this.copyTo(restore, true)
126164
return restore
127165
}

src/main/kotlin/components/Tasks.kt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package components
22

3+
import java.io.File
4+
35
internal fun signingReportTask(): String = "${when (getOs()) {
46
Os.WINDOWS -> wrapperWindows
57
Os.OSX -> wrapperOsX
@@ -15,9 +17,35 @@ internal fun pluginBuildTask(): String = "${when (getOs()) {
1517
Os.OSX -> wrapperOsX
1618
}} build -d --exclude-task test"
1719

18-
internal fun deleteFolderCommand(directory: String): String = when (getOs()) {
19-
Os.WINDOWS -> "$deleteCommandWindows $directory"
20-
Os.OSX -> "$deleteCommandOsX $directory"
20+
internal val librarySetupTask = """
21+
${copyCommand()} src${File.separator}main${File.separator}kotlin${File.separator}components${File.separator}jni${File.separator}$osxLib out${File.separator}production${File.separator}classes${File.separator}$osxLib &&
22+
${copyCommand()} src${File.separator}main${File.separator}kotlin${File.separator}components${File.separator}jni${File.separator}$winLib out${File.separator}production${File.separator}classes${File.separator}$winLib
23+
""".trimIndent()
24+
25+
internal val prepareTask = """
26+
${resetCommand(testProjectName)}
27+
git clone https://github.com/StringCare/$testProjectName.git &&
28+
cd $testProjectName
29+
""".trimIndent()
30+
31+
internal val buildTask = """
32+
cd $testProjectName &&
33+
${gradleWrapper()} build &&
34+
${gradleWrapper()} --stop
35+
""".trimIndent()
36+
37+
// gradlew task needs export ANDROID_SDK_ROOT=/Users/efrainespada/Library/Android/sdk
38+
// echo "sdk.dir=${System.getenv("ANDROID_SDK_ROOT")}" > local.properties &&
39+
internal val signingReportTask = """
40+
$prepareTask &&
41+
${signingReportTask()}
42+
""".trimIndent()
43+
44+
internal fun resetCommand(directory: String): String {
45+
return when(getOs()) {
46+
Os.OSX -> "rm -rf $directory && "
47+
Os.WINDOWS -> "rmdir /q/s $directory && "
48+
}
2149
}
2250

2351
internal fun copyCommand(): String = when (getOs()) {

src/main/kotlin/components/Vars.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package components
22

3+
import java.io.File
4+
5+
internal const val testProjectName = "KotlinSample"
6+
internal const val defaultMainModule = "app"
7+
internal val mainModuleTest = "$testProjectName${File.separator}$defaultMainModule"
8+
39
internal const val resourceBackup = "resbackup"
410
internal const val extensionName = "stringcare"
511
internal const val winLib = "libsignKey.dll"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package models
2+
3+
data class ExecutionResult(val command: String, val result: String)

src/test/kotlin/SCTest.kt

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,10 @@ import org.junit.Test
44
import utils.modifyForTest
55
import java.io.File
66

7-
87
class SCTest {
98

109
private val logger by logger()
1110

12-
private val projectName = "KotlinSample"
13-
private val mainModule = "app"
14-
private val mainModuleTest = "$projectName${File.separator}$mainModule"
15-
16-
private val librarySetupTask = """
17-
${copyCommand()} src${File.separator}main${File.separator}kotlin${File.separator}components${File.separator}jni${File.separator}$osxLib out${File.separator}production${File.separator}classes${File.separator}$osxLib &&
18-
${copyCommand()} src${File.separator}main${File.separator}kotlin${File.separator}components${File.separator}jni${File.separator}$winLib out${File.separator}production${File.separator}classes${File.separator}$winLib
19-
""".trimIndent()
20-
21-
private val prepareTask = """
22-
${deleteFolderCommand(projectName)} &&
23-
git clone https://github.com/StringCare/$projectName.git &&
24-
cd $projectName
25-
""".trimIndent()
26-
27-
private val buildTask = """
28-
cd $projectName
29-
${gradleWrapper()} build
30-
""".trimIndent()
31-
32-
// gradlew task needs export ANDROID_SDK_ROOT=/Users/efrainespada/Library/Android/sdk
33-
// echo "sdk.dir=${System.getenv("ANDROID_SDK_ROOT")}" > local.properties &&
34-
private val signingReportTask = """
35-
$prepareTask &&
36-
${signingReportTask()}
37-
""".trimIndent()
38-
3911
@Before
4012
fun setup() {
4113
librarySetupTask.runCommand()
@@ -65,33 +37,33 @@ class SCTest {
6537
@Test
6638
fun `04 - (PLUGIN) locate string files for default configuration`() {
6739
prepareTask.runCommand { _, _ ->
68-
assert(locateFiles(projectName, defaultConfig()).isNotEmpty())
40+
assert(locateFiles(testProjectName, defaultConfig()).isNotEmpty())
6941
}
7042
}
7143

7244
@Test
7345
fun `05 - (PLUGIN) backup string files`() {
7446
prepareTask.runCommand { _, _ ->
75-
assert(backupFiles(projectName, defaultConfig()).isNotEmpty())
47+
assert(backupFiles(testProjectName, defaultConfig()).isNotEmpty())
7648
}
7749
}
7850

7951
@Test
8052
fun `06 - (PLUGIN) restore string files`() {
8153
prepareTask.runCommand { _, _ ->
82-
assert(restoreFiles(projectName, mainModule).isEmpty())
83-
assert(backupFiles(projectName, defaultConfig().apply {
54+
assert(restoreFiles(testProjectName, defaultMainModule).isEmpty())
55+
assert(backupFiles(testProjectName, defaultConfig().apply {
8456
stringFiles.add("strings_extra.xml")
8557
srcFolders.add("src${File.separator}other_source")
8658
}).isNotEmpty())
87-
assert(restoreFiles(projectName, mainModule).isNotEmpty())
59+
assert(restoreFiles(testProjectName, defaultMainModule).isNotEmpty())
8860
}
8961
}
9062

9163
@Test
9264
fun `07 - (PLUGIN) xml parsing`() {
9365
prepareTask.runCommand { _, _ ->
94-
val files = locateFiles(projectName, defaultConfig())
66+
val files = locateFiles(testProjectName, defaultConfig())
9567
files.forEach {
9668
assert(parseXML(it.file).isNotEmpty())
9769
}
@@ -103,7 +75,7 @@ class SCTest {
10375
signingReportTask.runCommand { _, report ->
10476
val key = report.extractFingerprint()
10577
assert(key.isNotEmpty())
106-
val files = locateFiles(projectName, defaultConfig())
78+
val files = locateFiles(testProjectName, defaultConfig())
10779
files.forEach { file ->
10880
val entities = parseXML(file.file)
10981
entities.forEach { entity ->
@@ -123,7 +95,7 @@ class SCTest {
12395
signingReportTask.runCommand { _, report ->
12496
val key = report.extractFingerprint()
12597
assert(key.isNotEmpty())
126-
val files = locateFiles(projectName, defaultConfig().apply {
98+
val files = locateFiles(testProjectName, defaultConfig().apply {
12799
stringFiles.add("strings_extra.xml")
128100
srcFolders.add("src${File.separator}other_source")
129101
})
@@ -156,13 +128,13 @@ class SCTest {
156128
@Test
157129
fun `10 - (PLUGIN) obfuscate xml`() {
158130
signingReportTask.runCommand { _, report ->
159-
val files = locateFiles(projectName, defaultConfig())
131+
val files = locateFiles(testProjectName, defaultConfig())
160132
files.forEach { file ->
161133
val entities = parseXML(file.file)
162134
assert(entities.isNotEmpty())
163135
modifyXML(file.file, mainModuleTest, report.extractFingerprint(), true)
164136
}
165-
val filesObfuscated = locateFiles(projectName, defaultConfig())
137+
val filesObfuscated = locateFiles(testProjectName, defaultConfig())
166138
filesObfuscated.forEach { file ->
167139
val entities = parseXML(file.file)
168140
assert(entities.isEmpty())
@@ -174,7 +146,7 @@ class SCTest {
174146
@Test
175147
fun `11 - (ANDROID COMPILATION) obfuscate xml and build (not real workflow)`() {
176148
signingReportTask.runCommand { _, report ->
177-
val files = locateFiles(projectName, defaultConfig().apply {
149+
val files = locateFiles(testProjectName, defaultConfig().apply {
178150
stringFiles.add("strings_extra.xml")
179151
srcFolders.add("src${File.separator}other_source")
180152
})
@@ -183,7 +155,7 @@ class SCTest {
183155
assert(entities.isNotEmpty())
184156
modifyXML(file.file, mainModuleTest, report.extractFingerprint(), true)
185157
}
186-
val filesObfuscated = locateFiles(projectName, defaultConfig().apply {
158+
val filesObfuscated = locateFiles(testProjectName, defaultConfig().apply {
187159
stringFiles.add("strings_extra.xml")
188160
srcFolders.add("src${File.separator}other_source")
189161
})
@@ -210,7 +182,7 @@ class SCTest {
210182
assert(report.contains("BUILD SUCCESSFUL"))
211183
}
212184
prepareTask.runCommand { _, _ ->
213-
modifyForTest(projectName)
185+
modifyForTest(testProjectName)
214186
buildTask.runCommand { _, androidReport ->
215187
assert(androidReport.contains("BUILD SUCCESSFUL"))
216188
}

0 commit comments

Comments
 (0)