Skip to content

Commit

Permalink
fix: fix tasks skipping conditions and dependencies (#208)
Browse files Browse the repository at this point in the history
Fixes #206
  • Loading branch information
v1nc3n4 authored Aug 23, 2023
1 parent aebc675 commit b46b870
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ tasks.named<Wrapper>("wrapper") {

tasks.withType<Test> {
useJUnitPlatform()
outputs.upToDateWhen { false }
jvmArgs(
"--add-opens", "java.base/java.lang=ALL-UNNAMED",
"--add-opens", "java.base/java.util=ALL-UNNAMED"
)
outputs.upToDateWhen { false }
}

tasks.register<Test>("integrationTest") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,8 @@ void should_skip_task_when_package_json_file_is_not_a_file() throws IOException
}

@Test
void should_fail_when_corepack_executable_is_not_a_file() throws IOException {
// The fact that the corepack executable is not present is enough to simulate the following use cases:
// - The Node.js distribution is already provided, but the install directory was not set accordingly.
// - The Node.js distribution is already provided, but the install directory contains a non-supported release.
// - The Node.js distribution was downloaded but is a non-supported release.
// - The Node.js distribution was downloaded but was corrupted later so as the corepack executable is not
// present anymore.
void should_fail_when_node_install_directory_is_not_a_directory() throws IOException {
Files.copy(getResourcePath("package-any-manager.json"), projectDirectoryPath.resolve("package.json"));
Files.createDirectory(projectDirectoryPath.resolve(FrontendGradlePlugin.DEFAULT_NODE_INSTALL_DIRECTORY_NAME));
createBuildFile(projectDirectoryPath, new FrontendMapBuilder().nodeDistributionProvided(true).toMap());

final BuildResult result = runGradleAndExpectFailure(projectDirectoryPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,16 @@ protected void configureInstallPackageManagerTask(final InstallPackageManagerTas
task.setGroup(TASK_GROUP);
task.setDescription("Installs the package manager.");
task.getPackageJsonDirectory().set(frontendExtension.getPackageJsonDirectory().getAsFile());
task.getNodeInstallDirectory().set(frontendExtension.getNodeInstallDirectory().getAsFile());
task.getNodeInstallDirectory().set(frontendExtension.getNodeInstallDirectory().map(nodeInstallDirectory -> {
final Path nodeInstallDirectoryPath = nodeInstallDirectory.getAsFile().toPath();
if (!Files.isDirectory(nodeInstallDirectoryPath)) {
// Performing this verification ahead of time avoids automatic creation of any parent directory by
// Gradle if the task is not skipped. This may be the case if the Node.js distribution is provided, but
// the install directory is not correctly configured and points to nowhere.
throw new GradleException("Node.js install directory not found: " + nodeInstallDirectoryPath);
}
return nodeInstallDirectory.getAsFile();
}));
final Provider<ResolvePackageManagerTask> resolvePackageManagerTaskProvider = taskContainer.named(
RESOLVE_PACKAGE_MANAGER_TASK_NAME, ResolvePackageManagerTask.class);
task
Expand All @@ -399,27 +408,26 @@ protected void configureInstallPackageManagerTask(final InstallPackageManagerTas
ResolvePackageManagerTask::getPackageManagerSpecificationFile));
task
.getPackageManagerExecutableFile()
.set(resolvePackageManagerTaskProvider
.fileProvider(resolvePackageManagerTaskProvider
.flatMap(ResolvePackageManagerTask::getPackageManagerExecutablePathFile)
.map(f -> {
final Path filePath = f.getAsFile().toPath();
if (!Files.exists(filePath)) {
// Setting the output property to null avoids automatic creation of any parent directories by
// Setting the output property to null avoids automatic creation of any parent directory by
// Gradle if the task is not skipped. If it is skipped, the file system would not have been
// touched by Gradle.
return null;
}
try {
return getBeanOrFail(beanRegistryId, FileManager.class).readString(filePath,
StandardCharsets.UTF_8);
return Paths
.get(getBeanOrFail(beanRegistryId, FileManager.class).readString(filePath,
StandardCharsets.UTF_8))
.toFile();
} catch (final IOException e) {
throw new GradleException(
"Cannot read path to package manager executable from file: " + filePath, e);
}
})
.map(packageManagerExecutablePathFilePath -> () -> Paths
.get(packageManagerExecutablePathFilePath)
.toFile()));
}));
task.setOnlyIf(t -> isTaskExecuted(taskContainer, RESOLVE_PACKAGE_MANAGER_TASK_NAME));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public class ResolvePackageManagerTask extends DefaultTask {
private final Property<File> nodeInstallDirectory;

/**
* File that will contain information about the package manager.
* File that will contain the specification of the package manager for the project.
*/
private final RegularFileProperty packageManagerSpecificationFile;

/**
* File that will contain information about the package manager.
* File that will contain the path to the package manager executable file.
*/
private final RegularFileProperty packageManagerExecutablePathFile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -43,7 +45,8 @@ void setUp() {
}

@Test
void should_register_tasks_with_default_extension_values_applied() {
void should_register_tasks_with_default_extension_values_applied() throws IOException {
Files.createDirectory(project.file(FrontendGradlePlugin.DEFAULT_NODE_INSTALL_DIRECTORY_NAME).toPath());
plugin.apply(project);

final FrontendExtension extension = Objects.requireNonNull(
Expand Down Expand Up @@ -106,7 +109,9 @@ void should_register_tasks_with_default_extension_values_applied() {
}

@Test
void should_register_tasks_with_custom_extension_values_applied() {
void should_register_tasks_with_custom_extension_values_applied() throws IOException {
final String nodeInstallDirectoryName = "node-dist";
Files.createDirectory(project.file(nodeInstallDirectoryName).toPath());
plugin.apply(project);

final FrontendExtension extension = Objects.requireNonNull(
Expand All @@ -115,7 +120,7 @@ void should_register_tasks_with_custom_extension_values_applied() {
extension.getNodeVersion().set("3.65.4");
extension.getNodeDistributionUrlRoot().set("https://node");
extension.getNodeDistributionUrlPathPattern().set("/node.tar.gz");
extension.getNodeInstallDirectory().set(project.file("node-dist"));
extension.getNodeInstallDirectory().set(project.file(nodeInstallDirectoryName));
extension.getInstallScript().set("run ci");
extension.getCleanScript().set("run clean");
extension.getAssembleScript().set("run build");
Expand Down

0 comments on commit b46b870

Please sign in to comment.