Skip to content

Commit aa51e66

Browse files
committed
Revert "Convert EmulatorConfig's HostAndPort to be an optional property"
This reverts commit c58e63b.
1 parent 7aa755a commit aa51e66

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

packages/rules-unit-testing/src/impl/discovery.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,29 @@ export interface DiscoveredEmulators {
8484
*/
8585
export function getEmulatorHostAndPort(
8686
emulator: keyof DiscoveredEmulators,
87-
endpoint?: HostAndPort,
87+
conf?: EmulatorConfig,
8888
discovered?: DiscoveredEmulators
89-
): HostAndPort | undefined {
90-
if (endpoint) {
91-
if (discovered && !discovered[emulator]) {
92-
console.warn(
93-
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
94-
'reports it as not running. This may lead to errors such as connection refused.'
95-
);
89+
) {
90+
if (conf && 'host' in conf && 'port' in conf) {
91+
const { host, port } = conf;
92+
if (host || port) {
93+
if (!host || !port) {
94+
throw new Error(
95+
`Invalid configuration ${emulator}.host=${host} and ${emulator}.port=${port}. ` +
96+
'If either parameter is supplied, both must be defined.'
97+
);
98+
}
99+
if (discovered && !discovered[emulator]) {
100+
console.warn(
101+
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
102+
'reports it as not running. This may lead to errors such as connection refused.'
103+
);
104+
}
105+
return {
106+
host: fixHostname(conf.host, discovered?.hub?.host),
107+
port: conf.port
108+
};
96109
}
97-
return {
98-
host: fixHostname(endpoint.host, discovered?.hub?.host),
99-
port: endpoint.port
100-
};
101110
}
102111
const envVar = EMULATOR_HOST_ENV_VARS[emulator];
103112
const fallback = discovered?.[emulator] || emulatorFromEnvVar(envVar);

packages/rules-unit-testing/src/initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function initializeTestEnvironment(
7373
for (const emulator of SUPPORTED_EMULATORS) {
7474
const hostAndPort = getEmulatorHostAndPort(
7575
emulator,
76-
config[emulator]?.endpoint,
76+
config[emulator],
7777
discovered
7878
);
7979
if (hostAndPort) {

packages/rules-unit-testing/src/public_types/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export interface TestEnvironmentConfig {
128128
* An object containing the hostname and port number of an emulator.
129129
* @public
130130
*/
131-
export type HostAndPort = {
131+
export interface HostAndPort {
132132
/**
133133
* The host of the emulator. Can be omitted if discovered automatically through the hub or
134134
* specified via environment variables. See `TestEnvironmentConfig` for details.
@@ -140,7 +140,7 @@ export type HostAndPort = {
140140
* specified via environment variables. See `TestEnvironmentConfig` for details.
141141
*/
142142
port: number;
143-
};
143+
}
144144

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

159154
/**
160155
* An object used to control the rules unit test environment. Can be used to create RulesTestContext

packages/rules-unit-testing/test/impl/discovery.test.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
EMULATOR_HOST_ENV_VARS,
2323
getEmulatorHostAndPort
2424
} from '../../src/impl/discovery';
25-
import { EmulatorConfig, HostAndPort } from '../../src/public_types';
25+
import { HostAndPort } from '../../src/public_types';
2626
import { restoreEnvVars, stashEnvVars } from '../test_utils';
2727

2828
describe('discoverEmulators()', () => {
@@ -106,8 +106,10 @@ describe('getEmulatorHostAndPort()', () => {
106106
expect(result).to.be.undefined;
107107
});
108108

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

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

126+
it('throws if only host is present', async () => {
127+
expect(() =>
128+
getEmulatorHostAndPort('hub', {
129+
host: '[::1]'
130+
} as HostAndPort)
131+
).to.throw(/hub.port=undefined/);
132+
});
133+
134+
it('throws if only port is present', async () => {
135+
expect(() =>
136+
getEmulatorHostAndPort('database', {
137+
port: 1234
138+
} as HostAndPort)
139+
).to.throw(/Invalid configuration database.host=undefined/);
140+
});
141+
124142
it('connect to 127.0.0.1 if host is wildcard 0.0.0.0', async () => {
125143
const result = getEmulatorHostAndPort('hub', {
126144
host: '0.0.0.0',
@@ -286,7 +304,9 @@ describe('getEmulatorHostAndPort()', () => {
286304

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

291311
expect(result?.host).to.equal('127.0.0.1');
292312
expect(result?.port).to.equal(3445);

0 commit comments

Comments
 (0)