|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.tasks.internal |
| 9 | + |
| 10 | +import com.facebook.react.tests.createProject |
| 11 | +import com.facebook.react.tests.createTestTask |
| 12 | +import java.io.* |
| 13 | +import org.assertj.core.api.Assertions.assertThat |
| 14 | +import org.junit.Assert.assertEquals |
| 15 | +import org.junit.Rule |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.rules.TemporaryFolder |
| 18 | + |
| 19 | +class PrepareGflagsTaskTest { |
| 20 | + |
| 21 | + @get:Rule val tempFolder = TemporaryFolder() |
| 22 | + |
| 23 | + @Test(expected = IllegalStateException::class) |
| 24 | + fun prepareGflagsTask_withMissingConfiguration_fails() { |
| 25 | + val task = createTestTask<PrepareGflagsTask>() |
| 26 | + |
| 27 | + task.taskAction() |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun prepareGflagsTask_copiesCMakefile() { |
| 32 | + val gflagspath = tempFolder.newFolder("gflagspath") |
| 33 | + val output = tempFolder.newFolder("output") |
| 34 | + val project = createProject() |
| 35 | + val gflagsThirdPartyPath = File(project.projectDir, "src/main/jni/third-party/gflags/") |
| 36 | + val task = |
| 37 | + createTestTask<PrepareGflagsTask>(project = project) { |
| 38 | + it.gflagsPath.setFrom(gflagspath) |
| 39 | + it.gflagsThirdPartyPath.set(gflagsThirdPartyPath) |
| 40 | + it.gflagsVersion.set("1.0.0") |
| 41 | + it.outputDir.set(output) |
| 42 | + } |
| 43 | + File(gflagsThirdPartyPath, "CMakeLists.txt").apply { |
| 44 | + parentFile.mkdirs() |
| 45 | + createNewFile() |
| 46 | + } |
| 47 | + task.taskAction() |
| 48 | + |
| 49 | + assertThat(output.listFiles()!!.any { it.name == "CMakeLists.txt" }).isTrue() |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun prepareGflagsTask_copiesSourceCodeAndHeaders() { |
| 54 | + val gflagspath = tempFolder.newFolder("gflagspath") |
| 55 | + val gflagsThirdPartyPath = tempFolder.newFolder("gflagspath/jni") |
| 56 | + val output = tempFolder.newFolder("output") |
| 57 | + val task = |
| 58 | + createTestTask<PrepareGflagsTask> { |
| 59 | + it.gflagsPath.setFrom(gflagspath) |
| 60 | + it.gflagsThirdPartyPath.set(gflagsThirdPartyPath) |
| 61 | + it.gflagsVersion.set("1.0.0") |
| 62 | + it.outputDir.set(output) |
| 63 | + } |
| 64 | + File(gflagspath, "gflags-1.0.0/src/gflags.cc").apply { |
| 65 | + parentFile.mkdirs() |
| 66 | + createNewFile() |
| 67 | + } |
| 68 | + File(gflagspath, "gflags-1.0.0/src/util.h").apply { |
| 69 | + parentFile.mkdirs() |
| 70 | + createNewFile() |
| 71 | + } |
| 72 | + |
| 73 | + task.taskAction() |
| 74 | + |
| 75 | + assertThat(File(output, "gflags/gflags.cc").exists()).isTrue() |
| 76 | + assertThat(File(output, "gflags/util.h").exists()).isTrue() |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun prepareGflagsTask_replacesTokenCorrectly() { |
| 81 | + val gflagspath = tempFolder.newFolder("gflagspath") |
| 82 | + val gflagsThirdPartyPath = tempFolder.newFolder("gflagspath/jni") |
| 83 | + val output = tempFolder.newFolder("output") |
| 84 | + val task = |
| 85 | + createTestTask<PrepareGflagsTask> { |
| 86 | + it.gflagsPath.setFrom(gflagspath) |
| 87 | + it.gflagsThirdPartyPath.set(gflagsThirdPartyPath) |
| 88 | + it.gflagsVersion.set("1.0.0") |
| 89 | + it.outputDir.set(output) |
| 90 | + } |
| 91 | + File(gflagspath, "gflags-1.0.0/src/gflags_declare.h.in").apply { |
| 92 | + parentFile.mkdirs() |
| 93 | + writeText( |
| 94 | + """ |
| 95 | +#define GFLAGS_NAMESPACE @GFLAGS_NAMESPACE@ |
| 96 | +#include <string> |
| 97 | +#if @HAVE_STDINT_H@ |
| 98 | +# include <stdint.h> |
| 99 | +#elif @HAVE_SYS_TYPES_H@ |
| 100 | +# include <sys/types.h> |
| 101 | +#elif @HAVE_INTTYPES_H@ |
| 102 | +# include <inttypes.h> |
| 103 | +#endif |
| 104 | +
|
| 105 | +
|
| 106 | +namespace GFLAGS_NAMESPACE { |
| 107 | +
|
| 108 | +#if @GFLAGS_INTTYPES_FORMAT_C99@ // C99 |
| 109 | +typedef int32_t int32; |
| 110 | +typedef uint32_t uint32; |
| 111 | +typedef int64_t int64; |
| 112 | +typedef uint64_t uint64; |
| 113 | +#elif @GFLAGS_INTTYPES_FORMAT_BSD@ // BSD |
| 114 | +typedef int32_t int32; |
| 115 | +typedef u_int32_t uint32; |
| 116 | +typedef int64_t int64; |
| 117 | +typedef u_int64_t uint64; |
| 118 | +#elif @GFLAGS_INTTYPES_FORMAT_VC7@ // Windows |
| 119 | +typedef __int32 int32; |
| 120 | +typedef unsigned __int32 uint32; |
| 121 | +typedef __int64 int64; |
| 122 | +typedef unsigned __int64 uint64; |
| 123 | +#else |
| 124 | +# error Do not know how to define a 32-bit integer quantity on your system |
| 125 | +#endif |
| 126 | +
|
| 127 | +} // namespace GFLAGS_NAMESPACE |
| 128 | +""") |
| 129 | + } |
| 130 | + File(gflagspath, "gflags-1.0.0/src/config.h.in").apply { |
| 131 | + parentFile.mkdirs() |
| 132 | + createNewFile() |
| 133 | + writeText("#cmakedefine") |
| 134 | + } |
| 135 | + File(gflagspath, "gflags-1.0.0/src/gflags_ns.h.in").apply { |
| 136 | + parentFile.mkdirs() |
| 137 | + createNewFile() |
| 138 | + writeText("@ns@ @NS@") |
| 139 | + } |
| 140 | + File(gflagspath, "gflags-1.0.0/src/gflags.h.in").apply { |
| 141 | + parentFile.mkdirs() |
| 142 | + createNewFile() |
| 143 | + writeText("@GFLAGS_ATTRIBUTE_UNUSED@\n@INCLUDE_GFLAGS_NS_H@") |
| 144 | + } |
| 145 | + File(gflagspath, "gflags-1.0.0/src/gflags_completions.h.in").apply { |
| 146 | + parentFile.mkdirs() |
| 147 | + createNewFile() |
| 148 | + writeText("@GFLAGS_NAMESPACE@") |
| 149 | + } |
| 150 | + |
| 151 | + task.taskAction() |
| 152 | + |
| 153 | + val declareFile = File(output, "gflags/gflags_declare.h") |
| 154 | + assertThat(declareFile.exists()).isTrue() |
| 155 | + assertEquals( |
| 156 | + declareFile.readText(), |
| 157 | + """ |
| 158 | +#define GFLAGS_NAMESPACE gflags |
| 159 | +#include <string> |
| 160 | +#if 1 |
| 161 | +# include <stdint.h> |
| 162 | +#elif 1 |
| 163 | +# include <sys/types.h> |
| 164 | +#elif 1 |
| 165 | +# include <inttypes.h> |
| 166 | +#endif |
| 167 | +
|
| 168 | +
|
| 169 | +namespace GFLAGS_NAMESPACE { |
| 170 | +
|
| 171 | +#if 1 // C99 |
| 172 | +typedef int32_t int32; |
| 173 | +typedef uint32_t uint32; |
| 174 | +typedef int64_t int64; |
| 175 | +typedef uint64_t uint64; |
| 176 | +#elif 1 // BSD |
| 177 | +typedef int32_t int32; |
| 178 | +typedef u_int32_t uint32; |
| 179 | +typedef int64_t int64; |
| 180 | +typedef u_int64_t uint64; |
| 181 | +#elif 1 // Windows |
| 182 | +typedef __int32 int32; |
| 183 | +typedef unsigned __int32 uint32; |
| 184 | +typedef __int64 int64; |
| 185 | +typedef unsigned __int64 uint64; |
| 186 | +#else |
| 187 | +# error Do not know how to define a 32-bit integer quantity on your system |
| 188 | +#endif |
| 189 | +
|
| 190 | +} // namespace GFLAGS_NAMESPACE |
| 191 | +""") |
| 192 | + |
| 193 | + val configFile = File(output, "gflags/config.h") |
| 194 | + assertThat(configFile.exists()).isTrue() |
| 195 | + assertEquals(configFile.readText(), "//cmakedefine") |
| 196 | + |
| 197 | + val nsFile = File(output, "gflags/gflags_google.h") |
| 198 | + assertThat(nsFile.exists()).isTrue() |
| 199 | + assertEquals(nsFile.readText(), "google GOOGLE") |
| 200 | + |
| 201 | + val gflagsFile = File(output, "gflags/gflags.h") |
| 202 | + assertThat(gflagsFile.exists()).isTrue() |
| 203 | + assertEquals(gflagsFile.readText(), "\n#include \"gflags/gflags_google.h\"") |
| 204 | + |
| 205 | + val completionsFile = File(output, "gflags/gflags_completions.h") |
| 206 | + assertThat(completionsFile.exists()).isTrue() |
| 207 | + assertEquals(completionsFile.readText(), "gflags") |
| 208 | + } |
| 209 | +} |
0 commit comments