-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathconfig.spec.ts
More file actions
193 lines (165 loc) · 6.38 KB
/
config.spec.ts
File metadata and controls
193 lines (165 loc) · 6.38 KB
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
import * as path from "path";
import * as fs from "fs";
import * as os from "os";
import { expect } from "chai";
import { Config } from "./config";
import { FIREBASE_JSON_PATH as VALID_CONFIG_PATH } from "./test/fixtures/valid-config";
import { FIXTURE_DIR as SIMPLE_CONFIG_DIR } from "./test/fixtures/config-imports";
import { FIXTURE_DIR as DUP_TOP_LEVEL_CONFIG_DIR } from "./test/fixtures/dup-top-level";
import { FIREBASE_JSON_PATH as EMPTY_CONFIG_PATH } from "./test/fixtures/empty-config";
describe("Config", () => {
describe("#load", () => {
it("should load a cjson file when configPath is specified", () => {
const cwd = __dirname;
const config = Config.load({
cwd,
configPath: path.relative(cwd, VALID_CONFIG_PATH),
});
expect(config).to.not.be.null;
expect(config?.get("database.rules")).to.eq("config/security-rules.json");
});
it("should fall back to {} when the file is empty", () => {
const cwd = __dirname;
const config = Config.load({
cwd,
configPath: path.relative(cwd, EMPTY_CONFIG_PATH),
});
expect(config).to.not.be.null;
expect(config?.data).to.deep.eq({});
});
});
describe("#path", () => {
it("should skip an absolute path", () => {
const config = new Config({}, { cwd: SIMPLE_CONFIG_DIR });
const absPath = "/Users/something";
expect(config.path(absPath)).to.eq(absPath);
});
it("should append a non-absolute path", () => {
const config = new Config({}, { cwd: SIMPLE_CONFIG_DIR });
const relativePath = "something";
expect(config.path(relativePath)).to.eq(path.join(SIMPLE_CONFIG_DIR, relativePath));
});
});
describe("#parseFile", () => {
it("should load a cjson file", () => {
const config = new Config({}, { cwd: SIMPLE_CONFIG_DIR });
expect(config.parseFile("hosting", "hosting.json").public).to.equal(".");
});
it("should error out for an unknown file", () => {
const config = new Config({}, { cwd: SIMPLE_CONFIG_DIR });
expect(() => {
config.parseFile("hosting", "i-dont-exist.json");
}).to.throw("Imported file i-dont-exist.json does not exist");
});
it("should error out for an unrecognized extension", () => {
const config = new Config({}, { cwd: SIMPLE_CONFIG_DIR });
expect(() => {
config.parseFile("hosting", "unsupported.txt");
}).to.throw("unsupported.txt is not of a supported config file type");
});
});
describe("#materialize", () => {
it("should assign unaltered if an object is found", () => {
const config = new Config({ example: { foo: "bar" } }, {});
expect(config.materialize("example").foo).to.equal("bar");
});
it("should prevent top-level key duplication", () => {
const config = new Config({ rules: "rules.json" }, { cwd: DUP_TOP_LEVEL_CONFIG_DIR });
expect(config.materialize("rules")).to.deep.equal({ ".read": true });
});
});
describe("functions.source", () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "firebase-test"));
});
afterEach(() => {
if (tmpDir) {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});
it("injects default source when default dir exists but source is missing", () => {
fs.mkdirSync(path.join(tmpDir, Config.DEFAULT_FUNCTIONS_SOURCE));
const cfg = new Config({ functions: {} }, { cwd: tmpDir, projectDir: tmpDir });
expect(cfg.get("functions.source")).to.be.equal("functions");
});
it("does not injects default source when default dir is missing", () => {
const cfg = new Config(
{ functions: { runtime: "nodejs20" } },
{ cwd: tmpDir, projectDir: tmpDir },
);
expect(cfg.get("functions.source")).to.be.undefined;
});
it("does not inject source for remoteSource", () => {
fs.mkdirSync(path.join(tmpDir, Config.DEFAULT_FUNCTIONS_SOURCE));
const cfg = new Config(
{
functions: {
remoteSource: { repository: "https://github.com/org/repo", ref: "main" },
runtime: "nodejs20",
},
},
{ cwd: tmpDir, projectDir: tmpDir },
);
expect(cfg.get("functions.source")).to.be.undefined;
});
it("injects into the first empty entry only when default dir exists", () => {
fs.mkdirSync(path.join(tmpDir, Config.DEFAULT_FUNCTIONS_SOURCE));
const cfg = new Config(
{
functions: [
{ source: "custom-functions" },
{ runtime: "nodejs20" },
{
remoteSource: { repository: "https://github.com/org/repo", ref: "main" },
runtime: "nodejs20",
},
],
},
{ cwd: tmpDir, projectDir: tmpDir },
);
expect(cfg.get("functions.[0].source")).to.equal("custom-functions");
expect(cfg.get("functions.[1].source")).to.equal("functions");
expect(cfg.get("functions.[2].source")).to.be.undefined;
});
it("injects only one entry when multiple are empty", () => {
fs.mkdirSync(path.join(tmpDir, Config.DEFAULT_FUNCTIONS_SOURCE));
const cfg = new Config(
{
functions: [{}, {}],
},
{ cwd: tmpDir, projectDir: tmpDir },
);
// Injects into the first empty entry only
expect(cfg.get("functions.[0].source")).to.equal("functions");
expect(cfg.get("functions.[1].source")).to.be.undefined;
});
it("does not inject when no entry is empty", () => {
fs.mkdirSync(path.join(tmpDir, Config.DEFAULT_FUNCTIONS_SOURCE));
const cfg = new Config(
{
functions: [
{ source: "dir-a" },
{
remoteSource: { repository: "https://github.com/org/repo", ref: "main" },
runtime: "nodejs20",
},
],
},
{ cwd: tmpDir, projectDir: tmpDir },
);
expect(cfg.get("functions.[0].source")).to.equal("dir-a");
expect(cfg.get("functions.[1].source")).to.be.undefined;
});
it("does not inject for arrays when default dir is missing", () => {
const cfg = new Config(
{
functions: [{}, { source: "something" }],
},
{ cwd: tmpDir, projectDir: tmpDir },
);
expect(cfg.get("functions.[0].source")).to.be.undefined;
expect(cfg.get("functions.[1].source")).to.equal("something");
});
});
});