|
| 1 | +package io.embrace.android.gradle.plugin.config.variant |
| 2 | + |
| 3 | +import io.embrace.android.gradle.fakes.FakeConfigFileDirectory |
| 4 | +import io.embrace.android.gradle.fakes.FakeSystemWrapper |
| 5 | +import io.embrace.android.gradle.plugin.model.AndroidCompactedVariantData |
| 6 | +import org.junit.AfterClass |
| 7 | +import org.junit.Assert.assertEquals |
| 8 | +import org.junit.Assert.assertNull |
| 9 | +import org.junit.BeforeClass |
| 10 | +import org.junit.Test |
| 11 | +import java.io.File |
| 12 | +import java.nio.file.Files |
| 13 | +import kotlin.io.path.pathString |
| 14 | + |
| 15 | +class BuildVariantConfigFromFileTest { |
| 16 | + |
| 17 | + @Test(expected = IllegalStateException::class) |
| 18 | + fun `build with no config file finders should throw exception`() { |
| 19 | + buildVariantConfig( |
| 20 | + fakeVariantInfo, |
| 21 | + projectDirectory, |
| 22 | + emptyList() |
| 23 | + ) |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `config file not found by config finder does not throw exception`() { |
| 28 | + val variantConfiguration = buildVariantConfig( |
| 29 | + fakeVariantInfo, |
| 30 | + projectDirectory, |
| 31 | + listOf(VariantConfigurationFileFinder(projectDirectory, listOf())) |
| 32 | + ) |
| 33 | + |
| 34 | + assertNull(variantConfiguration) |
| 35 | + } |
| 36 | + |
| 37 | + @Test(expected = IllegalArgumentException::class) |
| 38 | + fun `bad config file throws exception`() { |
| 39 | + configFile.writeText("not a json") |
| 40 | + buildVariantConfig( |
| 41 | + fakeVariantInfo, |
| 42 | + projectDirectory, |
| 43 | + listOf(fileFinder) |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun `variantConfiguration is built correctly`() { |
| 49 | + val json = """ |
| 50 | + { |
| 51 | + "app_id": "abcde", |
| 52 | + "api_token": "12345678901234567890123456789012", |
| 53 | + "ndk_enabled": true |
| 54 | + } |
| 55 | + """.trimIndent() |
| 56 | + configFile.writeText(json) |
| 57 | + |
| 58 | + val variantConfiguration = buildVariantConfig( |
| 59 | + fakeVariantInfo, |
| 60 | + projectDirectory, |
| 61 | + listOf(fileFinder) |
| 62 | + ) |
| 63 | + |
| 64 | + assertEquals("abcde", variantConfiguration?.appId) |
| 65 | + assertEquals("12345678901234567890123456789012", variantConfiguration?.apiToken) |
| 66 | + assertEquals(true, variantConfiguration?.ndkEnabled) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `apiToken is read from environment variable when it is not set in config file`() { |
| 71 | + val json = """ |
| 72 | + { |
| 73 | + "app_id": "abcde", |
| 74 | + "ndk_enabled": true |
| 75 | + } |
| 76 | + """.trimIndent() |
| 77 | + configFile.writeText(json) |
| 78 | + |
| 79 | + val fakeSystemWrapper = FakeSystemWrapper().apply { |
| 80 | + setEnvironmentVariable("EMBRACE_API_TOKEN", "env12345678901234567890123456789") |
| 81 | + } |
| 82 | + |
| 83 | + val variantConfiguration = buildVariantConfig( |
| 84 | + fakeVariantInfo, |
| 85 | + projectDirectory, |
| 86 | + listOf(fileFinder), |
| 87 | + fakeSystemWrapper |
| 88 | + ) |
| 89 | + |
| 90 | + assertEquals("env12345678901234567890123456789", variantConfiguration?.apiToken) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `apiToken is not read from environment variable when it is set in config file`() { |
| 95 | + val json = """ |
| 96 | + { |
| 97 | + "api_token": "config78901234567890123456789012" |
| 98 | + } |
| 99 | + """.trimIndent() |
| 100 | + configFile.writeText(json) |
| 101 | + |
| 102 | + val fakeSystemWrapper = FakeSystemWrapper().apply { |
| 103 | + setEnvironmentVariable("EMBRACE_API_TOKEN", "env12345678901234567890123456789") |
| 104 | + } |
| 105 | + |
| 106 | + val variantConfiguration = buildVariantConfig( |
| 107 | + fakeVariantInfo, |
| 108 | + projectDirectory, |
| 109 | + listOf(fileFinder), |
| 110 | + fakeSystemWrapper |
| 111 | + ) |
| 112 | + |
| 113 | + assertEquals("config78901234567890123456789012", variantConfiguration?.apiToken) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun `appId is read from environment variable when it is not set in config file`() { |
| 118 | + val json = """ |
| 119 | + { |
| 120 | + "api_token": "config78901234567890123456789012", |
| 121 | + "ndk_enabled": true |
| 122 | + } |
| 123 | + """.trimIndent() |
| 124 | + configFile.writeText(json) |
| 125 | + |
| 126 | + val fakeSystemWrapper = FakeSystemWrapper().apply { |
| 127 | + setEnvironmentVariable("EMBRACE_APP_ID", "env12") |
| 128 | + } |
| 129 | + |
| 130 | + val variantConfiguration = buildVariantConfig( |
| 131 | + fakeVariantInfo, |
| 132 | + projectDirectory, |
| 133 | + listOf(fileFinder), |
| 134 | + fakeSystemWrapper |
| 135 | + ) |
| 136 | + |
| 137 | + assertEquals("env12", variantConfiguration?.appId) |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + fun `appId is not read from environment variable when it is set in config file`() { |
| 142 | + val json = """ |
| 143 | + { |
| 144 | + "app_id": "confg" |
| 145 | + } |
| 146 | + """.trimIndent() |
| 147 | + configFile.writeText(json) |
| 148 | + |
| 149 | + val fakeSystemWrapper = FakeSystemWrapper().apply { |
| 150 | + setEnvironmentVariable("EMBRACE_APP_ID", "env12") |
| 151 | + } |
| 152 | + |
| 153 | + val variantConfiguration = buildVariantConfig( |
| 154 | + fakeVariantInfo, |
| 155 | + projectDirectory, |
| 156 | + listOf(fileFinder), |
| 157 | + fakeSystemWrapper |
| 158 | + ) |
| 159 | + |
| 160 | + assertEquals("confg", variantConfiguration?.appId) |
| 161 | + } |
| 162 | + |
| 163 | + companion object { |
| 164 | + private lateinit var configFile: File |
| 165 | + private lateinit var fileFinder: VariantConfigurationFileFinder |
| 166 | + private lateinit var projectDirectory: FakeConfigFileDirectory |
| 167 | + |
| 168 | + /** |
| 169 | + * Creates a fake project directory with a subdirectory `src` that contains the Embrace config file. |
| 170 | + * We can update the config file directly in the tests to simulate different scenarios. |
| 171 | + */ |
| 172 | + private fun createFakeProjectDirectory(): FakeConfigFileDirectory { |
| 173 | + val tempDirectory = Files.createTempDirectory("test") |
| 174 | + Files.createDirectories(tempDirectory.resolve("src")) |
| 175 | + return FakeConfigFileDirectory(tempDirectory.pathString, true).apply { |
| 176 | + subDirectoriesWithConfigFiles.add("src") |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private fun getConfigFileForProjectDirectory(projectDirectory: FakeConfigFileDirectory): File { |
| 181 | + val dirPath = projectDirectory.asFile.toPath() |
| 182 | + return dirPath.resolve("src").resolve("embrace-config.json").toFile() |
| 183 | + } |
| 184 | + |
| 185 | + // Create the fake project directory and config file finder before running the tests. |
| 186 | + @JvmStatic |
| 187 | + @BeforeClass |
| 188 | + fun setup() { |
| 189 | + projectDirectory = createFakeProjectDirectory() |
| 190 | + configFile = getConfigFileForProjectDirectory(projectDirectory) |
| 191 | + fileFinder = VariantConfigurationFileFinder(projectDirectory, listOf("src")) |
| 192 | + } |
| 193 | + |
| 194 | + // Clean up the temporary directory after all tests have run. |
| 195 | + @JvmStatic |
| 196 | + @AfterClass |
| 197 | + fun tearDown() { |
| 198 | + // Clean up the temporary directory after tests |
| 199 | + projectDirectory.asFile.deleteRecursively() |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private val fakeVariantInfo = AndroidCompactedVariantData( |
| 204 | + name = "variant-name", |
| 205 | + flavorName = "flavor-name", |
| 206 | + buildTypeName = "buildType-name", |
| 207 | + isBuildTypeDebuggable = false, |
| 208 | + versionName = "1.0", |
| 209 | + productFlavors = listOf("product-flavor", "2nd-product-flavor"), |
| 210 | + sourceMapPath = "source" |
| 211 | + ) |
| 212 | +} |
0 commit comments