feat(pbs): resolve editorial chains and expose read-only external sources
This commit is contained in:
+8
-2
@@ -26,6 +26,7 @@ final class SourceCrawler extends SimpleFileVisitor<Path> {
|
||||
|
||||
private final ReadOnlySet<String> allowedExtensions;
|
||||
private final List<Path> paths;
|
||||
private Path traversalRoot;
|
||||
|
||||
public SourceCrawler(
|
||||
final ReadOnlySet<String> allowedExtensions,
|
||||
@@ -36,7 +37,12 @@ final class SourceCrawler extends SimpleFileVisitor<Path> {
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attrs) {
|
||||
for (final var directory : path) {
|
||||
if (traversalRoot == null) {
|
||||
// The caller already admitted this source root. Ignore only descendants,
|
||||
// not ancestors such as .workspace containing an explicitly resolved library.
|
||||
traversalRoot = path;
|
||||
}
|
||||
for (final var directory : traversalRoot.relativize(path)) {
|
||||
if (!isAllowedPath(directory)) {
|
||||
return FileVisitResult.SKIP_SUBTREE;
|
||||
}
|
||||
@@ -83,4 +89,4 @@ final class SourceCrawler extends SimpleFileVisitor<Path> {
|
||||
|
||||
return allowedExtensions.contains(extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package p.studio.compiler.workspaces.stages;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.utilities.structures.ReadOnlySet;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SourceCrawlerTest {
|
||||
@TempDir Path project;
|
||||
|
||||
@Test
|
||||
void resolvedWorkspaceLibraryEntersFileTable() throws Exception {
|
||||
final Path library = Files.createDirectories(project.resolve(".workspace/repos/lib/src")).getParent();
|
||||
Files.createDirectories(project.resolve("src"));
|
||||
Files.writeString(project.resolve("prometeu.json"), """
|
||||
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game",
|
||||
"dependencies":[{"path":".workspace/repos/lib"}]}
|
||||
""");
|
||||
Files.writeString(library.resolve("prometeu.json"), """
|
||||
{"name":"lib","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game",
|
||||
"dependencies":[]}
|
||||
""");
|
||||
Files.writeString(project.resolve(".workspace/repos/registry.json"), "{}");
|
||||
final Path source = Files.writeString(library.resolve("src/main.pbs"), "fn value() -> int { return 1; }");
|
||||
Files.writeString(library.resolve("src/mod.barrel"), "export { value };\n");
|
||||
final var context = p.studio.compiler.models.BuilderPipelineContext.fromConfig(
|
||||
new p.studio.compiler.messages.BuilderPipelineConfig(false, project.toString()));
|
||||
final var logs = p.studio.utilities.logs.LogAggregator.empty();
|
||||
org.junit.jupiter.api.Assertions.assertFalse(new ResolveDepsPipelineStage().run(context, logs).hasErrors());
|
||||
org.junit.jupiter.api.Assertions.assertFalse(new LoadSourcesPipelineStage().run(context, logs).hasErrors());
|
||||
final var loaded = new ArrayList<Path>();
|
||||
for (int i = 0; i < context.fileTable.size(); i++) {
|
||||
loaded.add(context.fileTable.get(new p.studio.compiler.source.identifiers.FileId(i)).getCanonPath());
|
||||
}
|
||||
org.junit.jupiter.api.Assertions.assertTrue(loaded.contains(source.toRealPath()), loaded.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitlyAdmittedDependencyRootInsideWorkspaceIsTraversed() throws Exception {
|
||||
final Path sourceRoot = Files.createDirectories(project.resolve(".workspace/repos/lib/src"));
|
||||
final Path source = Files.writeString(sourceRoot.resolve("main.pbs"), "fn value() -> int { return 1; }");
|
||||
final Path incidental = Files.createDirectories(sourceRoot.resolve(".workspace/incidental"));
|
||||
Files.writeString(incidental.resolve("hidden.pbs"), "fn hidden() -> int { return 2; }");
|
||||
final var paths = new ArrayList<Path>();
|
||||
Files.walkFileTree(sourceRoot, new SourceCrawler(ReadOnlySet.wrap(Set.of("pbs")), paths));
|
||||
assertEquals(java.util.List.of(source), paths);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ordinaryRootDoesNotCollectIncidentalWorkspaceSources() throws Exception {
|
||||
final Path source = Files.writeString(project.resolve("main.pbs"), "fn value() -> int { return 1; }");
|
||||
final Path hidden = Files.createDirectories(project.resolve(".workspace/repos/lib"));
|
||||
Files.writeString(hidden.resolve("hidden.pbs"), "fn hidden() -> int { return 2; }");
|
||||
final var paths = new ArrayList<Path>();
|
||||
Files.walkFileTree(project, new SourceCrawler(ReadOnlySet.wrap(Set.of("pbs")), paths));
|
||||
assertEquals(java.util.List.of(source), paths);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user