From b97b57fe6bb2b945a24cfc376d5a3b6ecfe570d9 Mon Sep 17 00:00:00 2001 From: Hunter Mellema <124718352+hpmellema@users.noreply.github.com> Date: Thu, 3 Aug 2023 10:08:44 -0600 Subject: [PATCH] Add exceptions for invalid paths in template definition (#1907) Add exceptions for invalid paths in template definition. Errors are thrown for both invalid included file paths and for invalid root template paths. --- .../amazon/smithy/cli/InitCommandTest.java | 42 +++++++++++++++++++ .../smithy-templates/smithy-templates.json | 11 +++++ .../smithy/cli/commands/InitCommand.java | 19 +++++---- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java b/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java index d649e8250de..a8f77ba9d19 100644 --- a/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java +++ b/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java @@ -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.") @@ -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.") @@ -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 args, Path root) { StringBuilder output = new StringBuilder(); int result = IoUtils.runCommand(args, root, output, Collections.emptyMap()); diff --git a/smithy-cli/src/it/resources/software/amazon/smithy/cli/projects/smithy-templates/smithy-templates.json b/smithy-cli/src/it/resources/software/amazon/smithy/cli/projects/smithy-templates/smithy-templates.json index cde68000b6b..2f9d6a00b5d 100644 --- a/smithy-cli/src/it/resources/software/amazon/smithy/cli/projects/smithy-templates/smithy-templates.json +++ b/smithy-cli/src/it/resources/software/amazon/smithy/cli/projects/smithy-templates/smithy-templates.json @@ -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" + ] } } } diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java index 3ef7154394e..79f5961897b 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java @@ -201,7 +201,7 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options op final String templatePath = getTemplatePath(templateNode, template); List includedFiles = getIncludedFiles(templateNode); - try (ProgressTracker t = new ProgressTracker(env, + try (ProgressTracker ignored = new ProgressTracker(env, ProgressStyle.dots("cloning template", "template cloned"), standardOptions.quiet() )) { @@ -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())) { @@ -266,15 +271,13 @@ private static List getIncludedFiles(ObjectNode templateNode) { } private static void copyIncludedFiles(String temp, String dest, List 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());