Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exceptions for invalid paths in template definition #1907

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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