Skip to content

Commit

Permalink
Merge pull request #67 from batoulapps/support_wasm
Browse files Browse the repository at this point in the history
Support wasm/js
  • Loading branch information
ahmedre authored May 13, 2024
2 parents d71d833 + 0ae6605 commit 8a9deb3
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## development
- support wasm target

## version 0.0.5
- update Kotlin to 1.9.22
Expand Down
27 changes: 26 additions & 1 deletion adhan/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
Expand Down Expand Up @@ -27,6 +28,17 @@ kotlin {
}
}

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
nodejs {
testTask {
useMocha {
timeout = "30s"
}
}
}
}

linuxX64()
linuxArm64()
mingwX64()
Expand All @@ -53,10 +65,11 @@ kotlin {

val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
api("com.squareup.okio:okio:3.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")
}
}

Expand All @@ -75,6 +88,12 @@ kotlin {
implementation(npm("@js-joda/timezone", "2.3.0"))
}
}

val wasmJsTest by getting {
dependencies {
implementation(npm("@js-joda/timezone", "2.3.0"))
}
}
}

// set an environment variable and read it in the test
Expand Down Expand Up @@ -187,6 +206,12 @@ publishing {
}
}

// for wasm to be able to run tests
with(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin.apply(rootProject)) {
nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

// auto replace yarn.lock
rootProject.plugins.withType(org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlugin::class.java) {
rootProject.the<YarnRootExtension>().yarnLockMismatchReport =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.batoulapps.adhan2
import okio.FileSystem

expect class TestUtil() {
fun fileSystem(): FileSystem
fun fileSystem(): FileSystem?
fun environmentVariable(name: String): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import okio.Path.Companion.toPath

Expand All @@ -41,7 +40,10 @@ class TimingTest {
val testUtil = TestUtil()

val root = (testUtil.environmentVariable("ADHAN_ROOT") ?: "").toPath()
val fs = testUtil.fileSystem()

// disable time verification tests for platforms without filesystem support
// currently, this is just wasm
val fs = testUtil.fileSystem() ?: return

val jsonPath = root.resolve("Shared/Times/")
assertTrue(fs.exists(jsonPath), "Json Path Does not Exist: $jsonPath")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ external object JsJodaTimeZoneModule
private val jsJodaTz = JsJodaTimeZoneModule

actual class TestUtil actual constructor() {
actual fun fileSystem(): FileSystem = NodeJsFileSystem
actual fun fileSystem(): FileSystem? = NodeJsFileSystem
actual fun environmentVariable(name: String): String? = js("globalThis.process.env[name]") as String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.batoulapps.adhan2
import okio.FileSystem

actual class TestUtil actual constructor() {
actual fun fileSystem(): FileSystem = FileSystem.SYSTEM
actual fun fileSystem(): FileSystem? = FileSystem.SYSTEM
actual fun environmentVariable(name: String): String? = System.getenv(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import platform.posix.getenv

@OptIn(ExperimentalForeignApi::class)
actual class TestUtil actual constructor() {
actual fun fileSystem(): FileSystem = FileSystem.SYSTEM
actual fun fileSystem(): FileSystem? = FileSystem.SYSTEM
actual fun environmentVariable(name: String): String? = getenv(name)?.toKString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.batoulapps.adhan2

import okio.FileSystem

@JsModule("@js-joda/timezone")
external object JsJodaTimeZoneModule

private val jsJodaTz = JsJodaTimeZoneModule

actual class TestUtil actual constructor() {
actual fun fileSystem(): FileSystem? = null
actual fun environmentVariable(name: String): String? = null
}
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ allprojects {
mavenCentral()
}
}

// Disable NPM to NodeJS nightly compatibility check.
// Drop this when NodeJs version that supports latest Wasm become stable
// Required so wasm tests can run
tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask>().configureEach {
args.add("--ignore-engines")
}

0 comments on commit 8a9deb3

Please sign in to comment.