forked from smithy-lang/smithy-language-server
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from disneystreaming/limit-import-depth
Fix default imports and add a test
- Loading branch information
Showing
6 changed files
with
203 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package software.amazon.smithy.lsp.ext; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import com.google.common.io.Files; | ||
|
||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
|
||
public class Harness implements AutoCloseable { | ||
private File root; | ||
private SmithyProject project; | ||
|
||
private Harness(File root, SmithyProject project) { | ||
this.root = root; | ||
this.project = project; | ||
} | ||
|
||
public File getRoot() { | ||
return this.root; | ||
} | ||
|
||
public SmithyProject getProject() { | ||
return this.project; | ||
} | ||
|
||
private static File safeCreateFile(String path, String contents, File root) throws Exception { | ||
File f = Paths.get(root.getAbsolutePath(), path).toFile(); | ||
new File(f.getParent()).mkdirs(); | ||
try (FileWriter fw = new FileWriter(f)) { | ||
fw.write(contents); | ||
fw.flush(); | ||
} | ||
|
||
return f; | ||
} | ||
|
||
public File addFile(String path, String contents) throws Exception { | ||
File f = safeCreateFile(path, contents, this.root); | ||
Either<Exception, SmithyProject> loaded = this.project.recompile(f); | ||
if (loaded.isRight()) | ||
this.project = loaded.getRight(); | ||
else | ||
throw loaded.getLeft(); | ||
|
||
return f; | ||
} | ||
|
||
public File file(String path) throws Exception { | ||
return Paths.get(root.getAbsolutePath(), path).toFile(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
root.deleteOnExit(); | ||
} | ||
|
||
public static Harness create(SmithyBuildExtensions ext) throws Exception { | ||
// TODO: How to make this safe? | ||
File hs = Files.createTempDir(); | ||
Either<Exception, SmithyProject> loaded = SmithyProject.load(ext, hs); | ||
if (loaded.isRight()) | ||
return new Harness(hs, loaded.getRight()); | ||
else | ||
throw loaded.getLeft(); | ||
} | ||
|
||
public static Harness create(SmithyBuildExtensions ext, Map<String, String> files) throws Exception { | ||
// TODO: How to make this safe? | ||
File hs = Files.createTempDir(); | ||
for (Entry<String, String> entry : files.entrySet()) { | ||
safeCreateFile(entry.getKey(), entry.getValue(), hs); | ||
} | ||
Either<Exception, SmithyProject> loaded = SmithyProject.load(ext, hs); | ||
if (loaded.isRight()) | ||
return new Harness(hs, loaded.getRight()); | ||
else | ||
throw loaded.getLeft(); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/software/amazon/smithy/lsp/ext/SmithyProjectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.lsp.ext; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
|
||
import software.amazon.smithy.utils.ListUtils; | ||
import software.amazon.smithy.utils.MapUtils; | ||
|
||
public class SmithyProjectTest { | ||
|
||
final Path DUMMY_PATH = Paths.get("smithy-build.json"); | ||
|
||
@Test | ||
public void walkingImports() throws Exception { | ||
List<String> imports = Arrays.asList("bla", "foo"); | ||
Map<String, String> files = MapUtils.ofEntries(MapUtils.entry("test.smithy", "namespace testRoot"), | ||
MapUtils.entry("bar/test.smithy", "namespace testBar"), | ||
MapUtils.entry("foo/test.smithy", "namespace testFoo"), | ||
MapUtils.entry("bla/test.smithy", "namespace testBla")); | ||
|
||
try (Harness hs = Harness.create(SmithyBuildExtensions.builder().imports(imports).build(), files)) { | ||
File inFoo = hs.file("foo/test.smithy"); | ||
File inBla = hs.file("bla/test.smithy"); | ||
|
||
List<File> smithyFiles = hs.getProject().getSmithyFiles(); | ||
|
||
assertEquals(ListUtils.of(inBla, inFoo), smithyFiles); | ||
} | ||
|
||
} | ||
} |