Skip to content

Commit 2a704bc

Browse files
authored
more interface re-writes (#82)
* more interface re-writes * slightly better mocking
1 parent 59ba123 commit 2a704bc

File tree

12 files changed

+31
-33
lines changed

12 files changed

+31
-33
lines changed

src/feature-toggles.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const VError = require("verror");
1919
const yaml = require("yaml");
2020
const redis = require("./redis-wrapper");
2121
const { REDIS_INTEGRATION_MODE } = redis;
22-
const cfEnv = require("./shared/cf-env");
22+
const { cfEnv } = require("./shared/cf-env");
2323
const { Logger } = require("./shared/logger");
2424
const { HandlerCollection } = require("./shared/handler-collection");
2525
const { LimitedLazyCache } = require("./shared/cache");
@@ -1652,13 +1652,14 @@ class FeatureToggles {
16521652
}
16531653

16541654
module.exports = {
1655+
ENV,
1656+
DEFAULT_REDIS_CHANNEL,
1657+
DEFAULT_REDIS_KEY,
1658+
SCOPE_ROOT_KEY,
16551659
FeatureToggles,
1660+
toggles: FeatureToggles.getInstance(),
16561661

16571662
_: {
1658-
ENV,
1659-
DEFAULT_REDIS_CHANNEL,
1660-
DEFAULT_REDIS_KEY,
1661-
SCOPE_ROOT_KEY,
16621663
CONFIG_KEY,
16631664
CONFIG_INFO_KEY,
16641665
_getLogger: () => logger,

src/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"use strict";
22

3-
const { FeatureToggles } = require("./feature-toggles");
4-
5-
const toggles = FeatureToggles.getInstance();
3+
const { toggles } = require("./feature-toggles");
64

75
module.exports = toggles;

src/redis-wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
const redis = require("redis");
1010
const VError = require("verror");
11-
const cfEnv = require("./shared/cf-env");
11+
const { cfEnv } = require("./shared/cf-env");
1212
const { Logger } = require("./shared/logger");
1313
const { HandlerCollection } = require("./shared/handler-collection");
1414
const { Semaphore } = require("./shared/semaphore");

src/shared/cf-env.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ class CfEnv {
6262
}
6363
}
6464

65-
module.exports = CfEnv.getInstance();
65+
module.exports = {
66+
ENV,
67+
cfEnv: CfEnv.getInstance(),
68+
};

src/shared/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const util = require("util");
33
const VError = require("verror");
44

5-
const cfEnv = require("./cf-env");
5+
const { cfEnv } = require("./cf-env");
66
const { tryRequire } = require("./static");
77
const cds = tryRequire("@sap/cds");
88

test/__mocks__/cf-env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ const cfEnv = {
1010
};
1111
cfEnv._reset();
1212

13-
module.exports = cfEnv;
13+
module.exports = { cfEnv };

test/feature-toggles.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ const { FEATURE, mockConfig, redisKey, redisChannel } = require("./__common__/mo
1111
const { fallbackValuesFromInfos, stateFromInfos } = require("./__common__/from-info");
1212

1313
const {
14+
DEFAULT_REDIS_CHANNEL,
15+
DEFAULT_REDIS_KEY,
16+
SCOPE_ROOT_KEY,
1417
FeatureToggles,
15-
_: { DEFAULT_REDIS_CHANNEL, DEFAULT_REDIS_KEY, SCOPE_ROOT_KEY, CONFIG_KEY, CONFIG_INFO_KEY },
18+
_: { CONFIG_KEY, CONFIG_INFO_KEY },
1619
} = featureTogglesModule;
1720

1821
const { readFile: readFileSpy } = require("fs");
@@ -21,7 +24,7 @@ jest.mock("fs", () => ({
2124
access: jest.fn((cb) => cb()),
2225
}));
2326

24-
const envMock = require("../src/shared/cf-env");
27+
const { cfEnv: envMock } = require("../src/shared/cf-env");
2528
jest.mock("../src/shared/cf-env", () => require("./__mocks__/cf-env"));
2629

2730
const redisWrapperMock = require("../src/redis-wrapper");
@@ -52,7 +55,6 @@ describe("feature toggles test", () => {
5255
beforeEach(async () => {
5356
envMock._reset();
5457
redisWrapperMock._reset();
55-
envMock._reset();
5658
toggles = new FeatureToggles({ redisKey, redisChannel });
5759
jest.clearAllMocks();
5860
});

test/integration-local/feature-toggles.integration.test.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
// and test only the local mode here.
55

66
const fs = jest.requireActual("fs");
7+
const mockReadFile = jest.fn();
8+
const mockAccess = jest.fn((cb) => cb());
79
jest.mock("fs", () => ({
8-
readFile: jest.fn(),
9-
access: jest.fn((cb) => cb()),
10+
readFile: mockReadFile,
11+
access: mockAccess,
1012
}));
1113

1214
const { stateFromInfo } = require("../__common__/from-info");
@@ -42,8 +44,7 @@ describe("local integration test", () => {
4244

4345
describe("init", () => {
4446
test("init fails resolving for bad config paths", async () => {
45-
const { readFile: readFileSpy } = require("fs");
46-
readFileSpy.mockImplementationOnce(fs.readFile);
47+
mockReadFile.mockImplementationOnce(fs.readFile);
4748
await expect(toggles.initializeFeatures({ configFile: "fantasy_name" })).rejects.toMatchInlineSnapshot(
4849
`[FeatureTogglesError: initialization aborted, could not read config file: ENOENT: no such file or directory, open 'fantasy_name']`
4950
);
@@ -57,8 +58,6 @@ describe("local integration test", () => {
5758
});
5859

5960
test("init config precedence", async () => {
60-
const { readFile: readFileSpy } = require("fs");
61-
6261
const configForRuntime = {
6362
[FEATURE.A]: {
6463
fallbackValue: "fallbackRuntimeA",
@@ -97,7 +96,7 @@ describe("local integration test", () => {
9796
type: "string",
9897
},
9998
};
100-
readFileSpy.mockImplementationOnce((filepath, callback) =>
99+
mockReadFile.mockImplementationOnce((filepath, callback) =>
101100
callback(null, Buffer.from(JSON.stringify(configForFile)))
102101
);
103102

@@ -232,8 +231,6 @@ describe("local integration test", () => {
232231
test("custom module validations just module from CONFIG_DIR", async () => {
233232
jest.mock("./virtual-validator-just-module", () => jest.fn(), { virtual: true });
234233
const mockValidator = require("./virtual-validator-just-module");
235-
const { readFile: readFileSpy } = require("fs");
236-
237234
const config = {
238235
[FEATURE.A]: {
239236
fallbackValue: "fallback",
@@ -242,7 +239,7 @@ describe("local integration test", () => {
242239
},
243240
};
244241
const configBuffer = Buffer.from(JSON.stringify(config));
245-
readFileSpy.mockImplementationOnce((filepath, callback) => callback(null, configBuffer));
242+
mockReadFile.mockImplementationOnce((filepath, callback) => callback(null, configBuffer));
246243
await toggles.initializeFeatures({
247244
configFile: "./test/integration-local/virtual-config.json",
248245
});
@@ -254,7 +251,6 @@ describe("local integration test", () => {
254251
test("custom module validations with call from CONFIG_DIR", async () => {
255252
jest.mock("./virtual-validator-with-call", () => ({ validator: jest.fn() }), { virtual: true });
256253
const { validator: mockValidator } = require("./virtual-validator-with-call");
257-
const { readFile: readFileSpy } = require("fs");
258254

259255
const config = {
260256
[FEATURE.A]: {
@@ -264,7 +260,7 @@ describe("local integration test", () => {
264260
},
265261
};
266262
const configBuffer = Buffer.from(JSON.stringify(config));
267-
readFileSpy.mockImplementationOnce((filepath, callback) => callback(null, configBuffer));
263+
mockReadFile.mockImplementationOnce((filepath, callback) => callback(null, configBuffer));
268264
await toggles.initializeFeatures({
269265
configFile: "./test/integration-local/virtual-config.json",
270266
});

test/redis-wrapper.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const envMock = require("../src/shared/cf-env");
3+
const { cfEnv: envMock } = require("../src/shared/cf-env");
44
jest.mock("../src/shared/cf-env", () => require("./__mocks__/cf-env"));
55

66
const mockMessageHandler = jest.fn();

test/shared/cf-env.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe("cfenv test", () => {
108108
backupCfServicesEnv = process.env.VCAP_SERVICES;
109109
process.env.VCAP_APPLICATION = JSON.stringify(mockCfAppEnv);
110110
process.env.VCAP_SERVICES = JSON.stringify(mockCfServicesEnv);
111-
cfEnv = require("../../src/shared/cf-env");
111+
({ cfEnv } = require("../../src/shared/cf-env"));
112112
});
113113

114114
afterAll(() => {

0 commit comments

Comments
 (0)