Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Adds support for setting user labels on functions via `runWith()`.
- Adds support for FIREBASE_CONFIG env as the name of a JSON file
- Formalize module exports. Loggers can now be accessed at 'firebase-functions/logger' and 'firebase-functions/logger/compat'
- Fixes an issue where Remote Config coiuld not be emulated in Windows machines on the classic Command Prompt.
29 changes: 19 additions & 10 deletions spec/v1/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,55 @@

import { expect } from 'chai';
import * as fs from 'fs';
import * as mockRequire from 'mock-require';
import * as process from 'process';
import Sinon = require('sinon');

import * as config from '../../src/v1/config';

describe('config()', () => {
let readFileSync: Sinon.SinonStub;
let cwdStub: Sinon.SinonStub;

before(() => {
readFileSync = Sinon.stub(fs, 'readFileSync');
readFileSync.throws('Unexpected call');
process.env.PWD = '/srv';
cwdStub = Sinon.stub(process, 'cwd');
cwdStub.returns('/srv');
});

after(() => {
delete process.env.PWD;
Sinon.verifyAndRestore();
});

afterEach(() => {
mockRequire.stopAll();
delete config.config.singleton;
(config as any).firebaseConfigCache = null;
delete process.env.FIREBASE_CONFIG;
delete process.env.CLOUD_RUNTIME_CONFIG;
});

it('loads config values from .runtimeconfig.json', () => {
mockRequire('/srv/.runtimeconfig.json', { foo: 'bar', firebase: {} });
const json = JSON.stringify({
foo: 'bar',
firebase: {},
});
readFileSync
.withArgs('/srv/.runtimeconfig.json')
.returns(Buffer.from(json));
const loaded = config.config();
expect(loaded).to.not.have.property('firebase');
expect(loaded).to.have.property('foo', 'bar');
});

it('does not provide firebase config if .runtimeconfig.json not invalid', () => {
mockRequire('/srv/.runtimeconfig.json', 'does-not-exist');
readFileSync.withArgs('/srv/.runtimeconfig.json').returns('invalid JSON');
expect(config.firebaseConfig()).to.be.null;
});

it('does not provide firebase config if .ruuntimeconfig.json has no firebase property', () => {
mockRequire('/srv/.runtimeconfig.json', {});
readFileSync
.withArgs('/srv/.runtimeconfig.json')
.returns(Buffer.from('{}'));
expect(config.firebaseConfig()).to.be.null;
});

Expand All @@ -78,7 +86,7 @@ describe('config()', () => {

it('loads Firebase configs from FIREBASE_CONFIG env variable pointing to a file', () => {
const oldEnv = process.env;
process.env = {
(process as any).env = {
...oldEnv,
FIREBASE_CONFIG: '.firebaseconfig.json',
};
Expand All @@ -91,13 +99,14 @@ describe('config()', () => {
'foo@firebaseio.com'
);
} finally {
process.env = oldEnv;
(process as any).env = oldEnv;
}
});

it('accepts alternative locations for config file', () => {
process.env.CLOUD_RUNTIME_CONFIG = 'another.json';
mockRequire('another.json', { foo: 'bar', firebase: {} });
const json = JSON.stringify({ foo: 'bar', firebase: {} });
readFileSync.withArgs('another.json').returns(Buffer.from(json));
expect(config.firebaseConfig()).to.not.be.null;
expect(config.config()).to.have.property('foo', 'bar');
});
Expand Down
10 changes: 6 additions & 4 deletions src/v1/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ export function firebaseConfig(): firebase.AppOptions | null {
try {
const configPath =
process.env.CLOUD_RUNTIME_CONFIG ||
path.join(process.env.PWD, '.runtimeconfig.json');
const config = require(configPath);
path.join(process.cwd(), '.runtimeconfig.json');
const contents = fs.readFileSync(configPath);
const config = JSON.parse(contents.toString('utf8'));
if (config.firebase) {
firebaseConfigCache = config.firebase;
return firebaseConfigCache;
Expand All @@ -115,8 +116,9 @@ function init() {
try {
const configPath =
process.env.CLOUD_RUNTIME_CONFIG ||
path.join(process.env.PWD, '.runtimeconfig.json');
const parsed = require(configPath);
path.join(process.cwd(), '.runtimeconfig.json');
const contents = fs.readFileSync(configPath);
const parsed = JSON.parse(contents.toString('utf8'));
delete parsed.firebase;
config.singleton = parsed;
return;
Expand Down