Skip to content

Commit

Permalink
Provide for better Platform support and testing (#350)
Browse files Browse the repository at this point in the history
* Add classes to allow migrations of methods to specific platforms

* Move some common methods

* Move platform to config package

* Shared JVM Native tests work

* Move parseString to common

* Works good for all platforms, stub for JvmNative

* Add two Reader tests to Native

* Make default build 2.13 and rename ConfigDocumentTest to ConfigDocumentFactoryTest

* Move ConfigFactoryTests to impl like other tests

* Move tests from ConfigTest to ConfigFactoryJvmTest and then ConfigTest to shared

* Make test class names singular

* Checkpoint commit for ConfigOrigin setup

* Update to more shared tests, add URL for Native, from 189 to 228 tests for Native

* Add env vars to shared in anticipation of using them in Native or JS

* Move ConfigSubstitutionTests to shared

* Json4s almost works

* Json4s cleanup

* Got json4s test to work

* Make comments generic

* Json4sTest is now cross platform

* Small updates, JS setting commented out

* Upgrade to new version of json4s to fix JS ClassCastException problem

* Upgrade Scala.js to 1.15.0, allow Reader tests now

* Update docs and readme for release
  • Loading branch information
ekrich authored Dec 28, 2023
1 parent 43444bc commit 7b7bddb
Show file tree
Hide file tree
Showing 36 changed files with 4,520 additions and 2,994 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ At a high level, the process is as follows:
## Versions
Release [1.5.1](https://github.com/ekrich/sconfig/releases/tag/v1.5.1) - (2023-09-14)<br/>
Release [1.6.0](https://github.com/ekrich/sconfig/releases/tag/v1.6.0) - (2023-12-28)<br/>
Release [1.5.1](https://github.com/ekrich/sconfig/releases/tag/v1.5.1) - (2023-09-15)<br/>
Release [1.5.0](https://github.com/ekrich/sconfig/releases/tag/v1.5.0) - (2022-09-19)<br/>
Release [1.4.9](https://github.com/ekrich/sconfig/releases/tag/v1.4.9) - (2022-01-25)<br/>
Release [1.4.8](https://github.com/ekrich/sconfig/releases/tag/v1.4.8) - (2022-01-12)<br/>
Expand Down
38 changes: 23 additions & 15 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ addCommandAlias(
).mkString(";", ";", "")
)

val prevVersion = "1.5.1"
val nextVersion = "1.5.2"
val prevVersion = "1.6.0"
val nextVersion = "1.7.0"

// stable snapshot is not great for publish local
def versionFmt(out: sbtdynver.GitDescribeOutput): String = {
Expand Down Expand Up @@ -53,7 +53,7 @@ val scCompat = "2.11.0"
val versionsBase = Seq(scala212, scala213)
val versions = versionsBase :+ scala3

ThisBuild / scalaVersion := scala3
ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := versions
ThisBuild / versionScheme := Some("early-semver")
ThisBuild / mimaFailOnNoPrevious := false
Expand Down Expand Up @@ -116,14 +116,21 @@ lazy val sconfig = crossProject(JVMPlatform, NativePlatform, JSPlatform)
scalacOptions ++= {
if (isScala3.value) dotcOpts else scalacOpts
},
libraryDependencies += "org.scala-lang.modules" %%% "scala-collection-compat" % scCompat,
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-s", "-v")
libraryDependencies ++= Seq(
"org.scala-lang.modules" %%% "scala-collection-compat" % scCompat,
"org.json4s" %%% "json4s-native-core" % "4.0.7" % Test
),
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-s", "-v"),
// env vars for tests
Test / envVars ++= Map(
"testList.0" -> "0",
"testList.1" -> "1",
"testClassesPath" -> (Test / classDirectory).value.getPath
)
)
.jvmSettings(
crossScalaVersions := versions,
libraryDependencies ++= Seq(
("io.crashbox" %% "spray-json" % "1.3.5-7" % Test)
.cross(CrossVersion.for3Use2_13),
"com.github.sbt" % "junit-interface" % "0.13.3" % Test
// includes junit 4.13.2
),
Expand All @@ -141,12 +148,6 @@ lazy val sconfig = crossProject(JVMPlatform, NativePlatform, JSPlatform)
Test / fork := true,
run / fork := true,
Test / run / fork := true,
// env vars for tests
Test / envVars ++= Map(
"testList.0" -> "0",
"testList.1" -> "1",
"testClassesPath" -> (Test / classDirectory).value.getPath
),
// uncomment for debugging
// Test / javaOptions += "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005",
// mima settings
Expand All @@ -156,14 +157,21 @@ lazy val sconfig = crossProject(JVMPlatform, NativePlatform, JSPlatform)
.nativeConfigure(_.enablePlugins(ScalaNativeJUnitPlugin))
.nativeSettings(
crossScalaVersions := versions,
nativeConfig ~= (_.withLinkStubs(true)),
nativeConfig ~= (
_.withLinkStubs(true)
.withEmbedResources(true)
),
logLevel := Level.Info, // Info or Debug
libraryDependencies += "org.ekrich" %%% "sjavatime" % javaTime % "provided"
)
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
.jsSettings(
crossScalaVersions := versions,
libraryDependencies += "org.ekrich" %%% "sjavatime" % javaTime % "provided"
libraryDependencies ++= Seq(
"org.ekrich" %%% "sjavatime" % javaTime % "provided",
("org.scala-js" %%% "scalajs-weakreferences" % "1.0.0")
.cross(CrossVersion.for3Use2_13)
)
)

lazy val `scalafix-rules` = (project in file("scalafix/rules"))
Expand Down
28 changes: 28 additions & 0 deletions docs/SCALA-NATIVE.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ val maxCol = conf.getInt("maxColumn")
val isGit = conf.getBoolean("project.git")
```

## Using Reader - StringReader example

Both JS and Native now support `java.io.Reader` which allows using `sconfig`
to parse all supported formats by passing a filename with extension. See the
following examples from the shared `ConfigFactoryTest` file.

```scala
val filename = "/test01.properties"
val fileStr =
"""
|# test01.properties file
|fromProps.abc=abc
|fromProps.one=1
|fromProps.bool=true
|fromProps.specialChars=hello^^
""".stripMargin

// create Reader
var test01Reader = new StringReader(fileStr)

val config = ConfigFactory.parseReader(
test01Reader,
ConfigParseOptions.defaults
.setSyntaxFromFilename(filename)
)
val specialChars = config.getString("fromProps.specialChars")
```

### How to read a HOCON configuation file into a String for Scala Native

In order to read the configuration file into a `String` you need to know the relative
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolvers ++= Resolver.sonatypeOssRepos("snapshots")

// versions
val crossVer = "1.3.2"
val scalaJSVersion = "1.14.0"
val scalaJSVersion = "1.15.0"
val scalaNativeVersion = "0.4.16"
val scalafix = "0.11.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.ekrich.config

/**
* [[ConfigFactory]] methods for Scala.js platform
*/
abstract class PlatformConfigFactory extends ConfigFactoryCommon {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import java.{util => ju}
/**
* To workaround missing implementations
*/
class PlatformClassLoader(cl: ClassLoader) extends ClassLoaderLike {
class PlatformClassLoader(cl: ClassLoader) extends TraitClassLoader {
def getResources(name: String): ju.Enumeration[URL] = ???
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package org.ekrich.config.impl
/**
* To workaround missing implementations
*/
class PlatformThread(thread: Thread) extends ThreadLike {
class PlatformThread(thread: Thread) extends TraitThread {
def getContextClassLoader(): ClassLoader = ???
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import java.net.{URI, URL}
/**
* To workaround missing implementations
*/
class PlatformUri(uri: URI) extends UriLike {
class PlatformUri(uri: URI) extends TraitUri {
def toURL(): URL = ???
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.ekrich.config

/**
* [[ConfigFactory]] methods common to JVM and Native
*/
abstract class ConfigFactoryJvmNative extends ConfigFactoryCommon {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import java.{util => ju}
/**
* To workaround missing implementations
*/
class PlatformClassLoader(cl: ClassLoader) extends ClassLoaderLike {
class PlatformClassLoader(cl: ClassLoader) extends TraitClassLoader {
def getResources(name: String): ju.Enumeration[URL] = cl.getResources(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import java.net.{URI, URL}
/**
* To workaround missing implementations
*/
class PlatformUri(uri: URI) extends UriLike {
class PlatformUri(uri: URI) extends TraitUri {
def toURL(): URL = uri.toURL()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.ekrich.config.impl

// import org.junit.Assert._
// import org.junit.Test

class ConfigFactoryJvmNativeTest {
// Empty for now - Scala.js has parody with Scala Native for now
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.ekrich.config

/**
* [[ConfigFactory]] methods for Scala JVM platform
*/
abstract class PlatformConfigFactory extends ConfigFactoryJvmNative {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package org.ekrich.config.impl
/**
* To workaround missing implementations in Scala.js and Scala Native
*/
class PlatformThread(thread: Thread) extends ThreadLike {
class PlatformThread(thread: Thread) extends TraitThread {
def getContextClassLoader(): ClassLoader = thread.getContextClassLoader()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.junit.Test
import scala.jdk.CollectionConverters._
import FileUtils._

class ConfigDocumentTest extends TestUtils {
class ConfigDocumentFactoryTest extends TestUtils {
private def configDocumentReplaceJsonTest(
origText: String,
finalText: String,
Expand Down
Loading

0 comments on commit 7b7bddb

Please sign in to comment.