forked from tools/josh
Move glob emptiness check to expander
This commit is contained in:
parent
690d290052
commit
90701ab1eb
|
@ -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": {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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([]);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue