diff --git a/.github/workflows/csharp.yml b/.github/workflows/csharp.yml new file mode 100644 index 00000000..a24d6d11 --- /dev/null +++ b/.github/workflows/csharp.yml @@ -0,0 +1,62 @@ +# Optional: exercises the C# fact extractor and the tests that need it. +# The main Maven workflows need no .NET; the tests they cannot run are run here. + +name: C# analysis + +on: + push: + branches: [ main ] + paths: + - 'csharp-fact-extractor/**' + - 'codebase-graph-builder/**' + - 'change-proneness-ranker/**' + - 'report/**' + - '.github/workflows/csharp.yml' + pull_request: + branches: [ main ] + paths: + - 'csharp-fact-extractor/**' + - 'codebase-graph-builder/**' + - 'change-proneness-ranker/**' + - 'report/**' + - '.github/workflows/csharp.yml' + +permissions: + contents: read + +jobs: + csharp: + runs-on: ubuntu-latest + + steps: + - name: Check out Git repository + uses: actions/checkout@v4 + + - name: Set up JDK 25 + uses: actions/setup-java@v4 + with: + java-version: 25 + distribution: 'zulu' + + - name: Set up .NET 10 + uses: actions/setup-dotnet@v4 + with: + dotnet-version: '10.0.x' + + - name: Restore the fixtures the tests analyse + run: | + dotnet restore csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.sln + dotnet restore csharp-fact-extractor/fixtures/TwoProjects/Alpha/Alpha.csproj + dotnet restore csharp-fact-extractor/fixtures/Shapes/Shapes.csproj + dotnet restore csharp-fact-extractor/fixtures/Members/Members.csproj + + - name: Test the extractor + run: dotnet test csharp-fact-extractor/tests/CSharpFactExtractor.Tests.csproj + + - name: Publish the extractor + run: dotnet publish csharp-fact-extractor/src -c Release -o csharp-fact-extractor/publish + + - name: Run the Java tests that need the extractor + env: + REFACTORFIRST_CSHARP_EXTRACTOR: ${{ github.workspace }}/csharp-fact-extractor/publish/CSharpFactExtractor.dll + run: mvn -B package -Dtest='CSharp*,CompositeGraphBuilderCSharp*,GitLogReaderCSharp*' -Dsurefire.failIfNoSpecifiedTests=false diff --git a/README.md b/README.md index 4842ef56..0d719365 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,52 @@ A RefactorFirst report will show up in the site report when you run ```mvn site` ``` +## Analysing C# code (experimental) +RefactorFirst can also analyse C#. A small .NET program, `csharp-fact-extractor`, reads the code with Roslyn and hands the facts to RefactorFirst, which applies the same metrics and thresholds it uses for Java and Kotlin. + +C# analysis is **off unless you configure it**. The normal build and a normal run need no .NET. + +### Security: only analyse C# code you trust +To read a solution, the extractor asks MSBuild to load its project files. Loading a project file can run build logic that the project (or a package it imports) defines. **Treat a C# run like building the code: only point it at repositories you trust.** If you set the extractor path once in your shell profile, it applies to every folder you analyse afterwards that contains a `.sln`, `.slnx` or `.csproj`. + +### Setup +1. Install the .NET 10 SDK. +2. Build the extractor once, from the root of this repository: + ``` + dotnet publish csharp-fact-extractor/src -c Release -o csharp-fact-extractor/publish + ``` +3. Run `dotnet restore` on the solution you want to analyse. Without restored packages many types do not resolve and results are incomplete. +4. Tell RefactorFirst where the extractor is (settings below). +5. Run the usual command from the root of the C# repository. No `pom.xml` is needed there, only Maven and Java 17 or newer: + ```bash + mvn org.hjug.refactorfirst.plugin:refactor-first-maven-plugin:0.11.0:htmlReport + ``` + Use a RefactorFirst version that includes C# support. To try it before that version is released, run `mvn install -DskipTests` in this repository first and use the version it prints (for example `0.11.0-SNAPSHOT`). + + In a folder with no `pom.xml` the report is currently written to a folder literally named `${project.basedir}/target/site`. The last line of the run prints the full path. + +|Setting|Environment variable|System property| +|------|------|------| +|Path to `CSharpFactExtractor.dll` (required)|```REFACTORFIRST_CSHARP_EXTRACTOR```|```-Drefactorfirst.csharp.extractor```| +|Which `dotnet` to launch, if the first one on the PATH has no .NET 10 runtime (optional)|```REFACTORFIRST_DOTNET```|```-Drefactorfirst.dotnet```| + +### What gets analysed +* The `.sln` or `.slnx` in the folder being analysed. If there are several, the first by name is used and a warning names the others. +* If there is no solution there, every `.csproj` found up to six folders deep, outside `bin`, `obj`, `node_modules` and `.git`. +* Test projects are left out when ```excludeTests``` is true (the default). A project counts as a test project if its name ends in `Test`, `Tests`, `Spec` or `Specs`, or it references xUnit, NUnit, MSTest or the .NET test platform. +* Generated code is left out: anything under `obj` or `bin`, and files ending `.g.cs`, `.g.i.cs`, `.Designer.cs` or `.generated.cs`. +* If C# analysis fails for any reason, a warning is logged and the Java and Kotlin results are still reported. + +### Known gaps +* The CSV and JSON report types carry no C# results. +* No coupling (CBO) table and no Significant Duplication detection for C#. +* Types outside any namespace are left out. +* `.cs` files count toward change history whenever a repository contains them, whether or not C# analysis is configured. +* The same full type name declared in two projects is analysed once; the run prints a warning. +* A run stops after 30 minutes. + +The JSON the extractor writes is described in [csharp-fact-extractor/docs/facts-contract.md](csharp-fact-extractor/docs/facts-contract.md). + ## Configuration Options Care has been taken to use sensible defaults, though if you wish to override these defaults you can specify the following parameters. Specify with -D if running on the command line. e.g. ```-DbackEdgeAnalysisCount=0 `DanalyzeCycles=false``` or in the configuration section (as in the above examples) if including in a Maven build. diff --git a/change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java b/change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java index 465fb835..e75fb639 100644 --- a/change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java +++ b/change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java @@ -24,6 +24,12 @@ public class GitLogReader implements AutoCloseable { static final String JAVA_FILE_TYPE = ".java"; static final String KOTLIN_FILE_TYPE = ".kt"; + static final String CSHARP_FILE_TYPE = ".cs"; + + /** True for the source files whose change history feeds change proneness. */ + static boolean isAnalyzedSourceFile(String path) { + return path.endsWith(JAVA_FILE_TYPE) || path.endsWith(KOTLIN_FILE_TYPE) || path.endsWith(CSHARP_FILE_TYPE); + } private Repository gitRepository; @@ -222,10 +228,7 @@ public TreeMap captureChangeCountByCommitTimestamp() throws IO try { int count = 0; for (DiffEntry entry : getDiffEntries(newer, older)) { - if (entry.getNewPath().endsWith(JAVA_FILE_TYPE) - || entry.getOldPath().endsWith(JAVA_FILE_TYPE) - || entry.getNewPath().endsWith(KOTLIN_FILE_TYPE) - || entry.getOldPath().endsWith(KOTLIN_FILE_TYPE)) { + if (isAnalyzedSourceFile(entry.getNewPath()) || isAnalyzedSourceFile(entry.getOldPath())) { count++; } } @@ -264,8 +267,7 @@ Map walkFirstCommit(RevCommit firstCommit) throws IOException if (treeWalk.isSubtree()) { treeWalk.enterSubtree(); } else { - if (treeWalk.getPathString().endsWith(JAVA_FILE_TYPE) - || treeWalk.getPathString().endsWith(KOTLIN_FILE_TYPE)) { + if (isAnalyzedSourceFile(treeWalk.getPathString())) { firstCommitCount++; } } diff --git a/change-proneness-ranker/src/test/java/org/hjug/git/GitLogReaderCSharpTest.java b/change-proneness-ranker/src/test/java/org/hjug/git/GitLogReaderCSharpTest.java new file mode 100644 index 00000000..8efa45b7 --- /dev/null +++ b/change-proneness-ranker/src/test/java/org/hjug/git/GitLogReaderCSharpTest.java @@ -0,0 +1,90 @@ +package org.hjug.git; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Map; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.revwalk.RevCommit; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class GitLogReaderCSharpTest { + + @TempDir + File tempFolder; + + private Git git; + + @BeforeEach + void setUp() throws Exception { + git = Git.init().setDirectory(tempFolder).call(); + } + + @AfterEach + void tearDown() { + git.getRepository().close(); + } + + @Test + void givenSourcePaths_whenClassified_thenCSharpCountsAlongsideJavaAndKotlin() { + assertTrue(GitLogReader.isAnalyzedSourceFile("src/Greeter.cs")); + assertTrue(GitLogReader.isAnalyzedSourceFile("src/Main.java")); + assertTrue(GitLogReader.isAnalyzedSourceFile("src/Main.kt")); + assertFalse(GitLogReader.isAnalyzedSourceFile("src/App.csproj")); + assertFalse(GitLogReader.isAnalyzedSourceFile("src/App.sln")); + assertFalse(GitLogReader.isAnalyzedSourceFile("README.md")); + } + + @Test + void givenCommitsTouchingCsFiles_whenCounted_thenTheyAreIncludedAndOtherFilesAreNot() throws Exception { + GitLogReader gitLogReader = new GitLogReader(git); + + write("Greeter.cs", "class Greeter { }"); + write("README.md", "hello"); + git.add().addFilepattern(".").call(); + RevCommit first = git.commit().setMessage("first").call(); + + // Commits need different timestamps; they are keyed by commit time in seconds. + Thread.sleep(1000); + + write("Greeter.cs", "class Greeter { int x; }"); + write("MessageStore.cs", "class MessageStore { }"); + write("README.md", "hello again"); + git.add().addFilepattern(".").call(); + RevCommit second = git.commit().setMessage("second").call(); + + Map counts = gitLogReader.captureChangeCountByCommitTimestamp(); + + assertEquals(1, counts.get(first.getCommitTime()).intValue(), "Greeter.cs only; README.md is not source"); + assertEquals(2, counts.get(second.getCommitTime()).intValue(), "Greeter.cs changed, MessageStore.cs added"); + } + + @Test + void givenACsFileCommittedTwice_whenItsLogIsRead_thenBothCommitsAreCounted() throws Exception { + GitLogReader gitLogReader = new GitLogReader(git); + + write("Greeter.cs", "class Greeter { }"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("first").call(); + Thread.sleep(1000); + write("Greeter.cs", "class Greeter { int x; }"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("second").call(); + + ScmLogInfo log = gitLogReader.fileLog("Greeter.cs"); + + assertEquals(2, log.getCommitCount()); + } + + private void write(String name, String content) throws IOException { + Files.write(new File(git.getRepository().getWorkTree(), name).toPath(), content.getBytes(UTF_8)); + } +} diff --git a/codebase-graph-builder/pom.xml b/codebase-graph-builder/pom.xml index 3ceafd95..bc8c8eb2 100644 --- a/codebase-graph-builder/pom.xml +++ b/codebase-graph-builder/pom.xml @@ -34,6 +34,11 @@ org.jgrapht jgrapht-core + + + com.fasterxml.jackson.core + jackson-databind + io.micrometer micrometer-core diff --git a/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/CompositeGraphBuilder.java b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/CompositeGraphBuilder.java index 01e7a30d..649f91ce 100644 --- a/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/CompositeGraphBuilder.java +++ b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/CompositeGraphBuilder.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.util.*; import lombok.extern.slf4j.Slf4j; +import org.hjug.graphbuilder.graphbuilder.CSharpSourceFileGraphBuilder; import org.hjug.graphbuilder.graphbuilder.JavaSourceFileGraphBuilder; import org.hjug.graphbuilder.graphbuilder.KotlinSourceFileGraphBuilder; import org.hjug.graphbuilder.metrics.DisharmonyDetector.ClassDisharmony; @@ -25,6 +26,17 @@ @Slf4j public class CompositeGraphBuilder { + private final CSharpSourceFileGraphBuilder csharpBuilder; + + public CompositeGraphBuilder() { + this(new CSharpSourceFileGraphBuilder()); + } + + /** For tests: lets the C# reader be replaced so no .NET process is needed. */ + CompositeGraphBuilder(CSharpSourceFileGraphBuilder csharpBuilder) { + this.csharpBuilder = csharpBuilder; + } + /** * Builds a unified graph for Java and Kotlin source files in a repository. * @@ -85,14 +97,29 @@ public CodebaseGraphDTO getCodebaseGraphDTO(String repositoryPath, String reposi // compile dependency of this module, so the Kotlin parser is always // on the classpath; a failure here (parse error, IO, etc.) falls back // to the Java-only graph. + CodebaseGraphDTO merged = javaDto; try { KotlinSourceFileGraphBuilder kotlinBuilder = new KotlinSourceFileGraphBuilder(); CodebaseGraphDTO kotlinDto = kotlinBuilder.buildGraph(repositoryPath, repositoryRoot, config); - return merge(javaDto, kotlinDto); + merged = merge(javaDto, kotlinDto); } catch (Exception e) { log.warn("Kotlin analysis failed; falling back to Java-only graph", e); - return javaDto; } + + // C# needs .NET and a separate extractor program, so it is only attempted when the + // folder holds a solution or project. A failure here never costs the Java/Kotlin result. + try { + if (csharpBuilder.hasCSharpSources(repositoryPath)) { + CodebaseGraphDTO csharpDto = csharpBuilder.buildGraph(repositoryPath, repositoryRoot, config); + merged = merge(merged, csharpDto); + } + } catch (IOException e) { + // Expected failures (no extractor configured, .NET missing, bad facts file) explain themselves. + log.warn("C# analysis failed; continuing without C#: {}", e.getMessage()); + } catch (Exception e) { + log.warn("C# analysis failed; continuing without C#", e); + } + return merged; } /** diff --git a/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpExtractorRunner.java b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpExtractorRunner.java new file mode 100644 index 00000000..710242d5 --- /dev/null +++ b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpExtractorRunner.java @@ -0,0 +1,184 @@ +package org.hjug.graphbuilder.graphbuilder; + +import java.io.IOException; +import java.nio.file.FileVisitOption; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import lombok.extern.slf4j.Slf4j; + +/** Finds the C# solution in a folder and runs csharp-fact-extractor on it. */ +@Slf4j +final class CSharpExtractorRunner { + + static final String EXTRACTOR_PROPERTY = "refactorfirst.csharp.extractor"; + static final String EXTRACTOR_ENV = "REFACTORFIRST_CSHARP_EXTRACTOR"; + /** Which dotnet to launch. The extractor needs a .NET 10 runtime, and the first dotnet on the PATH may be older. */ + static final String DOTNET_PROPERTY = "refactorfirst.dotnet"; + + static final String DOTNET_ENV = "REFACTORFIRST_DOTNET"; + private static final long TIMEOUT_MINUTES = 30; + private static final int PROJECT_SEARCH_DEPTH = 6; + /** Folders never searched for projects: build output, packages and version control. */ + private static final Set SKIPPED_FOLDERS = Set.of("bin", "obj", "node_modules", ".git"); + + private final String extractorDll; + private final String dotnet; + + CSharpExtractorRunner() { + this( + System.getProperty(EXTRACTOR_PROPERTY, System.getenv(EXTRACTOR_ENV)), + System.getProperty(DOTNET_PROPERTY, System.getenv(DOTNET_ENV))); + } + + CSharpExtractorRunner(String extractorDll) { + this(extractorDll, null); + } + + CSharpExtractorRunner(String extractorDll, String dotnet) { + this.extractorDll = extractorDll; + this.dotnet = dotnet == null || dotnet.isBlank() ? "dotnet" : dotnet; + } + + /** + * What to hand the extractor: the .sln or .slnx in the folder itself if there is one, otherwise + * every .csproj found outside build output. A repository with several projects and no solution at + * its root must have all of them analysed, not an arbitrary one. + */ + static List findTargets(Path directory) throws IOException { + if (!Files.isDirectory(directory)) { + return List.of(); + } + List solutions; + try (Stream top = Files.list(directory)) { + solutions = top.filter(Files::isRegularFile) + .filter(p -> endsWith(p, ".sln") || endsWith(p, ".slnx")) + .sorted() + .collect(Collectors.toList()); + } + if (!solutions.isEmpty()) { + if (solutions.size() > 1) { + log.warn( + "Several solutions in {}; analysing {} and ignoring {}", + directory, + solutions.get(0).getFileName(), + solutions.subList(1, solutions.size())); + } + return List.of(solutions.get(0)); + } + List projects = new ArrayList<>(); + Files.walkFileTree( + directory, EnumSet.noneOf(FileVisitOption.class), PROJECT_SEARCH_DEPTH, new SimpleFileVisitor<>() { + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { + boolean skipped = !dir.equals(directory) + && SKIPPED_FOLDERS.contains(dir.getFileName().toString()); + return skipped ? FileVisitResult.SKIP_SUBTREE : FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + if (attrs.isRegularFile() && endsWith(file, ".csproj")) { + projects.add(file); + } + return FileVisitResult.CONTINUE; + } + + /** A folder that cannot be read is passed over; it must not stop the search. */ + @Override + public FileVisitResult visitFileFailed(Path file, IOException e) { + return FileVisitResult.CONTINUE; + } + }); + Collections.sort(projects); + return projects; + } + + /** Runs the extractor with test projects left out, in the repository root. */ + Path run(List targets, Path repositoryRoot) throws IOException { + return run(targets, repositoryRoot, repositoryRoot, false); + } + + /** + * Runs the extractor and returns the facts file it wrote. The caller deletes the file. + * + * @param workingDirectory where the extractor runs: the analysed folder, so that .NET picks the + * SDK that folder pins (global.json) and not the one where Java was started + * @param includeTests true to analyse test projects too + */ + Path run(List targets, Path repositoryRoot, Path workingDirectory, boolean includeTests) throws IOException { + if (extractorDll == null || extractorDll.isBlank()) { + throw new IOException("C# sources found but no extractor is configured. Set " + EXTRACTOR_ENV + " (or -D" + + EXTRACTOR_PROPERTY + ") to the path of CSharpFactExtractor.dll"); + } + if (!Files.isRegularFile(Path.of(extractorDll))) { + throw new IOException("C# fact extractor not found at " + extractorDll); + } + Path out = Files.createTempFile("refactorfirst-csharp-facts", ".json"); + List command = command(targets, repositoryRoot, out, includeTests); + log.info("Running C# fact extractor on {}", targets); + Process process; + try { + process = new ProcessBuilder(command) + .directory(workingDirectory.toFile()) + .inheritIO() + .start(); + } catch (IOException e) { + Files.deleteIfExists(out); + throw new IOException( + "Could not start '" + dotnet + "'; is .NET installed? Set " + DOTNET_ENV + + " to the full path of a dotnet that has the .NET 10 runtime", + e); + } + try { + if (!process.waitFor(TIMEOUT_MINUTES, TimeUnit.MINUTES)) { + process.destroyForcibly(); + Files.deleteIfExists(out); + throw new IOException("C# fact extractor timed out after " + TIMEOUT_MINUTES + " minutes"); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + process.destroyForcibly(); + Files.deleteIfExists(out); + throw new IOException("Interrupted while waiting for the C# fact extractor", e); + } + if (process.exitValue() != 0) { + Files.deleteIfExists(out); + throw new IOException("C# fact extractor exited with code " + process.exitValue() + + ". If the output above says to install or update .NET, set " + DOTNET_ENV + + " to the full path of a dotnet that has the .NET 10 runtime"); + } + return out; + } + + /** The command line. Paths are made absolute because the extractor runs in another folder. */ + List command(List targets, Path repositoryRoot, Path out, boolean includeTests) { + List command = new ArrayList<>( + List.of(dotnet, Path.of(extractorDll).toAbsolutePath().toString())); + targets.forEach(target -> command.add(target.toAbsolutePath().toString())); + command.addAll(List.of( + "--root", + repositoryRoot.toAbsolutePath().toString(), + "--out", + out.toAbsolutePath().toString())); + if (includeTests) { + command.add("--include-tests"); + } + return command; + } + + private static boolean endsWith(Path path, String extension) { + return path.getFileName().toString().toLowerCase(Locale.ROOT).endsWith(extension); + } +} diff --git a/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFactLists.java b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFactLists.java new file mode 100644 index 00000000..d6ea5c5d --- /dev/null +++ b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFactLists.java @@ -0,0 +1,19 @@ +package org.hjug.graphbuilder.graphbuilder; + +import java.util.List; + +/** + * List defaults for {@link CSharpFacts}. The extractor may leave a list out of the JSON; + * the records then hold an empty list instead of null. + * + * Kept outside CSharpFacts on purpose: its nested records use this too, and a helper + * on the outer record would make them depend on the record that contains them (a cycle). + */ +final class CSharpFactLists { + + private CSharpFactLists() {} + + static List orEmpty(List list) { + return list == null ? List.of() : list; + } +} diff --git a/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFacts.java b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFacts.java new file mode 100644 index 00000000..fe58f6cc --- /dev/null +++ b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFacts.java @@ -0,0 +1,90 @@ +package org.hjug.graphbuilder.graphbuilder; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +/** + * The facts contract written by csharp-fact-extractor: raw facts about a C# codebase, + * from which the usual collectors compute the same metrics they compute for Java. + * See csharp-fact-extractor/docs/facts-contract.md. + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public record CSharpFacts(int schemaVersion, String root, List namespaces, List types) { + + static final int SUPPORTED_SCHEMA_VERSION = 1; + + public CSharpFacts { + namespaces = CSharpFactLists.orEmpty(namespaces); + types = CSharpFactLists.orEmpty(types); + } + + static CSharpFacts read(InputStream in) throws IOException { + CSharpFacts facts = new ObjectMapper().readValue(in, CSharpFacts.class); + if (facts.schemaVersion() != SUPPORTED_SCHEMA_VERSION) { + throw new IOException("Unsupported C# facts schema version " + facts.schemaVersion() + " (expected " + + SUPPORTED_SCHEMA_VERSION + ")"); + } + return facts; + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public record TypeFacts( + String fqn, + String namespace, + String name, + String sourceFile, + int loc, + String parent, + List attributes, + List dependencies, + List methods, + int protectedMembers, + List overriddenMethods, + List usedParentMembers) { + + public TypeFacts { + attributes = CSharpFactLists.orEmpty(attributes); + dependencies = CSharpFactLists.orEmpty(dependencies); + methods = CSharpFactLists.orEmpty(methods); + overriddenMethods = CSharpFactLists.orEmpty(overriddenMethods); + usedParentMembers = CSharpFactLists.orEmpty(usedParentMembers); + } + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public record AttributeFacts(String name, @JsonProperty("isPublic") boolean isPublic) {} + + @JsonIgnoreProperties(ignoreUnknown = true) + public record DependencyFacts(String to, int count) {} + + @JsonIgnoreProperties(ignoreUnknown = true) + public record MethodFacts( + String name, + String signature, + int loc, + int cyclo, + int maxNesting, + int parameters, + @JsonProperty("isAccessor") boolean isAccessor, + @JsonProperty("isConstructor") boolean isConstructor, + List accessedVariables, + List accessedOwnAttributes, + List accessedForeignAttributes, + List accessedForeignClasses, + List calledForeignMethods, + List calledForeignMethodClasses) { + + public MethodFacts { + accessedVariables = CSharpFactLists.orEmpty(accessedVariables); + accessedOwnAttributes = CSharpFactLists.orEmpty(accessedOwnAttributes); + accessedForeignAttributes = CSharpFactLists.orEmpty(accessedForeignAttributes); + accessedForeignClasses = CSharpFactLists.orEmpty(accessedForeignClasses); + calledForeignMethods = CSharpFactLists.orEmpty(calledForeignMethods); + calledForeignMethodClasses = CSharpFactLists.orEmpty(calledForeignMethodClasses); + } + } +} diff --git a/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFactsReplayer.java b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFactsReplayer.java new file mode 100644 index 00000000..313a870d --- /dev/null +++ b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFactsReplayer.java @@ -0,0 +1,71 @@ +package org.hjug.graphbuilder.graphbuilder; + +import java.util.Map; +import org.hjug.graphbuilder.GraphDependencyCollector; +import org.hjug.graphbuilder.metrics.ClassMetrics; +import org.hjug.graphbuilder.metrics.GraphMetricsCollector; +import org.hjug.graphbuilder.metrics.MethodMetrics; + +/** Replays extractor facts into the same collectors the Java and Kotlin visitors feed. */ +final class CSharpFactsReplayer { + + private CSharpFactsReplayer() {} + + static void replay( + CSharpFacts facts, + GraphDependencyCollector dependencies, + GraphMetricsCollector metrics, + Map classToSourceFilePathMapping) { + + facts.namespaces().forEach(dependencies::registerPackage); + + for (CSharpFacts.TypeFacts type : facts.types()) { + dependencies.registerClassVertex(type.fqn()); + dependencies.recordClassLocation(type.fqn(), type.sourceFile()); + classToSourceFilePathMapping.put(type.fqn(), type.sourceFile()); + + ClassMetrics classMetrics = metrics.getOrCreateClassMetrics(type.fqn()); + classMetrics.setClassName(type.name()); + classMetrics.setPackageName(type.namespace()); + classMetrics.setSourceFilePath(type.sourceFile()); + classMetrics.setLinesOfCode(type.loc()); + if (type.parent() != null) { + classMetrics.setParentClass(type.parent()); + } + type.attributes().forEach(a -> classMetrics.addAttribute(a.name(), a.isPublic())); + // What Refused Parent Bequest and Tradition Breaker are computed from. + classMetrics.setNumberOfProtectedMembers(type.protectedMembers()); + type.overriddenMethods().forEach(classMetrics::addOverriddenMethod); + type.usedParentMembers().forEach(classMetrics::addUsedParentMember); + + for (CSharpFacts.DependencyFacts dependency : type.dependencies()) { + classMetrics.addDependency(dependency.to()); + // The first call creates the edge at weight 1; each further call adds 1. + for (int i = 0; i < dependency.count(); i++) { + dependencies.addClassDependency(type.fqn(), dependency.to()); + } + } + + for (CSharpFacts.MethodFacts method : type.methods()) { + MethodMetrics methodMetrics = new MethodMetrics(method.name(), method.signature()); + methodMetrics.setLinesOfCode(method.loc()); + methodMetrics.setCyclomaticComplexity(method.cyclo()); + methodMetrics.setMaxNestingDepth(method.maxNesting()); + methodMetrics.setNumberOfParameters(method.parameters()); + methodMetrics.setAccessor(method.isAccessor()); + methodMetrics.setConstructor(method.isConstructor()); + method.accessedVariables().forEach(methodMetrics::addAccessedVariable); + method.accessedOwnAttributes().forEach(methodMetrics::addAccessedOwnAttribute); + method.accessedForeignAttributes().forEach(methodMetrics::addAccessedForeignAttribute); + method.accessedForeignClasses().forEach(methodMetrics::addAccessedForeignClass); + method.calledForeignMethods().forEach(methodMetrics::addCalledForeignMethod); + method.calledForeignMethodClasses().forEach(methodMetrics::addCalledForeignMethodClass); + classMetrics.addMethod(methodMetrics); + + // Same shape the Java visitor uses: callee signature, caller class, "CallerClass::callerSignature". + String caller = type.fqn() + "::" + method.signature(); + method.calledForeignMethods().forEach(callee -> metrics.recordIncomingCall(callee, type.fqn(), caller)); + } + } + } +} diff --git a/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpSourceFileGraphBuilder.java b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpSourceFileGraphBuilder.java new file mode 100644 index 00000000..5bd0843e --- /dev/null +++ b/codebase-graph-builder/src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpSourceFileGraphBuilder.java @@ -0,0 +1,101 @@ +package org.hjug.graphbuilder.graphbuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.extern.slf4j.Slf4j; +import org.hjug.graphbuilder.CodebaseGraphDTO; +import org.hjug.graphbuilder.GraphBuilderConfig; +import org.hjug.graphbuilder.GraphDependencyCollector; +import org.hjug.graphbuilder.VertexInfo; +import org.hjug.graphbuilder.metrics.GraphMetricsCollector; +import org.jgrapht.Graph; +import org.jgrapht.graph.DefaultDirectedWeightedGraph; +import org.jgrapht.graph.DefaultWeightedEdge; + +/** + * Third language reader: C#, by way of the csharp-fact-extractor .NET program. + * The extractor writes raw facts; this class replays them into the collectors the Java + * and Kotlin visitors feed, so every metric and smell rule downstream is shared. + */ +@Slf4j +public class CSharpSourceFileGraphBuilder implements SourceFileGraphBuilder { + + private final CSharpExtractorRunner runner; + + /** What the last search found, so asking "is there C#?" and then building does not search twice. */ + private Path searchedDirectory; + + private List foundTargets = List.of(); + + public CSharpSourceFileGraphBuilder() { + this(new CSharpExtractorRunner()); + } + + CSharpSourceFileGraphBuilder(CSharpExtractorRunner runner) { + this.runner = runner; + } + + /** True when the folder holds a C# solution or project worth launching .NET for. */ + public boolean hasCSharpSources(String repositoryPath) throws IOException { + return !targetsIn(absolute(repositoryPath)).isEmpty(); + } + + private List targetsIn(Path directory) throws IOException { + if (!directory.equals(searchedDirectory)) { + foundTargets = CSharpExtractorRunner.findTargets(directory); + searchedDirectory = directory; + } + return foundTargets; + } + + private static Path absolute(String path) { + return Path.of(path).toAbsolutePath().normalize(); + } + + @Override + public CodebaseGraphDTO buildGraph(String repositoryPath, String repositoryRoot, GraphBuilderConfig config) + throws IOException { + Path directory = absolute(repositoryPath); + List targets = targetsIn(directory); + if (targets.isEmpty()) { + throw new IOException("No .sln, .slnx or .csproj found under " + repositoryPath); + } + Path root = repositoryRoot == null || repositoryRoot.isEmpty() ? directory : absolute(repositoryRoot); + // Test projects are left out unless the caller asked for tests to be analysed. + boolean includeTests = config != null && !config.isExcludeTests(); + Path facts = runner.run(targets, root, directory, includeTests); + try { + return buildGraphFromFacts(facts); + } finally { + Files.deleteIfExists(facts); + } + } + + /** Builds the graph from a facts file already on disk. Separate so it can be tested without .NET. */ + protected CodebaseGraphDTO buildGraphFromFacts(Path factsFile) throws IOException { + Graph classGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + Graph packageGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + GraphDependencyCollector dependencies = new GraphDependencyCollector(classGraph, packageGraph); + GraphMetricsCollector metrics = new GraphMetricsCollector(classGraph, packageGraph); + Map classToSourceFilePathMapping = new HashMap<>(); + + CSharpFacts facts; + try (InputStream in = Files.newInputStream(factsFile)) { + facts = CSharpFacts.read(in); + } + log.info( + "CSharpSourceFileGraphBuilder: replaying {} C# types", + facts.types().size()); + CSharpFactsReplayer.replay(facts, dependencies, metrics, classToSourceFilePathMapping); + + return JavaSourceFileGraphBuilder.finalizeDto( + classGraph, packageGraph, dependencies, classToSourceFilePathMapping, metrics); + } +} diff --git a/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/CompositeGraphBuilderCSharpTest.java b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/CompositeGraphBuilderCSharpTest.java new file mode 100644 index 00000000..b74ee5dc --- /dev/null +++ b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/CompositeGraphBuilderCSharpTest.java @@ -0,0 +1,119 @@ +package org.hjug.graphbuilder; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import org.hjug.graphbuilder.graphbuilder.CSharpSourceFileGraphBuilder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class CompositeGraphBuilderCSharpTest { + + private static final String JAVA_CLASS = "package demo;\npublic class Hello { }\n"; + + @Test + void givenJavaOnlyFolder_whenBuilt_thenCSharpReaderIsNotRun(@TempDir Path dir) throws IOException { + writeJavaClass(dir); + AtomicBoolean ran = new AtomicBoolean(false); + CSharpSourceFileGraphBuilder spy = new CSharpSourceFileGraphBuilder() { + @Override + public CodebaseGraphDTO buildGraph(String path, String root, GraphBuilderConfig config) { + ran.set(true); + return null; + } + }; + + CodebaseGraphDTO dto = + new CompositeGraphBuilder(spy).getCodebaseGraphDTO(dir.toString(), GraphBuilderConfig.defaultConfig()); + + assertFalse(ran.get(), "no .sln or .csproj, so .NET must not be launched"); + assertTrue(classNames(dto).contains("demo.Hello")); + } + + @Test + void givenCSharpFailure_whenBuilt_thenJavaGraphIsStillReturned(@TempDir Path dir) throws IOException { + writeJavaClass(dir); + Files.writeString(dir.resolve("App.sln"), ""); + CSharpSourceFileGraphBuilder failing = new CSharpSourceFileGraphBuilder() { + @Override + public CodebaseGraphDTO buildGraph(String path, String root, GraphBuilderConfig config) throws IOException { + throw new IOException("dotnet not found"); + } + }; + + CodebaseGraphDTO dto = new CompositeGraphBuilder(failing) + .getCodebaseGraphDTO(dir.toString(), GraphBuilderConfig.defaultConfig()); + + assertTrue(classNames(dto).contains("demo.Hello")); + } + + @Test + void givenJavaAndCSharp_whenBuilt_thenBothSetsOfClassesAreInOneGraph(@TempDir Path dir) throws Exception { + writeJavaClass(dir); + Files.writeString(dir.resolve("App.sln"), ""); + + CodebaseGraphDTO dto = new CompositeGraphBuilder(readerFromGoldenFacts(dir)) + .getCodebaseGraphDTO(dir.toString(), GraphBuilderConfig.defaultConfig()); + + List names = classNames(dto); + assertTrue(names.contains("demo.Hello")); + assertTrue(names.contains("TinyGreeter.Greeter")); + assertTrue(names.contains("TinyGreeter.MessageStore")); + assertTrue(names.contains("TinyGreeter.Program")); + assertEquals("Greeter.cs", dto.getClassToSourceFilePathMapping().get("TinyGreeter.Greeter")); + } + + @Test + void givenCSharpOnly_whenBuilt_thenEdgesAndWeightsSurviveTheMerge(@TempDir Path dir) throws Exception { + Files.writeString(dir.resolve("App.sln"), ""); + + CodebaseGraphDTO dto = new CompositeGraphBuilder(readerFromGoldenFacts(dir)) + .getCodebaseGraphDTO(dir.toString(), GraphBuilderConfig.defaultConfig()); + + var graph = dto.getClassReferencesGraph(); + assertEquals(3, graph.vertexSet().size()); + assertEquals(3, graph.edgeSet().size()); + VertexInfo greeter = vertex(dto, "TinyGreeter.Greeter"); + VertexInfo store = vertex(dto, "TinyGreeter.MessageStore"); + assertEquals(4.0, graph.getEdgeWeight(graph.getEdge(greeter, store))); + assertEquals(2.0, graph.getEdgeWeight(graph.getEdge(store, greeter))); + } + + private static void writeJavaClass(Path dir) throws IOException { + Files.createDirectories(dir.resolve("demo")); + Files.writeString(dir.resolve("demo/Hello.java"), JAVA_CLASS); + } + + /** A reader that skips .NET and replays the extractor's checked-in output for TinyGreeter. */ + private CSharpSourceFileGraphBuilder readerFromGoldenFacts(Path dir) throws IOException { + Path facts = dir.resolve("facts.json"); + try (var in = getClass().getResourceAsStream("/csharp/tiny-greeter-facts.json")) { + Files.copy(in, facts); + } + return new CSharpSourceFileGraphBuilder() { + @Override + public CodebaseGraphDTO buildGraph(String path, String root, GraphBuilderConfig config) throws IOException { + return buildGraphFromFacts(facts); + } + }; + } + + private static List classNames(CodebaseGraphDTO dto) { + return dto.getClassReferencesGraph().vertexSet().stream() + .map(VertexInfo::getName) + .toList(); + } + + private static VertexInfo vertex(CodebaseGraphDTO dto, String name) { + return dto.getClassReferencesGraph().vertexSet().stream() + .filter(v -> v.getName().equals(name)) + .findFirst() + .orElseThrow(); + } +} diff --git a/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpExtractorRunnerTest.java b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpExtractorRunnerTest.java new file mode 100644 index 00000000..bb77e1ee --- /dev/null +++ b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpExtractorRunnerTest.java @@ -0,0 +1,209 @@ +package org.hjug.graphbuilder.graphbuilder; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Locale; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class CSharpExtractorRunnerTest { + + @Test + void givenFolderWithoutCSharp_whenAsked_thenNoSolutionOrProjectIsFound(@TempDir Path dir) throws IOException { + Files.writeString(dir.resolve("Main.java"), "class Main {}"); + + assertTrue(CSharpExtractorRunner.findTargets(dir).isEmpty()); + } + + @Test + void givenSolutionAndProject_whenAsked_thenSolutionWins(@TempDir Path dir) throws IOException { + Files.writeString(dir.resolve("App.sln"), ""); + Files.createDirectories(dir.resolve("src")); + Files.writeString(dir.resolve("src/App.csproj"), ""); + + assertEquals(List.of("App.sln"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenNewFormatSolution_whenAsked_thenItIsFound(@TempDir Path dir) throws IOException { + Files.writeString(dir.resolve("App.slnx"), ""); + + assertEquals(List.of("App.slnx"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenOnlyAProjectInASubfolder_whenAsked_thenItIsFound(@TempDir Path dir) throws IOException { + Files.createDirectories(dir.resolve("src/App")); + Files.writeString(dir.resolve("src/App/App.csproj"), ""); + + assertEquals(List.of("App.csproj"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenOnlyBuildOutput_whenAsked_thenItIsIgnored(@TempDir Path dir) throws IOException { + Files.createDirectories(dir.resolve("obj")); + Files.writeString(dir.resolve("obj/Generated.csproj"), ""); + + assertTrue(CSharpExtractorRunner.findTargets(dir).isEmpty()); + } + + @Test + void givenNoExtractorConfigured_whenRun_thenThrowsNamingTheSetting(@TempDir Path dir) throws IOException { + Files.writeString(dir.resolve("App.sln"), ""); + + IOException e = assertThrows( + IOException.class, () -> new CSharpExtractorRunner(null).run(List.of(dir.resolve("App.sln")), dir)); + + assertTrue(e.getMessage().contains(CSharpExtractorRunner.EXTRACTOR_ENV)); + } + + @Test + void givenExtractorPathThatDoesNotExist_whenRun_thenThrowsNamingThePath(@TempDir Path dir) throws IOException { + Files.writeString(dir.resolve("App.sln"), ""); + String missing = dir.resolve("nowhere/CSharpFactExtractor.dll").toString(); + + IOException e = assertThrows( + IOException.class, () -> new CSharpExtractorRunner(missing).run(List.of(dir.resolve("App.sln")), dir)); + + assertTrue(e.getMessage().contains(missing)); + } + + @Test + void givenSeveralProjectsAndNoSolution_whenAsked_thenAllOfThemAreFound(@TempDir Path dir) throws IOException { + Files.createDirectories(dir.resolve("App")); + Files.createDirectories(dir.resolve("App.Tests")); + Files.createDirectories(dir.resolve("App/obj")); + Files.writeString(dir.resolve("App/App.csproj"), ""); + Files.writeString(dir.resolve("App.Tests/App.Tests.csproj"), ""); + Files.writeString(dir.resolve("App/obj/Generated.csproj"), ""); + + assertEquals(List.of("App.Tests.csproj", "App.csproj"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenASolutionInASubfolderOnly_whenAsked_thenTheProjectsAreUsedInstead(@TempDir Path dir) throws IOException { + Files.createDirectories(dir.resolve("App")); + Files.writeString(dir.resolve("App/App.sln"), ""); + Files.writeString(dir.resolve("App/App.csproj"), ""); + + assertEquals(List.of("App.csproj"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenADotnetThatCannotBeStarted_whenRun_thenTheMessageNamesTheDotnetSetting(@TempDir Path dir) + throws IOException { + Files.writeString(dir.resolve("App.sln"), ""); + Path dll = Files.writeString(dir.resolve("CSharpFactExtractor.dll"), ""); + + IOException e = assertThrows(IOException.class, () -> new CSharpExtractorRunner( + dll.toString(), dir.resolve("no-such-dotnet").toString()) + .run(List.of(dir.resolve("App.sln")), dir)); + + assertTrue(e.getMessage().contains(CSharpExtractorRunner.DOTNET_ENV)); + } + + @Test + void givenProjectsUnderPackageAndGitFolders_whenAsked_thenThoseFoldersAreNotSearched(@TempDir Path dir) + throws IOException { + Files.createDirectories(dir.resolve("node_modules/pkg")); + Files.createDirectories(dir.resolve(".git/hooks")); + Files.createDirectories(dir.resolve("src/bin")); + Files.writeString(dir.resolve("node_modules/pkg/Pkg.csproj"), ""); + Files.writeString(dir.resolve(".git/hooks/Hook.csproj"), ""); + Files.writeString(dir.resolve("src/bin/Out.csproj"), ""); + Files.writeString(dir.resolve("src/App.csproj"), ""); + + assertEquals(List.of("App.csproj"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenSeveralSolutions_whenAsked_thenTheFirstByNameIsUsed(@TempDir Path dir) throws IOException { + Files.writeString(dir.resolve("App.Mobile.sln"), ""); + Files.writeString(dir.resolve("App.sln"), ""); + + assertEquals(List.of("App.Mobile.sln"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenAFolderNamedLikeASolution_whenAsked_thenItIsNotTakenForOne(@TempDir Path dir) throws IOException { + Files.createDirectories(dir.resolve("old.sln")); + Files.writeString(dir.resolve("App.csproj"), ""); + + assertEquals(List.of("App.csproj"), names(CSharpExtractorRunner.findTargets(dir))); + } + + @Test + void givenTestsAreWanted_whenTheCommandIsBuilt_thenItAsksTheExtractorToIncludeThem(@TempDir Path dir) { + CSharpExtractorRunner runner = new CSharpExtractorRunner("tools/CSharpFactExtractor.dll", "dotnet"); + List targets = List.of(dir.resolve("App.sln")); + + List without = runner.command(targets, dir, dir.resolve("facts.json"), false); + List with = runner.command(targets, dir, dir.resolve("facts.json"), true); + + assertFalse(without.contains("--include-tests")); + assertTrue(with.contains("--include-tests")); + // The extractor runs in the analysed folder, so a relative extractor path must be made absolute. + assertTrue(Path.of(with.get(1)).isAbsolute()); + } + + @Test + void givenAnExtractorThatFails_whenRun_thenTheExitCodeIsReportedAndNoFileIsLeft(@TempDir Path dir) + throws IOException { + Path dll = Files.writeString(dir.resolve("CSharpFactExtractor.dll"), ""); + Path dotnet = stubDotnet(dir, "exit 3"); + + IOException e = + assertThrows(IOException.class, () -> new CSharpExtractorRunner(dll.toString(), dotnet.toString()) + .run(List.of(dir.resolve("App.sln")), dir)); + + assertTrue(e.getMessage().contains("exited with code 3"), e.getMessage()); + } + + @Test + void givenAFolderWithASpaceInItsName_whenRun_thenTheExtractorRunsInThatFolderWithIntactArguments(@TempDir Path dir) + throws IOException { + Path analysed = Files.createDirectories(dir.resolve("my code")); + Path solution = Files.writeString(analysed.resolve("App.sln"), ""); + Path dll = Files.writeString(dir.resolve("CSharpFactExtractor.dll"), ""); + // Stands in for dotnet: writes the folder it runs in and its arguments, one per line, to the --out file. + Path dotnet = stubDotnet( + dir, + "for last; do :; done", + "out=$last; prev=", + "for a; do if [ \"$prev\" = --out ]; then out=$a; fi; prev=$a; done", + "pwd -P > \"$out\"", + "for a; do echo \"$a\" >> \"$out\"; done"); + + Path facts = new CSharpExtractorRunner(dll.toString(), dotnet.toString()) + .run(List.of(solution), analysed, analysed, true); + try { + List lines = Files.readAllLines(facts); + assertEquals(analysed.toRealPath().toString(), lines.get(0)); + assertTrue(lines.contains(solution.toAbsolutePath().toString()), lines.toString()); + assertTrue(lines.contains("--include-tests"), lines.toString()); + } finally { + Files.deleteIfExists(facts); + } + } + + /** A shell script used in place of dotnet, so the process handling is tested without .NET. */ + private static Path stubDotnet(Path dir, String... lines) throws IOException { + assumeFalse(System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win"), "needs a POSIX shell"); + Path script = dir.resolve("fake-dotnet.sh"); + Files.writeString(script, "#!/bin/sh\n" + String.join("\n", lines) + "\n"); + assertTrue(script.toFile().setExecutable(true)); + return script; + } + + private static List names(List paths) { + return paths.stream().map(p -> p.getFileName().toString()).toList(); + } +} diff --git a/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpFactListsTest.java b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpFactListsTest.java new file mode 100644 index 00000000..fb92a6cd --- /dev/null +++ b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpFactListsTest.java @@ -0,0 +1,41 @@ +package org.hjug.graphbuilder.graphbuilder; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.Test; + +class CSharpFactListsTest { + + @Test + void aMissingListBecomesEmpty() { + assertEquals(List.of(), CSharpFactLists.orEmpty(null)); + } + + @Test + void aPresentListIsKeptAsIs() { + List names = List.of("a", "b"); + assertSame(names, CSharpFactLists.orEmpty(names)); + } + + @Test + void theRecordsStillDefaultTheirLists() { + CSharpFacts facts = new CSharpFacts(1, "root", null, null); + assertTrue(facts.namespaces().isEmpty()); + assertTrue(facts.types().isEmpty()); + } + + /** The nested records must not call back into the record that contains them. */ + @Test + void theHelperDoesNotLiveOnTheOuterRecord() throws IOException { + String source = Files.readString( + Path.of("src/main/java/org/hjug/graphbuilder/graphbuilder/CSharpFacts.java"), StandardCharsets.UTF_8); + assertTrue(!source.contains("List orEmpty("), "orEmpty is declared in CSharpFacts again"); + } +} diff --git a/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpFactsReplayerTest.java b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpFactsReplayerTest.java new file mode 100644 index 00000000..868f4772 --- /dev/null +++ b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpFactsReplayerTest.java @@ -0,0 +1,156 @@ +package org.hjug.graphbuilder.graphbuilder; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import org.hjug.graphbuilder.GraphDependencyCollector; +import org.hjug.graphbuilder.VertexInfo; +import org.hjug.graphbuilder.metrics.ClassMetrics; +import org.hjug.graphbuilder.metrics.GraphMetricsCollector; +import org.hjug.graphbuilder.metrics.MethodMetrics; +import org.jgrapht.Graph; +import org.jgrapht.graph.DefaultDirectedWeightedGraph; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.junit.jupiter.api.Test; + +/** + * Reads the facts file that csharp-fact-extractor produces for its TinyGreeter fixture. + * The extractor's own ContractTests keep that file in step with the C# side. + */ +class CSharpFactsReplayerTest { + + @Test + void givenTinyGreeterFacts_whenReplayed_thenGraphHasTheCycleAndMetrics() throws Exception { + Graph classGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + Graph packageGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + GraphDependencyCollector dependencies = new GraphDependencyCollector(classGraph, packageGraph); + GraphMetricsCollector metrics = new GraphMetricsCollector(classGraph, packageGraph); + Map classToFile = new HashMap<>(); + + try (InputStream in = getClass().getResourceAsStream("/csharp/tiny-greeter-facts.json")) { + CSharpFactsReplayer.replay(CSharpFacts.read(in), dependencies, metrics, classToFile); + } + + assertEquals(3, classGraph.vertexSet().size()); + assertEquals(4.0, weight(classGraph, "TinyGreeter.Greeter", "TinyGreeter.MessageStore")); + assertEquals(2.0, weight(classGraph, "TinyGreeter.MessageStore", "TinyGreeter.Greeter")); + assertEquals(3.0, weight(classGraph, "TinyGreeter.Program", "TinyGreeter.Greeter")); + assertEquals("Greeter.cs", classToFile.get("TinyGreeter.Greeter")); + assertTrue(dependencies.getPackagesInCodebase().contains("TinyGreeter")); + + ClassMetrics greeter = metrics.getClassMetrics("TinyGreeter.Greeter"); + assertEquals(2, greeter.getAttributes().size()); + assertEquals(0, greeter.getNumberOfPublicAttributes()); + assertEquals(4, greeter.getNumberOfMethods()); + assertEquals(2, greeter.getNumberOfAccessorMethods()); + assertEquals(5, greeter.getWeightedMethodCount()); + + MethodMetrics greet = greeter.getMethods().get("Greet(string)"); + assertEquals(2, greet.getCouplingIntensity()); + assertEquals(2, greet.getNumberOfAccessedVariables()); + assertEquals(1, greet.getMaxNestingDepth()); + } + + /** + * End to end through the real .NET extractor: methods are kept one per signature, so overloads + * that differ only by a type parameter or by ref/out, and a static constructor beside an instance + * one, must each arrive under their own signature. Runs only when the extractor is configured. + */ + @Test + void givenRealExtractor_whenOverloadsAreReplayed_thenEveryMethodIsKept() throws Exception { + String extractor = System.getenv(CSharpExtractorRunner.EXTRACTOR_ENV); + assumeTrue(extractor != null && Files.exists(Path.of(extractor)), "extractor not configured"); + Path fixture = Path.of("..", "csharp-fact-extractor", "fixtures", "Members") + .toAbsolutePath() + .normalize(); + assumeTrue(Files.isDirectory(fixture), "fixture not found"); + + Graph classGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + Graph packageGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + GraphDependencyCollector dependencies = new GraphDependencyCollector(classGraph, packageGraph); + GraphMetricsCollector metrics = new GraphMetricsCollector(classGraph, packageGraph); + + Path facts = new CSharpExtractorRunner().run(CSharpExtractorRunner.findTargets(fixture), fixture); + try (InputStream in = Files.newInputStream(facts)) { + CSharpFactsReplayer.replay(CSharpFacts.read(in), dependencies, metrics, new HashMap<>()); + } finally { + Files.deleteIfExists(facts); + } + + assertEquals( + Set.of("Add(int)", "Add(ref int)", "Read(out int)", "Save()", "Save()"), + metrics.getClassMetrics("Members.Overloads").getMethods().keySet()); + assertEquals( + Set.of("Cache()", "static Cache()", "Size()"), + metrics.getClassMetrics("Members.Cache").getMethods().keySet()); + } + + @Test + void givenASubclassInTheFacts_whenReplayed_thenParentOverridesAndUsedMembersReachTheMetrics() throws Exception { + Graph classGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + Graph packageGraph = + new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class); + GraphDependencyCollector dependencies = new GraphDependencyCollector(classGraph, packageGraph); + GraphMetricsCollector metrics = new GraphMetricsCollector(classGraph, packageGraph); + + try (InputStream in = getClass().getResourceAsStream("/csharp/two-projects-facts.json")) { + CSharpFactsReplayer.replay(CSharpFacts.read(in), dependencies, metrics, new HashMap<>()); + } + + ClassMetrics parent = metrics.getClassMetrics("Beta.BaseStore"); + ClassMetrics child = metrics.getClassMetrics("Beta.BetaStore"); + assertEquals(3, parent.getNumberOfProtectedMembers()); + assertEquals("Beta.BaseStore", child.getParentClass()); + assertEquals(1, child.getNumberOfOverriddenMethods()); + assertEquals(2, child.getNumberOfUsedParentMembers()); + } + + @Test + void givenUnknownSchemaVersion_whenRead_thenRejectedWithAClearMessage() { + String json = "{\"schemaVersion\": 99, \"root\": \".\", \"namespaces\": [], \"types\": []}"; + + IOException e = assertThrows( + IOException.class, + () -> CSharpFacts.read(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)))); + + assertTrue(e.getMessage().contains("99")); + } + + @Test + void givenUnknownFields_whenRead_thenTheyAreIgnored() throws Exception { + String json = + "{\"schemaVersion\": 1, \"root\": \".\", \"somethingNew\": true, \"namespaces\": [], \"types\": []}"; + + CSharpFacts facts = CSharpFacts.read(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))); + + assertEquals(0, facts.types().size()); + } + + private static double weight(Graph graph, String from, String to) { + VertexInfo source = graph.vertexSet().stream() + .filter(v -> v.getName().equals(from)) + .findFirst() + .orElseThrow(); + VertexInfo target = graph.vertexSet().stream() + .filter(v -> v.getName().equals(to)) + .findFirst() + .orElseThrow(); + return graph.getEdgeWeight(graph.getEdge(source, target)); + } +} diff --git a/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpSourceFileGraphBuilderTest.java b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpSourceFileGraphBuilderTest.java new file mode 100644 index 00000000..34310822 --- /dev/null +++ b/codebase-graph-builder/src/test/java/org/hjug/graphbuilder/graphbuilder/CSharpSourceFileGraphBuilderTest.java @@ -0,0 +1,99 @@ +package org.hjug.graphbuilder.graphbuilder; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.hjug.graphbuilder.CodebaseGraphDTO; +import org.hjug.graphbuilder.GraphBuilderConfig; +import org.hjug.graphbuilder.VertexInfo; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class CSharpSourceFileGraphBuilderTest { + + @Test + void givenFactsFile_whenBuilt_thenDtoHasTheCycle(@TempDir Path dir) throws Exception { + Path facts = dir.resolve("facts.json"); + try (var in = getClass().getResourceAsStream("/csharp/tiny-greeter-facts.json")) { + Files.copy(in, facts); + } + + CodebaseGraphDTO dto = new CSharpSourceFileGraphBuilder().buildGraphFromFacts(facts); + + assertEquals(3, dto.getClassReferencesGraph().vertexSet().size()); + assertEquals(3, dto.getClassReferencesGraph().edgeSet().size()); + assertEquals("Greeter.cs", dto.getClassToSourceFilePathMapping().get("TinyGreeter.Greeter")); + assertTrue(dto.getClassDisharmoniesOfType("God Class").isEmpty(), "three tiny classes are no God class"); + } + + @Test + void givenFolderWithASolution_whenAsked_thenHasCSharpSources(@TempDir Path dir) throws IOException { + assertFalse(new CSharpSourceFileGraphBuilder().hasCSharpSources(dir.toString())); + + Files.writeString(dir.resolve("App.sln"), ""); + + assertTrue(new CSharpSourceFileGraphBuilder().hasCSharpSources(dir.toString())); + } + + /** + * End to end through the real .NET extractor. Runs only when REFACTORFIRST_CSHARP_EXTRACTOR + * points at a built CSharpFactExtractor.dll, so the normal build needs no .NET. + */ + @Test + void givenRealExtractor_whenTinyGreeterIsBuilt_thenTheCycleIsFound() throws Exception { + String extractor = System.getenv(CSharpExtractorRunner.EXTRACTOR_ENV); + assumeTrue(extractor != null && Files.exists(Path.of(extractor)), "extractor not configured"); + Path fixture = Path.of("..", "csharp-fact-extractor", "fixtures", "TinyGreeter") + .toAbsolutePath() + .normalize(); + assumeTrue(Files.isDirectory(fixture), "fixture not found"); + + CodebaseGraphDTO dto = new CSharpSourceFileGraphBuilder() + .buildGraph(fixture.toString(), fixture.toString(), GraphBuilderConfig.defaultConfig()); + + var names = dto.getClassReferencesGraph().vertexSet().stream() + .map(VertexInfo::getName) + .sorted() + .toList(); + assertEquals( + java.util.List.of("TinyGreeter.Greeter", "TinyGreeter.MessageStore", "TinyGreeter.Program"), names); + assertEquals(3, dto.getClassReferencesGraph().edgeSet().size()); + } + + /** A type that exists only in generated code must not come back into the graph as a dependency target. */ + @Test + void givenRealExtractor_whenATypeIsOnlyInGeneratedCode_thenItIsNotInTheGraph() throws Exception { + String extractor = System.getenv(CSharpExtractorRunner.EXTRACTOR_ENV); + assumeTrue(extractor != null && Files.exists(Path.of(extractor)), "extractor not configured"); + Path fixture = Path.of("..", "csharp-fact-extractor", "fixtures", "Members") + .toAbsolutePath() + .normalize(); + assumeTrue(Files.isDirectory(fixture), "fixture not found"); + + CodebaseGraphDTO dto = new CSharpSourceFileGraphBuilder() + .buildGraph(fixture.toString(), fixture.toString(), GraphBuilderConfig.defaultConfig()); + + var names = dto.getClassReferencesGraph().vertexSet().stream() + .map(VertexInfo::getName) + .sorted() + .toList(); + assertEquals( + java.util.List.of( + "Members.Cache", + "Members.Clerk", + "Members.Holder", + "Members.Nester", + "Members.Node", + "Members.Order", + "Members.OverloadCaller", + "Members.Overloads", + "Members.UsesGenerated", + "Members.Validator"), + names); + } +} diff --git a/codebase-graph-builder/src/test/resources/csharp/tiny-greeter-facts.json b/codebase-graph-builder/src/test/resources/csharp/tiny-greeter-facts.json new file mode 100644 index 00000000..99577371 --- /dev/null +++ b/codebase-graph-builder/src/test/resources/csharp/tiny-greeter-facts.json @@ -0,0 +1,231 @@ +{ + "schemaVersion": 1, + "root": ".", + "namespaces": [ + "TinyGreeter" + ], + "types": [ + { + "fqn": "TinyGreeter.Greeter", + "namespace": "TinyGreeter", + "name": "Greeter", + "sourceFile": "Greeter.cs", + "loc": 20, + "parent": null, + "attributes": [ + { + "name": "Punctuation", + "isPublic": false + }, + { + "name": "_store", + "isPublic": false + } + ], + "dependencies": [ + { + "to": "TinyGreeter.MessageStore", + "count": 4 + } + ], + "methods": [ + { + "name": "Greet", + "signature": "Greet(string)", + "loc": 8, + "cyclo": 2, + "maxNesting": 1, + "parameters": 1, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [ + "Punctuation", + "_store" + ], + "accessedOwnAttributes": [ + "Punctuation", + "_store" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [ + "System.String.IsNullOrEmpty(string)", + "TinyGreeter.MessageStore.Find(string)" + ], + "calledForeignMethodClasses": [ + "System.String", + "TinyGreeter.MessageStore" + ] + }, + { + "name": "Greeter", + "signature": "Greeter()", + "loc": 4, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": false, + "isConstructor": true, + "accessedVariables": [ + "_store" + ], + "accessedOwnAttributes": [ + "_store" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + }, + { + "name": "get_Punctuation", + "signature": "get_Punctuation()", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": true, + "isConstructor": false, + "accessedVariables": [ + "Punctuation" + ], + "accessedOwnAttributes": [ + "Punctuation" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + }, + { + "name": "set_Punctuation", + "signature": "set_Punctuation(string)", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 1, + "isAccessor": true, + "isConstructor": false, + "accessedVariables": [ + "Punctuation" + ], + "accessedOwnAttributes": [ + "Punctuation" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + } + ], + "protectedMembers": 0, + "overriddenMethods": [], + "usedParentMembers": [] + }, + { + "fqn": "TinyGreeter.MessageStore", + "namespace": "TinyGreeter", + "name": "MessageStore", + "sourceFile": "MessageStore.cs", + "loc": 14, + "parent": null, + "attributes": [ + { + "name": "_owner", + "isPublic": false + } + ], + "dependencies": [ + { + "to": "TinyGreeter.Greeter", + "count": 2 + } + ], + "methods": [ + { + "name": "Find", + "signature": "Find(string)", + "loc": 4, + "cyclo": 2, + "maxNesting": 0, + "parameters": 1, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [], + "accessedOwnAttributes": [], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + }, + { + "name": "MessageStore", + "signature": "MessageStore(Greeter)", + "loc": 4, + "cyclo": 1, + "maxNesting": 0, + "parameters": 1, + "isAccessor": false, + "isConstructor": true, + "accessedVariables": [ + "_owner" + ], + "accessedOwnAttributes": [ + "_owner" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + } + ], + "protectedMembers": 0, + "overriddenMethods": [], + "usedParentMembers": [] + }, + { + "fqn": "TinyGreeter.Program", + "namespace": "TinyGreeter", + "name": "Program", + "sourceFile": "Program.cs", + "loc": 8, + "parent": null, + "attributes": [], + "dependencies": [ + { + "to": "TinyGreeter.Greeter", + "count": 3 + } + ], + "methods": [ + { + "name": "Main", + "signature": "Main(string[])", + "loc": 5, + "cyclo": 1, + "maxNesting": 0, + "parameters": 1, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [ + "greeter" + ], + "accessedOwnAttributes": [], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [ + "System.Console.WriteLine(string)", + "TinyGreeter.Greeter.Greet(string)" + ], + "calledForeignMethodClasses": [ + "System.Console", + "TinyGreeter.Greeter" + ] + } + ], + "protectedMembers": 0, + "overriddenMethods": [], + "usedParentMembers": [] + } + ] +} \ No newline at end of file diff --git a/codebase-graph-builder/src/test/resources/csharp/two-projects-facts.json b/codebase-graph-builder/src/test/resources/csharp/two-projects-facts.json new file mode 100644 index 00000000..d5a6ae65 --- /dev/null +++ b/codebase-graph-builder/src/test/resources/csharp/two-projects-facts.json @@ -0,0 +1,251 @@ +{ + "schemaVersion": 1, + "root": ".", + "namespaces": [ + "Alpha", + "Beta" + ], + "types": [ + { + "fqn": "Alpha.AlphaService", + "namespace": "Alpha", + "name": "AlphaService", + "sourceFile": "Alpha/AlphaService.cs", + "loc": 6, + "parent": null, + "attributes": [ + { + "name": "_store", + "isPublic": false + } + ], + "dependencies": [ + { + "to": "Beta.BetaStore", + "count": 3 + } + ], + "methods": [ + { + "name": "Count", + "signature": "Count()", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [ + "_store" + ], + "accessedOwnAttributes": [ + "_store" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [ + "Beta.BetaStore.get_Size()" + ], + "calledForeignMethodClasses": [ + "Beta.BetaStore" + ] + } + ], + "protectedMembers": 0, + "overriddenMethods": [], + "usedParentMembers": [] + }, + { + "fqn": "Beta.BaseStore", + "namespace": "Beta", + "name": "BaseStore", + "sourceFile": "Beta/BaseStore.cs", + "loc": 13, + "parent": null, + "attributes": [ + { + "name": "Capacity", + "isPublic": false + } + ], + "dependencies": [], + "methods": [ + { + "name": "Describe", + "signature": "Describe()", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [], + "accessedOwnAttributes": [], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + }, + { + "name": "Limit", + "signature": "Limit()", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [ + "Capacity" + ], + "accessedOwnAttributes": [ + "Capacity" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + }, + { + "name": "Reset", + "signature": "Reset()", + "loc": 4, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [ + "Capacity" + ], + "accessedOwnAttributes": [ + "Capacity" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + } + ], + "protectedMembers": 3, + "overriddenMethods": [], + "usedParentMembers": [] + }, + { + "fqn": "Beta.BetaStore", + "namespace": "Beta", + "name": "BetaStore", + "sourceFile": "Beta/BetaStore.cs", + "loc": 12, + "parent": "Beta.BaseStore", + "attributes": [ + { + "name": "Size", + "isPublic": false + } + ], + "dependencies": [ + { + "to": "Beta.BaseStore", + "count": 3 + } + ], + "methods": [ + { + "name": "Clear", + "signature": "Clear()", + "loc": 5, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [ + "Size" + ], + "accessedOwnAttributes": [ + "Size" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [ + "Beta.BaseStore.Reset()" + ], + "calledForeignMethodClasses": [ + "Beta.BaseStore" + ] + }, + { + "name": "Limit", + "signature": "Limit()", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": [ + "Capacity" + ], + "accessedOwnAttributes": [], + "accessedForeignAttributes": [ + "Beta.BaseStore.Capacity" + ], + "accessedForeignClasses": [ + "Beta.BaseStore" + ], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + }, + { + "name": "get_Size", + "signature": "get_Size()", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 0, + "isAccessor": true, + "isConstructor": false, + "accessedVariables": [ + "Size" + ], + "accessedOwnAttributes": [ + "Size" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + }, + { + "name": "set_Size", + "signature": "set_Size(int)", + "loc": 1, + "cyclo": 1, + "maxNesting": 0, + "parameters": 1, + "isAccessor": true, + "isConstructor": false, + "accessedVariables": [ + "Size" + ], + "accessedOwnAttributes": [ + "Size" + ], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": [], + "calledForeignMethodClasses": [] + } + ], + "protectedMembers": 0, + "overriddenMethods": [ + "Limit()" + ], + "usedParentMembers": [ + "Capacity", + "Reset" + ] + } + ] +} \ No newline at end of file diff --git a/csharp-fact-extractor/.gitignore b/csharp-fact-extractor/.gitignore new file mode 100644 index 00000000..7538de61 --- /dev/null +++ b/csharp-fact-extractor/.gitignore @@ -0,0 +1,3 @@ +bin/ +obj/ +publish/ diff --git a/csharp-fact-extractor/CSharpFactExtractor.sln b/csharp-fact-extractor/CSharpFactExtractor.sln new file mode 100644 index 00000000..3b014b82 --- /dev/null +++ b/csharp-fact-extractor/CSharpFactExtractor.sln @@ -0,0 +1,56 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpFactExtractor", "src\CSharpFactExtractor.csproj", "{6B39E310-536F-46A8-BFFF-35B0F64840C2}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpFactExtractor.Tests", "tests\CSharpFactExtractor.Tests.csproj", "{B3055D10-62FB-4942-9E60-8AF57C1217C5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Debug|x64.ActiveCfg = Debug|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Debug|x64.Build.0 = Debug|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Debug|x86.ActiveCfg = Debug|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Debug|x86.Build.0 = Debug|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Release|Any CPU.Build.0 = Release|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Release|x64.ActiveCfg = Release|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Release|x64.Build.0 = Release|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Release|x86.ActiveCfg = Release|Any CPU + {6B39E310-536F-46A8-BFFF-35B0F64840C2}.Release|x86.Build.0 = Release|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Debug|x64.ActiveCfg = Debug|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Debug|x64.Build.0 = Debug|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Debug|x86.ActiveCfg = Debug|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Debug|x86.Build.0 = Debug|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Release|Any CPU.Build.0 = Release|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Release|x64.ActiveCfg = Release|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Release|x64.Build.0 = Release|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Release|x86.ActiveCfg = Release|Any CPU + {B3055D10-62FB-4942-9E60-8AF57C1217C5}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {6B39E310-536F-46A8-BFFF-35B0F64840C2} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {B3055D10-62FB-4942-9E60-8AF57C1217C5} = {0AB3BF05-4346-4AA6-1389-037BE0695223} + EndGlobalSection +EndGlobal diff --git a/csharp-fact-extractor/docs/facts-contract.md b/csharp-fact-extractor/docs/facts-contract.md new file mode 100644 index 00000000..44ed533c --- /dev/null +++ b/csharp-fact-extractor/docs/facts-contract.md @@ -0,0 +1,70 @@ +# Facts contract, schema version 1 + +One JSON file per run. Paths are relative to `root`, with forward slashes. + +```json +{ + "schemaVersion": 1, + "root": "/absolute/path/to/repo", + "namespaces": ["TinyGreeter"], + "types": [ + { + "fqn": "TinyGreeter.Greeter", + "namespace": "TinyGreeter", + "name": "Greeter", + "sourceFile": "Greeter.cs", + "loc": 21, + "parent": null, + "attributes": [{ "name": "_store", "isPublic": false }], + "dependencies": [{ "to": "TinyGreeter.MessageStore", "count": 4 }], + "methods": [ + { + "name": "Greet", + "signature": "Greet(string)", + "loc": 8, + "cyclo": 2, + "maxNesting": 1, + "parameters": 1, + "isAccessor": false, + "isConstructor": false, + "accessedVariables": ["Punctuation", "_store"], + "accessedOwnAttributes": ["Punctuation", "_store"], + "accessedForeignAttributes": [], + "accessedForeignClasses": [], + "calledForeignMethods": ["TinyGreeter.MessageStore.Find(string)"], + "calledForeignMethodClasses": ["TinyGreeter.MessageStore"] + } + ], + "protectedMembers": 0, + "overriddenMethods": [], + "usedParentMembers": [] + } + ] +} +``` + +The numbers in this example are illustrative. The authoritative example is the +checked-in file `codebase-graph-builder/src/test/resources/csharp/tiny-greeter-facts.json`, +which is the extractor's real output for `fixtures/TinyGreeter`. + +Rules +- `fqn` = namespace + "." + name. Nested types: `Outer$Inner`. Generic arity is dropped (`Box` is `Box`), unless a type with the same name is declared beside it in the same project: then the generic one is named the .NET way, so `Result` and `Result` are `Result` and ``Result`1``. +- A full name declared in more than one project appears once (the first project wins) and the extractor prints a warning. +- Types outside any namespace are left out, and so are delegates. +- `dependencies` lists only types declared in the analysed source, never framework or package types, never the type itself. +- `count` = how many names in the type's code resolve to the other type or to one of its members. +- `parent` = fqn of the base class if it is in the analysed source, else null. +- Method `signature` = name + type parameters, if any, as `` + "(" + parameter type names joined by "," + ")". A parameter passed by `ref`, `out` or `in` keeps that word before its type. So `Save()` and `Save()`, `Add(int)` and `Add(ref int)`, are four different signatures. A static constructor is `static Name()`, so it does not share the instance constructor's `Name()`. +- `calledForeignMethods` entries are the callee's type fqn + "." + its signature. +- `accessedVariables` = names of fields used (own or foreign), own auto-properties used, and locals declared in the method. Cohesion is computed from it. +- A call to a method of any other type counts as a foreign call, framework types included. Constructor calls do not. +- An auto-property is one private attribute plus `get_X` / `set_X` accessor methods. Using your own auto-property is using that attribute; using another type's property is a call to its accessor: the setter when it is assigned, the getter when it is read, both for `+=` and `++`. In an object initializer, `Child = { ... }` is a read: the existing value is filled, not replaced. +- Complexity: 1, plus one for each `if`, loop, non-default `case` or switch-expression arm, `catch`, `&&`, `||` and `?:`. Nesting counts `if`, loops and `catch`. +- `protectedMembers` = how many of the type's own fields, properties and methods are protected. `overriddenMethods` = signatures of its methods marked `override`. `usedParentMembers` = names of its base class's members that it uses. "Base class" means one declared in the analysed source. These feed Refused Parent Bequest and Tradition Breaker. +- Types declared only in generated files are left out: anything under `obj` or `bin`, and files ending `.g.cs`, `.g.i.cs`, `.Designer.cs` or `.generated.cs`. They are not listed as dependencies either. +- A partial method is measured over both of its halves, so its body counts even when it is in another file. +- The extractor takes one solution, or several project files when the repository has no solution at its root. +- Test projects are left out unless `--include-tests` is given. A test project is one whose name ends in `Test`, `Tests`, `Spec` or `Specs` (a trailing target framework such as `(net8.0)` is ignored), or that references xUnit, NUnit, MSTest or the .NET test platform. +- A primary constructor's `loc` is the length of its parameter list, not of the type that declares it. +- Raw facts only. ATFD, TCC and WMC are computed on the Java side. +- A reader must ignore fields it does not know and reject a `schemaVersion` it does not know. diff --git a/csharp-fact-extractor/fixtures/Members/Cache.cs b/csharp-fact-extractor/fixtures/Members/Cache.cs new file mode 100644 index 00000000..44de9891 --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/Cache.cs @@ -0,0 +1,16 @@ +namespace Members; + +/// A static constructor and an instance constructor: both are written "Cache()". +public class Cache +{ + private static int size; + + static Cache() + { + if (Environment.ProcessorCount > 1) { size = 2; } + } + + public Cache() { } + + public int Size() => size; +} diff --git a/csharp-fact-extractor/fixtures/Members/GeneratedHelper.g.cs b/csharp-fact-extractor/fixtures/Members/GeneratedHelper.g.cs new file mode 100644 index 00000000..1935c70e --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/GeneratedHelper.g.cs @@ -0,0 +1,6 @@ +namespace Members; + +public class GeneratedHelper +{ + public static int Answer() => 42; +} diff --git a/csharp-fact-extractor/fixtures/Members/Holder.cs b/csharp-fact-extractor/fixtures/Members/Holder.cs new file mode 100644 index 00000000..36d4fcea --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/Holder.cs @@ -0,0 +1,43 @@ +namespace Members; + +public class Node +{ + private int value; + + public int Value + { + get { return value; } + set { this.value = value; } + } +} + +public class Holder +{ + private Node child = new Node(); + private List items = new List(); + + public Node Child + { + get { return child; } + set { child = value; } + } + + public List Items + { + get { return items; } + set { items = value; } + } +} + +/// Three ways to write a property inside an object initializer. Only the middle one replaces it. +public class Nester +{ + /// `Child = { ... }` reads the existing child and sets a value on it. + public Holder Nested() { return new Holder { Child = { Value = 1 } }; } + + /// `Child = new Node()` replaces the child. + public Holder Replaced() { return new Holder { Child = new Node() }; } + + /// `Items = { ... }` reads the existing list and adds to it. + public Holder Filled() { return new Holder { Items = { 1, 2 } }; } +} diff --git a/csharp-fact-extractor/fixtures/Members/Members.csproj b/csharp-fact-extractor/fixtures/Members/Members.csproj new file mode 100644 index 00000000..b7601447 --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/Members.csproj @@ -0,0 +1,9 @@ + + + + net10.0 + enable + enable + + + diff --git a/csharp-fact-extractor/fixtures/Members/Order.cs b/csharp-fact-extractor/fixtures/Members/Order.cs new file mode 100644 index 00000000..b92b9fd3 --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/Order.cs @@ -0,0 +1,23 @@ +namespace Members; + +public class Order +{ + private int total; + + public int Total + { + get { return total; } + set { total = value; } + } +} + +public class Clerk +{ + public void Write(Order order) { order.Total = 5; } + + public int Read(Order order) { return order.Total; } + + public void Bump(Order order) { order.Total += 1; } + + public Order Create() { return new Order { Total = 2 }; } +} diff --git a/csharp-fact-extractor/fixtures/Members/Overloads.cs b/csharp-fact-extractor/fixtures/Members/Overloads.cs new file mode 100644 index 00000000..230742db --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/Overloads.cs @@ -0,0 +1,27 @@ +namespace Members; + +/// Legal overloads that differ only by a type parameter or by how a parameter is passed. +public class Overloads +{ + public int Saved; + + public void Save() { Saved++; } + + public void Save() { if (typeof(T) == typeof(int)) { Saved++; } } + + public void Add(int amount) { Saved += amount; } + + public void Add(ref int amount) { if (amount > 0) { amount = 0; } } + + public void Read(out int amount) { amount = Saved; } +} + +public class OverloadCaller +{ + public void Go(Overloads overloads) + { + int amount = 1; + overloads.Save(); + overloads.Add(ref amount); + } +} diff --git a/csharp-fact-extractor/fixtures/Members/UsesGenerated.cs b/csharp-fact-extractor/fixtures/Members/UsesGenerated.cs new file mode 100644 index 00000000..a1c1026e --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/UsesGenerated.cs @@ -0,0 +1,6 @@ +namespace Members; + +public class UsesGenerated +{ + public int Ask() => GeneratedHelper.Answer(); +} diff --git a/csharp-fact-extractor/fixtures/Members/Validator.Rules.cs b/csharp-fact-extractor/fixtures/Members/Validator.Rules.cs new file mode 100644 index 00000000..c27451d8 --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/Validator.Rules.cs @@ -0,0 +1,19 @@ +namespace Members; + +public partial class Validator +{ + private int notes; + + public partial int Check(int amount) + { + if (amount > 10) return 2; + if (amount > 0) return 1; + Note(amount); + return 0; + } + + partial void Note(int amount) + { + if (amount < 0) { notes++; } + } +} diff --git a/csharp-fact-extractor/fixtures/Members/Validator.cs b/csharp-fact-extractor/fixtures/Members/Validator.cs new file mode 100644 index 00000000..1a7f8638 --- /dev/null +++ b/csharp-fact-extractor/fixtures/Members/Validator.cs @@ -0,0 +1,9 @@ +namespace Members; + +/// The declarations; the bodies are in Validator.Rules.cs. +public partial class Validator +{ + public partial int Check(int amount); + + partial void Note(int amount); +} diff --git a/csharp-fact-extractor/fixtures/Shapes/Counter.cs b/csharp-fact-extractor/fixtures/Shapes/Counter.cs new file mode 100644 index 00000000..e17a1bf5 --- /dev/null +++ b/csharp-fact-extractor/fixtures/Shapes/Counter.cs @@ -0,0 +1,18 @@ +namespace Shapes; + +/// A class with a primary constructor: the constructor is the parameter list, not the whole class. +public class Counter(int start) +{ + private int _value = start; + + public int Next() + { + _value++; + return _value; + } + + public Result Current() + { + return new Result { Value = _value }; + } +} diff --git a/csharp-fact-extractor/fixtures/Shapes/Result.cs b/csharp-fact-extractor/fixtures/Shapes/Result.cs new file mode 100644 index 00000000..c385d69c --- /dev/null +++ b/csharp-fact-extractor/fixtures/Shapes/Result.cs @@ -0,0 +1,22 @@ +namespace Shapes; + +/// Two types with one name: a plain one and a generic one. Both must survive extraction. +public class Result +{ + public bool Ok { get; set; } + + public string Describe() => Ok ? "ok" : "failed"; +} + +public class Result +{ + public T? Value { get; set; } + + public bool HasValue() => Value is not null; +} + +/// A generic type with no namesake keeps its plain name. +public class Box +{ + public T? Content { get; set; } +} diff --git a/csharp-fact-extractor/fixtures/Shapes/Shapes.csproj b/csharp-fact-extractor/fixtures/Shapes/Shapes.csproj new file mode 100644 index 00000000..b7601447 --- /dev/null +++ b/csharp-fact-extractor/fixtures/Shapes/Shapes.csproj @@ -0,0 +1,9 @@ + + + + net10.0 + enable + enable + + + diff --git a/csharp-fact-extractor/fixtures/TinyGreeter/Greeter.cs b/csharp-fact-extractor/fixtures/TinyGreeter/Greeter.cs new file mode 100644 index 00000000..aefc1516 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TinyGreeter/Greeter.cs @@ -0,0 +1,22 @@ +namespace TinyGreeter; + +public class Greeter +{ + private readonly MessageStore _store; + + public Greeter() + { + _store = new MessageStore(this); + } + + public string Punctuation { get; set; } = "!"; + + public string Greet(string name) + { + if (string.IsNullOrEmpty(name)) + { + return _store.Find("hello") + Punctuation; + } + return _store.Find("hello") + " " + name + Punctuation; + } +} diff --git a/csharp-fact-extractor/fixtures/TinyGreeter/MessageStore.cs b/csharp-fact-extractor/fixtures/TinyGreeter/MessageStore.cs new file mode 100644 index 00000000..21c1ce67 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TinyGreeter/MessageStore.cs @@ -0,0 +1,16 @@ +namespace TinyGreeter; + +public class MessageStore +{ + private readonly Greeter _owner; + + public MessageStore(Greeter owner) + { + _owner = owner; + } + + public string Find(string key) + { + return key == "hello" ? "Hello" : key; + } +} diff --git a/csharp-fact-extractor/fixtures/TinyGreeter/Program.cs b/csharp-fact-extractor/fixtures/TinyGreeter/Program.cs new file mode 100644 index 00000000..421b2172 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TinyGreeter/Program.cs @@ -0,0 +1,10 @@ +namespace TinyGreeter; + +public class Program +{ + public static void Main(string[] args) + { + var greeter = new Greeter(); + System.Console.WriteLine(greeter.Greet("world")); + } +} diff --git a/csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.csproj b/csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.sln b/csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.sln new file mode 100644 index 00000000..f1b9e121 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TinyGreeter/TinyGreeter.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyGreeter", "TinyGreeter.csproj", "{22E09B72-6F97-4AE7-B3AB-4CDD3632842D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Debug|x64.ActiveCfg = Debug|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Debug|x64.Build.0 = Debug|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Debug|x86.ActiveCfg = Debug|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Debug|x86.Build.0 = Debug|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Release|Any CPU.Build.0 = Release|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Release|x64.ActiveCfg = Release|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Release|x64.Build.0 = Release|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Release|x86.ActiveCfg = Release|Any CPU + {22E09B72-6F97-4AE7-B3AB-4CDD3632842D}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/csharp-fact-extractor/fixtures/TwoProjects/Alpha/Alpha.csproj b/csharp-fact-extractor/fixtures/TwoProjects/Alpha/Alpha.csproj new file mode 100644 index 00000000..ccc12f93 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TwoProjects/Alpha/Alpha.csproj @@ -0,0 +1,13 @@ + + + + + + + + net10.0 + enable + enable + + + diff --git a/csharp-fact-extractor/fixtures/TwoProjects/Alpha/AlphaService.cs b/csharp-fact-extractor/fixtures/TwoProjects/Alpha/AlphaService.cs new file mode 100644 index 00000000..1da59284 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TwoProjects/Alpha/AlphaService.cs @@ -0,0 +1,8 @@ +namespace Alpha; + +public class AlphaService +{ + private readonly Beta.BetaStore _store = new Beta.BetaStore(); + + public int Count() => _store.Size; +} diff --git a/csharp-fact-extractor/fixtures/TwoProjects/Beta/BaseStore.cs b/csharp-fact-extractor/fixtures/TwoProjects/Beta/BaseStore.cs new file mode 100644 index 00000000..9637f895 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TwoProjects/Beta/BaseStore.cs @@ -0,0 +1,15 @@ +namespace Beta; + +public abstract class BaseStore +{ + protected int Capacity; + + protected virtual string Describe() => "base"; + + public virtual int Limit() => Capacity; + + protected void Reset() + { + Capacity = 0; + } +} diff --git a/csharp-fact-extractor/fixtures/TwoProjects/Beta/Beta.csproj b/csharp-fact-extractor/fixtures/TwoProjects/Beta/Beta.csproj new file mode 100644 index 00000000..b7601447 --- /dev/null +++ b/csharp-fact-extractor/fixtures/TwoProjects/Beta/Beta.csproj @@ -0,0 +1,9 @@ + + + + net10.0 + enable + enable + + + diff --git a/csharp-fact-extractor/fixtures/TwoProjects/Beta/BetaStore.cs b/csharp-fact-extractor/fixtures/TwoProjects/Beta/BetaStore.cs new file mode 100644 index 00000000..31bc999b --- /dev/null +++ b/csharp-fact-extractor/fixtures/TwoProjects/Beta/BetaStore.cs @@ -0,0 +1,14 @@ +namespace Beta; + +public class BetaStore : BaseStore +{ + public int Size { get; set; } + + public override int Limit() => Capacity + 1; + + public void Clear() + { + Reset(); + Size = 0; + } +} diff --git a/csharp-fact-extractor/fixtures/TwoProjects/Beta/Generated.g.cs b/csharp-fact-extractor/fixtures/TwoProjects/Beta/Generated.g.cs new file mode 100644 index 00000000..f93362ab --- /dev/null +++ b/csharp-fact-extractor/fixtures/TwoProjects/Beta/Generated.g.cs @@ -0,0 +1,7 @@ +// +namespace Beta; + +public class GeneratedThing +{ + public int X { get; set; } +} diff --git a/csharp-fact-extractor/src/CSharpFactExtractor.csproj b/csharp-fact-extractor/src/CSharpFactExtractor.csproj new file mode 100644 index 00000000..595987a5 --- /dev/null +++ b/csharp-fact-extractor/src/CSharpFactExtractor.csproj @@ -0,0 +1,18 @@ + + + + Exe + + net10.0 + enable + enable + + + + + + + + + + diff --git a/csharp-fact-extractor/src/DependencyWalker.cs b/csharp-fact-extractor/src/DependencyWalker.cs new file mode 100644 index 00000000..369d0b93 --- /dev/null +++ b/csharp-fact-extractor/src/DependencyWalker.cs @@ -0,0 +1,36 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace CSharpFactExtractor; + +public static class DependencyWalker +{ + /// Every name inside the type's code that resolves to another source type, or to a member of one. + public static List Collect(INamedTypeSymbol type, Compilation compilation) + { + string self = TypeNames.Fqn(type); + var counts = new Dictionary(); + foreach (SyntaxReference reference in TypeNames.SourceReferences(type)) + { + SyntaxNode declaration = reference.GetSyntax(); + SemanticModel model = compilation.GetSemanticModel(reference.SyntaxTree); + // Stay out of nested type declarations: their names belong to the nested type, not this one. + var names = declaration + .DescendantNodes(node => node == declaration || node is not BaseTypeDeclarationSyntax) + .OfType(); + foreach (SimpleNameSyntax name in names) + { + ISymbol? symbol = model.GetSymbolInfo(name).Symbol; + INamedTypeSymbol? target = symbol as INamedTypeSymbol ?? symbol?.ContainingType; + if (target is null || target.TypeKind == TypeKind.Delegate || !TypeNames.IsInSource(target)) continue; + // A type that exists only in generated files is not in the type list, so it is not a dependency either. + if (TypeNames.IsGeneratedOnly(target.OriginalDefinition)) continue; + if (TypeNames.Namespace(target).Length == 0) continue; + string fqn = TypeNames.Fqn(target); + if (fqn == self) continue; + counts[fqn] = counts.GetValueOrDefault(fqn) + 1; + } + } + return counts.OrderBy(c => c.Key, StringComparer.Ordinal).Select(c => new DependencyFacts(c.Key, c.Value)).ToList(); + } +} diff --git a/csharp-fact-extractor/src/Extractor.cs b/csharp-fact-extractor/src/Extractor.cs new file mode 100644 index 00000000..86426139 --- /dev/null +++ b/csharp-fact-extractor/src/Extractor.cs @@ -0,0 +1,116 @@ +using Microsoft.Build.Locator; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.MSBuild; + +namespace CSharpFactExtractor; + +public static class Extractor +{ + private static readonly object RegistrationLock = new(); + + public static Task RunAsync(string solutionOrProject, string root, bool excludeTests) => + RunAsync(new[] { solutionOrProject }, root, excludeTests); + + /// One solution, or several project files when the repository has no solution at its root. + public static async Task RunAsync(IReadOnlyList targets, string root, bool excludeTests) + { + lock (RegistrationLock) + { + if (!MSBuildLocator.IsRegistered) MSBuildLocator.RegisterDefaults(); + } + return await ExtractAsync(targets, root, excludeTests); + } + + // Separate method: nothing that touches Microsoft.Build types may be JIT-compiled before the locator has run. + private static async Task ExtractAsync(IReadOnlyList targets, string root, bool excludeTests) + { + using var workspace = MSBuildWorkspace.Create(); + foreach (string target in targets.Select(Path.GetFullPath)) + { + if (!target.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) + { + await workspace.OpenSolutionAsync(target); + } + // A project already pulled in as another project's reference is not opened twice. + else if (!workspace.CurrentSolution.Projects.Any(p => string.Equals(p.FilePath, target, StringComparison.OrdinalIgnoreCase))) + { + await workspace.OpenProjectAsync(target); + } + } + Solution solution = workspace.CurrentSolution; + + // Unrestored packages and broken project files show up here; without them types stop resolving. + var failures = workspace.Diagnostics.Where(d => d.Kind == WorkspaceDiagnosticKind.Failure).ToList(); + if (failures.Count > 0) + { + Console.Error.WriteLine($"warning: MSBuild reported {failures.Count} problem(s) loading the code; results may be incomplete. Has `dotnet restore` been run?"); + foreach (WorkspaceDiagnostic failure in failures.Take(3)) Console.Error.WriteLine(" " + failure.Message.Split('\n')[0]); + } + + var types = new Dictionary(); + var duplicates = new SortedSet(StringComparer.Ordinal); + foreach (Project project in solution.Projects) + { + if (excludeTests && TestProjects.IsTestProject(project.Name, project.MetadataReferences.Select(r => r.Display ?? ""))) continue; + Compilation? compilation = await project.GetCompilationAsync(); + if (compilation is null) continue; + foreach (INamedTypeSymbol type in AllTypes(compilation.Assembly.GlobalNamespace)) + { + if (type.TypeKind == TypeKind.Delegate || TypeNames.Namespace(type).Length == 0) continue; + string fqn = TypeNames.Fqn(type); + // The same full name in two projects: the first one is kept, and the run says so below. + if (types.ContainsKey(fqn)) + { + duplicates.Add(fqn); + continue; + } + MemberFacts members = MemberWalker.Collect(type, compilation); + types[fqn] = new TypeFacts( + fqn, TypeNames.Namespace(type), type.Name, LargestFile(type, root), Loc(type), + Parent(type), members.Attributes, DependencyWalker.Collect(type, compilation), members.Methods, + members.ProtectedMembers, members.OverriddenMethods, members.UsedParentMembers); + } + } + if (duplicates.Count > 0) + { + Console.Error.WriteLine($"warning: {duplicates.Count} type name(s) are declared in more than one project; only the first of each was analysed."); + foreach (string duplicate in duplicates.Take(3)) Console.Error.WriteLine(" " + duplicate); + } + var list = types.Values.OrderBy(t => t.Fqn, StringComparer.Ordinal).ToList(); + return new Facts(1, root, list.Select(t => t.Namespace).Distinct().OrderBy(n => n, StringComparer.Ordinal).ToList(), list); + } + + private static IEnumerable AllTypes(INamespaceSymbol ns) + { + foreach (INamedTypeSymbol t in ns.GetTypeMembers()) + foreach (INamedTypeSymbol x in WithNested(t)) yield return x; + foreach (INamespaceSymbol child in ns.GetNamespaceMembers()) + foreach (INamedTypeSymbol x in AllTypes(child)) yield return x; + } + + private static IEnumerable WithNested(INamedTypeSymbol t) + { + if (!TypeNames.IsInSource(t) || t.IsImplicitlyDeclared) yield break; + // A type that exists only in generated files is left out, along with anything nested in it. + if (!TypeNames.SourceReferences(t).Any()) yield break; + yield return t; + foreach (INamedTypeSymbol n in t.GetTypeMembers()) + foreach (INamedTypeSymbol x in WithNested(n)) yield return x; + } + + private static string LargestFile(INamedTypeSymbol type, string root) => + TypeNames.SourceReferences(type) + .OrderByDescending(r => r.Span.Length) + .Select(r => Path.GetRelativePath(root, r.SyntaxTree.FilePath).Replace('\\', '/')) + .First(); + + private static int Loc(INamedTypeSymbol type) => + TypeNames.SourceReferences(type).Sum(r => + { + var span = r.SyntaxTree.GetLineSpan(r.Span); + return span.EndLinePosition.Line - span.StartLinePosition.Line + 1; + }); + + private static string? Parent(INamedTypeSymbol type) => + type.BaseType is { } b && TypeNames.IsInSource(b) ? TypeNames.Fqn(b) : null; +} diff --git a/csharp-fact-extractor/src/Facts.cs b/csharp-fact-extractor/src/Facts.cs new file mode 100644 index 00000000..d93921de --- /dev/null +++ b/csharp-fact-extractor/src/Facts.cs @@ -0,0 +1,19 @@ +namespace CSharpFactExtractor; + +public record Facts(int SchemaVersion, string Root, List Namespaces, List Types); + +public record TypeFacts( + string Fqn, string Namespace, string Name, string SourceFile, int Loc, string? Parent, + List Attributes, List Dependencies, List Methods, + int ProtectedMembers, List OverriddenMethods, List UsedParentMembers); + +public record AttributeFacts(string Name, bool IsPublic); + +public record DependencyFacts(string To, int Count); + +public record MethodFacts( + string Name, string Signature, int Loc, int Cyclo, int MaxNesting, int Parameters, + bool IsAccessor, bool IsConstructor, + List AccessedVariables, + List AccessedOwnAttributes, List AccessedForeignAttributes, List AccessedForeignClasses, + List CalledForeignMethods, List CalledForeignMethodClasses); diff --git a/csharp-fact-extractor/src/MemberWalker.cs b/csharp-fact-extractor/src/MemberWalker.cs new file mode 100644 index 00000000..f7953e26 --- /dev/null +++ b/csharp-fact-extractor/src/MemberWalker.cs @@ -0,0 +1,311 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace CSharpFactExtractor; + +/// Attributes, methods and per-method facts for one type. +/// Counting follows the Java reader (ComplexityCalculator and MetricsVisitorLogic) so one set of thresholds fits both languages. +public record MemberFacts( + List Attributes, List Methods, + int ProtectedMembers, List OverriddenMethods, List UsedParentMembers); + +public static class MemberWalker +{ + public static MemberFacts Collect(INamedTypeSymbol type, Compilation compilation) + { + // The base class, when it is part of the analysed code: its members used here feed Refused Parent Bequest. + string? parent = type.BaseType is { } b && TypeNames.IsInSource(b) ? TypeNames.Fqn(b) : null; + var usedParentMembers = new SortedSet(StringComparer.Ordinal); + var overridden = new List(); + int protectedMembers = 0; + var attributes = new List(); + var methods = new List(); + var autoProperties = new HashSet(SymbolEqualityComparer.Default); + + if (type.TypeKind != TypeKind.Enum) + { + foreach (ISymbol member in type.GetMembers()) + { + if (TypeNames.IsGeneratedOnly(member)) continue; + if (IsProtected(member) && member is IFieldSymbol { IsImplicitlyDeclared: false } or IPropertySymbol) protectedMembers++; + switch (member) + { + // An auto-property is a private attribute plus its accessor methods, as a Java field with getter and setter. + case IFieldSymbol { AssociatedSymbol: IPropertySymbol property }: + autoProperties.Add(property); + attributes.Add(new AttributeFacts(property.Name, false)); + break; + case IFieldSymbol { IsImplicitlyDeclared: false } field: + attributes.Add(new AttributeFacts(field.Name, field.DeclaredAccessibility == Accessibility.Public)); + break; + } + } + } + + foreach (IMethodSymbol method in type.GetMembers().OfType()) + { + if (method.IsImplicitlyDeclared && method.AssociatedSymbol is not IPropertySymbol) continue; + if (!IsCounted(method.MethodKind)) continue; + if (TypeNames.IsGeneratedOnly(method)) continue; + if (method.MethodKind == MethodKind.Ordinary && IsProtected(method)) protectedMembers++; + MethodFacts facts = Describe(method, type, compilation, autoProperties, parent, usedParentMembers); + if (method.IsOverride && method.MethodKind == MethodKind.Ordinary) overridden.Add(facts.Signature); + methods.Add(facts); + } + + return new MemberFacts( + attributes.OrderBy(a => a.Name, StringComparer.Ordinal).ToList(), + methods.OrderBy(m => m.Signature, StringComparer.Ordinal).ToList(), + protectedMembers, + overridden.OrderBy(s => s, StringComparer.Ordinal).ToList(), + usedParentMembers.ToList()); + } + + private static bool IsProtected(ISymbol symbol) => symbol.DeclaredAccessibility is + Accessibility.Protected or Accessibility.ProtectedOrInternal or Accessibility.ProtectedAndInternal; + + private static bool IsCounted(MethodKind kind) => kind is + MethodKind.Ordinary or MethodKind.Constructor or MethodKind.StaticConstructor or MethodKind.Destructor or + MethodKind.UserDefinedOperator or MethodKind.Conversion or MethodKind.PropertyGet or MethodKind.PropertySet or + MethodKind.ExplicitInterfaceImplementation; + + private static MethodFacts Describe( + IMethodSymbol method, INamedTypeSymbol type, Compilation compilation, HashSet autoProperties, + string? parent, SortedSet usedParentMembers) + { + string self = TypeNames.Fqn(type); + var variables = new SortedSet(StringComparer.Ordinal); + var ownAttributes = new SortedSet(StringComparer.Ordinal); + var foreignAttributes = new SortedSet(StringComparer.Ordinal); + var foreignClasses = new SortedSet(StringComparer.Ordinal); + var foreignMethods = new SortedSet(StringComparer.Ordinal); + var foreignMethodClasses = new SortedSet(StringComparer.Ordinal); + var complexity = new ComplexityWalker(); + int loc = 0, statements = 0; + bool hasBody = false; + + // A partial method's declaration has no body; the body is in its other half, possibly in another file. + IEnumerable references = method.PartialImplementationPart is { } implementation + ? TypeNames.SourceReferences(method).Concat(TypeNames.SourceReferences(implementation)) + : TypeNames.SourceReferences(method); + + foreach (SyntaxReference reference in references) + { + SyntaxNode node = reference.GetSyntax(); + // A primary constructor is declared by the type itself; its length is its parameter list, not the whole type. + SyntaxNode measured = node is TypeDeclarationSyntax { ParameterList: { } parameters } ? parameters : node; + var span = measured.SyntaxTree.GetLineSpan(measured.Span); + loc += span.EndLinePosition.Line - span.StartLinePosition.Line + 1; + + // The part that runs: a block or an expression body. An auto-accessor (`get;`) has neither. + SyntaxNode? body = node switch + { + BaseMethodDeclarationSyntax m => (SyntaxNode?)m.Body ?? m.ExpressionBody, + AccessorDeclarationSyntax a => (SyntaxNode?)a.Body ?? a.ExpressionBody, + ArrowExpressionClauseSyntax arrow => arrow, + _ => null, + }; + if (body is null) continue; + hasBody = true; + statements += body is BlockSyntax block ? block.Statements.Count : 1; + complexity.Visit(body); + + SemanticModel model = compilation.GetSemanticModel(node.SyntaxTree); + // A constructor's `: base(...)` / `: this(...)` call is part of what it runs. + IEnumerable scope = node is ConstructorDeclarationSyntax { Initializer: { } initializer } + ? body.DescendantNodesAndSelf().Concat(initializer.DescendantNodesAndSelf()) + : body.DescendantNodesAndSelf(); + + foreach (SyntaxNode n in scope) + { + switch (n) + { + // Locals count as accessed variables, as in the Java reader. + case VariableDeclaratorSyntax local when local.Parent?.Parent is LocalDeclarationStatementSyntax or ForStatementSyntax or UsingStatementSyntax: + variables.Add(local.Identifier.ValueText); + break; + case ForEachStatementSyntax each: + variables.Add(each.Identifier.ValueText); + break; + case SingleVariableDesignationSyntax designation: + variables.Add(designation.Identifier.ValueText); + break; + case SimpleNameSyntax name: + Classify(model.GetSymbolInfo(name).Symbol, name); + break; + } + } + } + + void Classify(ISymbol? symbol, SimpleNameSyntax name) + { + switch (symbol) + { + case IFieldSymbol field when field.ContainingType is { } owner: + if (parent != null && TypeNames.Fqn(owner) == parent) usedParentMembers.Add(field.Name); + variables.Add(field.Name); + if (TypeNames.Fqn(owner) == self) ownAttributes.Add(field.Name); + else + { + foreignClasses.Add(TypeNames.Fqn(owner)); + foreignAttributes.Add(TypeNames.Fqn(owner) + "." + field.Name); + } + break; + + case IPropertySymbol property when property.ContainingType is { } owner: + if (parent != null && TypeNames.Fqn(owner) == parent) usedParentMembers.Add(property.Name); + if (TypeNames.Fqn(owner) == self) + { + // Using your own auto-property is using the attribute behind it. + if (autoProperties.Contains(property.OriginalDefinition)) + { + variables.Add(property.Name); + ownAttributes.Add(property.Name); + } + } + else + { + // Someone else's property is a call to its accessor, as a Java getter or setter is. + foreach (IMethodSymbol accessor in AccessorsUsed(property, name)) + { + foreignMethods.Add(TypeNames.Fqn(owner) + "." + TypeNames.Signature(accessor.OriginalDefinition)); + foreignMethodClasses.Add(TypeNames.Fqn(owner)); + } + } + break; + + case IMethodSymbol called when called.MethodKind is MethodKind.Ordinary or MethodKind.ReducedExtension: + IMethodSymbol definition = (called.ReducedFrom ?? called).OriginalDefinition; + if (definition.ContainingType is { } declaring && TypeNames.Fqn(declaring) != self) + { + if (parent != null && TypeNames.Fqn(declaring) == parent) usedParentMembers.Add(definition.Name); + foreignMethods.Add(TypeNames.Fqn(declaring) + "." + TypeNames.Signature(definition)); + foreignMethodClasses.Add(TypeNames.Fqn(declaring)); + } + break; + } + } + + bool isPropertyAccessor = method.MethodKind is MethodKind.PropertyGet or MethodKind.PropertySet; + // An auto-accessor (`get;` / `set;`) has no body but does touch the attribute behind it, as a Java getter touches its field. + if (isPropertyAccessor && !hasBody && method.AssociatedSymbol is IPropertySymbol own && autoProperties.Contains(own)) + { + variables.Add(own.Name); + ownAttributes.Add(own.Name); + } + return new MethodFacts( + method.MethodKind is MethodKind.Constructor or MethodKind.StaticConstructor ? type.Name : method.Name, + SignatureOf(method, type), + Math.Max(loc, 1), + complexity.Cyclomatic, + complexity.MaxNesting, + method.Parameters.Length, + isPropertyAccessor || LooksLikeAnAccessor(method.Name, hasBody, statements), + method.MethodKind is MethodKind.Constructor or MethodKind.StaticConstructor, + variables.ToList(), ownAttributes.ToList(), foreignAttributes.ToList(), foreignClasses.ToList(), + foreignMethods.ToList(), foreignMethodClasses.ToList()); + } + + /// Constructors are named after their type (Roslyn calls them ".ctor"). + private static string SignatureOf(IMethodSymbol method, INamedTypeSymbol type) + { + string signature = TypeNames.Signature(method); + return method.MethodKind switch + { + MethodKind.Constructor => type.Name + signature[signature.IndexOf('(')..], + // Kept apart from the parameterless instance constructor, which is also written "Name()". + MethodKind.StaticConstructor => "static " + type.Name + signature[signature.IndexOf('(')..], + _ => signature, + }; + } + + /// The setter when the property is assigned, the getter when it is read, both for `+=` and `++`. + /// In an object initializer, `Child = { ... }` reads the property and fills what it returns; it is not replaced. + /// Falls back to whichever accessor exists. + private static List AccessorsUsed(IPropertySymbol property, SimpleNameSyntax name) + { + // `order.Total` is the expression that is read or written, not the bare name inside it. + ExpressionSyntax expression = name.Parent is MemberAccessExpressionSyntax access && access.Name == name ? access : name; + bool written = false, read = true; + switch (expression.Parent) + { + case AssignmentExpressionSyntax { Right: InitializerExpressionSyntax } nested when nested.Left == expression: + break; + case AssignmentExpressionSyntax assignment when assignment.Left == expression: + written = true; + read = !assignment.IsKind(SyntaxKind.SimpleAssignmentExpression); + break; + case PrefixUnaryExpressionSyntax prefix when prefix.IsKind(SyntaxKind.PreIncrementExpression) || prefix.IsKind(SyntaxKind.PreDecrementExpression): + case PostfixUnaryExpressionSyntax postfix when postfix.IsKind(SyntaxKind.PostIncrementExpression) || postfix.IsKind(SyntaxKind.PostDecrementExpression): + written = true; + break; + } + IMethodSymbol? getter = property.GetMethod, setter = property.SetMethod; + var used = new List(); + if (read && (getter ?? setter) is { } readWith) used.Add(readWith); + if (written && (setter ?? getter) is { } writtenWith && !used.Contains(writtenWith, SymbolEqualityComparer.Default)) used.Add(writtenWith); + return used; + } + + /// The Java reader's rule: named get/is/set and at most one statement. + private static bool LooksLikeAnAccessor(string name, bool hasBody, int statements) => + hasBody && statements <= 1 && + (name.StartsWith("Get", StringComparison.Ordinal) || name.StartsWith("Is", StringComparison.Ordinal) || name.StartsWith("Set", StringComparison.Ordinal)); + + /// Mirrors ComplexityCalculator.java: +1 for if, each loop, each non-default case, catch, &&, || and ?:. + /// Nesting counts if, loops and catch. + private sealed class ComplexityWalker : CSharpSyntaxWalker + { + private int _nesting; + public int Cyclomatic { get; private set; } = 1; + public int MaxNesting { get; private set; } + + private void Nested(Action visitChildren) + { + Cyclomatic++; + _nesting++; + MaxNesting = Math.Max(MaxNesting, _nesting); + visitChildren(); + _nesting--; + } + + public override void VisitIfStatement(IfStatementSyntax node) => Nested(() => base.VisitIfStatement(node)); + public override void VisitForStatement(ForStatementSyntax node) => Nested(() => base.VisitForStatement(node)); + public override void VisitForEachStatement(ForEachStatementSyntax node) => Nested(() => base.VisitForEachStatement(node)); + public override void VisitForEachVariableStatement(ForEachVariableStatementSyntax node) => Nested(() => base.VisitForEachVariableStatement(node)); + public override void VisitWhileStatement(WhileStatementSyntax node) => Nested(() => base.VisitWhileStatement(node)); + public override void VisitDoStatement(DoStatementSyntax node) => Nested(() => base.VisitDoStatement(node)); + public override void VisitCatchClause(CatchClauseSyntax node) => Nested(() => base.VisitCatchClause(node)); + + public override void VisitCaseSwitchLabel(CaseSwitchLabelSyntax node) + { + Cyclomatic++; + base.VisitCaseSwitchLabel(node); + } + + public override void VisitCasePatternSwitchLabel(CasePatternSwitchLabelSyntax node) + { + Cyclomatic++; + base.VisitCasePatternSwitchLabel(node); + } + + public override void VisitSwitchExpressionArm(SwitchExpressionArmSyntax node) + { + if (node.Pattern is not DiscardPatternSyntax) Cyclomatic++; + base.VisitSwitchExpressionArm(node); + } + + public override void VisitBinaryExpression(BinaryExpressionSyntax node) + { + if (node.IsKind(SyntaxKind.LogicalAndExpression) || node.IsKind(SyntaxKind.LogicalOrExpression)) Cyclomatic++; + base.VisitBinaryExpression(node); + } + + public override void VisitConditionalExpression(ConditionalExpressionSyntax node) + { + Cyclomatic++; + base.VisitConditionalExpression(node); + } + } +} diff --git a/csharp-fact-extractor/src/Program.cs b/csharp-fact-extractor/src/Program.cs new file mode 100644 index 00000000..3065ea57 --- /dev/null +++ b/csharp-fact-extractor/src/Program.cs @@ -0,0 +1,26 @@ +using System.Text.Json; +using CSharpFactExtractor; + +// At least one solution or project must come before the first flag. +if (args.Length < 1 || args[0].StartsWith("--")) +{ + Console.Error.WriteLine("usage: CSharpFactExtractor [ ...] --root --out [--include-tests]"); + return 2; +} +// Everything before the first flag is a solution or project file to analyse. +string[] targets = args.TakeWhile(a => !a.StartsWith("--")).Select(Path.GetFullPath).ToArray(); +string root = ArgAfter("--root") ?? Path.GetDirectoryName(targets[0])!; +string outPath = ArgAfter("--out") ?? "facts.json"; +bool excludeTests = !args.Contains("--include-tests"); + +Facts facts = await Extractor.RunAsync(targets, Path.GetFullPath(root), excludeTests); +var json = JsonSerializer.Serialize(facts, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }); +await File.WriteAllTextAsync(outPath, json); +Console.WriteLine($"wrote {facts.Types.Count} types to {outPath}"); +return 0; + +string? ArgAfter(string flag) +{ + int i = Array.IndexOf(args, flag); + return i >= 0 && i + 1 < args.Length ? args[i + 1] : null; +} diff --git a/csharp-fact-extractor/src/TestProjects.cs b/csharp-fact-extractor/src/TestProjects.cs new file mode 100644 index 00000000..6a927442 --- /dev/null +++ b/csharp-fact-extractor/src/TestProjects.cs @@ -0,0 +1,29 @@ +using System.Text.RegularExpressions; + +namespace CSharpFactExtractor; + +/// Decides which projects hold tests, so they can be left out like src/test is on the Java side. +public static class TestProjects +{ + // "App.Tests", "AppTest", "App.UnitTests", "App.Specs". Case matters: "Contest" is not a test project. + private static readonly Regex TestName = new(@"(Tests?|Specs?)$", RegexOptions.Compiled); + + // A project loaded once per target framework is named "App.Tests(net8.0)". + private static readonly Regex TargetFrameworkSuffix = new(@"\([^()]*\)$", RegexOptions.Compiled); + + private static readonly string[] TestFrameworkAssemblies = + { + "xunit.", "nunit.framework", "Microsoft.VisualStudio.TestPlatform.", "Microsoft.TestPlatform.", "MSTest.", "TUnit.", + }; + + /// True when the project's name says it is a test project, or it references a test framework. + public static bool IsTestProject(string projectName, IEnumerable referencePaths) + { + string name = TargetFrameworkSuffix.Replace(projectName.Trim(), ""); + if (TestName.IsMatch(name)) return true; + return referencePaths + .Select(path => path.Replace('\\', '/')) + .Select(path => path[(path.LastIndexOf('/') + 1)..]) + .Any(file => TestFrameworkAssemblies.Any(prefix => file.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))); + } +} diff --git a/csharp-fact-extractor/src/TypeNames.cs b/csharp-fact-extractor/src/TypeNames.cs new file mode 100644 index 00000000..eced5aae --- /dev/null +++ b/csharp-fact-extractor/src/TypeNames.cs @@ -0,0 +1,73 @@ +using Microsoft.CodeAnalysis; + +namespace CSharpFactExtractor; + +public static class TypeNames +{ + /// Namespace + name; nested types joined with '$' so the namespace stays the package. + /// A generic type keeps its plain name unless a type with the same name sits beside it + /// (Result and Result); then the generic one is named the .NET way, "Result`1". + public static string Fqn(INamedTypeSymbol type) + { + var names = new Stack(); + for (INamedTypeSymbol? t = type.OriginalDefinition; t != null; t = t.ContainingType) + names.Push(t.Arity > 0 && HasNamesake(t) ? t.Name + "`" + t.Arity : t.Name); + var ns = Namespace(type); + return (ns.Length == 0 ? "" : ns + ".") + string.Join("$", names); + } + + /// True when another type with the same name is declared in the same namespace or outer type of the same project. + private static bool HasNamesake(INamedTypeSymbol type) + { + INamespaceOrTypeSymbol? container = (INamespaceOrTypeSymbol?)type.ContainingType ?? type.ContainingNamespace; + return container is not null && container.GetTypeMembers(type.Name).Count(IsInSource) > 1; + } + + public static string Namespace(INamedTypeSymbol type) => + type.ContainingNamespace is { IsGlobalNamespace: false } ns ? ns.ToDisplayString() : ""; + + public static bool IsInSource(ISymbol symbol) => symbol.Locations.Any(l => l.IsInSource); + + private static readonly string[] GeneratedSuffixes = { ".g.cs", ".g.i.cs", ".designer.cs", ".generated.cs" }; + + /// Build output and tool-generated files (Razor, designers, source generators): nobody refactors these. + public static bool IsGeneratedPath(string? path) + { + if (string.IsNullOrEmpty(path)) return true; + string normalized = path.Replace('\\', '/'); + if (normalized.Contains("/obj/") || normalized.Contains("/bin/")) return true; + return GeneratedSuffixes.Any(suffix => normalized.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)); + } + + /// The symbol's declarations that are in hand-written files. + public static IEnumerable SourceReferences(ISymbol symbol) => + symbol.DeclaringSyntaxReferences.Where(r => !IsGeneratedPath(r.SyntaxTree.FilePath)); + + /// True when the symbol is declared only in generated files. + public static bool IsGeneratedOnly(ISymbol symbol) => + symbol.DeclaringSyntaxReferences.Length > 0 && !SourceReferences(symbol).Any(); + + // Nullable annotations ("string?") are left out so a method's own signature and a caller's view of it always match. + private static readonly SymbolDisplayFormat ParameterFormat = SymbolDisplayFormat.MinimallyQualifiedFormat.WithMiscellaneousOptions( + SymbolDisplayFormat.MinimallyQualifiedFormat.MiscellaneousOptions & ~SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier); + + /// Type parameters and ref/out/in are part of the name: Save() and Save(), Add(int) and Add(ref int) are different methods. + public static string Signature(IMethodSymbol method) + { + string typeParameters = method.Arity > 0 ? "<" + string.Join(",", method.TypeParameters.Select(t => t.Name)) + ">" : ""; + return method.Name + typeParameters + "(" + string.Join(",", method.Parameters.Select(Parameter)) + ")"; + } + + private static string Parameter(IParameterSymbol parameter) + { + string passedBy = parameter.RefKind switch + { + RefKind.Ref => "ref ", + RefKind.Out => "out ", + RefKind.In => "in ", + RefKind.RefReadOnlyParameter => "ref readonly ", + _ => "", + }; + return passedBy + parameter.Type.ToDisplayString(ParameterFormat); + } +} diff --git a/csharp-fact-extractor/tests/CSharpFactExtractor.Tests.csproj b/csharp-fact-extractor/tests/CSharpFactExtractor.Tests.csproj new file mode 100644 index 00000000..5a9ac782 --- /dev/null +++ b/csharp-fact-extractor/tests/CSharpFactExtractor.Tests.csproj @@ -0,0 +1,26 @@ + + + + net10.0 + enable + enable + false + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/csharp-fact-extractor/tests/ContractTests.cs b/csharp-fact-extractor/tests/ContractTests.cs new file mode 100644 index 00000000..36026786 --- /dev/null +++ b/csharp-fact-extractor/tests/ContractTests.cs @@ -0,0 +1,22 @@ +using System.Text.Json; +using CSharpFactExtractor; +using Xunit; + +public class ContractTests +{ + /// The Java side's tests read this same file, so this test is what keeps the two programs in step. + [Fact] + public async Task Given_TinyGreeter_When_Extracted_Then_OutputMatchesTheCheckedInFactsFile() + { + var root = Path.GetDirectoryName(TypeListTests.FixtureSln)!; + var golden = Path.GetFullPath(Path.Combine(root, "..", "..", "..", + "codebase-graph-builder", "src", "test", "resources", "csharp", "tiny-greeter-facts.json")); + var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; + + Facts actual = (await Extractor.RunAsync(TypeListTests.FixtureSln, root, excludeTests: true)) with { Root = "." }; + + Assert.Equal( + File.ReadAllText(golden).ReplaceLineEndings("\n").Trim(), + JsonSerializer.Serialize(actual, options).ReplaceLineEndings("\n").Trim()); + } +} diff --git a/csharp-fact-extractor/tests/DependencyTests.cs b/csharp-fact-extractor/tests/DependencyTests.cs new file mode 100644 index 00000000..9eb7d4bb --- /dev/null +++ b/csharp-fact-extractor/tests/DependencyTests.cs @@ -0,0 +1,23 @@ +using CSharpFactExtractor; +using Xunit; + +public class DependencyTests +{ + [Fact] + public async Task Given_TinyGreeter_When_Extracted_Then_CycleBetweenGreeterAndMessageStore() + { + var root = Path.GetDirectoryName(TypeListTests.FixtureSln)!; + Facts facts = await Extractor.RunAsync(TypeListTests.FixtureSln, root, excludeTests: true); + TypeFacts greeter = facts.Types.Single(t => t.Name == "Greeter"); + TypeFacts store = facts.Types.Single(t => t.Name == "MessageStore"); + TypeFacts program = facts.Types.Single(t => t.Name == "Program"); + + DependencyFacts toStore = Assert.Single(greeter.Dependencies); + DependencyFacts toGreeter = Assert.Single(store.Dependencies); + Assert.Equal("TinyGreeter.MessageStore", toStore.To); + Assert.Equal("TinyGreeter.Greeter", toGreeter.To); + Assert.True(toStore.Count > toGreeter.Count, "Greeter uses MessageStore more than the reverse"); + Assert.Equal("TinyGreeter.Greeter", Assert.Single(program.Dependencies).To); + Assert.DoesNotContain(facts.Types.SelectMany(t => t.Dependencies), d => d.To.StartsWith("System")); + } +} diff --git a/csharp-fact-extractor/tests/GeneratedAndInheritanceTests.cs b/csharp-fact-extractor/tests/GeneratedAndInheritanceTests.cs new file mode 100644 index 00000000..8641817a --- /dev/null +++ b/csharp-fact-extractor/tests/GeneratedAndInheritanceTests.cs @@ -0,0 +1,51 @@ +using CSharpFactExtractor; +using Xunit; + +public class GeneratedAndInheritanceTests +{ + private static string Fixture => Path.GetFullPath( + Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", "fixtures", "TwoProjects")); + + private static Task Extract() => + Extractor.RunAsync(new[] { Path.Combine(Fixture, "Alpha", "Alpha.csproj") }, Fixture, excludeTests: true); + + [Theory] + [InlineData("/repo/App/obj/Debug/net8.0/Pages_Index_cshtml.g.cs", true)] + [InlineData("/repo/App/bin/Debug/Thing.cs", true)] + [InlineData("/repo/App/Models/Thing.g.cs", true)] + [InlineData("/repo/App/Models/Thing.g.i.cs", true)] + [InlineData("/repo/App/Form1.Designer.cs", true)] + [InlineData("/repo/App/Api.generated.cs", true)] + [InlineData("/repo/App/Models/Thing.cs", false)] + [InlineData("/repo/App/Objects/Binder.cs", false)] + public void Given_APath_When_Classified_Then_GeneratedCodeIsRecognised(string path, bool expected) + { + Assert.Equal(expected, TypeNames.IsGeneratedPath(path)); + } + + [Fact] + public async Task Given_AGeneratedFile_When_Extracted_Then_ItsTypesAreLeftOut() + { + Facts facts = await Extract(); + + Assert.DoesNotContain(facts.Types, t => t.Name == "GeneratedThing"); + Assert.Equal(new[] { "Alpha.AlphaService", "Beta.BaseStore", "Beta.BetaStore" }, facts.Types.Select(t => t.Fqn)); + } + + [Fact] + public async Task Given_ASubclass_When_Extracted_Then_OverridesAndUsedParentMembersAreReported() + { + Facts facts = await Extract(); + TypeFacts parent = facts.Types.Single(t => t.Name == "BaseStore"); + TypeFacts child = facts.Types.Single(t => t.Name == "BetaStore"); + + // Capacity, Describe and Reset are protected; Limit is public. + Assert.Equal(3, parent.ProtectedMembers); + Assert.Empty(parent.OverriddenMethods); + + Assert.Equal("Beta.BaseStore", child.Parent); + Assert.Equal(new[] { "Limit()" }, child.OverriddenMethods); + Assert.Equal(new[] { "Capacity", "Reset" }, child.UsedParentMembers); + Assert.Equal(0, child.ProtectedMembers); + } +} diff --git a/csharp-fact-extractor/tests/MemberIdentityTests.cs b/csharp-fact-extractor/tests/MemberIdentityTests.cs new file mode 100644 index 00000000..878cf33b --- /dev/null +++ b/csharp-fact-extractor/tests/MemberIdentityTests.cs @@ -0,0 +1,88 @@ +using CSharpFactExtractor; +using Xunit; + +public class MemberIdentityTests +{ + private static string Fixture => Path.GetFullPath( + Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", "fixtures", "Members")); + + private static Task Extract() => + Extractor.RunAsync(new[] { Path.Combine(Fixture, "Members.csproj") }, Fixture, excludeTests: true); + + private static async Task TypeNamed(string name) => (await Extract()).Types.Single(t => t.Name == name); + + /// Save() and Save(), Add(int) and Add(ref int) used to share one signature, and the consumer keeps one method per signature. + [Fact] + public async Task Given_OverloadsThatDifferByTypeParameterOrPassingMode_When_Extracted_Then_EachHasItsOwnSignature() + { + TypeFacts overloads = await TypeNamed("Overloads"); + + Assert.Equal( + new[] { "Add(int)", "Add(ref int)", "Read(out int)", "Save()", "Save()" }, + overloads.Methods.Select(m => m.Signature).OrderBy(s => s, StringComparer.Ordinal)); + } + + /// A caller must name the overload exactly as the overload names itself. + [Fact] + public async Task Given_ACallToAGenericOrRefOverload_When_Extracted_Then_TheCallUsesTheSameSignature() + { + MethodFacts go = Assert.Single((await TypeNamed("OverloadCaller")).Methods); + + Assert.Equal(new[] { "Members.Overloads.Add(ref int)", "Members.Overloads.Save()" }, go.CalledForeignMethods); + } + + [Fact] + public async Task Given_AStaticAndAnInstanceConstructor_When_Extracted_Then_EachHasItsOwnSignature() + { + TypeFacts cache = await TypeNamed("Cache"); + + Assert.Equal( + new[] { "Cache()", "static Cache()" }, + cache.Methods.Where(m => m.IsConstructor).Select(m => m.Signature).OrderBy(s => s, StringComparer.Ordinal)); + } + + [Theory] + [InlineData("Write", new[] { "Members.Order.set_Total(int)" })] + [InlineData("Read", new[] { "Members.Order.get_Total()" })] + [InlineData("Bump", new[] { "Members.Order.get_Total()", "Members.Order.set_Total(int)" })] + [InlineData("Create", new[] { "Members.Order.set_Total(int)" })] + public async Task Given_APropertyOfAnotherType_When_ReadOrWritten_Then_TheMatchingAccessorIsCalled(string method, string[] expected) + { + TypeFacts clerk = await TypeNamed("Clerk"); + + Assert.Equal(expected, clerk.Methods.Single(m => m.Name == method).CalledForeignMethods); + } + + /// `Child = { Value = 1 }` and `Items = { 1, 2 }` read the property; only `Child = new Node()` replaces it. + [Theory] + [InlineData("Nested", new[] { "Members.Holder.get_Child()", "Members.Node.set_Value(int)" })] + [InlineData("Replaced", new[] { "Members.Holder.set_Child(Node)" })] + [InlineData("Filled", new[] { "Members.Holder.get_Items()" })] + public async Task Given_APropertyInAnObjectInitializer_When_NestedOrReplaced_Then_TheMatchingAccessorIsCalled(string method, string[] expected) + { + TypeFacts nester = await TypeNamed("Nester"); + + Assert.Equal(expected, nester.Methods.Single(m => m.Name == method).CalledForeignMethods); + } + + /// A partial method's declaration has no body; the body is in its other half. + [Fact] + public async Task Given_APartialMethod_When_Extracted_Then_ItsBodyIsMeasured() + { + TypeFacts validator = await TypeNamed("Validator"); + + MethodFacts check = validator.Methods.Single(m => m.Name == "Check"); + Assert.Equal(3, check.Cyclo); + Assert.True(check.Loc > 1); + Assert.Equal(2, validator.Methods.Single(m => m.Name == "Note").Cyclo); + } + + [Fact] + public async Task Given_ATypeDeclaredOnlyInGeneratedCode_When_Used_Then_ItIsNotADependency() + { + Facts facts = await Extract(); + + Assert.DoesNotContain(facts.Types, t => t.Name == "GeneratedHelper"); + Assert.Empty(facts.Types.Single(t => t.Name == "UsesGenerated").Dependencies); + } +} diff --git a/csharp-fact-extractor/tests/MemberTests.cs b/csharp-fact-extractor/tests/MemberTests.cs new file mode 100644 index 00000000..74ac3ef3 --- /dev/null +++ b/csharp-fact-extractor/tests/MemberTests.cs @@ -0,0 +1,70 @@ +using CSharpFactExtractor; +using Xunit; + +public class MemberTests +{ + private static async Task ExtractFixture() + { + var root = Path.GetDirectoryName(TypeListTests.FixtureSln)!; + return await Extractor.RunAsync(TypeListTests.FixtureSln, root, excludeTests: true); + } + + [Fact] + public async Task Given_Greeter_When_Extracted_Then_AttributesAccessorsAndGreetFacts() + { + TypeFacts greeter = (await ExtractFixture()).Types.Single(t => t.Name == "Greeter"); + + // A field is an attribute; an auto-property is a private attribute plus accessor methods (Option A). + Assert.Equal(new[] { "Punctuation", "_store" }, greeter.Attributes.Select(a => a.Name).OrderBy(x => x, StringComparer.Ordinal)); + Assert.All(greeter.Attributes, a => Assert.False(a.IsPublic)); + Assert.Equal( + new[] { "get_Punctuation()", "set_Punctuation(string)" }, + greeter.Methods.Where(m => m.IsAccessor).Select(m => m.Signature).OrderBy(x => x, StringComparer.Ordinal)); + Assert.Single(greeter.Methods, m => m.IsConstructor); + // An auto-accessor touches the attribute behind it, as a Java getter touches its field. + Assert.All(greeter.Methods.Where(m => m.IsAccessor), m => Assert.Equal(new[] { "Punctuation" }, m.AccessedOwnAttributes)); + + MethodFacts greet = greeter.Methods.Single(m => m.Name == "Greet"); + Assert.Equal("Greet(string)", greet.Signature); + Assert.Equal(1, greet.Parameters); + Assert.Equal(2, greet.Cyclo); + Assert.Equal(1, greet.MaxNesting); + Assert.Equal(8, greet.Loc); + Assert.False(greet.IsAccessor); + Assert.Equal(new[] { "Punctuation", "_store" }, greet.AccessedOwnAttributes); + Assert.Equal(new[] { "Punctuation", "_store" }, greet.AccessedVariables); + Assert.Empty(greet.AccessedForeignAttributes); + Assert.Empty(greet.AccessedForeignClasses); + // Like the Java reader, calls to framework types count as foreign calls too. + Assert.Equal( + new[] { "System.String.IsNullOrEmpty(string)", "TinyGreeter.MessageStore.Find(string)" }, + greet.CalledForeignMethods); + Assert.Equal(new[] { "System.String", "TinyGreeter.MessageStore" }, greet.CalledForeignMethodClasses); + } + + [Fact] + public async Task Given_MessageStore_When_Extracted_Then_TernaryCountsAsABranch() + { + TypeFacts store = (await ExtractFixture()).Types.Single(t => t.Name == "MessageStore"); + + MethodFacts find = store.Methods.Single(m => m.Name == "Find"); + Assert.Equal(2, find.Cyclo); + Assert.Equal(0, find.MaxNesting); + Assert.Empty(find.AccessedOwnAttributes); + + MethodFacts constructor = store.Methods.Single(m => m.IsConstructor); + Assert.Equal("MessageStore(Greeter)", constructor.Signature); + Assert.Equal(new[] { "_owner" }, constructor.AccessedOwnAttributes); + } + + [Fact] + public async Task Given_Program_When_Extracted_Then_LocalIsAnAccessedVariableAndGreetIsAForeignCall() + { + TypeFacts program = (await ExtractFixture()).Types.Single(t => t.Name == "Program"); + + MethodFacts main = program.Methods.Single(m => m.Name == "Main"); + Assert.Equal(new[] { "greeter" }, main.AccessedVariables); + Assert.Contains("TinyGreeter.Greeter.Greet(string)", main.CalledForeignMethods); + Assert.Contains("System.Console.WriteLine(string)", main.CalledForeignMethods); + } +} diff --git a/csharp-fact-extractor/tests/MultiProjectTests.cs b/csharp-fact-extractor/tests/MultiProjectTests.cs new file mode 100644 index 00000000..cb9e862a --- /dev/null +++ b/csharp-fact-extractor/tests/MultiProjectTests.cs @@ -0,0 +1,38 @@ +using CSharpFactExtractor; +using Xunit; + +public class MultiProjectTests +{ + private static string Fixture => Path.GetFullPath( + Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", "fixtures", "TwoProjects")); + + /// A repository with projects but no solution at its root: every project is analysed, once. + [Fact] + public async Task Given_TwoProjectsAndNoSolution_When_BothAreGiven_Then_TypesFromBothAppearOnce() + { + var targets = new[] + { + Path.Combine(Fixture, "Alpha", "Alpha.csproj"), + Path.Combine(Fixture, "Beta", "Beta.csproj"), + }; + + Facts facts = await Extractor.RunAsync(targets, Fixture, excludeTests: true); + + Assert.Equal(new[] { "Alpha.AlphaService", "Beta.BaseStore", "Beta.BetaStore" }, facts.Types.Select(t => t.Fqn)); + Assert.Equal(new[] { "Alpha", "Beta" }, facts.Namespaces); + TypeFacts alpha = facts.Types.Single(t => t.Name == "AlphaService"); + Assert.Equal("Alpha/AlphaService.cs", alpha.SourceFile); + Assert.Equal("Beta.BetaStore", Assert.Single(alpha.Dependencies).To); + // Another type's property is a call to its accessor. + Assert.Contains("Beta.BetaStore.get_Size()", alpha.Methods.Single(m => m.Name == "Count").CalledForeignMethods); + } + + /// Alpha references Beta, so loading Alpha alone already brings Beta in; naming Beta too must not double it. + [Fact] + public async Task Given_OnlyTheReferencingProject_When_Extracted_Then_TheReferencedProjectIsIncluded() + { + Facts facts = await Extractor.RunAsync(new[] { Path.Combine(Fixture, "Alpha", "Alpha.csproj") }, Fixture, excludeTests: true); + + Assert.Equal(new[] { "Alpha.AlphaService", "Beta.BaseStore", "Beta.BetaStore" }, facts.Types.Select(t => t.Fqn)); + } +} diff --git a/csharp-fact-extractor/tests/ShapesTests.cs b/csharp-fact-extractor/tests/ShapesTests.cs new file mode 100644 index 00000000..273444b3 --- /dev/null +++ b/csharp-fact-extractor/tests/ShapesTests.cs @@ -0,0 +1,43 @@ +using CSharpFactExtractor; +using Xunit; + +public class ShapesTests +{ + private static string Fixture => Path.GetFullPath( + Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", "fixtures", "Shapes")); + + private static Task Extract() => + Extractor.RunAsync(new[] { Path.Combine(Fixture, "Shapes.csproj") }, Fixture, excludeTests: true); + + /// Result and Result used to share one name, and one silently replaced the other. + [Fact] + public async Task Given_APlainAndAGenericTypeWithOneName_When_Extracted_Then_BothAreKept() + { + Facts facts = await Extract(); + + Assert.Equal( + new[] { "Shapes.Box", "Shapes.Counter", "Shapes.Result", "Shapes.Result`1" }, + facts.Types.Select(t => t.Fqn)); + Assert.Contains(facts.Types.Single(t => t.Fqn == "Shapes.Result").Methods, m => m.Name == "Describe"); + Assert.Contains(facts.Types.Single(t => t.Fqn == "Shapes.Result`1").Methods, m => m.Name == "HasValue"); + } + + /// Whoever uses the generic one must point at the same name it was given. + [Fact] + public async Task Given_AUserOfTheGenericNamesake_When_Extracted_Then_ItsDependencyUsesTheSameName() + { + TypeFacts counter = (await Extract()).Types.Single(t => t.Name == "Counter"); + + Assert.Equal(new[] { "Shapes.Result`1" }, counter.Dependencies.Select(d => d.To)); + } + + [Fact] + public async Task Given_APrimaryConstructor_When_Extracted_Then_ItsLengthIsNotTheWholeClass() + { + TypeFacts counter = (await Extract()).Types.Single(t => t.Name == "Counter"); + + MethodFacts constructor = Assert.Single(counter.Methods, m => m.IsConstructor); + Assert.Equal(1, constructor.Loc); + Assert.True(counter.Loc > 10); + } +} diff --git a/csharp-fact-extractor/tests/TestProjectsTests.cs b/csharp-fact-extractor/tests/TestProjectsTests.cs new file mode 100644 index 00000000..08861cd0 --- /dev/null +++ b/csharp-fact-extractor/tests/TestProjectsTests.cs @@ -0,0 +1,38 @@ +using CSharpFactExtractor; +using Xunit; + +public class TestProjectsTests +{ + private static readonly string[] None = Array.Empty(); + + [Theory] + [InlineData("App.Tests")] + [InlineData("App.Test")] + [InlineData("AppTests")] + [InlineData("AppTest")] + [InlineData("App.UnitTests")] + [InlineData("App.Specs")] + [InlineData("App.Tests(net8.0)")] + public void Given_ATestLikeName_Then_ItIsATestProject(string name) => + Assert.True(TestProjects.IsTestProject(name, None)); + + [Theory] + [InlineData("App")] + [InlineData("App.Core")] + [InlineData("Contest")] + [InlineData("App.Testing.Helpers.Api")] + [InlineData("App(net8.0)")] + public void Given_AnOrdinaryName_Then_ItIsNotATestProject(string name) => + Assert.False(TestProjects.IsTestProject(name, None)); + + [Theory] + [InlineData("/pkgs/xunit.core/2.9.3/lib/netstandard1.1/xunit.core.dll")] + [InlineData(@"C:\pkgs\nunit\4.0.0\lib\net6.0\nunit.framework.dll")] + [InlineData("/pkgs/mstest/Microsoft.VisualStudio.TestPlatform.TestFramework.dll")] + public void Given_AnOrdinaryNameThatReferencesATestFramework_Then_ItIsATestProject(string reference) => + Assert.True(TestProjects.IsTestProject("App.Checks", new[] { reference })); + + [Fact] + public void Given_OrdinaryReferences_Then_ItIsNotATestProject() => + Assert.False(TestProjects.IsTestProject("App", new[] { "/pkgs/System.Text.Json.dll", "/pkgs/Serilog.dll" })); +} diff --git a/csharp-fact-extractor/tests/TypeListTests.cs b/csharp-fact-extractor/tests/TypeListTests.cs new file mode 100644 index 00000000..eeb60bbd --- /dev/null +++ b/csharp-fact-extractor/tests/TypeListTests.cs @@ -0,0 +1,23 @@ +using CSharpFactExtractor; +using Xunit; + +public class TypeListTests +{ + public static string FixtureSln => + Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", "fixtures", "TinyGreeter", "TinyGreeter.sln")); + + [Fact] + public async Task Given_TinyGreeter_When_Extracted_Then_ThreeTypesWithFilesAndNamespace() + { + var root = Path.GetDirectoryName(FixtureSln)!; + + Facts facts = await Extractor.RunAsync(FixtureSln, root, excludeTests: true); + + Assert.Equal(1, facts.SchemaVersion); + Assert.Equal(new[] { "TinyGreeter" }, facts.Namespaces); + Assert.Equal( + new[] { "TinyGreeter.Greeter", "TinyGreeter.MessageStore", "TinyGreeter.Program" }, + facts.Types.Select(t => t.Fqn).OrderBy(x => x)); + Assert.Equal("Greeter.cs", facts.Types.Single(t => t.Name == "Greeter").SourceFile); + } +} diff --git a/report/src/test/java/org/hjug/refactorfirst/report/CSharpOnlyReportTest.java b/report/src/test/java/org/hjug/refactorfirst/report/CSharpOnlyReportTest.java new file mode 100644 index 00000000..c9a3b537 --- /dev/null +++ b/report/src/test/java/org/hjug/refactorfirst/report/CSharpOnlyReportTest.java @@ -0,0 +1,104 @@ +package org.hjug.refactorfirst.report; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Stream; +import org.eclipse.jgit.api.Git; +import org.hjug.refactorfirst.report.json.JsonReportExecutor; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Runs each report type on a repository that holds only C#, through the real .NET extractor. + * Runs only when REFACTORFIRST_CSHARP_EXTRACTOR points at a built CSharpFactExtractor.dll, + * so the normal build needs no .NET. + */ +class CSharpOnlyReportTest { + + private static final String EXTRACTOR_ENV = "REFACTORFIRST_CSHARP_EXTRACTOR"; + private static final List FIXTURE_FILES = + List.of("TinyGreeter.sln", "TinyGreeter.csproj", "Program.cs", "Greeter.cs", "MessageStore.cs"); + + private Path repo; + + @BeforeEach + void makeCSharpOnlyRepository() throws Exception { + String extractor = System.getenv(EXTRACTOR_ENV); + assumeTrue(extractor != null && Files.exists(Path.of(extractor)), "extractor not configured"); + Path fixture = Path.of("..", "csharp-fact-extractor", "fixtures", "TinyGreeter") + .toAbsolutePath() + .normalize(); + assumeTrue(Files.isDirectory(fixture), "fixture not found"); + + // Real path: the report writer refuses any symlink in the output path, and macOS temp folders sit under one. + repo = Files.createTempDirectory( + Path.of(System.getProperty("java.io.tmpdir")).toRealPath(), "rf-csharp-only"); + for (String file : FIXTURE_FILES) { + Files.copy(fixture.resolve(file), repo.resolve(file)); + } + try (Git git = Git.init().setDirectory(repo.toFile()).call()) { + git.add().addFilepattern(".").call(); + git.commit().setMessage("first").setSign(false).call(); + Thread.sleep(1000); + Files.writeString( + repo.resolve("Greeter.cs"), Files.readString(repo.resolve("Greeter.cs")) + "\n// touched\n"); + git.add().addFilepattern(".").call(); + git.commit().setMessage("second").setSign(false).call(); + } + } + + @AfterEach + void deleteRepository() throws IOException { + if (repo == null) { + return; + } + try (Stream paths = Files.walk(repo)) { + paths.sorted(Comparator.reverseOrder()).forEach(p -> p.toFile().delete()); + } + } + + @Test + void givenCSharpOnlyRepository_whenSimpleHtmlReportRuns_thenTheCycleAndItsClassesAreReported() throws Exception { + Path out = Files.createDirectories(repo.resolve("out")); + + new SimpleHtmlReport() + .execute(50, true, false, false, true, "src/test", "tiny", "1.0", repo.toFile(), out.toString()); + + String html = Files.readString(out.resolve("refactor-first-report.html")); + assertTrue(html.contains("Class Cycles")); + assertTrue(html.contains("Current Class Cycle Count: 1")); + assertTrue(html.contains("Greeter")); + assertTrue(html.contains("MessageStore")); + } + + @Test + void givenCSharpOnlyRepository_whenHtmlReportRuns_thenItIsWritten() throws Exception { + Path out = Files.createDirectories(repo.resolve("out")); + + new HtmlReport() + .execute(50, true, false, false, true, "src/test", "tiny", "1.0", repo.toFile(), out.toString()); + + assertTrue(Files.readString(out.resolve("refactor-first-report.html")).contains("MessageStore")); + } + + @Test + void givenCSharpOnlyRepository_whenCsvAndJsonReportsRun_thenTheJavaOnlyCouplingStepDoesNotFail() throws Exception { + Path out = Files.createDirectories(repo.resolve("out")); + + assertDoesNotThrow(() -> new CsvReport().execute(false, "tiny", "1.0", out.toString(), repo.toFile())); + assertDoesNotThrow(() -> new JsonReportExecutor().execute(repo.toFile(), out.toString())); + + try (Stream written = Files.list(out)) { + assertEquals(2, written.count(), "one CSV and one JSON file"); + } + } +}