Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfeo committed Sep 5, 2024
1 parent 6145507 commit d2220d4
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ data class Config(
*/
val cacheDir: File =
env["DEVELOCITY_API_CACHE_DIR"]?.let(::File)
?: let {
?: run {
val userHome = checkNotNull(systemProperties.userHome) { ERROR_NULL_USER_HOME }
File(userHome, ".develocity-api-kotlin-cache"),
}
File(userHome, ".develocity-api-kotlin-cache")
},

/**
* Max size of the HTTP cache. By default, uses environment variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RealLoggerFactory(
}

private fun setLogLevel() {
System.setProperty(SIMPLE_LOGGER_LOG_LEVEL, config.logLevel)
System.setProperty(LOG_LEVEL_SYSTEM_PROPERTY, config.logLevel)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ConfigTest {
@BeforeTest
fun before() {
env = FakeEnv("DEVELOCITY_API_URL" to "https://example.com/api/")
systemProperties = FakeSystemProperties()
}

@Test
Expand Down Expand Up @@ -51,4 +52,24 @@ class ConfigTest {
(env as FakeEnv)["DEVELOCITY_API_READ_TIMEOUT_MILLIS"] = "100000"
assertEquals(100_000L, Config().readTimeoutMillis)
}

@Test
fun `Given logLevel in env, logLevel is env value`() {
(env as FakeEnv)["DEVELOCITY_API_LOG_LEVEL"] = "trace"
assertEquals("trace", Config().logLevel)
}

@Test
fun `Given logLevel in System props and not in env, logLevel is prop value`() {
(env as FakeEnv)["DEVELOCITY_API_LOG_LEVEL"] = null
(systemProperties as FakeSystemProperties).logLevel = "info"
assertEquals("info", Config().logLevel)
}

@Test
fun `Given no logLevel set, logLevel is off`() {
(env as FakeEnv)["DEVELOCITY_API_LOG_LEVEL"] = null
(systemProperties as FakeSystemProperties).logLevel = null
assertEquals("off", Config().logLevel)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,19 @@ import kotlin.test.assertEquals

class LoggerFactoryTest {

private val logLevelProperty = RealLoggerFactory.LOG_LEVEL_SYSTEM_PROPERTY

@BeforeTest
@AfterTest
fun cleanup() {
System.clearProperty(SIMPLE_LOGGER_LOG_LEVEL)
System.clearProperty(logLevelProperty)
env = FakeEnv("DEVELOCITY_API_URL" to "https://example.com/")
}

@Test
fun `Logging off by default`() {
val loggerFactory = RealLoggerFactory(Config())
loggerFactory.newLogger(LoggerFactoryTest::class)
assertEquals("off", System.getProperty(SIMPLE_LOGGER_LOG_LEVEL))
}

@Test
fun `Pre-existing defaultLogLevel is honored`() {
val loggerFactory = RealLoggerFactory(Config(logLevel = "bar"))
System.setProperty(SIMPLE_LOGGER_LOG_LEVEL, "foo")
loggerFactory.newLogger(LoggerFactoryTest::class)
assertEquals("foo", System.getProperty(SIMPLE_LOGGER_LOG_LEVEL))
}

@Test
fun `Logging can be set from config`() {
fun `Level always copied from`() {
val loggerFactory = RealLoggerFactory(Config(logLevel = "foo"))
loggerFactory.newLogger(LoggerFactoryTest::class)
assertEquals("foo", System.getProperty(SIMPLE_LOGGER_LOG_LEVEL))
assertEquals("foo", System.getProperty(logLevelProperty))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.gabrielfeo.develocity.api.internal

data class FakeSystemProperties(
override var userHome: String? = System.getProperty("java.io.tmpdir"),
override var logLevel: String? = null,
) : SystemProperties

0 comments on commit d2220d4

Please sign in to comment.