1- "use strict" ;
2-
3- import assert from "assert"
4- import fs from "fs"
5- import path from "path"
6- import FileSystemLoader from "../src/file-system-loader"
1+ import assert from 'assert'
2+ import { existsSync , readdirSync , readFileSync } from 'fs'
3+ import { join } from 'path'
4+ import FileSystemLoader from '../src/file-system-loader'
75
86let normalize = ( str ) => {
9- return str . replace ( / \r \n ? / g, "\n" ) ;
7+ return str . replace ( / \r \n ? / g, '\n' ) ;
108}
119
1210const pipelines = {
13- " test-cases" : undefined ,
14- " cssi" : [ ]
11+ ' test-cases' : undefined ,
12+ ' cssi' : [ ]
1513}
1614
1715Object . keys ( pipelines ) . forEach ( dirname => {
1816 describe ( dirname , ( ) => {
19- let testDir = path . join ( __dirname , dirname )
20- fs . readdirSync ( testDir ) . forEach ( testCase => {
21- if ( fs . existsSync ( path . join ( testDir , testCase , "source.css" ) ) ) {
22- it ( "should " + testCase . replace ( / - / g, " " ) , done => {
23- let expected = normalize ( fs . readFileSync ( path . join ( testDir , testCase , "expected.css" ) , "utf-8" ) )
24- let loader = new FileSystemLoader ( testDir , pipelines [ dirname ] )
25- let expectedTokens = JSON . parse ( fs . readFileSync ( path . join ( testDir , testCase , "expected.json" ) , "utf-8" ) )
26- loader . fetch ( `${ testCase } /source.css` , "/" ) . then ( tokens => {
27- assert . equal ( loader . finalSource , expected )
28- assert . equal ( JSON . stringify ( tokens ) , JSON . stringify ( expectedTokens ) )
29- } ) . then ( done , done )
17+ let testDir = join ( __dirname , dirname )
18+ readdirSync ( testDir ) . forEach ( testCase => {
19+ if ( existsSync ( join ( testDir , testCase , 'source.css' ) ) ) {
20+ it ( 'should ' + testCase . replace ( / - / g, ' ' ) , done => {
21+ const loader = new FileSystemLoader ( { rootDir : testDir , use : pipelines [ dirname ] } )
22+
23+ let expected = normalize ( readFileSync ( join ( testDir , testCase , 'expected.css' ) , 'utf-8' ) )
24+ let expectedTokens = JSON . parse ( readFileSync ( join ( testDir , testCase , 'expected.json' ) , 'utf-8' ) )
25+ let filepath = join ( testDir , testCase , 'source.css' )
26+
27+ loader . fetch ( filepath , filepath , null )
28+ . then ( tokens => {
29+ assert . equal ( loader . finalSource , expected )
30+ assert . equal ( JSON . stringify ( tokens ) , JSON . stringify ( expectedTokens ) )
31+ } ) . then ( done , done )
3032 } ) ;
3133 }
3234 } ) ;
@@ -35,16 +37,20 @@ Object.keys( pipelines ).forEach( dirname => {
3537
3638// special case for testing multiple sources
3739describe ( 'multiple sources' , ( ) => {
38- let testDir = path . join ( __dirname , 'test-cases' )
40+ let testDir = join ( __dirname , 'test-cases' )
3941 let testCase = 'multiple-sources' ;
4042 let dirname = 'test-cases' ;
41- if ( fs . existsSync ( path . join ( testDir , testCase , "source1.css" ) ) ) {
42- it ( "should " + testCase . replace ( / - / g, " " ) , done => {
43- let expected = normalize ( fs . readFileSync ( path . join ( testDir , testCase , "expected.css" ) , "utf-8" ) )
44- let loader = new FileSystemLoader ( testDir , pipelines [ dirname ] )
45- let expectedTokens = JSON . parse ( fs . readFileSync ( path . join ( testDir , testCase , "expected.json" ) , "utf-8" ) )
46- loader . fetch ( `${ testCase } /source1.css` , "/" ) . then ( tokens1 => {
47- loader . fetch ( `${ testCase } /source2.css` , "/" ) . then ( tokens2 => {
43+ if ( existsSync ( join ( testDir , testCase , 'source1.css' ) ) ) {
44+ it ( 'should ' + testCase . replace ( / - / g, ' ' ) , done => {
45+ const loader = new FileSystemLoader ( { rootDir : testDir , use : pipelines [ dirname ] } )
46+
47+ let expected = normalize ( readFileSync ( join ( testDir , testCase , 'expected.css' ) , 'utf-8' ) )
48+ let expectedTokens = JSON . parse ( readFileSync ( join ( testDir , testCase , 'expected.json' ) , 'utf-8' ) )
49+ let filepath1 = join ( testDir , testCase , 'source1.css' )
50+ let filepath2 = join ( testDir , testCase , 'source2.css' )
51+
52+ loader . fetch ( filepath1 , filepath1 , null ) . then ( tokens1 => {
53+ loader . fetch ( filepath2 , filepath2 , null ) . then ( tokens2 => {
4854 assert . equal ( loader . finalSource , expected )
4955 const tokens = Object . assign ( { } , tokens1 , tokens2 ) ;
5056 assert . equal ( JSON . stringify ( tokens ) , JSON . stringify ( expectedTokens ) )
0 commit comments