From f18076a88c441370625f9bc64ec0d5b1d766c7dd Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Sun, 10 Dec 2023 13:57:22 -0500 Subject: [PATCH] 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. --- .../src/testing/builder-harness.ts | 8 +++++- .../src/testing/jasmine-helpers.ts | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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; + }, + }; +}