From 2c11b28c253152dbb2dd1ae9479355f6bb15d711 Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Tue, 26 Sep 2023 03:15:38 -0700 Subject: [PATCH] Polish and directory test --- .../openrewrite/FindDuplicateSourceFiles.java | 2 +- .../org/openrewrite/LanguageComposition.java | 4 +- .../table/LanguageCompositionPerFile.java | 6 +-- .../openrewrite/LanguageCompositionTest.java | 46 +++++++++++++++++-- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/openrewrite/FindDuplicateSourceFiles.java b/src/main/java/org/openrewrite/FindDuplicateSourceFiles.java index 11ebd9f..a293799 100644 --- a/src/main/java/org/openrewrite/FindDuplicateSourceFiles.java +++ b/src/main/java/org/openrewrite/FindDuplicateSourceFiles.java @@ -31,7 +31,7 @@ public String getDisplayName() { @Override public String getDescription() { - return "Record the presence of source files that occur more than once in an LST."; + return "Record the presence of LSTs with duplicate paths, indicating that the same file was parsed more than once."; } @Override diff --git a/src/main/java/org/openrewrite/LanguageComposition.java b/src/main/java/org/openrewrite/LanguageComposition.java index edcaa22..20f352b 100644 --- a/src/main/java/org/openrewrite/LanguageComposition.java +++ b/src/main/java/org/openrewrite/LanguageComposition.java @@ -63,7 +63,7 @@ public String getDescription() { } @Data - static class Accumulator { + public static class Accumulator { Map> folderToLanguageToCounts = new HashMap<>(); } @@ -73,7 +73,7 @@ private static String containingFolderPath(SourceFile s) { if(lastSlash == -1) { return ""; } - return sourcePath.substring(0, lastSlash); + return s.getSourcePath().toString().substring(0, lastSlash); } @Override diff --git a/src/main/java/org/openrewrite/table/LanguageCompositionPerFile.java b/src/main/java/org/openrewrite/table/LanguageCompositionPerFile.java index 01c4970..03d6dd7 100644 --- a/src/main/java/org/openrewrite/table/LanguageCompositionPerFile.java +++ b/src/main/java/org/openrewrite/table/LanguageCompositionPerFile.java @@ -37,9 +37,9 @@ public static class Row { description = "The language of the source file.") String language; - @Column(displayName = "Class", - description = "The OpenRewrite class that was parsed for this source file.") - String parserClass; + @Column(displayName = "LST type", + description = "The Lossless Semantic Tree type of this source file.") + String sourceFileType; @Column(displayName = "Lines of text", description = "The number of lines of text in the source file. " + diff --git a/src/test/java/org/openrewrite/LanguageCompositionTest.java b/src/test/java/org/openrewrite/LanguageCompositionTest.java index 2cc6a33..3099759 100644 --- a/src/test/java/org/openrewrite/LanguageCompositionTest.java +++ b/src/test/java/org/openrewrite/LanguageCompositionTest.java @@ -21,8 +21,11 @@ import org.openrewrite.table.LanguageCompositionPerRepository; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.SourceSpecs; +import org.openrewrite.text.PlainText; import static org.assertj.core.api.Assertions.assertThat; +import static org.openrewrite.PathUtils.separatorsToSystem; import static org.openrewrite.java.Assertions.java; import static org.openrewrite.test.SourceSpecs.dir; import static org.openrewrite.test.SourceSpecs.text; @@ -31,9 +34,7 @@ public class LanguageCompositionTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new LanguageComposition()) - .expectedCyclesThatMakeChanges(0) - .cycles(1); + spec.recipe(new LanguageComposition()); } @Test @@ -73,7 +74,7 @@ void javaAndPlainText() { spec.dataTable(LanguageCompositionPerFolder.Row.class, table -> { assertThat(table).hasSize(2); assertThat(table).contains( - new LanguageCompositionPerFolder.Row("src/java/main/com/whatever", "Java", 1, 3), + new LanguageCompositionPerFolder.Row(separatorsToSystem("src/java/main/com/whatever"), "Java", 1, 3), new LanguageCompositionPerFolder.Row("", "Plain text", 2, 4) ); }); @@ -131,4 +132,41 @@ void foo() { ) ); } + + @Test + void folderCounts() { + rewriteRun( + spec -> { + spec.dataTable(LanguageCompositionPerFile.Row.class, table -> { + assertThat(table).hasSize(3); + assertThat(table).containsOnlyOnce( + new LanguageCompositionPerFile.Row(separatorsToSystem("src/main/file.txt"), "Plain text", PlainText.class.getName(), 2, false), + new LanguageCompositionPerFile.Row(separatorsToSystem("src/resources/file.txt"), "Plain text", PlainText.class.getName(), 3, false), + new LanguageCompositionPerFile.Row(separatorsToSystem("file.txt"), "Plain text", PlainText.class.getName(), 4, false) + ); + }); + spec.dataTable(LanguageCompositionPerFolder.Row.class, table -> { + assertThat(table).hasSize(3); + assertThat(table).containsOnlyOnce( + new LanguageCompositionPerFolder.Row(separatorsToSystem("src/main"), "Plain text", 1, 2), + new LanguageCompositionPerFolder.Row(separatorsToSystem("src/resources"), "Plain text", 1, 3), + new LanguageCompositionPerFolder.Row(separatorsToSystem(""), "Plain text", 1, 4) + ); + }); + }, + dir("src", + dir("main", textFileWithLineCount(2)), + dir("resources", textFileWithLineCount(3)) + ), + textFileWithLineCount(4) + ); + } + + SourceSpecs textFileWithLineCount(int lineCount) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < lineCount; i++) { + sb.append("line ").append(i).append("\n"); + } + return text(sb.toString()); + } }