Skip to content

Commit b97b57f

Browse files
hpmellemaSteven Yuan
authored andcommitted
Add exceptions for invalid paths in template definition (smithy-lang#1907)
Add exceptions for invalid paths in template definition. Errors are thrown for both invalid included file paths and for invalid root template paths.
1 parent 8fbcef0 commit b97b57f

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public void unexpectedTemplate() {
127127
.append(System.lineSeparator())
128128
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
129129
.append(System.lineSeparator())
130+
.append("bad-include-path Template with incorrect include path. Should throw error.")
131+
.append(System.lineSeparator())
132+
.append("bad-template-path Template with incorrect path. Should throw error.")
133+
.append(System.lineSeparator())
130134
.append("included-file-json Smithy Quickstart example with json file included.")
131135
.append(System.lineSeparator())
132136
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
@@ -199,6 +203,10 @@ public void withListArg() {
199203
.append(System.lineSeparator())
200204
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
201205
.append(System.lineSeparator())
206+
.append("bad-include-path Template with incorrect include path. Should throw error.")
207+
.append(System.lineSeparator())
208+
.append("bad-template-path Template with incorrect path. Should throw error.")
209+
.append(System.lineSeparator())
202210
.append("included-file-json Smithy Quickstart example with json file included.")
203211
.append(System.lineSeparator())
204212
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
@@ -253,6 +261,40 @@ public void executesInitSuccessQuiet() {
253261
});
254262
}
255263

264+
@Test
265+
public void badTemplatePathFailureExpected() {
266+
IntegUtils.withProject(PROJECT_NAME, templatesDir -> {
267+
setupTemplatesDirectory(templatesDir);
268+
269+
IntegUtils.withTempDir("badTemplatePath", dir -> {
270+
RunResult result = IntegUtils.run(
271+
dir, ListUtils.of("init", "-t", "bad-template-path", "-u", templatesDir.toString()));
272+
assertThat(Files.exists(Paths.get(dir.toString(), "bad-template-path")), is(false));
273+
assertThat(result.getExitCode(), is(1));
274+
assertThat(result.getOutput(),
275+
containsString("Template path `getting-started-example/does-not-exist` for template"
276+
+" \"bad-template-path\" is invalid."));
277+
});
278+
});
279+
}
280+
281+
@Test
282+
public void badIncludePathFailureExpected() {
283+
IntegUtils.withProject(PROJECT_NAME, templatesDir -> {
284+
setupTemplatesDirectory(templatesDir);
285+
286+
IntegUtils.withTempDir("badIncludePath", dir -> {
287+
RunResult result = IntegUtils.run(
288+
dir, ListUtils.of("init", "-t", "bad-include-path", "-u", templatesDir.toString()));
289+
assertThat(Files.exists(Paths.get(dir.toString(), " bad-include-path")), is(false));
290+
assertThat(result.getExitCode(), is(1));
291+
assertThat(result.getOutput(),
292+
containsString("File or directory `getting-started-example/does-not-exist` is marked"
293+
+ " for inclusion in template \"bad-include-path\", but was not found"));
294+
});
295+
});
296+
}
297+
256298
private static void run(List<String> args, Path root) {
257299
StringBuilder output = new StringBuilder();
258300
int result = IoUtils.runCommand(args, root, output, Collections.emptyMap());

smithy-cli/src/it/resources/software/amazon/smithy/cli/projects/smithy-templates/smithy-templates.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
"getting-started-example/common-gradle-configs/gradlew",
1919
"getting-started-example/common-gradle-configs/gradle.properties"
2020
]
21+
},
22+
"bad-template-path": {
23+
"documentation": "Template with incorrect path. Should throw error.",
24+
"path": "getting-started-example/does-not-exist"
25+
},
26+
"bad-include-path": {
27+
"documentation": "Template with incorrect include path. Should throw error.",
28+
"path": "getting-started-example/included-files-gradle",
29+
"include": [
30+
"getting-started-example/does-not-exist"
31+
]
2132
}
2233
}
2334
}

smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options op
201201
final String templatePath = getTemplatePath(templateNode, template);
202202
List<String> includedFiles = getIncludedFiles(templateNode);
203203

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

217+
if (!Files.exists(temp.resolve(templatePath))) {
218+
throw new CliError(String.format("Template path `%s` for template \"%s\" is invalid.",
219+
templatePath, template));
220+
}
221+
217222
IoUtils.copyDir(Paths.get(temp.toString(), templatePath), dest);
218-
copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template, env);
223+
copyIncludedFiles(temp.toString(), dest.toString(), includedFiles, template);
219224

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

268273
private static void copyIncludedFiles(String temp, String dest, List<String> includedFiles,
269-
String templateName, Env env) throws IOException {
274+
String templateName) throws IOException {
270275
for (String included : includedFiles) {
271276
Path includedPath = Paths.get(temp, included);
272277
if (!Files.exists(includedPath)) {
273-
try (ColorBuffer buffer = ColorBuffer.of(env.colors(), env.stderr())) {
274-
buffer.println(String.format(
275-
"File or directory %s is marked for inclusion in template %s but was not found",
276-
included, templateName), ColorTheme.WARNING);
277-
}
278+
throw new CliError(String.format(
279+
"File or directory `%s` is marked for inclusion in template \"%s\", but was not found",
280+
included, templateName));
278281
}
279282

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

0 commit comments

Comments
 (0)