Skip to content

Commit 34b6e27

Browse files
czrnodkz
authored andcommitted
fix: resolve to config from closest package.json
This also introduces reInitialisePackageJson() for test scripts.
1 parent efad669 commit 34b6e27

File tree

2 files changed

+83
-9
lines changed

2 files changed

+83
-9
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import fs from 'fs';
2+
import tmp from 'tmp';
3+
import { promisify } from 'util';
4+
import resolveConfig, { reInitializePackageJson } from '../resolve-config';
5+
6+
const mkdirAsync = promisify(fs.mkdir);
7+
const writeFileAsync = promisify(fs.writeFile);
8+
9+
const outerPackageJson = {
10+
config: {
11+
mongodbMemoryServer: {
12+
version: '3.0.0',
13+
},
14+
},
15+
};
16+
const innerPackageJson = {
17+
config: {
18+
mongodbMemoryServer: {
19+
version: '4.0.0',
20+
},
21+
},
22+
};
23+
24+
describe('resolveConfig', () => {
25+
const originalDir = process.cwd();
26+
let tmpObj;
27+
28+
describe('configuration from closest package.json', () => {
29+
beforeAll(async () => {
30+
// Set up test project/subproject structure in a temporary directory:
31+
//
32+
// project/
33+
// |-- subproject/
34+
// | +-- package.json
35+
// +-- package.json
36+
37+
tmpObj = tmp.dirSync({ unsafeCleanup: true });
38+
const tmpName = tmpObj.name;
39+
40+
await mkdirAsync(`${tmpName}/project`);
41+
await mkdirAsync(`${tmpName}/project/subproject`);
42+
43+
// prettier-ignore
44+
await Promise.all([
45+
writeFileAsync(
46+
`${tmpName}/project/package.json`,
47+
JSON.stringify(outerPackageJson)
48+
),
49+
writeFileAsync(
50+
`${tmpName}/project/subproject/package.json`,
51+
JSON.stringify(innerPackageJson)
52+
),
53+
]);
54+
});
55+
56+
afterAll(() => {
57+
process.chdir(originalDir);
58+
tmpObj.removeCallback();
59+
});
60+
61+
test('in project', () => {
62+
process.chdir(`${tmpObj.name}/project`);
63+
reInitializePackageJson();
64+
const got = resolveConfig('VERSION');
65+
expect(got).toBe('3.0.0');
66+
});
67+
68+
test('in subproject', () => {
69+
process.chdir(`${tmpObj.name}/project/subproject`);
70+
reInitializePackageJson();
71+
const got = resolveConfig('VERSION');
72+
expect(got).toBe('4.0.0');
73+
});
74+
});
75+
});

packages/mongodb-memory-server-core/src/util/resolve-config.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ const ENV_CONFIG_PREFIX = 'MONGOMS_';
55
const defaultValues = new Map<string, string>();
66

77
function getPackageJson(): Package | undefined {
8-
const pjIterator = finder(__dirname);
9-
let next = pjIterator.next();
10-
let packageJson = next.value;
11-
while (!next.done) {
12-
packageJson = next.value;
13-
next = pjIterator.next();
14-
}
15-
return packageJson;
8+
const pjIterator = finder(process.cwd());
9+
return pjIterator.next().value;
1610
}
17-
const packageJson = getPackageJson();
1811

1912
export function setDefaultValue(key: string, value: string): void {
2013
defaultValues.set(key, value);
2114
}
2215

16+
let packageJson: Package | undefined;
17+
export function reInitializePackageJson(): void {
18+
packageJson = getPackageJson();
19+
}
20+
reInitializePackageJson();
21+
2322
export default function resolveConfig(variableName: string): string | undefined {
2423
return (
2524
process.env[`${ENV_CONFIG_PREFIX}${variableName}`] ||

0 commit comments

Comments
 (0)