Skip to content

Commit fcd9adc

Browse files
clydinalan-agius4
authored andcommitted
test(@angular-devkit/build-angular): add support for builder harness directory expectations
When using the builder harness in unit tests, expectations can now be made directly for directories. Currently the existence, or lack thereof, can be tested using the harness. This is similar to be existing file expectations. More capability may be added as needed in the future. (cherry picked from commit f18076a)
1 parent f63566e commit fcd9adc

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/angular_devkit/build_angular/src/testing/builder-harness.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { WorkspaceHost } from '@angular-devkit/architect/node';
2323
import { TestProjectHost } from '@angular-devkit/architect/testing';
2424
import { getSystemPath, json, logging } from '@angular-devkit/core';
25-
import { existsSync, readFileSync, readdirSync } from 'node:fs';
25+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
2626
import fs from 'node:fs/promises';
2727
import { dirname, join } from 'node:path';
2828
import {
@@ -351,6 +351,12 @@ export class BuilderHarness<T> {
351351
return existsSync(fullPath);
352352
}
353353

354+
hasDirectory(path: string): boolean {
355+
const fullPath = this.resolvePath(path);
356+
357+
return statSync(fullPath, { throwIfNoEntry: false })?.isDirectory() ?? false;
358+
}
359+
354360
hasFileMatch(directory: string, pattern: RegExp): boolean {
355361
const fullPath = this.resolvePath(directory);
356362

packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts

+28
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export class JasmineBuilderHarness<T> extends BuilderHarness<T> {
4242
expectFile(path: string): HarnessFileMatchers {
4343
return expectFile(path, this);
4444
}
45+
expectDirectory(path: string): HarnessDirectoryMatchers {
46+
return expectDirectory(path, this);
47+
}
4548
}
4649

4750
export interface HarnessFileMatchers {
@@ -51,6 +54,11 @@ export interface HarnessFileMatchers {
5154
readonly size: jasmine.Matchers<number>;
5255
}
5356

57+
export interface HarnessDirectoryMatchers {
58+
toExist(): boolean;
59+
toNotExist(): boolean;
60+
}
61+
5462
/**
5563
* Add a Jasmine expectation filter to an expectation that always fails with a message.
5664
* @param base The base expectation (`expect(...)`) to use.
@@ -125,3 +133,23 @@ export function expectFile<T>(path: string, harness: BuilderHarness<T>): Harness
125133
},
126134
};
127135
}
136+
137+
export function expectDirectory<T>(
138+
path: string,
139+
harness: BuilderHarness<T>,
140+
): HarnessDirectoryMatchers {
141+
return {
142+
toExist() {
143+
const exists = harness.hasDirectory(path);
144+
expect(exists).toBe(true, 'Expected directory to exist: ' + path);
145+
146+
return exists;
147+
},
148+
toNotExist() {
149+
const exists = harness.hasDirectory(path);
150+
expect(exists).toBe(false, 'Expected directory to not exist: ' + path);
151+
152+
return !exists;
153+
},
154+
};
155+
}

0 commit comments

Comments
 (0)