Skip to content

Commit 9091e4c

Browse files
committed
Remove upward search from dw.json loading, complete test isolation
- Change loadDwJson() default to look only at ./dw.json (no parent search) - Update DwJsonSource to use same logic, remove findDwJson import - Keep findDwJson() exported for users who need explicit upward search - Set SFCC_CONFIG=/dev/null and MRT_CREDENTIALS_FILE=/dev/null in isolateConfig() - Update test to verify new behavior (no upward search)
1 parent a155bc7 commit 9091e4c

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

packages/b2c-tooling-sdk/src/config/dw-json.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,32 @@ function selectConfig(json: DwJsonMultiConfig, instanceName?: string): DwJsonCon
149149
/**
150150
* Loads configuration from a dw.json file.
151151
*
152-
* Searches upward from the current directory (or specified startDir) for a dw.json file.
153-
* Supports both single-config and multi-config formats.
152+
* If an explicit path is provided, uses that file. Otherwise, looks for dw.json
153+
* in the startDir (or cwd). Does NOT search parent directories.
154+
*
155+
* Use `findDwJson()` if you need to search upward through parent directories.
154156
*
155157
* @param options - Loading options
156158
* @returns The parsed config, or undefined if no dw.json found
157159
*
158160
* @example
159-
* // Auto-find dw.json
161+
* // Load from ./dw.json (current directory)
160162
* const config = loadDwJson();
161163
*
164+
* // Load from specific directory
165+
* const config = loadDwJson({ startDir: '/path/to/project' });
166+
*
162167
* // Use named instance
163168
* const config = loadDwJson({ instance: 'staging' });
164169
*
165170
* // Explicit path
166171
* const config = loadDwJson({ path: './config/dw.json' });
167172
*/
168173
export function loadDwJson(options: LoadDwJsonOptions = {}): DwJsonConfig | undefined {
169-
const dwJsonPath = options.path || findDwJson(options.startDir);
174+
// If explicit path provided, use it. Otherwise default to ./dw.json (no upward search)
175+
const dwJsonPath = options.path ?? path.join(options.startDir || process.cwd(), 'dw.json');
170176

171-
if (!dwJsonPath || !fs.existsSync(dwJsonPath)) {
177+
if (!fs.existsSync(dwJsonPath)) {
172178
return undefined;
173179
}
174180

packages/b2c-tooling-sdk/src/config/sources/dw-json-source.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*
99
* @internal This module is internal to the SDK. Use ConfigResolver instead.
1010
*/
11-
import {loadDwJson, findDwJson} from '../dw-json.js';
11+
import * as path from 'node:path';
12+
import {loadDwJson} from '../dw-json.js';
1213
import {mapDwJsonToNormalizedConfig} from '../mapping.js';
1314
import type {ConfigSource, NormalizedConfig, ResolveConfigOptions} from '../types.js';
1415

@@ -33,8 +34,8 @@ export class DwJsonSource implements ConfigSource {
3334
return undefined;
3435
}
3536

36-
// Track the path for diagnostics
37-
this.lastPath = options.configPath || findDwJson(options.startDir);
37+
// Track the path for diagnostics - use explicit path or default location
38+
this.lastPath = options.configPath ?? path.join(options.startDir || process.cwd(), 'dw.json');
3839

3940
return mapDwJsonToNormalizedConfig(dwConfig);
4041
}

packages/b2c-tooling-sdk/test/config/sources.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('config/sources', () => {
4646
expect(config.codeVersion).to.equal('v1');
4747
});
4848

49-
it('loads config from dw.json in parent directory', () => {
49+
it('does NOT load config from dw.json in parent directory (no upward search)', () => {
5050
const subDir = path.join(tempDir, 'subdir');
5151
fs.mkdirSync(subDir);
5252
const dwJsonPath = path.join(tempDir, 'dw.json');
@@ -57,11 +57,13 @@ describe('config/sources', () => {
5757
}),
5858
);
5959

60+
// Change to subdirectory - should NOT find parent's dw.json
6061
process.chdir(subDir);
6162
const resolver = new ConfigResolver();
6263
const {config} = resolver.resolve();
6364

64-
expect(config.hostname).to.equal('parent.demandware.net');
65+
// Parent dw.json should NOT be found (no upward search)
66+
expect(config.hostname).to.be.undefined;
6567
});
6668

6769
it('handles OAuth credentials from dw.json', () => {

packages/b2c-tooling-sdk/test/helpers/config-isolation.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export function isolateConfig(): void {
6464
delete process.env[key];
6565
}
6666

67+
// SET isolation env vars - oclif will pick these up during flag parsing
68+
// /dev/null exists but is empty (JSON.parse fails), so config sources find nothing
69+
process.env.SFCC_CONFIG = '/dev/null';
70+
process.env.MRT_CREDENTIALS_FILE = '/dev/null';
71+
6772
state = {savedEnvVars};
6873
}
6974

@@ -76,7 +81,11 @@ export function isolateConfig(): void {
7681
export function restoreConfig(): void {
7782
if (!state) return;
7883

79-
// Restore env vars
84+
// Remove isolation env vars we set
85+
delete process.env.SFCC_CONFIG;
86+
delete process.env.MRT_CREDENTIALS_FILE;
87+
88+
// Restore original env vars
8089
for (const [key, value] of Object.entries(state.savedEnvVars)) {
8190
if (value === undefined) {
8291
delete process.env[key];

0 commit comments

Comments
 (0)