Skip to content

Commit 5046b2c

Browse files
committed
Share validation tests
1 parent 8db7989 commit 5046b2c

File tree

17 files changed

+304
-262
lines changed

17 files changed

+304
-262
lines changed

docs/SCALA-NATIVE.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ val config = ConfigFactory.parseReader(
7373
val specialChars = config.getString("fromProps.specialChars")
7474
```
7575

76-
### How to read a HOCON configuation file into a String for Scala Native
76+
In Scala Native `java.io.FileReader` is available so you can create a
77+
`FileReader` from a `File`.
7778

78-
In order to read the configuration file into a `String` you need to know the relative
79+
### How to read a HOCON configuation file using Scala Native
80+
81+
In order to read a configuration file you need to know the relative
7982
path from where the executable was started or use an absolute path. If the
8083
Scala Native executable is `run` from `sbt` it will have the current working directory
8184
equal to the directory at the base of your project where `sbt` was started. If curious
@@ -88,16 +91,21 @@ println(s"Working Dir: $dir")
8891
```
8992

9093
Continuing the same thought process you can use the following code to read the file
91-
into a `String` from a simple `sbt` project where the `src` directory is at the top
94+
from a simple `sbt` project where the `src` directory is at the top
9295
level of your project and you are using the `run` command. If you package your
9396
application or run the application executable directly, then making the path relative
9497
to the binary with the code above could be your best option. Another option is to use
95-
the `"user.home"` or the `"user.dir"` property to configure the file path.
98+
the `"user.home"` or the `"user.dir"` property to configure the file path. Note: When the
99+
executable is added to the path, the current working directory is where you start the
100+
executable from so keep that in mind
96101

97102
```scala
98-
import java.nio.file.{Files, Paths}
99-
val bytes = Files.readAllBytes(Paths.get("src/main/resources/myapp.conf"))
100-
val configStr = new String(bytes)
103+
import java.io.File
104+
val file = new File("src/main/resources/myapp.conf")
105+
// ConfigDocument
106+
val configDocument = ConfigDocumentFactory.parseFile(file)
107+
// or Config
108+
val config = ConfigFactory.parseFile(file)
101109
```
102110

103111
Using this code with the code above gives you a working solution to use `sconfig`

project/plugins.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ resolvers ++= Resolver.sonatypeOssRepos("snapshots")
55
val crossVer = "1.3.2"
66
val scalaJSVersion = "1.17.0"
77
val scalaNativeVersion = "0.5.5"
8-
val scalafix = "0.12.1"
8+
val scalafix = "0.13.0"
99

1010
// includes sbt-dynver sbt-pgp sbt-sonatype sbt-git
1111
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.6.1")
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.ekrich.config.parser
22

33
/**
4-
* [[ConfigFactoryDocument]] methods for Scala.js platform
4+
* [[ConfigDocumentFactory]] methods for Scala.js platform
55
*/
66
abstract class PlatformConfigDocumentFactory
77
extends ConfigDocumentFactoryShared {}

sconfig/jvm-native/src/main/scala/org/ekrich/config/parser/ConfigDocumentFactoryJvmNative.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.ekrich.config.ConfigParseOptions
66
import org.ekrich.config.impl.Parseable
77

88
/**
9-
* [[ConfigFactoryDocument]] methods common to JVM and Native
9+
* [[ConfigDocumentFactory]] methods common to JVM and Native
1010
*/
1111
abstract class ConfigDocumentFactoryJvmNative
1212
extends ConfigDocumentFactoryShared {

sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/ConfigFactoryDocumentTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package org.ekrich.config.impl
33
// import org.junit.Assert._
44
// import org.junit.Test
55

6-
class ConfigFactoryDocumentTest {
6+
class ConfigDocumentFactoryTest {
77
// Empty for now - Scala.js has parody with Scala Native for now
88
}

sconfig/jvm/src/test/scala/org/ekrich/config/impl/FileUtils.scala sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/FileUtils.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object FileUtils {
2727
else ""
2828

2929
val resourceDir = {
30-
val f = new File("src/test/resources")
30+
val f = TestPath.file()
3131
if (!f.exists()) {
3232
val here = new File(".").getAbsolutePath
3333
throw new Exception(
@@ -39,6 +39,9 @@ object FileUtils {
3939
f
4040
}
4141

42+
// TODO: can't test if file exists because some tests require the absense
43+
// of the file. The problem is that later on in the framework, not test
44+
// is done so silent failures can occur
4245
def resourceFile(filename: String): File =
4346
new File(resourceDir, filename)
4447

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (C) 2011 Typesafe Inc. <http://typesafe.com>
3+
*/
4+
package org.ekrich.config.impl
5+
6+
import org.junit._
7+
8+
import org.ekrich.config.ConfigFactory
9+
import org.ekrich.config.ConfigParseOptions
10+
import org.ekrich.config.ConfigException
11+
import FileUtils._
12+
13+
class ValidationFileTest extends TestUtils {
14+
// TODO: There is a problem upstream where an exception is not thrown
15+
// when the file does not exist. Added a check in FileUtils for native only
16+
// jvm would need it.
17+
@Test
18+
def validation(): Unit = {
19+
val reference = ConfigFactory.parseFile(
20+
resourceFile("validate-reference.conf"),
21+
ConfigParseOptions.defaults
22+
)
23+
val conf = ConfigFactory.parseFile(
24+
resourceFile("validate-invalid.conf"),
25+
ConfigParseOptions.defaults
26+
)
27+
val e = intercept[ConfigException.ValidationFailed] {
28+
conf.checkValid(reference)
29+
}
30+
31+
val expecteds = Seq(
32+
Missing("willBeMissing", 1, "number"),
33+
WrongType("int3", 7, "number", "object"),
34+
WrongType("float2", 9, "number", "boolean"),
35+
WrongType("float3", 10, "number", "list"),
36+
WrongType("bool1", 11, "boolean", "number"),
37+
WrongType("bool3", 13, "boolean", "object"),
38+
Missing("object1.a", 17, "string"),
39+
WrongType("object2", 18, "object", "list"),
40+
WrongType("object3", 19, "object", "number"),
41+
WrongElementType("array3", 22, "boolean", "object"),
42+
WrongElementType("array4", 23, "object", "number"),
43+
WrongType("array5", 24, "list", "number"),
44+
WrongType("a.b.c.d.e.f.g", 28, "boolean", "number"),
45+
Missing("a.b.c.d.e.f.j", 28, "boolean"),
46+
WrongType("a.b.c.d.e.f.i", 30, "boolean", "list")
47+
)
48+
49+
checkValidationException(e, expecteds)
50+
}
51+
52+
@Test
53+
def validationWithRoot(): Unit = {
54+
val objectWithB = parseObject("""{ b : c }""")
55+
val reference = ConfigFactory
56+
.parseFile(
57+
resourceFile("validate-reference.conf"),
58+
ConfigParseOptions.defaults
59+
)
60+
.withFallback(objectWithB)
61+
val conf = ConfigFactory.parseFile(
62+
resourceFile("validate-invalid.conf"),
63+
ConfigParseOptions.defaults
64+
)
65+
val e = intercept[ConfigException.ValidationFailed] {
66+
conf.checkValid(reference, "a", "b")
67+
}
68+
69+
val expecteds = Seq(
70+
Missing("b", 1, "string"),
71+
WrongType("a.b.c.d.e.f.g", 28, "boolean", "number"),
72+
Missing("a.b.c.d.e.f.j", 28, "boolean"),
73+
WrongType("a.b.c.d.e.f.i", 30, "boolean", "list")
74+
)
75+
76+
checkValidationException(e, expecteds)
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.ekrich.config.parser
22

33
/**
4-
* [[ConfigFactoryDocument]] methods for Scala JVM platform
4+
* [[ConfigDocumentFactory]] methods for Scala JVM platform
55
*/
66
abstract class PlatformConfigDocumentFactory
77
extends ConfigDocumentFactoryJvmNative {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.ekrich.config.impl
2+
3+
import java.io.File
4+
5+
// See: https://github.com/scala-native/scala-native/issues/4077
6+
object TestPath {
7+
def file(): File = new File("src/test/resources")
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (C) 2011 Typesafe Inc. <http://typesafe.com>
3+
*/
4+
package org.ekrich.config.impl
5+
6+
import org.junit._
7+
8+
import org.ekrich.config.ConfigException
9+
10+
class ValidationSerializableTest extends TestUtils {
11+
@Test
12+
def validationFailedSerializable(): Unit = {
13+
// Reusing a previous test case to generate an error
14+
val reference = parseConfig("""{ a : [{},{},{}] }""")
15+
val conf = parseConfig("""{ a : 42 }""")
16+
val e = intercept[ConfigException.ValidationFailed] {
17+
conf.checkValid(reference)
18+
}
19+
val expecteds = Seq(WrongType("a", 1, "list", "number"))
20+
21+
val actual = checkSerializableNoMeaningfulEquals(e)
22+
checkValidationException(actual, expecteds)
23+
}
24+
}

sconfig/jvm/src/test/scala/org/ekrich/config/impl/ValidationTest.scala

-162
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.ekrich.config.parser
22

33
/**
4-
* [[ConfigFactoryDocument]] methods for Scala Native platform
4+
* [[ConfigDocumentFactory]] methods for Scala Native platform
55
*/
66
abstract class PlatformConfigDocumentFactory
77
extends ConfigDocumentFactoryJvmNative {}

0 commit comments

Comments
 (0)