Skip to content

Commit

Permalink
fix tests relying on unix paths
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed Aug 9, 2024
1 parent a97e5c2 commit e168144
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,11 @@ public void multiRootAddingWatchedFile() throws Exception {

server.didChange(RequestBuilders.didChange()
.uri(fooUri)
.text("$version: \"2\"\nnamespace com.foo\nstructure Foo {}\n")
.text("""
$version: "2"
namespace com.foo
structure Foo {}
""")
.range(LspAdapter.origin())
.build());

Expand All @@ -1817,7 +1821,9 @@ public void multiRootAddingWatchedFile() throws Exception {

server.didChange(RequestBuilders.didChange()
.uri(fooUri)
.text("\nstructure Bar {}")
.text("""
structure Bar {}""")
.range(LspAdapter.point(3, 0))
.build());

Expand All @@ -1826,9 +1832,9 @@ public void multiRootAddingWatchedFile() throws Exception {
Project projectFoo = server.getProjects().getProjectByName("foo");
Project projectBar = server.getProjects().getProjectByName("bar");

assertThat(projectFoo.smithyFiles(), hasKey(endsWith("model/main.smithy")));
assertThat(projectBar.smithyFiles(), hasKey(endsWith("model/main.smithy")));
assertThat(projectBar.smithyFiles(), hasKey(endsWith("model/other.smithy")));
assertThat(projectFoo.smithyFiles(), hasKey(endsWith("main.smithy")));
assertThat(projectBar.smithyFiles(), hasKey(endsWith("main.smithy")));
assertThat(projectBar.smithyFiles(), hasKey(endsWith("other.smithy")));

assertThat(projectFoo.modelResult(), SmithyMatchers.hasValue(SmithyMatchers.hasShapeWithId("com.foo#Foo")));
assertThat(projectFoo.modelResult(), SmithyMatchers.hasValue(SmithyMatchers.hasShapeWithId("com.foo#Bar")));
Expand Down Expand Up @@ -1913,8 +1919,8 @@ public void multiRootChangingBuildFile() throws Exception {
Project projectFoo = server.getProjects().getProjectByName("foo");
Project projectBar = server.getProjects().getProjectByName("bar");

assertThat(projectFoo.smithyFiles(), hasKey(endsWith("model/main.smithy")));
assertThat(projectBar.smithyFiles(), hasKey(endsWith("model/main.smithy")));
assertThat(projectFoo.smithyFiles(), hasKey(endsWith("main.smithy")));
assertThat(projectBar.smithyFiles(), hasKey(endsWith("main.smithy")));
assertThat(projectBar.smithyFiles(), hasKey(endsWith("other.smithy")));

assertThat(projectFoo.modelResult(), SmithyMatchers.hasValue(SmithyMatchers.hasShapeWithId("com.foo#Foo")));
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/software/amazon/smithy/lsp/UtilMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package software.amazon.smithy.lsp;

import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Optional;
import org.hamcrest.CustomTypeSafeMatcher;
import org.hamcrest.Description;
Expand Down Expand Up @@ -33,4 +35,18 @@ public void describeMismatchSafely(Optional<T> item, Description description) {
}
};
}

public static Matcher<PathMatcher> canMatchPath(Path path) {
return new CustomTypeSafeMatcher<PathMatcher>("A matcher that matches " + path) {
@Override
protected boolean matchesSafely(PathMatcher item) {
return item.matches(path);
}

@Override
protected void describeMismatchSafely(PathMatcher item, Description mismatchDescription) {
mismatchDescription.appendText("did not match " + item.toString());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
package software.amazon.smithy.lsp.handler;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.hasItem;

import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions;
import org.eclipse.lsp4j.Registration;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.model.SmithyBuildConfig;
import software.amazon.smithy.lsp.TestWorkspace;
import software.amazon.smithy.lsp.UtilMatchers;
import software.amazon.smithy.lsp.project.Project;
import software.amazon.smithy.lsp.project.ProjectLoader;
import software.amazon.smithy.lsp.project.ProjectManager;
Expand Down Expand Up @@ -44,17 +45,21 @@ public void createsCorrectRegistrations() {
.build();

Project project = ProjectLoader.load(workspace.getRoot(), new ProjectManager(), new HashSet<>()).unwrap();
List<String> watcherPatterns = FileWatcherRegistrationHandler.getSmithyFileWatcherRegistrations(List.of(project))
List<PathMatcher> matchers = FileWatcherRegistrationHandler.getSmithyFileWatcherRegistrations(List.of(project))
.stream()
.map(Registration::getRegisterOptions)
.map(o -> (DidChangeWatchedFilesRegistrationOptions) o)
.flatMap(options -> options.getWatchers().stream())
.map(watcher -> watcher.getGlobPattern().getLeft())
.collect(Collectors.toList());
// The watcher glob patterns will look different between windows/unix, so turning
// them into path matchers lets us do platform-agnostic assertions.
.map(pattern -> FileSystems.getDefault().getPathMatcher("glob:" + pattern))
.toList();

assertThat(watcherPatterns, containsInAnyOrder(
endsWith("foo/**.{smithy,json}"),
endsWith("other/**.{smithy,json}"),
endsWith("abc.smithy")));
assertThat(matchers, hasItem(UtilMatchers.canMatchPath(workspace.getRoot().resolve("foo/abc.smithy"))));
assertThat(matchers, hasItem(UtilMatchers.canMatchPath(workspace.getRoot().resolve("foo/foo/abc/def.smithy"))));
assertThat(matchers, hasItem(UtilMatchers.canMatchPath(workspace.getRoot().resolve("other/abc.smithy"))));
assertThat(matchers, hasItem(UtilMatchers.canMatchPath(workspace.getRoot().resolve("other/foo/abc.smithy"))));
assertThat(matchers, hasItem(UtilMatchers.canMatchPath(workspace.getRoot().resolve("abc.smithy"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.HashSet;
import org.hamcrest.CustomTypeSafeMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.model.SmithyBuildConfig;
import software.amazon.smithy.lsp.TestWorkspace;
import software.amazon.smithy.lsp.UtilMatchers;
import software.amazon.smithy.utils.ListUtils;

public class ProjectFilePatternsTest {
Expand Down Expand Up @@ -44,24 +42,10 @@ public void createsPathMatchers() {
PathMatcher buildMatcher = ProjectFilePatterns.getBuildFilesPathMatcher(project);

Path root = project.root();
assertThat(smithyMatcher, matches(root.resolve("abc.smithy")));
assertThat(smithyMatcher, matches(root.resolve("foo/bar/baz.smithy")));
assertThat(smithyMatcher, matches(root.resolve("other/bar.smithy")));
assertThat(buildMatcher, matches(root.resolve("smithy-build.json")));
assertThat(buildMatcher, matches(root.resolve(".smithy-project.json")));
}

static Matcher<PathMatcher> matches(Path path) {
return new CustomTypeSafeMatcher<PathMatcher>("A matcher that matches " + path) {
@Override
protected boolean matchesSafely(PathMatcher item) {
return item.matches(path);
}

@Override
protected void describeMismatchSafely(PathMatcher item, Description mismatchDescription) {
mismatchDescription.appendText("did not match " + item.toString());
}
};
assertThat(smithyMatcher, UtilMatchers.canMatchPath(root.resolve("abc.smithy")));
assertThat(smithyMatcher, UtilMatchers.canMatchPath(root.resolve("foo/bar/baz.smithy")));
assertThat(smithyMatcher, UtilMatchers.canMatchPath(root.resolve("other/bar.smithy")));
assertThat(buildMatcher, UtilMatchers.canMatchPath(root.resolve("smithy-build.json")));
assertThat(buildMatcher, UtilMatchers.canMatchPath(root.resolve(".smithy-project.json")));
}
}

0 comments on commit e168144

Please sign in to comment.