@@ -5,6 +5,7 @@ import * as path from 'path';
5
5
6
6
import { compareNodes } from '../../../../../tools/postcss/compare-nodes' ;
7
7
import { createLocalAngularPackageImporter } from '../../../../../tools/sass/local-sass-importer' ;
8
+ import { pathToFileURL } from 'url' ;
8
9
9
10
// Note: For Windows compatibility, we need to resolve the directory paths through runfiles
10
11
// which are guaranteed to reside in the source tree.
@@ -13,6 +14,15 @@ const packagesDir = path.join(runfiles.resolveWorkspaceRelative('src/cdk/_index.
13
14
14
15
const localPackageSassImporter = createLocalAngularPackageImporter ( packagesDir ) ;
15
16
17
+ const mdcSassImporter = {
18
+ findFileUrl : ( url : string ) => {
19
+ if ( url . toString ( ) . startsWith ( '@material' ) ) {
20
+ return pathToFileURL ( path . join ( runfiles . resolveWorkspaceRelative ( './node_modules' ) , url ) ) ;
21
+ }
22
+ return null ;
23
+ } ,
24
+ } ;
25
+
16
26
describe ( 'theming api' , ( ) => {
17
27
/** Map of known selectors for density styles and their corresponding AST rule. */
18
28
let knownDensitySelectors : Map < string , Rule > ;
@@ -51,8 +61,6 @@ describe('theming api', () => {
51
61
} ) ;
52
62
53
63
it ( 'should not warn if color styles and density are not duplicated' , ( ) => {
54
- spyOn ( process . stderr , 'write' ) . and . callThrough ( ) ;
55
-
56
64
const parsed = parse (
57
65
transpile ( `
58
66
$theme: mat-light-theme((
@@ -78,7 +86,7 @@ describe('theming api', () => {
78
86
79
87
expect ( hasDensityStyles ( parsed , null ) ) . toBe ( 'all' ) ;
80
88
expect ( hasDensityStyles ( parsed , '.dark-theme' ) ) . toBe ( 'none' ) ;
81
- expect ( process . stderr . write ) . toHaveBeenCalledTimes ( 0 ) ;
89
+ expectNoWarning ( / T h e s a m e c o l o r s t y l e s a r e g e n e r a t e d m u l t i p l e t i m e s / ) ;
82
90
} ) ;
83
91
84
92
it ( 'should warn if default density styles are duplicated' , ( ) => {
@@ -217,7 +225,6 @@ describe('theming api', () => {
217
225
} ) ;
218
226
219
227
it ( 'not warn if default density would be generated multiple times' , ( ) => {
220
- spyOn ( process . stderr , 'write' ) ;
221
228
transpile ( `
222
229
$light-theme: mat-light-theme($mat-red, $mat-blue);
223
230
$dark-theme: mat-dark-theme($mat-red, $mat-blue);
@@ -228,7 +235,7 @@ describe('theming api', () => {
228
235
}
229
236
` ) ;
230
237
231
- expect ( process . stderr . write ) . toHaveBeenCalledTimes ( 0 ) ;
238
+ expectNoWarning ( / T h e s a m e d e n s i t y s t y l e s a r e g e n e r a t e d m u l t i p l e t i m e s / ) ;
232
239
} ) ;
233
240
234
241
it ( 'should be possible to modify color configuration directly' , ( ) => {
@@ -319,25 +326,36 @@ describe('theming api', () => {
319
326
` ,
320
327
{
321
328
loadPaths : [ testDir ] ,
322
- importers : [ localPackageSassImporter ] ,
329
+ importers : [ localPackageSassImporter , mdcSassImporter ] ,
323
330
} ,
324
331
) . css . toString ( ) ;
325
332
}
326
333
334
+ /** Expects the given warning to be reported in Sass. */
335
+ function expectWarning ( message : RegExp ) {
336
+ expect ( getMatchingWarning ( message ) )
337
+ . withContext ( 'Expected warning to be printed.' )
338
+ . toBeDefined ( ) ;
339
+ }
340
+
341
+ /** Expects the given warning not to be reported in Sass. */
342
+ function expectNoWarning ( message : RegExp ) {
343
+ expect ( getMatchingWarning ( message ) )
344
+ . withContext ( 'Expected no warning to be printed.' )
345
+ . toBeUndefined ( ) ;
346
+ }
347
+
327
348
/**
328
- * Expects the given warning to be reported in Sass. Dart sass directly writes
349
+ * Gets first instance of the given warning reported in Sass. Dart sass directly writes
329
350
* to the `process.stderr` stream, so we spy on the `stderr.write` method. We
330
351
* cannot expect a specific amount of writes as Sass calls `stderr.write` multiple
331
352
* times for a warning (e.g. spacing and stack trace)
332
353
*/
333
- function expectWarning ( message : RegExp ) {
354
+ function getMatchingWarning ( message : RegExp ) {
334
355
const writeSpy = process . stderr . write as jasmine . Spy ;
335
- const match = writeSpy . calls
336
- . all ( )
337
- . find (
338
- ( s : jasmine . CallInfo < typeof process . stderr . write > ) =>
339
- typeof s . args [ 0 ] === 'string' && message . test ( s . args [ 0 ] ) ,
340
- ) ;
341
- expect ( match ) . withContext ( 'Expected warning to be printed.' ) . toBeDefined ( ) ;
356
+ return ( writeSpy . calls ?. all ( ) ?? [ ] ) . find (
357
+ ( s : jasmine . CallInfo < typeof process . stderr . write > ) =>
358
+ typeof s . args [ 0 ] === 'string' && message . test ( s . args [ 0 ] ) ,
359
+ ) ;
342
360
}
343
361
} ) ;
0 commit comments