Skip to content

Commit

Permalink
Add exceptions for invalid paths in template definition (smithy-lang#…
Browse files Browse the repository at this point in the history
…1907)

Add exceptions for invalid paths in template definition. Errors are thrown for both invalid included file paths and for invalid root template paths.
  • Loading branch information
hpmellema authored and syall committed Aug 11, 2023
1 parent 8fbcef0 commit b97b57f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public void unexpectedTemplate() {
.append(System.lineSeparator())
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
.append(System.lineSeparator())
.append("bad-include-path Template with incorrect include path. Should throw error.")
.append(System.lineSeparator())
.append("bad-template-path Template with incorrect path. Should throw error.")
.append(System.lineSeparator())
.append("included-file-json Smithy Quickstart example with json file included.")
.append(System.lineSeparator())
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
Expand Down Expand Up @@ -199,6 +203,10 @@ public void withListArg() {
.append(System.lineSeparator())
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
.append(System.lineSeparator())
.append("bad-include-path Template with incorrect include path. Should throw error.")
.append(System.lineSeparator())
.append("bad-template-path Template with incorrect path. Should throw error.")
.append(System.lineSeparator())
.append("included-file-json Smithy Quickstart example with json file included.")
.append(System.lineSeparator())
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
Expand Down Expand Up @@ -253,6 +261,40 @@ public void executesInitSuccessQuiet() {
});
}

@Test
public void badTemplatePathFailureExpected() {
IntegUtils.withProject(PROJECT_NAME, templatesDir -> {
setupTemplatesDirectory(templatesDir);

IntegUtils.withTempDir("badTemplatePath", dir -> {
RunResult result = IntegUtils.run(
dir, ListUtils.of("init", "-t", "bad-template-path", "-u", templatesDir.toString()));
assertThat(Files.exists(Paths.get(dir.toString(), "bad-template-path")), is(false));
assertThat(result.getExitCode(), is(1));
assertThat(result.getOutput(),
containsString("Template path `getting-started-example/does-not-exist` for template"
+" \"bad-template-path\" is invalid."));
});
});
}

@Test
public void badIncludePathFailureExpected() {
IntegUtils.withProject(PROJECT_NAME, templatesDir -> {
setupTemplatesDirectory(templatesDir);

IntegUtils.withTempDir("badIncludePath", dir -> {
RunResult result = IntegUtils.run(
dir, ListUtils.of("init", "-t", "bad-include-path", "-u", templatesDir.toString()));
assertThat(Files.exists(Paths.get(dir.toString(), " bad-include-path")), is(false));
assertThat(result.getExitCode(), is(1));
assertThat(result.getOutput(),
containsString("File or directory `getting-started-example/does-not-exist` is marked"
+ " for inclusion in template \"bad-include-path\", but was not found"));
});
});
}

private static void run(List<String> args, Path root) {
StringBuilder output = new StringBuilder();
int result = IoUtils.runCommand(args, root, output, Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
"getting-started-example/common-gradle-configs/gradlew",
"getting-started-example/common-gradle-configs/gradle.properties"
]
},
"bad-template-path": {
"documentation": "Template with incorrect path. Should throw error.",
"path": "getting-started-example/does-not-exist"
},
"bad-include-path": {
"documentation": "Template with incorrect include path. Should throw error.",
"path": "getting-started-example/included-files-gradle",
"include": [
"getting-started-example/does-not-exist"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options op
final String templatePath = getTemplatePath(templateNode, template);
List<String> includedFiles = getIncludedFiles(templateNode);

try (ProgressTracker t = new ProgressTracker(env,
try (ProgressTracker ignored = new ProgressTracker(env,
ProgressStyle.dots("cloning template", "template cloned"),
standardOptions.quiet()
)) {
Expand All @@ -214,8 +214,13 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options op
exec(ListUtils.of("git", "checkout"), temp);
}

if (!Files.exists(temp.resolve(templatePath))) {
throw new CliError(String.format("Template path `%s` for template \"%s\" is invalid.",
templatePath, template));
}

IoUtils.copyDir(Paths.get(temp.toString(), templatePath), dest);
copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template, env);
copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template);

if (!standardOptions.quiet()) {
try (ColorBuffer buffer = ColorBuffer.of(env.colors(), env.stdout())) {
Expand Down Expand Up @@ -266,15 +271,13 @@ private static List<String> getIncludedFiles(ObjectNode templateNode) {
}

private static void copyIncludedFiles(String temp, String dest, List<String> includedFiles,
String templateName, Env env) throws IOException {
String templateName) throws IOException {
for (String included : includedFiles) {
Path includedPath = Paths.get(temp, included);
if (!Files.exists(includedPath)) {
try (ColorBuffer buffer = ColorBuffer.of(env.colors(), env.stderr())) {
buffer.println(String.format(
"File or directory %s is marked for inclusion in template %s but was not found",
included, templateName), ColorTheme.WARNING);
}
throw new CliError(String.format(
"File or directory `%s` is marked for inclusion in template \"%s\", but was not found",
included, templateName));
}

Path target = Paths.get(dest, Objects.requireNonNull(includedPath.getFileName()).toString());
Expand Down

0 comments on commit b97b57f

Please sign in to comment.