-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to support File based methods on Scala Native (#384)
* ConfigParseOptions, shared to all, FileUtils, JVM to JVM Native * Refactor ConfigFactoryDocumentTest to Shared and JVM - JS and Native tests from 318 to 346 * Rename ConfigFactoryCommon to ConfigFactoryShared * Initial refactor for ConfigDocumentFactory * Format to pass CI * Remove ConfigDocumentFactory java.io.File methods from shared and put in JvmNative * Add test for native - not sure this is correct * File reading now works on Native * Share validation tests * Share JVM/Native ConfigFactoryDocumentTest * Update README for release * Remove commented out code * Remove commented out validation serialization test that was moved to JVM only
- Loading branch information
Showing
44 changed files
with
1,371 additions
and
330 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
7 changes: 7 additions & 0 deletions
7
sconfig/js/src/main/scala/org/ekrich/config/parser/PlatformConfigDocumentFactory.scala
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,7 @@ | ||
package org.ekrich.config.parser | ||
|
||
/** | ||
* [[ConfigDocumentFactory]] methods for Scala.js platform | ||
*/ | ||
abstract class PlatformConfigDocumentFactory | ||
extends ConfigDocumentFactoryShared {} |
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
44 changes: 44 additions & 0 deletions
44
...g/jvm-native/src/main/scala/org/ekrich/config/parser/ConfigDocumentFactoryJvmNative.scala
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,44 @@ | ||
package org.ekrich.config.parser | ||
|
||
import java.io.File | ||
|
||
import org.ekrich.config.ConfigParseOptions | ||
import org.ekrich.config.impl.Parseable | ||
|
||
/** | ||
* [[ConfigDocumentFactory]] methods common to JVM and Native | ||
*/ | ||
abstract class ConfigDocumentFactoryJvmNative | ||
extends ConfigDocumentFactoryShared { | ||
|
||
/** | ||
* Parses a file into a ConfigDocument instance. | ||
* | ||
* @param file | ||
* the file to parse | ||
* @param options | ||
* parse options to control how the file is interpreted | ||
* @return | ||
* the parsed configuration | ||
* @throws org.ekrich.config.ConfigException | ||
* on IO or parse errors | ||
*/ | ||
def parseFile(file: File, options: ConfigParseOptions): ConfigDocument = | ||
Parseable.newFile(file, options).parseConfigDocument() | ||
|
||
/** | ||
* Parses a file into a ConfigDocument instance as with | ||
* [[#parseFile(file:java\.io\.File,options:org\.ekrich\.config\.ConfigParseOptions)* parseFile(File, ConfigParseOptions)]] | ||
* but always uses the default parse options. | ||
* | ||
* @param file | ||
* the file to parse | ||
* @return | ||
* the parsed configuration | ||
* @throws org.ekrich.config.ConfigException | ||
* on IO or parse errors | ||
*/ | ||
def parseFile(file: File): ConfigDocument = | ||
parseFile(file, ConfigParseOptions.defaults) | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/ConfigDocumentFactoryTest.scala
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,44 @@ | ||
package org.ekrich.config.impl | ||
|
||
import java.io.{BufferedReader, FileReader} | ||
|
||
import org.ekrich.config.parser._ | ||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
import FileUtils._ | ||
|
||
class ConfigDocumentFactoryTest extends TestUtils { | ||
|
||
@Test | ||
def configDocumentFileParse: Unit = { | ||
val configDocument = | ||
ConfigDocumentFactory.parseFile(resourceFile("/test03.conf")) | ||
val fileReader = new BufferedReader( | ||
new FileReader(resourceFile("/test03.conf")) | ||
) | ||
var line = fileReader.readLine() | ||
val sb = new StringBuilder() | ||
while (line != null) { | ||
sb.append(line) | ||
sb.append("\n") | ||
line = fileReader.readLine() | ||
} | ||
fileReader.close() | ||
val fileText = sb.toString() | ||
assertEquals(fileText, defaultLineEndingsToUnix(configDocument.render)) | ||
} | ||
|
||
private def defaultLineEndingsToUnix(s: String): String = | ||
s.replaceAll(System.lineSeparator(), "\n") | ||
|
||
@Test | ||
def configDocumentReaderParse: Unit = { | ||
val configDocument = ConfigDocumentFactory.parseReader( | ||
new FileReader(resourceFile("/test03.conf")) | ||
) | ||
val configDocumentFile = | ||
ConfigDocumentFactory.parseFile(resourceFile("/test03.conf")) | ||
assertEquals(configDocumentFile.render, configDocument.render) | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/ConfigFactoryJvmNativeTest.scala
This file was deleted.
Oops, something went wrong.
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
78 changes: 78 additions & 0 deletions
78
sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/ValidationFileTest.scala
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,78 @@ | ||
/** | ||
* Copyright (C) 2011 Typesafe Inc. <http://typesafe.com> | ||
*/ | ||
package org.ekrich.config.impl | ||
|
||
import org.junit._ | ||
|
||
import org.ekrich.config.ConfigFactory | ||
import org.ekrich.config.ConfigParseOptions | ||
import org.ekrich.config.ConfigException | ||
import FileUtils._ | ||
|
||
class ValidationFileTest extends TestUtils { | ||
// TODO: There is a problem upstream where an exception is not thrown | ||
// when the file does not exist. Added a check in FileUtils for native only | ||
// jvm would need it. | ||
@Test | ||
def validation(): Unit = { | ||
val reference = ConfigFactory.parseFile( | ||
resourceFile("validate-reference.conf"), | ||
ConfigParseOptions.defaults | ||
) | ||
val conf = ConfigFactory.parseFile( | ||
resourceFile("validate-invalid.conf"), | ||
ConfigParseOptions.defaults | ||
) | ||
val e = intercept[ConfigException.ValidationFailed] { | ||
conf.checkValid(reference) | ||
} | ||
|
||
val expecteds = Seq( | ||
Missing("willBeMissing", 1, "number"), | ||
WrongType("int3", 7, "number", "object"), | ||
WrongType("float2", 9, "number", "boolean"), | ||
WrongType("float3", 10, "number", "list"), | ||
WrongType("bool1", 11, "boolean", "number"), | ||
WrongType("bool3", 13, "boolean", "object"), | ||
Missing("object1.a", 17, "string"), | ||
WrongType("object2", 18, "object", "list"), | ||
WrongType("object3", 19, "object", "number"), | ||
WrongElementType("array3", 22, "boolean", "object"), | ||
WrongElementType("array4", 23, "object", "number"), | ||
WrongType("array5", 24, "list", "number"), | ||
WrongType("a.b.c.d.e.f.g", 28, "boolean", "number"), | ||
Missing("a.b.c.d.e.f.j", 28, "boolean"), | ||
WrongType("a.b.c.d.e.f.i", 30, "boolean", "list") | ||
) | ||
|
||
checkValidationException(e, expecteds) | ||
} | ||
|
||
@Test | ||
def validationWithRoot(): Unit = { | ||
val objectWithB = parseObject("""{ b : c }""") | ||
val reference = ConfigFactory | ||
.parseFile( | ||
resourceFile("validate-reference.conf"), | ||
ConfigParseOptions.defaults | ||
) | ||
.withFallback(objectWithB) | ||
val conf = ConfigFactory.parseFile( | ||
resourceFile("validate-invalid.conf"), | ||
ConfigParseOptions.defaults | ||
) | ||
val e = intercept[ConfigException.ValidationFailed] { | ||
conf.checkValid(reference, "a", "b") | ||
} | ||
|
||
val expecteds = Seq( | ||
Missing("b", 1, "string"), | ||
WrongType("a.b.c.d.e.f.g", 28, "boolean", "number"), | ||
Missing("a.b.c.d.e.f.j", 28, "boolean"), | ||
WrongType("a.b.c.d.e.f.i", 30, "boolean", "list") | ||
) | ||
|
||
checkValidationException(e, expecteds) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
sconfig/jvm/src/main/scala/org/ekrich/config/parser/PlatformConfigDocumentFactory.scala
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,7 @@ | ||
package org.ekrich.config.parser | ||
|
||
/** | ||
* [[ConfigDocumentFactory]] methods for Scala JVM platform | ||
*/ | ||
abstract class PlatformConfigDocumentFactory | ||
extends ConfigDocumentFactoryJvmNative {} |
8 changes: 8 additions & 0 deletions
8
sconfig/jvm/src/test/scala/org/ekrich/config/impl/TestPath.scala
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,8 @@ | ||
package org.ekrich.config.impl | ||
|
||
import java.io.File | ||
|
||
// See: https://github.com/scala-native/scala-native/issues/4077 | ||
object TestPath { | ||
def file(): File = new File("src/test/resources") | ||
} |
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
sconfig/jvm/src/test/scala/org/ekrich/config/impl/ValidationSerializableTest.scala
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,24 @@ | ||
/** | ||
* Copyright (C) 2011 Typesafe Inc. <http://typesafe.com> | ||
*/ | ||
package org.ekrich.config.impl | ||
|
||
import org.junit._ | ||
|
||
import org.ekrich.config.ConfigException | ||
|
||
class ValidationSerializableTest extends TestUtils { | ||
@Test | ||
def validationFailedSerializable(): Unit = { | ||
// Reusing a previous test case to generate an error | ||
val reference = parseConfig("""{ a : [{},{},{}] }""") | ||
val conf = parseConfig("""{ a : 42 }""") | ||
val e = intercept[ConfigException.ValidationFailed] { | ||
conf.checkValid(reference) | ||
} | ||
val expecteds = Seq(WrongType("a", 1, "list", "number")) | ||
|
||
val actual = checkSerializableNoMeaningfulEquals(e) | ||
checkValidationException(actual, expecteds) | ||
} | ||
} |
Oops, something went wrong.