Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional error handling #4

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/org/openrewrite/dotnet/UpgradeAssistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ public List<String> buildUpgradeAssistantCommand(Accumulator acc, ExecutionConte

@Override
public void runUpgradeAssistant(Accumulator acc, ExecutionContext ctx) {
for (Path projectFile : acc.getProjectFiles()) {
List<Path> projectFiles = acc.getProjectFiles();
if (projectFiles.isEmpty()) {
throw new IllegalStateException("No project files found in repository");
}
for (Path projectFile : projectFiles) {
execUpgradeAssistant(projectFile, acc, ctx);

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.RecipeException;
import org.openrewrite.SourceFile;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
Expand Down Expand Up @@ -122,7 +123,13 @@ protected void processOutput(Path solutionFile, Path output, Accumulator acc) {
}
}
} catch (IOException e) {
throw new RecipeException(e);
try {
throw new UncheckedIOException(
e.getMessage() + "\nupgrade-assistant:\n" + new String(Files.readAllBytes(output)),
e);
} catch (IOException ignored) {
throw new UncheckedIOException(e);
}
}
}

Expand Down
40 changes: 21 additions & 19 deletions src/main/java/org/openrewrite/dotnet/UpgradeAssistantRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openrewrite.text.PlainText;
import org.openrewrite.tree.ParseError;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -95,7 +96,6 @@ public Collection<? extends SourceFile> generate(Accumulator acc, ExecutionConte
protected void execUpgradeAssistant(Path inputFile, Accumulator acc, ExecutionContext ctx) {
List<String> command = buildUpgradeAssistantCommand(acc, ctx, inputFile);
Path out = null;
Path err = null;

try {
ProcessBuilder builder = new ProcessBuilder();
Expand All @@ -108,37 +108,39 @@ protected void execUpgradeAssistant(Path inputFile, Accumulator acc, ExecutionCo
WorkingDirectoryExecutionContextView.view(ctx).getWorkingDirectory(),
UPGRADE_ASSISTANT,
null);
err = Files.createTempFile(
WorkingDirectoryExecutionContextView.view(ctx).getWorkingDirectory(),
UPGRADE_ASSISTANT,
null);

builder.redirectOutput(ProcessBuilder.Redirect.to(out.toFile()));
builder.redirectError(ProcessBuilder.Redirect.to(err.toFile()));
builder.redirectError(ProcessBuilder.Redirect.to(out.toFile()));

Process process = builder.start();
process.waitFor(20, TimeUnit.MINUTES);
if (process.exitValue() != 0) {
String error = "Command failed: " + String.join(" ", command);
if (Files.exists(err)) {
error += "\n" + new String(Files.readAllBytes(err));
}
throw new RuntimeException(error);
} else {
for (Map.Entry<Path, Long> entry : acc.beforeModificationTimestamps.entrySet()) {
Path path = entry.getKey();
if (!Files.exists(path) || Files.getLastModifiedTime(path).toMillis() > entry.getValue()) {
acc.addModifiedFile(path);

// upgrade-assistent currently does not exit with non-zero on error nor does it
// log errors to stderr. Here we look for known fatal errors in stdout suggesting
// the command failed.
if (Files.exists(out)) {
try (BufferedReader reader = Files.newBufferedReader(out)) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.startsWith("Project path does not exist")) {
throw new RuntimeException("upgrade-assistant: " + line);
}
}
}
processOutput(inputFile, out, acc);
}

for (Map.Entry<Path, Long> entry : acc.beforeModificationTimestamps.entrySet()) {
Path path = entry.getKey();
if (!Files.exists(path) || Files.getLastModifiedTime(path).toMillis() > entry.getValue()) {
acc.addModifiedFile(path);
}
}
processOutput(inputFile, out, acc);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
deleteFile(out);
deleteFile(err);
}
}

Expand Down
42 changes: 26 additions & 16 deletions src/test/java/org/openrewrite/dotnet/UpgradeAssistantTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,31 @@ void upgradeDotNetMultipleProject(String currentVersion, String upgradedVersion)
@Test
void upgradeDotNetWithInvalidVersion() {
assertThatThrownBy(() ->
rewriteRun(
spec -> spec.recipe(new UpgradeAssistant("foo-bar")),
text(
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>
</Project>
""",
spec -> spec.path("src/Proj.csproj")
)
))
.cause()
.isInstanceOf(RecipeException.class)
.hasMessageContaining("Unknown target framework");
rewriteRun(
spec -> spec.recipe(new UpgradeAssistant("foo-bar")),
text(
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>
</Project>
""",
spec -> spec.path("src/Proj.csproj")
)
))
.cause()
.isInstanceOf(RecipeException.class)
.hasMessageContaining("Unknown target framework");
}

@Test
void upgradeDotNetWithNoProjectFiles() {
assertThatThrownBy(() ->
rewriteRun(
spec -> spec.recipe(new UpgradeAssistant("net9.0"))
))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("No project files found in repository");
}
}