diff --git a/packages/angular_devkit/build_angular/src/testing/builder-harness.ts b/packages/angular_devkit/build_angular/src/testing/builder-harness.ts index 5c881e9e7b04..05ba0d5d2df5 100644 --- a/packages/angular_devkit/build_angular/src/testing/builder-harness.ts +++ b/packages/angular_devkit/build_angular/src/testing/builder-harness.ts @@ -22,7 +22,7 @@ import { import { WorkspaceHost } from '@angular-devkit/architect/node'; import { TestProjectHost } from '@angular-devkit/architect/testing'; import { getSystemPath, json, logging } from '@angular-devkit/core'; -import { existsSync, readFileSync, readdirSync } from 'node:fs'; +import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'; import fs from 'node:fs/promises'; import { dirname, join } from 'node:path'; import { @@ -351,6 +351,12 @@ export class BuilderHarness { return existsSync(fullPath); } + hasDirectory(path: string): boolean { + const fullPath = this.resolvePath(path); + + return statSync(fullPath, { throwIfNoEntry: false })?.isDirectory() ?? false; + } + hasFileMatch(directory: string, pattern: RegExp): boolean { const fullPath = this.resolvePath(directory); diff --git a/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts b/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts index ade4a0b7978f..1d28cb0f2ff8 100644 --- a/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts +++ b/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts @@ -42,6 +42,9 @@ export class JasmineBuilderHarness extends BuilderHarness { expectFile(path: string): HarnessFileMatchers { return expectFile(path, this); } + expectDirectory(path: string): HarnessDirectoryMatchers { + return expectDirectory(path, this); + } } export interface HarnessFileMatchers { @@ -51,6 +54,11 @@ export interface HarnessFileMatchers { readonly size: jasmine.Matchers; } +export interface HarnessDirectoryMatchers { + toExist(): boolean; + toNotExist(): boolean; +} + /** * Add a Jasmine expectation filter to an expectation that always fails with a message. * @param base The base expectation (`expect(...)`) to use. @@ -125,3 +133,23 @@ export function expectFile(path: string, harness: BuilderHarness): Harness }, }; } + +export function expectDirectory( + path: string, + harness: BuilderHarness, +): HarnessDirectoryMatchers { + return { + toExist() { + const exists = harness.hasDirectory(path); + expect(exists).toBe(true, 'Expected directory to exist: ' + path); + + return exists; + }, + toNotExist() { + const exists = harness.hasDirectory(path); + expect(exists).toBe(false, 'Expected directory to not exist: ' + path); + + return !exists; + }, + }; +}