Skip to content

Commit

Permalink
adds filterExcept to normalizedHostingConfigs
Browse files Browse the repository at this point in the history
fixes #3397
  • Loading branch information
bkendall committed Jul 21, 2021
1 parent 92a30dc commit c2f71fa
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/hosting/normalizedHostingConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,41 @@ function filterOnly(configs: HostingConfig[], onlyString: string): HostingConfig
return filteredConfigs;
}

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

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

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

const configsBySite = new Map<string, HostingConfig>();
const configsByTarget = new Map<string, HostingConfig>();
for (const c of configs) {
if (c.site) {
configsBySite.set(c.site, c);
}
if (c.target) {
configsByTarget.set(c.target, c);
}
}

const filteredConfigs: HostingConfig[] = [];
for (const c of configs) {
if (!(exceptSet.has(c.site) || exceptSet.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 +124,8 @@ export function normalizedHostingConfigs(
}
}

const hostingConfigs: HostingConfig[] = filterOnly(configs, cmdOptions.only);
let hostingConfigs: HostingConfig[] = filterOnly(configs, cmdOptions.only);
hostingConfigs = filterExcept(configs, 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.only("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 c2f71fa

Please sign in to comment.