forked from tools/josh
1
0
Fork 0

Move glob emptiness check to expander

This commit is contained in:
Florine W. Dekker 2019-11-23 21:39:25 +01:00
parent 690d290052
commit 90701ab1eb
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
3 changed files with 29 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fwdekker.com",
"version": "0.24.3",
"version": "0.24.4",
"description": "The source code of [my personal website](https://fwdekker.com/).",
"author": "Felix W. Dekker",
"repository": {

View File

@ -431,7 +431,11 @@ export class Expander {
}
}
return this.globber.glob(expandedToken);
const tokens = this.globber.glob(expandedToken);
if (tokens.length === 0)
throw new IllegalArgumentError(`Token '${unescape(expandedToken)}' does not match any files.`);
return tokens;
}
}
@ -470,16 +474,9 @@ export class Globber {
if (!this.isGlob(token))
return [token];
let tokens: string[];
if (token.startsWith("/"))
tokens = this.glob2("/", token.slice(1), new Path("/"));
else
tokens = this.glob2("", token, this.cwd);
if (tokens.length === 0)
throw new IllegalArgumentError(`Token '${unescape(token)}' does not match any files.`);
return tokens;
return token.startsWith("/")
? this.glob2("/", token.slice(1), new Path("/"))
: this.glob2("", token, this.cwd);
}

View File

@ -643,6 +643,22 @@ describe("expander", () => {
expect(expander.expand("'b?'")).to.have.deep.members(["b?"]);
expect(expander.expand("\"b*\"")).to.have.deep.members(["b*"]);
});
it("throws an error if the globber returns an empty array", () => {
const globber = new class extends Globber {
constructor() {
super(new FileSystem(), "");
}
glob(): string[] {
return [];
}
};
expander = new Expander(new Environment(), globber);
expect(() => expander.expand("arg")).to.throw();
});
});
describe("home directory", () => {
@ -910,14 +926,14 @@ describe("globber", () => {
describe("shared cases", () => {
describe("no matches", () => {
it("throws an error if no matches are found", () => {
expect(() => createGlobber().glob(`x${escape}?`)).to.throw();
it("returns an empty array if no matches are found", () => {
expect(createGlobber().glob(`x${escape}?`)).to.have.deep.members([]);
});
it("throws an error if no matches are found because the cwd does not exist", () => {
it("returns an empty array if no matches are found because the cwd does not exist", () => {
const globber = createGlobber({"/a1": new File()}, "/dir");
expect(() => globber.glob(`a${escape}?`)).to.throw();
expect(globber.glob(`a${escape}?`)).to.have.deep.members([]);
});
});