|
| 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 |
| 9 | + |
| 10 | +import com.facebook.react.tests.createProject |
| 11 | +import com.facebook.react.tests.createTestTask |
| 12 | +import java.io.File |
| 13 | +import java.io.FileOutputStream |
| 14 | +import org.junit.Assert.assertEquals |
| 15 | +import org.junit.Assert.assertFalse |
| 16 | +import org.junit.Assert.assertTrue |
| 17 | +import org.junit.Rule |
| 18 | +import org.junit.Test |
| 19 | +import org.junit.rules.TemporaryFolder |
| 20 | + |
| 21 | +class RunAutolinkingConfigTaskTest { |
| 22 | + |
| 23 | + @get:Rule val tempFolder = TemporaryFolder() |
| 24 | + |
| 25 | + @Test |
| 26 | + fun runAutolinkingConfigTask_groupIsSetCorrectly() { |
| 27 | + val task = createTestTask<BundleHermesCTask> {} |
| 28 | + assertEquals("react", task.group) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun runAutolinkingConfigTask_staticInputs_areSetCorrectly() { |
| 33 | + val project = createProject() |
| 34 | + |
| 35 | + val task = |
| 36 | + createTestTask<RunAutolinkingConfigTask> { |
| 37 | + it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) |
| 38 | + it.autolinkLockFiles.set(project.files("packager.lock", "another-packager.lock")) |
| 39 | + it.autolinkConfigFile.set(tempFolder.newFile("dependencies.json")) |
| 40 | + it.autolinkOutputFile.set(tempFolder.newFile("output.json")) |
| 41 | + } |
| 42 | + |
| 43 | + assertEquals(3, task.inputs.files.files.size) |
| 44 | + task.autolinkLockFiles.get().files.forEach { |
| 45 | + assertTrue( |
| 46 | + it.name == "depedencies.json" || |
| 47 | + it.name == "packager.lock" || |
| 48 | + it.name == "another-packager.lock") |
| 49 | + } |
| 50 | + |
| 51 | + assertTrue(task.inputs.properties.containsKey("autolinkConfigCommand")) |
| 52 | + assertEquals(1, task.outputs.files.files.size) |
| 53 | + assertEquals(File(tempFolder.root, "output.json"), task.outputs.files.singleFile) |
| 54 | + assertEquals(listOf("rm", "-rf", "/"), task.autolinkConfigCommand.get()) |
| 55 | + |
| 56 | + assertEquals(2, task.autolinkLockFiles.get().files.size) |
| 57 | + task.autolinkLockFiles.get().files.forEach { |
| 58 | + assertTrue(it.name == "packager.lock" || it.name == "another-packager.lock") |
| 59 | + } |
| 60 | + |
| 61 | + assertEquals(File(tempFolder.root, "dependencies.json"), task.autolinkConfigFile.get().asFile) |
| 62 | + assertEquals(File(tempFolder.root, "output.json"), task.autolinkOutputFile.get().asFile) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun wipeOutputDir_worksCorrectly() { |
| 67 | + val outputDir = |
| 68 | + tempFolder.newFolder("output").apply { |
| 69 | + File(this, "output.json").createNewFile() |
| 70 | + File(this, "NothingToSeeHere.java").createNewFile() |
| 71 | + } |
| 72 | + |
| 73 | + val task = createTestTask<RunAutolinkingConfigTask> { it.autolinkOutputFile.set(outputDir) } |
| 74 | + task.wipeOutputDir() |
| 75 | + |
| 76 | + assertFalse(outputDir.exists()) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun setupConfigCommandLine_worksCorrectly() { |
| 81 | + val project = createProject() |
| 82 | + |
| 83 | + val task = |
| 84 | + createTestTask<RunAutolinkingConfigTask>(project) { |
| 85 | + it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) |
| 86 | + it.autolinkOutputFile.set(tempFolder.newFile("output.json")) |
| 87 | + } |
| 88 | + task.setupConfigCommandLine() |
| 89 | + |
| 90 | + assertEquals(project.projectDir, task.workingDir) |
| 91 | + assertTrue(task.standardOutput is FileOutputStream) |
| 92 | + assertEquals(listOf("rm", "-rf", "/"), task.commandLine) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun setupConfigCopyCommandLine_worksCorrectly() { |
| 97 | + val project = createProject() |
| 98 | + |
| 99 | + val task = |
| 100 | + createTestTask<RunAutolinkingConfigTask>(project) { |
| 101 | + it.autolinkConfigFile.set(tempFolder.newFile("dependencies.json")) |
| 102 | + it.autolinkOutputFile.set(tempFolder.newFile("output.json")) |
| 103 | + } |
| 104 | + task.setupConfigCopyCommandLine() |
| 105 | + |
| 106 | + assertEquals(project.projectDir, task.workingDir) |
| 107 | + assertTrue(task.standardOutput !is FileOutputStream) |
| 108 | + assertEquals("cp", task.commandLine[0]) |
| 109 | + assertEquals(File(tempFolder.root, "dependencies.json").absolutePath, task.commandLine[1]) |
| 110 | + assertEquals(File(tempFolder.root, "output.json").absolutePath, task.commandLine[2]) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun setupCommandLine_withoutAutolinkConfigFileConfigured_invokesCommand() { |
| 115 | + val project = createProject() |
| 116 | + |
| 117 | + val task = |
| 118 | + createTestTask<RunAutolinkingConfigTask>(project) { |
| 119 | + it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) |
| 120 | + it.autolinkOutputFile.set(tempFolder.newFile("output.json")) |
| 121 | + } |
| 122 | + task.setupCommandLine() |
| 123 | + |
| 124 | + assertEquals(listOf("rm", "-rf", "/"), task.commandLine) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun setupCommandLine_withoutMissingConfigFile_invokesCommand() { |
| 129 | + val project = createProject() |
| 130 | + |
| 131 | + val task = |
| 132 | + createTestTask<RunAutolinkingConfigTask>(project) { |
| 133 | + it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) |
| 134 | + it.autolinkConfigFile.set(File(tempFolder.root, "dependencies.json")) |
| 135 | + it.autolinkOutputFile.set(tempFolder.newFile("output.json")) |
| 136 | + } |
| 137 | + task.setupCommandLine() |
| 138 | + |
| 139 | + assertEquals(listOf("rm", "-rf", "/"), task.commandLine) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun setupCommandLine_withoutExistingConfigFile_invokesCp() { |
| 144 | + val project = createProject() |
| 145 | + val configFile = tempFolder.newFile("dependencies.json").apply { writeText("¯\\_(ツ)_/¯") } |
| 146 | + |
| 147 | + val task = |
| 148 | + createTestTask<RunAutolinkingConfigTask>(project) { |
| 149 | + it.autolinkConfigCommand.set(listOf("rm", "-rf", "/")) |
| 150 | + it.autolinkConfigFile.set(configFile) |
| 151 | + it.autolinkOutputFile.set(tempFolder.newFile("output.json")) |
| 152 | + } |
| 153 | + task.setupCommandLine() |
| 154 | + |
| 155 | + assertEquals( |
| 156 | + listOf("cp", configFile.absolutePath, File(tempFolder.root, "output.json").absolutePath), |
| 157 | + task.commandLine) |
| 158 | + } |
| 159 | +} |
0 commit comments