-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathcommand.spec.ts
196 lines (171 loc) · 6.02 KB
/
command.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { expect } from "chai";
import * as sinon from "sinon";
import * as rc from "./rc";
import * as nock from "nock";
import { Command, validateProjectId } from "./command";
import { FirebaseError } from "./error";
describe("Command", () => {
let command: Command;
beforeEach(() => {
command = new Command("example");
});
it("should allow all basic behavior", () => {
expect(() => {
command.description("description!");
command.option("-x, --foobar", "description", "value");
command.withForce();
command.before(
(arr: string[]) => {
return arr;
},
["foo", "bar"],
);
command.alias("example2");
command.help("here's how!");
command.action(() => {
// do nothing
});
}).not.to.throw();
});
describe("runner", () => {
let rcStub: sinon.SinonStub;
beforeEach(() => {
rcStub = sinon
.stub(rc, "loadRC")
.returns(new rc.RC(undefined, { projects: { default: "default-project" } }));
});
afterEach(() => {
rcStub.restore();
nock.cleanAll();
});
it("should work when no arguments are passed and options", async () => {
const run = command
.action((options) => {
options.foo = "bar";
return options;
})
.runner();
const result = run({ foo: "baz" });
await expect(result).to.eventually.have.property("foo", "bar");
});
it("should execute befores before the action", async () => {
const run = command
.before((options) => {
options.foo = true;
})
.action((options) => {
if (options.foo) {
options.bar = "baz";
}
return options;
})
.runner();
const result = run({});
await expect(result).to.eventually.have.property("bar");
});
it("should terminate execution if a before errors", async () => {
const run = command
.before(() => {
throw new Error("foo");
})
.action(() => {
throw new Error("THIS IS NOT FOO");
})
.runner();
const result = run();
return expect(result).to.be.rejectedWith("foo");
});
it("should reject the promise if an error is thrown", async () => {
const run = command
.action(() => {
throw new Error("foo");
})
.runner();
const result = run();
await expect(result).to.be.rejectedWith("foo");
});
it("should resolve a numeric --project flag into a project id", async () => {
nock("https://cloudresourcemanager.googleapis.com").get("/v1/projects/12345678").reply(200, {
projectNumber: "12345678",
projectId: "resolved-project",
});
const run = command
.action((options) => {
return {
project: options.project,
projectNumber: options.projectNumber,
projectId: options.projectId,
};
})
.runner();
const result = await run({ project: "12345678", token: "thisisatoken" });
expect(result).to.deep.eq({
projectId: "resolved-project",
projectNumber: "12345678",
project: "12345678",
});
});
it("should resolve a non-numeric --project flag into a project id", async () => {
const run = command
.action((options) => {
return {
project: options.project,
projectNumber: options.projectNumber,
projectId: options.projectId,
};
})
.runner();
const result = await run({ project: "resolved-project" });
expect(result).to.deep.eq({
projectId: "resolved-project",
projectNumber: undefined,
project: "resolved-project",
});
});
it("should use the 'default' alias if no project is passed", async () => {
const run = command
.action((options) => {
return {
project: options.project,
projectNumber: options.projectNumber,
projectId: options.projectId,
};
})
.runner();
const result = await run({});
expect(result).to.deep.eq({
projectId: "default-project",
projectNumber: undefined,
project: "default-project",
});
});
});
});
describe("validateProjectId", () => {
it("should not throw for valid project ids", () => {
expect(() => validateProjectId("example")).not.to.throw();
expect(() => validateProjectId("my-project")).not.to.throw();
expect(() => validateProjectId("myproject4fun")).not.to.throw();
});
it("should not throw for legacy project ids", () => {
// The project IDs below are not technically valid, but some legacy projects
// may have IDs like that. We should not block these.
// https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects#resource:-project
expect(() => validateProjectId("example-")).not.to.throw();
expect(() => validateProjectId("0123456")).not.to.throw();
expect(() => validateProjectId("google.com:some-project")).not.to.throw();
});
it("should block invalid project ids", () => {
expect(() => validateProjectId("EXAMPLE")).to.throw(FirebaseError, /Invalid project id/);
expect(() => validateProjectId("!")).to.throw(FirebaseError, /Invalid project id/);
expect(() => validateProjectId("with space")).to.throw(FirebaseError, /Invalid project id/);
expect(() => validateProjectId(" leadingspace")).to.throw(FirebaseError, /Invalid project id/);
expect(() => validateProjectId("trailingspace ")).to.throw(FirebaseError, /Invalid project id/);
expect(() => validateProjectId("has.dot")).to.throw(FirebaseError, /Invalid project id/);
});
it("should error with additional note for uppercase project ids", () => {
expect(() => validateProjectId("EXAMPLE")).to.throw(FirebaseError, /lowercase/);
expect(() => validateProjectId("Example")).to.throw(FirebaseError, /lowercase/);
expect(() => validateProjectId("Example-Project")).to.throw(FirebaseError, /lowercase/);
});
});