From aa8b641f5355e3af568ae15359529dd3f95eebda Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Tue, 30 Jun 2020 19:09:29 +0200 Subject: [PATCH] Implement false, true, and whatis Fixes #129. Fixes #131. --- package.json | 2 +- src/main/js/Commands.ts | 41 +++++++++++++++++++++++++++++++++++++-- src/test/Commands.spec.ts | 40 +++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 369333c..c27ea94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fwdekker.com", - "version": "0.35.0", + "version": "0.36.0", "description": "The source code of [my personal website](https://fwdekker.com/).", "author": "Felix W. Dekker", "browser": "dist/bundle.js", diff --git a/src/main/js/Commands.ts b/src/main/js/Commands.ts index 234e782..adae78f 100644 --- a/src/main/js/Commands.ts +++ b/src/main/js/Commands.ts @@ -507,6 +507,14 @@ return new Command( \`exit\`, \`Closes the terminal session.\`, new InputValidator({maxArgs: 0}) +)`, + "false": /* language=JavaScript */ `\ +return new Command( + () => {return ExitCode.MISC;}, + \`return unsuccessful exit code\`, + \`false\`, + \`Set the status environment variable to ${ExitCode.MISC}.\`.trimMultiLines(), + new InputValidator({minArgs: 0}) )`, "help": /* language=JavaScript */ `\ return new Command( @@ -519,8 +527,8 @@ return new Command( const command = josh.interpreter.resolve(commandName); if (command === undefined) { - streams.out.writeLine(\`Unknown command '\${commandName}'.\`); - return ExitCode.COMMAND_NOT_FOUND; + streams.err.writeLine(\`help: Unknown command '\${commandName}'.\`); + return ExitCode.USAGE; } let helpString = "Name\\n" + commandName; @@ -945,6 +953,14 @@ return new Command( \`Update the access and modification times of each file to the current time. If a file does not ${n} exist, it is created.\`.trimMultiLines(), new InputValidator({minArgs: 1}) +)`, + "true": /* language=JavaScript */ `\ +return new Command( + () => {return ExitCode.OK;}, + \`return successful exit code\`, + \`true\`, + \`Set the status environment variable to ${ExitCode.OK}.\`.trimMultiLines(), + new InputValidator({minArgs: 0}) )`, "useradd": /* language=JavaScript */ `\ return new Command( @@ -1049,6 +1065,27 @@ return new Command( \`Modifies the user with the given name. See the "useradd" command for more information on the fields ${n} that can be modified.\`.trimMultiLines(), new InputValidator({minArgs: 1, maxArgs: 1}) +)`, + "whatis": /* language=JavaScript */ `\ +return new Command( + (input, streams) => { + return input.args + .map(commandName => { + const command = josh.interpreter.resolve(commandName); + if (command === undefined) { + streams.err.writeLine(\`whatis: Unknown command '\${commandName}'.\`); + return ExitCode.USAGE; + } + + streams.out.writeLine("" + commandName + " - " + command.summary); + return ExitCode.OK; + }) + .reduce((acc, exitCode) => exitCode === ExitCode.OK ? acc : exitCode); + }, + \`display one-line documentation\`, + \`whatis command ...\`, + \`Displays a one-line summary for each command.\`.trimMultiLines(), + new InputValidator({minArgs: 1}) )`, "whoami": /* language=JavaScript */ `\ return new Command( diff --git a/src/test/Commands.spec.ts b/src/test/Commands.spec.ts index 149c3c1..79656ab 100644 --- a/src/test/Commands.spec.ts +++ b/src/test/Commands.spec.ts @@ -324,11 +324,25 @@ describe("commands", () => { }); }); + describe("false", () => { + beforeEach(() => loadCommand("false")); + + + it("sets the exit code to an erroneous value", () => { + expect(execute("false")).to.not.equal(ExitCode.OK); + }); + }); + describe("help", () => { beforeEach(() => loadCommand("help")); - it("outputs something", () => { + it("outputs an error if the command could not be found", () => { + expect(execute("help error")).to.equal(ExitCode.USAGE); + expect(readErr()).to.equal("help: Unknown command 'error'.\n"); + }); + + it("outputs something if the command could be found", () => { expect(execute("help help")).to.equal(ExitCode.OK); expect(readOut()).to.not.equal(""); }); @@ -722,6 +736,15 @@ describe("commands", () => { }); }); + describe("true", () => { + beforeEach(() => loadCommand("true")); + + + it("sets the exit code to a successful value", () => { + expect(execute("true")).to.equal(ExitCode.OK); + }); + }); + describe("useradd", () => { before(() => HashProvider.default = plainHashProvider); @@ -834,6 +857,21 @@ describe("commands", () => { }); }); + describe("whatis", () => { + beforeEach(() => loadCommand("whatis")); + + + it("outputs an error if a command could not be found", () => { + expect(execute("whatis error")).to.equal(ExitCode.USAGE); + expect(readErr()).to.equal("whatis: Unknown command 'error'.\n"); + }); + + it("outputs a short summary of itself", () => { + expect(execute("whatis whatis")).to.equal(ExitCode.OK); + expect(readOut()).to.equal("whatis - display one-line documentation\n"); + }); + }); + describe("whoami", () => { beforeEach(() => loadCommand("whoami"));