Skip to content

Commit

Permalink
Add integration test for discovering functions from a project reposit…
Browse files Browse the repository at this point in the history
…ory.
  • Loading branch information
taeold committed Nov 4, 2022
1 parent 0b62447 commit 960f1b4
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/node-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
- npm run test:emulator
- npm run test:extensions-emulator
- npm run test:frameworks
- npm run test:functions-discover
- npm run test:hosting
# - npm run test:hosting-rewrites # Long-running test that might conflict across test runs. Run this manually.
- npm run test:import-export
Expand Down Expand Up @@ -142,6 +143,7 @@ jobs:
- npm run test:emulator
# - npm run test:import-export # Fails becuase port 4000 is taken after first run - hub not shhutting down?
# - npm run test:extensions-emulator # Fails due to cannot find module sharp (not waiting for npm install?)
- npm run test:functions-discover
- npm run test:triggers-end-to-end
- npm run test:triggers-end-to-end:inspect
- npm run test:storage-deploy
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"test:extensions-emulator": "bash ./scripts/extensions-emulator-tests/run.sh",
"test:frameworks": "bash ./scripts/frameworks-tests/run.sh",
"test:functions-deploy": "bash ./scripts/functions-deploy-tests/run.sh",
"test:functions-discover": "bash ./scripts/functions-discover-tests/run.sh",
"test:hosting": "bash ./scripts/hosting-tests/run.sh",
"test:hosting-rewrites": "bash ./scripts/hosting-tests/rewrites-tests/run.sh",
"test:import-export": "bash ./scripts/emulator-import-export-tests/run.sh",
Expand Down
26 changes: 26 additions & 0 deletions scripts/functions-discover-tests/fixtures/codebases/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"functions": [
{
"source": "v1",
"codebase": "v1",
"runtime": "nodejs16",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
},
{
"source": "v2",
"codebase": "v2",
"runtime": "nodejs16",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -euxo pipefail # bash strict mode
IFS=$'\n\t'

(cd v1 && npm i)
(cd v2 && npm i)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const functions = require("firebase-functions");

exports.hellov1 = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "codebase-v1",
"dependencies": {
"firebase-functions": "^4.0.0"
},
"engines": {
"node": "16"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { onRequest } from "firebase-functions/v2/https";

export const hellov2 = onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "codebase-v2",
"type": "module",
"dependencies": {
"firebase-functions": "^4.0.0"
},
"engines": {
"node": "16"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions scripts/functions-discover-tests/fixtures/esm/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as functions from "firebase-functions";
import { onRequest } from "firebase-functions/v2/https";

export const hellov1 = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", { structuredData: true });
response.send("Hello from Firebase!");
});

export const hellov2 = onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "esm",
"type": "module",
"dependencies": {
"firebase-functions": "^4.0.0"
},
"engines": {
"node": "16"
}
}
5 changes: 5 additions & 0 deletions scripts/functions-discover-tests/fixtures/esm/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euxo pipefail # bash strict mode
IFS=$'\n\t'

cd functions && npm i
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const functions = require("firebase-functions");
const { onRequest } = require("firebase-functions/v2/https");

exports.hellov1 = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});

exports.hellov2 = onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "simple",
"dependencies": {
"firebase-functions": "^4.0.0"
},
"engines": {
"node": "16"
}
}
5 changes: 5 additions & 0 deletions scripts/functions-discover-tests/fixtures/simple/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euxo pipefail # bash strict mode
IFS=$'\n\t'

cd functions && npm i
12 changes: 12 additions & 0 deletions scripts/functions-discover-tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -euxo pipefail # bash strict mode
IFS=$'\n\t'

# Globally link the CLI for the testing framework
./scripts/npm-link.sh

for dir in ./scripts/functions-discover-tests/fixtures/*; do
(cd $dir && ./install.sh)
done

mocha scripts/functions-discover-tests/tests.ts
87 changes: 87 additions & 0 deletions scripts/functions-discover-tests/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as path from "path";

import { expect } from "chai";
import { CLIProcess } from "../integration-helpers/cli";
import { requireAuth } from "../../src/requireAuth";

const FIXTURES = path.join(__dirname, "fixtures");
const FIREBASE_PROJECT = process.env.FBTOOLS_TARGET_PROJECT || "danielylee-test-6";

interface Testcase {
name: string;
projectDir: string;
expects: {
codebase: string;
endpoints: string[];
}[];
}

describe("Function discovery test", function (this) {
this.timeout(1000_000);

before(async () => {
expect(FIREBASE_PROJECT).to.exist.and.not.be.empty;
if (process.env.CI === "true") {
// In CI, get auth credentials from environment variable.
await requireAuth({});
}
});

const testCases: Testcase[] = [
{
name: "simple",
projectDir: "simple",
expects: [
{
codebase: "default",
endpoints: ["hellov1", "hellov2"],
},
],
},
{
name: "esm",
projectDir: "esm",
expects: [
{
codebase: "default",
endpoints: ["hellov1", "hellov2"],
},
],
},
{
name: "codebases",
projectDir: "codebases",
expects: [
{
codebase: "v1",
endpoints: ["hellov1"],
},
{
codebase: "v2",
endpoints: ["hellov2"],
},
],
},
];

for (const tc of testCases) {
it(`discovers functions in a ${tc.name} project`, async () => {
const cli = new CLIProcess("default", path.join(FIXTURES, tc.projectDir));

let output: any;
await cli.start("functions:discover", FIREBASE_PROJECT, ["--json"], (data: any) => {
output = JSON.parse(data);
return true;
});
expect(output.status).to.equal("success");
for (const e of tc.expects) {
const endpoints = output.result?.[e.codebase]?.endpoints;
expect(endpoints).to.be.an("object").that.is.not.empty;
expect(Object.keys(endpoints)).to.have.length(e.endpoints.length);
expect(Object.keys(endpoints)).to.include.members(e.endpoints);
}

await cli.stop();
});
}
});

0 comments on commit 960f1b4

Please sign in to comment.