Skip to content

Commit

Permalink
adds filterExcept to normalizedHostingConfigs (#3593)
Browse files Browse the repository at this point in the history
* adds filterExcept to normalizedHostingConfigs

fixes #3397

* remove .only

* add changelog

* pass only'd configs to except

* one clarifying comment

* rename and cleanup
  • Loading branch information
bkendall committed Jul 23, 2021
1 parent c9560dc commit b5b6e5f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Fix Auth Emulator errors when importing many users (#3577).
- Fixes Auth Emulator errors when importing many users. (#3577)
- Fixes support for `--except` flag when used for deploying Hosting. (#3397)
28 changes: 27 additions & 1 deletion src/hosting/normalizedHostingConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ function filterOnly(configs: HostingConfig[], onlyString: string): HostingConfig
return filteredConfigs;
}

function filterExcept(configs: HostingConfig[], exceptOption: string): HostingConfig[] {
if (!exceptOption) {
return configs;
}

const exceptTargets = exceptOption.split(",");
if (exceptTargets.includes("hosting")) {
return [];
}

const exceptValues = new Set(
exceptTargets.filter((t) => t.startsWith("hosting:")).map((t) => t.replace("hosting:", ""))
);

const filteredConfigs: HostingConfig[] = [];
for (const c of configs) {
if (!(exceptValues.has(c.site) || exceptValues.has(c.target))) {
filteredConfigs.push(c);
}
}

return filteredConfigs;
}

/**
* Normalize options to HostingConfig array.
* @param cmdOptions the Firebase CLI options object.
Expand Down Expand Up @@ -89,7 +113,9 @@ export function normalizedHostingConfigs(
}
}

const hostingConfigs: HostingConfig[] = filterOnly(configs, cmdOptions.only);
// filter* functions check if the strings are empty for us.
let hostingConfigs: HostingConfig[] = filterOnly(configs, cmdOptions.only);
hostingConfigs = filterExcept(hostingConfigs, cmdOptions.except);

if (options.resolveTargets) {
for (const cfg of hostingConfigs) {
Expand Down
100 changes: 100 additions & 0 deletions src/test/hosting/normalizedHostingConfigs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,104 @@ describe("normalizedHostingConfigs", () => {
});
}
});

describe("with an except parameter, resolving targets", () => {
const DEFAULT_SITE = "default-hosting-site";
const TARGETED_SITE = "targeted-site";
const baseConfig = { public: "public", ignore: ["firebase.json"] };
const tests = [
{
desc: "a normal hosting config, omitting the default site",
cfg: Object.assign({}, baseConfig),
except: `hosting:${DEFAULT_SITE}`,
want: [],
},
{
desc: "a hosting config with multiple sites, no targets, omitting the second site",
cfg: [
Object.assign({}, baseConfig, { site: DEFAULT_SITE }),
Object.assign({}, baseConfig, { site: "different-site" }),
],
except: `hosting:different-site`,
want: [Object.assign({}, baseConfig, { site: DEFAULT_SITE })],
},
{
desc: "a normal hosting config with a target, omitting the target",
cfg: Object.assign({}, baseConfig, { target: "main" }),
except: "hosting:main",
want: [],
},
{
desc: "a hosting config with multiple targets, omitting one",
cfg: [
Object.assign({}, baseConfig, { target: "t-one" }),
Object.assign({}, baseConfig, { target: "t-two" }),
],
except: "hosting:t-two",
want: [Object.assign({}, baseConfig, { target: "t-one", site: TARGETED_SITE })],
},
{
desc: "a hosting config with multiple targets, omitting all hosting",
cfg: [
Object.assign({}, baseConfig, { target: "t-one" }),
Object.assign({}, baseConfig, { target: "t-two" }),
],
except: "hosting",
want: [],
},
{
desc: "a hosting config with multiple targets, omitting an invalid target",
cfg: [
Object.assign({}, baseConfig, { target: "t-one" }),
Object.assign({}, baseConfig, { target: "t-two" }),
],
except: "hosting:t-three",
want: [
Object.assign({}, baseConfig, { target: "t-one", site: TARGETED_SITE }),
Object.assign({}, baseConfig, { target: "t-two", site: TARGETED_SITE }),
],
},
{
desc: "a hosting config with multiple targets, with multiple matching targets",
cfg: [
Object.assign({}, baseConfig, { target: "t-one" }),
Object.assign({}, baseConfig, { target: "t-one" }),
Object.assign({}, baseConfig, { target: "t-other" }),
],
except: "hosting:t-other",
targetedSites: [TARGETED_SITE, TARGETED_SITE],
wantErr: /Hosting target.+t-one.+linked to multiple sites/,
},
{
desc: "a hosting config with multiple sites but no targets, only all hosting",
cfg: [Object.assign({}, baseConfig), Object.assign({}, baseConfig)],
except: "hosting:site",
wantErr: /Must supply either "site" or "target"/,
},
];

for (const t of tests) {
it(`should be able to parse ${t.desc}`, () => {
if (!Array.isArray(t.targetedSites)) {
t.targetedSites = [TARGETED_SITE];
}
const cmdConfig = {
site: DEFAULT_SITE,
except: t.except,
config: { get: () => t.cfg },
rc: { requireTarget: () => t.targetedSites },
};

if (t.wantErr) {
expect(() => normalizedHostingConfigs(cmdConfig, { resolveTargets: true })).to.throw(
FirebaseError,
t.wantErr
);
} else {
const got = normalizedHostingConfigs(cmdConfig, { resolveTargets: true });
expect(got).to.deep.equal(t.want);
}
});
}
});
});

0 comments on commit b5b6e5f

Please sign in to comment.