Skip to content

Commit 7f7fd16

Browse files
committed
refactor(cli): Allow for lower-case cache parameter values
1 parent 88c4e14 commit 7f7fd16

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

packages/cli/lib/cli/commands/build.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ build.builder = function(cli) {
9696
default: "Default",
9797
choices: ["Default", "Force", "ReadOnly", "Off"],
9898
})
99+
.coerce("cache", (opt) => {
100+
const lower = opt.toLowerCase();
101+
if (lower === "readonly" || lower === "read-only") {
102+
return "ReadOnly";
103+
}
104+
return lower.charAt(0).toUpperCase() + lower.slice(1);
105+
})
99106
.option("create-build-manifest", {
100107
describe: "Store build metadata in a '.ui5' directory in the build destination, " +
101108
"allowing reuse of the build result in other builds",

packages/cli/lib/cli/commands/serve.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ serve.builder = function(cli) {
7474
default: "Default",
7575
choices: ["Default", "Force", "ReadOnly", "Off"],
7676
})
77+
.coerce("cache", (opt) => {
78+
const lower = opt.toLowerCase();
79+
if (lower === "readonly" || lower === "read-only") {
80+
return "ReadOnly";
81+
}
82+
return lower.charAt(0).toUpperCase() + lower.slice(1);
83+
})
7784
.option("framework-version", {
7885
describe: "Overrides the framework version defined by the project. " +
7986
"Takes the same value as the version part of \"ui5 use\"",

packages/cli/test/lib/cli/base.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,63 @@ test.serial("ui5 --output-style", async (t) => {
304304
message: /^((?!Argument: output-style, Given: "Default).)*$/s
305305
}, "Does not throw an exception because of the --output-style input");
306306
});
307+
308+
test.serial("ui5 build --cache", async (t) => {
309+
// "--cache" uses a coerce to normalize the input letter case and to map "read-only" / "readonly"
310+
// variants to "ReadOnly". Since the coerce is hard to test in isolation, we check the result by
311+
// observing yargs' choice validation: invalid input is reported with the normalized form, while
312+
// valid (lower-case) input passes the choice validation and only fails later because there is no
313+
// project to build.
314+
await t.throwsAsync(ui5(["build", "--cache", "nonExistent"]), {
315+
message: /Argument: cache, Given: "Nonexistent", Choices: "Default", "Force", "ReadOnly", "Off"/s
316+
}, "Coercion correctly capitalizes the first letter and makes the rest lowercase");
317+
318+
await t.throwsAsync(ui5(["build", "--cache", "default"]), {
319+
message: /^((?!Argument: cache, Given: "Default).)*$/s
320+
}, "Does not throw an exception because of the --cache input ('default' coerced to 'Default')");
321+
322+
await t.throwsAsync(ui5(["build", "--cache", "FORCE"]), {
323+
message: /^((?!Argument: cache, Given: "Force).)*$/s
324+
}, "Does not throw an exception because of the --cache input ('FORCE' coerced to 'Force')");
325+
326+
await t.throwsAsync(ui5(["build", "--cache", "OfF"]), {
327+
message: /^((?!Argument: cache, Given: "Off).)*$/s
328+
}, "Does not throw an exception because of the --cache input ('OfF' coerced to 'Off')");
329+
330+
await t.throwsAsync(ui5(["build", "--cache", "readonly"]), {
331+
message: /^((?!Argument: cache, Given: "ReadOnly).)*$/s
332+
}, "Does not throw an exception because of the --cache input ('readonly' coerced to 'ReadOnly')");
333+
334+
await t.throwsAsync(ui5(["build", "--cache", "READONLY"]), {
335+
message: /^((?!Argument: cache, Given: "ReadOnly).)*$/s
336+
}, "Does not throw an exception because of the --cache input ('READONLY' coerced to 'ReadOnly')");
337+
338+
await t.throwsAsync(ui5(["build", "--cache", "read-only"]), {
339+
message: /^((?!Argument: cache, Given: "ReadOnly).)*$/s
340+
}, "Does not throw an exception because of the --cache input ('read-only' coerced to 'ReadOnly')");
341+
342+
await t.throwsAsync(ui5(["build", "--cache", "Read-Only"]), {
343+
message: /^((?!Argument: cache, Given: "ReadOnly).)*$/s
344+
}, "Does not throw an exception because of the --cache input ('Read-Only' coerced to 'ReadOnly')");
345+
});
346+
347+
test.serial("ui5 serve --cache", async (t) => {
348+
// Same pattern as the "ui5 build --cache" test above. "ui5 serve" also fails outside of a
349+
// UI5 project, so we can observe whether the failure is caused by the choice validation
350+
// of the --cache option.
351+
await t.throwsAsync(ui5(["serve", "--cache", "nonExistent"]), {
352+
message: /Argument: cache, Given: "Nonexistent", Choices: "Default", "Force", "ReadOnly", "Off"/s
353+
}, "Coercion correctly capitalizes the first letter and makes the rest lowercase");
354+
355+
await t.throwsAsync(ui5(["serve", "--cache", "force"]), {
356+
message: /^((?!Argument: cache, Given: "Force).)*$/s
357+
}, "Does not throw an exception because of the --cache input ('force' coerced to 'Force')");
358+
359+
await t.throwsAsync(ui5(["serve", "--cache", "readonly"]), {
360+
message: /^((?!Argument: cache, Given: "ReadOnly).)*$/s
361+
}, "Does not throw an exception because of the --cache input ('readonly' coerced to 'ReadOnly')");
362+
363+
await t.throwsAsync(ui5(["serve", "--cache", "read-only"]), {
364+
message: /^((?!Argument: cache, Given: "ReadOnly).)*$/s
365+
}, "Does not throw an exception because of the --cache input ('read-only' coerced to 'ReadOnly')");
366+
});

0 commit comments

Comments
 (0)