Skip to content

Commit

Permalink
Convert EmulatorConfig's HostAndPort to be an optional property
Browse files Browse the repository at this point in the history
  • Loading branch information
dlarocque committed Oct 16, 2024
1 parent c05e3f2 commit 0f491e2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 49 deletions.
33 changes: 12 additions & 21 deletions packages/rules-unit-testing/src/impl/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,20 @@ export interface DiscoveredEmulators {
*/
export function getEmulatorHostAndPort(
emulator: keyof DiscoveredEmulators,
conf?: EmulatorConfig,
endpoint?: HostAndPort,
discovered?: DiscoveredEmulators
) {
if (conf && 'host' in conf && 'port' in conf) {
const { host, port } = conf;
if (host || port) {
if (!host || !port) {
throw new Error(
`Invalid configuration ${emulator}.host=${host} and ${emulator}.port=${port}. ` +
'If either parameter is supplied, both must be defined.'
);
}
if (discovered && !discovered[emulator]) {
console.warn(
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
'reports it as not running. This may lead to errors such as connection refused.'
);
}
return {
host: fixHostname(conf.host, discovered?.hub?.host),
port: conf.port
};
): HostAndPort | undefined {
if (endpoint) {
if (discovered && !discovered[emulator]) {
console.warn(
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
'reports it as not running. This may lead to errors such as connection refused.'
);
}
return {
host: fixHostname(endpoint.host, discovered?.hub?.host),
port: endpoint.port
};
}
const envVar = EMULATOR_HOST_ENV_VARS[emulator];
const fallback = discovered?.[emulator] || emulatorFromEnvVar(envVar);
Expand Down
2 changes: 1 addition & 1 deletion packages/rules-unit-testing/src/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function initializeTestEnvironment(
for (const emulator of SUPPORTED_EMULATORS) {
const hostAndPort = getEmulatorHostAndPort(
emulator,
config[emulator],
config[emulator]?.endpoint,
discovered
);
if (hostAndPort) {
Expand Down
11 changes: 8 additions & 3 deletions packages/rules-unit-testing/src/public_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export interface TestEnvironmentConfig {
* An object containing the hostname and port number of an emulator.
* @public
*/
export interface HostAndPort {
export type HostAndPort = {
/**
* The host of the emulator. Can be omitted if discovered automatically through the hub or
* specified via environment variables. See `TestEnvironmentConfig` for details.
Expand All @@ -140,7 +140,7 @@ export interface HostAndPort {
* specified via environment variables. See `TestEnvironmentConfig` for details.
*/
port: number;
}
};

/**
* Configuration for a given emulator.
Expand All @@ -149,7 +149,12 @@ export interface HostAndPort {
export type EmulatorConfig = {
/** The security rules source code under test for this emulator. Strongly recommended. */
rules?: string;
} & (HostAndPort | {}); // Both or none of host and port should be specified.
/**
* Endpoint of the emulator. Can be omitted if discovered automatically through the hub.
* See `TestEnvironmentConfig` for details.
*/
endpoint?: HostAndPort;
};

/**
* An object used to control the rules unit test environment. Can be used to create RulesTestContext
Expand Down
28 changes: 4 additions & 24 deletions packages/rules-unit-testing/test/impl/discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
EMULATOR_HOST_ENV_VARS,
getEmulatorHostAndPort
} from '../../src/impl/discovery';
import { HostAndPort } from '../../src/public_types';
import { EmulatorConfig, HostAndPort } from '../../src/public_types';
import { restoreEnvVars, stashEnvVars } from '../test_utils';

describe('discoverEmulators()', () => {
Expand Down Expand Up @@ -106,10 +106,8 @@ describe('getEmulatorHostAndPort()', () => {
expect(result).to.be.undefined;
});

it('returns undefined if config option does not contain host/port', async () => {
const result = getEmulatorHostAndPort('hub', {
rules: '/* security rules only, no host/port */'
});
it('returns undefined if endpoint is undefined', async () => {
const result = getEmulatorHostAndPort('hub');

expect(result).to.be.undefined;
});
Expand All @@ -123,22 +121,6 @@ describe('getEmulatorHostAndPort()', () => {
expect(result?.host).to.equal('::1');
});

it('throws if only host is present', async () => {
expect(() =>
getEmulatorHostAndPort('hub', {
host: '[::1]'
} as HostAndPort)
).to.throw(/hub.port=undefined/);
});

it('throws if only port is present', async () => {
expect(() =>
getEmulatorHostAndPort('database', {
port: 1234
} as HostAndPort)
).to.throw(/Invalid configuration database.host=undefined/);
});

it('connect to 127.0.0.1 if host is wildcard 0.0.0.0', async () => {
const result = getEmulatorHostAndPort('hub', {
host: '0.0.0.0',
Expand Down Expand Up @@ -304,9 +286,7 @@ describe('getEmulatorHostAndPort()', () => {

it('takes host and port from env var if config has no host/port', async () => {
process.env[EMULATOR_HOST_ENV_VARS.hub] = '127.0.0.1:3445';
const result = getEmulatorHostAndPort('hub', {
rules: '/* security rules only, no host/port */'
});
const result = getEmulatorHostAndPort('hub');

expect(result?.host).to.equal('127.0.0.1');
expect(result?.port).to.equal(3445);
Expand Down

0 comments on commit 0f491e2

Please sign in to comment.