Skip to content

Commit f33cd26

Browse files
authored
Use CsvDataTableStore for data table export (#1125)
* Use CsvDataTableStore for data table export Replace recipeRun.exportDatatablesToCsv() with CsvDataTableStore set on the ExecutionContext before recipe execution. Depends on: openrewrite/rewrite#7088
1 parent 1c13c14 commit f33cd26

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/main/java/org/openrewrite/maven/AbstractRewriteBaseRunMojo.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,20 @@ protected LargeSourceSet loadSourceSet(Path repositoryRoot, Environment env, Exe
244244

245245
protected List<Result> runRecipe(Recipe recipe, LargeSourceSet sourceSet, ExecutionContext ctx) {
246246
getLog().info("Running recipe(s)...");
247-
RecipeRun recipeRun = recipe.run(sourceSet, ctx);
248247

248+
CsvDataTableStore csvDataTableStore = null;
249249
if (exportDatatables) {
250250
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss-SSS"));
251251
Path datatableDirectoryPath = Paths.get("target", "rewrite", "datatables", timestamp);
252252
getLog().info(String.format("Printing available datatables to: %s", datatableDirectoryPath));
253-
recipeRun.exportDatatablesToCsv(datatableDirectoryPath, ctx);
253+
csvDataTableStore = new CsvDataTableStore(datatableDirectoryPath);
254+
DataTableExecutionContextView.view(ctx).setDataTableStore(csvDataTableStore);
255+
}
256+
257+
RecipeRun recipeRun = recipe.run(sourceSet, ctx);
258+
259+
if (csvDataTableStore != null) {
260+
csvDataTableStore.close();
254261
}
255262

256263
return recipeRun.getChangeset().getAllResults().stream().filter(source -> {

src/test/java/org/openrewrite/maven/RewriteRunIT.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,30 @@ void datatable_export(MavenExecutionResult result) throws Exception {
223223
.findFirst()
224224
.orElseThrow(() -> new AssertionError("No timestamped directory found in " + datatablesDir));
225225

226-
csvFile = timestampedDir.resolve("org.openrewrite.maven.table.DependenciesInUse.csv");
226+
try (Stream<Path> csvFiles = Files.list(timestampedDir)) {
227+
csvFile = csvFiles
228+
.filter(p -> p.getFileName().toString().startsWith("org.openrewrite.maven.table.DependenciesInUse") &&
229+
p.getFileName().toString().endsWith(".csv"))
230+
.findFirst()
231+
.orElseThrow(() -> new AssertionError("No DependenciesInUse CSV file found in " + timestampedDir));
232+
}
227233
assertThat(csvFile).exists().isRegularFile();
228234
}
229235

230236
// Verify CSV contains expected structure and data rows
231-
// CSV format: header row, description row, data rows
237+
// CSV format: 3 comment lines (@name, @instanceName, @group), header row, data rows
232238
List<String> lines = Files.readAllLines(csvFile);
233-
assertThat(lines)
234-
.hasSizeGreaterThanOrEqualTo(4) // header + description + 2 data rows
239+
// Filter out comment lines
240+
List<String> dataLines = lines.stream()
241+
.filter(l -> !l.startsWith("#"))
242+
.collect(java.util.stream.Collectors.toList());
243+
assertThat(dataLines)
244+
.hasSizeGreaterThanOrEqualTo(3) // header + 2 data rows
235245
.first(as(STRING))
236246
.contains("Group", "Artifact"); // CSV header
237247

238-
// Get only the data rows (skip header and description rows)
239-
assertThat(lines.subList(2, lines.size()))
248+
// Get only the data rows (skip header)
249+
assertThat(dataLines.subList(1, dataLines.size()))
240250
.hasSizeGreaterThanOrEqualTo(2)
241251
.anySatisfy(line -> assertThat(line).contains("com.google.guava", "guava"))
242252
.anySatisfy(line -> assertThat(line).contains("org.projectlombok", "lombok"));

0 commit comments

Comments
 (0)