Skip to content

Add new option to preserveExternalChanges. #1253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions scripts/bin-test/sources/commonjs-preserve/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const functions = require("firebase-functions");
const functionsv2 = require("firebase-functions/v2");

exports.v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

exports.v1httpPreserve = functions
.runWith({ preserveExternalChanges: true })
.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});

functionsv2.setGlobalOptions({ preserveExternalChanges: true });

exports.v2http = functionsv2.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
});
3 changes: 3 additions & 0 deletions scripts/bin-test/sources/commonjs-preserve/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "commonjs-preserve"
}
49 changes: 48 additions & 1 deletion scripts/bin-test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,44 @@ const TIMEOUT_L = 10_000;
const TIMEOUT_M = 5_000;
const TIMEOUT_S = 1_000;

const DEFAULT_OPTIONS = {
availableMemoryMb: null,
maxInstances: null,
minInstances: null,
timeoutSeconds: null,
vpc: null,
serviceAccountEmail: null,
ingressSettings: null,
};

const DEFAULT_V1_OPTIONS = { ...DEFAULT_OPTIONS };

const DEFAULT_V2_OPTIONS = { ...DEFAULT_OPTIONS, concurrency: null };

const BASE_STACK = {
endpoints: {
v1http: {
...DEFAULT_V1_OPTIONS,
platform: "gcfv1",
entryPoint: "v1http",
httpsTrigger: {},
},
v1callable: {
...DEFAULT_V1_OPTIONS,
platform: "gcfv1",
entryPoint: "v1callable",
labels: {},
callableTrigger: {},
},
v2http: {
...DEFAULT_V2_OPTIONS,
platform: "gcfv2",
entryPoint: "v2http",
labels: {},
httpsTrigger: {},
},
v2callable: {
...DEFAULT_V2_OPTIONS,
platform: "gcfv2",
entryPoint: "v2callable",
labels: {},
Expand Down Expand Up @@ -82,7 +100,7 @@ async function startBin(
const getPort = promisify(portfinder.getPort) as () => Promise<number>;
const port = await getPort();

const proc = subprocess.spawn("./node_modules/.bin/firebase-functions", [], {
const proc = subprocess.spawn("npx", ["firebase-functions"], {
cwd: path.resolve(tc.modulePath),
env: {
PATH: process.env.PATH,
Expand Down Expand Up @@ -183,11 +201,13 @@ describe("functions.yaml", () => {
endpoints: {
...BASE_STACK.endpoints,
"g1-groupedhttp": {
...DEFAULT_V1_OPTIONS,
platform: "gcfv1",
entryPoint: "g1.groupedhttp",
httpsTrigger: {},
},
"g1-groupedcallable": {
...DEFAULT_V1_OPTIONS,
platform: "gcfv1",
entryPoint: "g1.groupedcallable",
labels: {},
Expand All @@ -196,6 +216,33 @@ describe("functions.yaml", () => {
},
},
},
{
name: "preserveChange",
modulePath: "./scripts/bin-test/sources/commonjs-preserve",
expected: {
endpoints: {
v1http: {
...DEFAULT_V1_OPTIONS,
platform: "gcfv1",
entryPoint: "v1http",
httpsTrigger: {},
},
v1httpPreserve: {
platform: "gcfv1",
entryPoint: "v1httpPreserve",
httpsTrigger: {},
},
v2http: {
platform: "gcfv2",
entryPoint: "v2http",
labels: {},
httpsTrigger: {},
},
},
requiredAPIs: [],
specVersion: "v1alpha1",
},
},
];

for (const tc of testcases) {
Expand Down
36 changes: 36 additions & 0 deletions spec/common/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE ignoreUnusedWarning OR OTHER DEALINGS IN THE
// SOFTWARE.
import { ResettableKeys, ResetValue } from "../../src/common/options";
import { expectNever, expectType } from "./metaprogramming";

describe("ResettableKeys", () => {
it("should pick out keys with a type that includes ResetValue", () => {
type A = { a: number; b: ResetValue; c: number | boolean | ResetValue };
expectType<keyof ResettableKeys<A>>("b");
expectType<keyof ResettableKeys<A>>("c");
});

it("should return an empty type if no keys are resettable", () => {
type A = { a: number };
expectNever<keyof ResettableKeys<A>>();
});
});
64 changes: 64 additions & 0 deletions spec/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import { ManifestEndpoint } from "../src/runtime/manifest";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing copyright notice, maybe we can have the linter add things like this?

import { RESET_VALUE } from "../src/common/options";

export const MINIMAL_V2_ENDPOINT: ManifestEndpoint = {
availableMemoryMb: RESET_VALUE,
concurrency: RESET_VALUE,
ingressSettings: RESET_VALUE,
maxInstances: RESET_VALUE,
minInstances: RESET_VALUE,
serviceAccountEmail: RESET_VALUE,
timeoutSeconds: RESET_VALUE,
vpc: RESET_VALUE,
};

export const MINIMAL_V1_ENDPOINT: ManifestEndpoint = {
availableMemoryMb: RESET_VALUE,
ingressSettings: RESET_VALUE,
maxInstances: RESET_VALUE,
minInstances: RESET_VALUE,
serviceAccountEmail: RESET_VALUE,
timeoutSeconds: RESET_VALUE,
vpc: RESET_VALUE,
};

export const FULL_ENDPOINT: ManifestEndpoint = {
region: ["us-west1"],
availableMemoryMb: 512,
timeoutSeconds: 60,
minInstances: 1,
maxInstances: 3,
concurrency: 20,
vpc: {
connector: "aConnector",
egressSettings: "ALL_TRAFFIC",
},
serviceAccountEmail: "root@",
ingressSettings: "ALLOW_ALL",
cpu: "gcf_gen1",
labels: {
hello: "world",
},
secretEnvironmentVariables: [{ key: "MY_SECRET" }],
};
29 changes: 25 additions & 4 deletions spec/runtime/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as functions from "../../src/v1";
import * as loader from "../../src/runtime/loader";
import { ManifestEndpoint, ManifestRequiredAPI, ManifestStack } from "../../src/runtime/manifest";
import { clearParams } from "../../src/params";
import { MINIMAL_V1_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../fixtures";
import { MINIMAL_SCHEDULE_TRIGGER, MINIMIAL_TASK_QUEUE_TRIGGER } from "../v1/providers/fixtures";

describe("extractStack", () => {
const httpFn = functions.https.onRequest(() => undefined);
Expand Down Expand Up @@ -32,8 +34,16 @@ describe("extractStack", () => {
loader.extractStack(module, endpoints, requiredAPIs);

expect(endpoints).to.be.deep.equal({
http: { entryPoint: "http", ...httpEndpoint },
callable: { entryPoint: "callable", ...callableEndpoint },
http: {
...MINIMAL_V1_ENDPOINT,
entryPoint: "http",
...httpEndpoint,
},
callable: {
...MINIMAL_V1_ENDPOINT,
entryPoint: "callable",
...callableEndpoint,
},
});

expect(requiredAPIs).to.be.empty;
Expand All @@ -51,9 +61,10 @@ describe("extractStack", () => {

expect(endpoints).to.be.deep.equal({
taskq: {
...MINIMAL_V1_ENDPOINT,
entryPoint: "taskq",
platform: "gcfv1",
taskQueueTrigger: {},
taskQueueTrigger: MINIMIAL_TASK_QUEUE_TRIGGER,
},
});

Expand All @@ -80,10 +91,12 @@ describe("extractStack", () => {

expect(endpoints).to.be.deep.equal({
fn1: {
...MINIMAL_V1_ENDPOINT,
entryPoint: "fn1",
...httpEndpoint,
},
"g1-fn2": {
...MINIMAL_V1_ENDPOINT,
entryPoint: "g1.fn2",
...httpEndpoint,
},
Expand Down Expand Up @@ -116,6 +129,7 @@ describe("extractStack", () => {

expect(endpoints).to.be.deep.equal({
fn: {
...MINIMAL_V1_ENDPOINT,
entryPoint: "fn",
platform: "gcfv1",
eventTrigger: {
Expand All @@ -142,11 +156,12 @@ describe("extractStack", () => {

expect(endpoints).to.be.deep.equal({
scheduled: {
...MINIMAL_V1_ENDPOINT,
entryPoint: "scheduled",
platform: "gcfv1",
// TODO: This label should not exist?
labels: {},
scheduleTrigger: { schedule: "every 5 minutes" },
scheduleTrigger: { ...MINIMAL_SCHEDULE_TRIGGER, schedule: "every 5 minutes" },
},
});

Expand Down Expand Up @@ -217,23 +232,27 @@ describe("loadStack", () => {
const expected: ManifestStack = {
endpoints: {
v1http: {
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
entryPoint: "v1http",
httpsTrigger: {},
},
v1callable: {
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
entryPoint: "v1callable",
labels: {},
callableTrigger: {},
},
v2http: {
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
entryPoint: "v2http",
labels: {},
httpsTrigger: {},
},
v2callable: {
...MINIMAL_V2_ENDPOINT,
platform: "gcfv2",
entryPoint: "v2callable",
labels: {},
Expand Down Expand Up @@ -293,11 +312,13 @@ describe("loadStack", () => {
endpoints: {
...expected.endpoints,
"g1-groupedhttp": {
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
entryPoint: "g1.groupedhttp",
httpsTrigger: {},
},
"g1-groupedcallable": {
...MINIMAL_V1_ENDPOINT,
platform: "gcfv1",
entryPoint: "g1.groupedcallable",
labels: {},
Expand Down
Loading