Skip to content

Cache emulator between runs #3956

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 1 commit into from
Oct 29, 2020
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
2 changes: 1 addition & 1 deletion scripts/emulator-testing/emulators/database-emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class DatabaseEmulator extends Emulator {

constructor(port = 8088, namespace = 'test-emulator') {
super(
'database-emulator.jar',
'firebase-database-emulator-v4.4.1.jar',
// Use locked version of emulator for test to be deterministic.
// The latest version can be found from database emulator doc:
// https://firebase.google.com/docs/database/security/test-rules-emulator
Expand Down
38 changes: 37 additions & 1 deletion scripts/emulator-testing/emulators/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { spawn } from 'child-process-promise';
import { ChildProcess } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as request from 'request';
// @ts-ignore
Expand All @@ -28,13 +29,25 @@ export abstract class Emulator {
binaryPath: string | null = null;
emulator: ChildProcess | null = null;

cacheDirectory: string;
cacheBinaryPath: string;

constructor(
private binaryName: string,
private binaryUrl: string,
public port: number
) {}
) {
this.cacheDirectory = path.join(os.homedir(), `.cache/firebase-js-sdk`);
this.cacheBinaryPath = path.join(this.cacheDirectory, binaryName);
}

download(): Promise<void> {
if (fs.existsSync(this.cacheBinaryPath)) {
console.log(`Emulator found in cache: ${this.cacheBinaryPath}`);
this.binaryPath = this.cacheBinaryPath;
return Promise.resolve();
}

return new Promise<void>((resolve, reject) => {
tmp.dir((err: Error | null, dir: string) => {
if (err) reject(err);
Expand All @@ -55,6 +68,10 @@ export abstract class Emulator {
if (err) reject(err);
console.log(`Changed emulator file permissions to 'rwxr-xr-x'.`);
this.binaryPath = filepath;

if (this.copyToCache()) {
console.log(`Cached emulator at ${this.cacheBinaryPath}`);
}
resolve();
});
})
Expand Down Expand Up @@ -129,4 +146,23 @@ export abstract class Emulator {
fs.unlinkSync(this.binaryPath);
}
}

private copyToCache(): boolean {
if (!this.binaryPath) {
return false;
}

try {
if (!fs.existsSync(this.cacheDirectory)) {
fs.mkdirSync(this.cacheDirectory, { recursive: true });
}
fs.copyFileSync(this.binaryPath, this.cacheBinaryPath);

return true;
} catch (e) {
console.warn(`Unable to cache ${this.binaryName}`, e);
}

return false;
}
}
2 changes: 1 addition & 1 deletion scripts/emulator-testing/emulators/firestore-emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class FirestoreEmulator extends Emulator {

constructor(port: number, projectId = 'test-emulator') {
super(
'firestore-emulator.jar',
'cloud-firestore-emulator-v1.11.7.jar',
// Use locked version of emulator for test to be deterministic.
// The latest version can be found from firestore emulator doc:
// https://firebase.google.com/docs/firestore/security/test-rules-emulator
Expand Down