<commit_before>package net.dean.jraw.test.util

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import net.dean.jraw.http.oauth.Credentials

object CredentialsUtil {
    val script: Credentials
    val app: Credentials

    init {
        val creds = getCredentials()
        val (script, app) = creds.script to creds.app
        this.script = Credentials.script(script.username, script.password, script.clientId, script.clientSecret)
        this.app = Credentials.installedApp(app.clientId, app.redirectUrl)
    }

    private fun isTravis() =
        System.getenv("TRAVIS") != null && System.getenv("TRAVIS").toBoolean()

    private fun getCredentials(): TestingCredentials =
        if (isTravis()) getTravisCredentials() else getLocalCredentials()

    private fun getTravisCredentials(): TestingCredentials = TestingCredentials(
        ScriptStub(
            username = getenv("SCRIPT_USERNAME"),
            password = getenv("<PASSWORD>_PASSWORD"),
            clientSecret = getenv("SCRIPT_CLIENT_SECRET"),
            clientId = getenv("SCRIPT_CLIENT_ID")
        ),
        AppStub(
            clientId = getenv("APP_CLIENT_ID"),
            redirectUrl = getenv("APP_REDIRECT_URL")
        )
    )

    private fun getLocalCredentialStream() = CredentialsUtil::class.java.getResourceAsStream("/credentials.json") ?:
        throw SetupRequiredException("Could not load credentials.json")

    private fun getLocalCredentials(): TestingCredentials =
        jacksonObjectMapper().readValue<TestingCredentials>(getLocalCredentialStream())

    private fun getenv(name: String) = System.getenv(name) ?:
        throw IllegalStateException("Expecting environmental variable $name to exist")
}<commit_msg>Handle errors finding credentials.json<commit_after>package net.dean.jraw.test.util

import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import net.dean.jraw.http.oauth.Credentials

object CredentialsUtil {
    val script: Credentials
    val app: Credentials

    init {
        val creds = getCredentials()
        val (script, app) = creds.script to creds.app
        this.script = Credentials.script(script.username, script.password, script.clientId, script.clientSecret)
        this.app = Credentials.installedApp(app.clientId, app.redirectUrl)
    }

    private fun isTravis() =
        System.getenv("TRAVIS") != null && System.getenv("TRAVIS").toBoolean()

    private fun getCredentials(): TestingCredentials =
        if (isTravis()) getTravisCredentials() else getLocalCredentials()

    private fun getTravisCredentials(): TestingCredentials = TestingCredentials(
        ScriptStub(
            username = getenv("SCRIPT_USERNAME"),
            password = getenv("<PASSWORD>"),
            clientSecret = getenv("SCRIPT_CLIENT_SECRET"),
            clientId = getenv("SCRIPT_CLIENT_ID")
        ),
        AppStub(
            clientId = getenv("APP_CLIENT_ID"),
            redirectUrl = getenv("APP_REDIRECT_URL")
        )
    )

    private fun getLocalCredentialStream() = CredentialsUtil::class.java.getResourceAsStream("/credentials.json") ?:
        throw SetupRequiredException("Could not load credentials.json")

    private fun getLocalCredentials(): TestingCredentials {
        try {
            return jacksonObjectMapper().readValue<TestingCredentials>(getLocalCredentialStream())
        } catch (e: Exception) {
            // If there's an error in CredentialsUtil's init block, a NoClassDefFoundError will be thrown instead of the
            // real cause, this error right here. Make sure the user knows about it.
            if (e is MissingKotlinParameterException) {
                System.err.println("credentials.json: missing property '${e.parameter.name}'")
            } else {
                System.err.println("${e.javaClass.name}: ${e.message}")
            }
            throw e
        }
    }

    private fun getenv(name: String) = System.getenv(name) ?:
        throw IllegalStateException("Expecting environmental variable $name to exist")
}
{'fraction_non_alphanumeric': 0.10120881725527377, 'fraction_numerical': 0.0, 'mean_word_length': 3.211576846307385, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7319860', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1149, 'n_words': 240}