Skip to content

Commit

Permalink
fix: fix tasks skipping conditions and dependencies
Browse files Browse the repository at this point in the history
Fixes #206
  • Loading branch information
v1nc3n4 committed Aug 23, 2023
1 parent aebc675 commit e71c8c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
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,17 @@ 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 is not a valid directory: " + nodeInstallDirectoryPath);
}
return nodeInstallDirectory.getAsFile();
}));
final Provider<ResolvePackageManagerTask> resolvePackageManagerTaskProvider = taskContainer.named(
RESOLVE_PACKAGE_MANAGER_TASK_NAME, ResolvePackageManagerTask.class);
task
Expand All @@ -399,27 +409,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

0 comments on commit e71c8c0

Please sign in to comment.