Fail CI on extra comma

This commit is contained in:
ligi 2020-11-13 04:10:10 +01:00
parent 0532b7cc3b
commit 87d90de9f4
No known key found for this signature in database
GPG Key ID: 8E81894010ABF23D
5 changed files with 53 additions and 2 deletions

View File

@ -2,6 +2,9 @@ package org.ethereum.lists.chains
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Klaxon
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.ethereum.lists.chains.model.Chain
import org.kethereum.erc55.isValid
import org.kethereum.model.Address
import org.kethereum.rpc.HttpEthereumRPC
@ -36,6 +39,9 @@ class ENSRegistryAddressMustBeValid: Exception("ens registry must have valid add
fun checkChain(it: File, connectRPC: Boolean) {
println("processing $it")
parseWithMoshi(it)
val jsonObject = Klaxon().parseJsonObject(it.reader())
val chainAsLong = getNumber(jsonObject, "chainId")
@ -93,6 +99,15 @@ fun checkChain(it: File, connectRPC: Boolean) {
}
}
/*
moshi fails for extra commas
https://github.com/ethereum-lists/chains/issues/126
*/
private fun parseWithMoshi(fileToParse: File) {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
moshi.adapter(Chain::class.java).fromJson(fileToParse.readText())
}
private fun getNumber(jsonObject: JsonObject, field: String): Long {
return when (val chainId = jsonObject[field]) {
is Int -> chainId.toLong()

View File

@ -0,0 +1,13 @@
package org.ethereum.lists.chains.model
data class Chain(
val name: String,
val shortName: String,
val chain: String,
val network: String,
val chainId: Long,
val networkId: Long,
val rpc: List<String>,
val faucets: List<String>,
val infoURL: String,
)

View File

@ -1,3 +1,4 @@
import com.squareup.moshi.JsonEncodingException
import org.ethereum.lists.chains.*
import org.junit.Test
import java.io.File
@ -69,12 +70,19 @@ class TheChainChecker {
}
@Test(expected = ExtensionMustBeJSON::class)
fun shouldFailFoNonJSON() {
fun shouldFailForNonJSON() {
val file = getFile("invalid/1.nojson")
checkChain(file, false)
}
@Test(expected = JsonEncodingException::class)
fun shouldFailForExtraComma() {
val file = getFile("invalid/extracomma.json")
checkChain(file, false)
}
private fun getFile(s: String) = File(javaClass.classLoader.getResource("test_chains/$s").file)

View File

@ -9,5 +9,6 @@
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": []
"faucets": [],
"infoURL": "https://ethereum.org"
}

View File

@ -0,0 +1,14 @@
{
"name": "Ethereum Mainnet",
"shortName": "eth",
"chain": "ETH",
"network": "mainnet",
"chainId": 1,
"networkId": 1,
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"infoURL": "https://ethereum.org",
}