|
1 | 1 | import * as assert from 'assert'; |
2 | | -import { PassThrough } from 'stream'; |
| 2 | +import { PassThrough, Readable } from 'stream'; |
3 | 3 |
|
4 | 4 | import * as sinon from 'sinon'; |
5 | 5 |
|
@@ -27,22 +27,21 @@ function getProvider(options?: Options): TestProvider { |
27 | 27 | } |
28 | 28 |
|
29 | 29 | function getEntries(provider: TestProvider, task: Task, entry: Entry): Promise<EntryItem[]> { |
30 | | - const reader = new PassThrough({ objectMode: true }); |
| 30 | + const reader = PassThrough.from([entry], { objectMode: true }); |
31 | 31 |
|
32 | 32 | provider.reader.dynamic.returns(reader); |
33 | 33 | provider.reader.static.returns(reader); |
34 | 34 |
|
35 | | - reader.push(entry); |
36 | | - reader.push(null); |
| 35 | + return waitStreamEnd(provider.read(task)); |
| 36 | +} |
37 | 37 |
|
| 38 | +function waitStreamEnd(stream: Readable): Promise<EntryItem[]> { |
38 | 39 | return new Promise((resolve, reject) => { |
39 | 40 | const items: EntryItem[] = []; |
40 | 41 |
|
41 | | - const api = provider.read(task); |
42 | | - |
43 | | - api.on('data', (item: EntryItem) => items.push(item)); |
44 | | - api.once('error', reject); |
45 | | - api.once('end', () => resolve(items)); |
| 42 | + stream.on('data', (item: EntryItem) => items.push(item)); |
| 43 | + stream.once('error', reject); |
| 44 | + stream.once('end', () => resolve(items)); |
46 | 45 | }); |
47 | 46 | } |
48 | 47 |
|
@@ -118,5 +117,69 @@ describe('Providers → ProviderStream', () => { |
118 | 117 |
|
119 | 118 | actual.emit('close'); |
120 | 119 | }); |
| 120 | + |
| 121 | + describe('includePatternBaseDirectory', () => { |
| 122 | + it('should return base pattern directory', async () => { |
| 123 | + const provider = getProvider({ |
| 124 | + onlyFiles: false, |
| 125 | + includePatternBaseDirectory: true |
| 126 | + }); |
| 127 | + const task = tests.task.builder().base('root').positive('*').build(); |
| 128 | + const baseEntry = tests.entry.builder().path('root').directory().build(); |
| 129 | + const fileEntry = tests.entry.builder().path('root/file.txt').file().build(); |
| 130 | + |
| 131 | + const staticReaderStream = PassThrough.from([baseEntry], { objectMode: true }); |
| 132 | + const dynamicReaderStream = PassThrough.from([fileEntry], { objectMode: true }); |
| 133 | + |
| 134 | + provider.reader.static.returns(staticReaderStream); |
| 135 | + provider.reader.dynamic.returns(dynamicReaderStream); |
| 136 | + |
| 137 | + const expected = ['root', 'root/file.txt']; |
| 138 | + |
| 139 | + const actual = await waitStreamEnd(provider.read(task)); |
| 140 | + |
| 141 | + assert.strictEqual(provider.reader.static.callCount, 1); |
| 142 | + assert.strictEqual(provider.reader.dynamic.callCount, 1); |
| 143 | + assert.deepStrictEqual(actual, expected); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should do not read base directory for static task', async () => { |
| 147 | + const provider = getProvider({ |
| 148 | + onlyFiles: false, |
| 149 | + includePatternBaseDirectory: true |
| 150 | + }); |
| 151 | + const task = tests.task.builder().base('root').positive('file.txt').static().build(); |
| 152 | + const baseEntry = tests.entry.builder().path('root/file.txt').directory().build(); |
| 153 | + |
| 154 | + const staticReaderStream = PassThrough.from([baseEntry], { objectMode: true }); |
| 155 | + const dynamicReaderStream = PassThrough.from([]); |
| 156 | + |
| 157 | + provider.reader.static.returns(staticReaderStream); |
| 158 | + provider.reader.dynamic.returns(dynamicReaderStream); |
| 159 | + |
| 160 | + await waitStreamEnd(provider.read(task)); |
| 161 | + |
| 162 | + assert.strictEqual(provider.reader.static.callCount, 1); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should do not read base directory when it is a dot', async () => { |
| 166 | + const provider = getProvider({ |
| 167 | + onlyFiles: false, |
| 168 | + includePatternBaseDirectory: true |
| 169 | + }); |
| 170 | + const task = tests.task.builder().base('.').positive('*').build(); |
| 171 | + const baseEntry = tests.entry.builder().path('.').directory().build(); |
| 172 | + |
| 173 | + const staticReaderStream = PassThrough.from([baseEntry], { objectMode: true }); |
| 174 | + const dynamicReaderStream = PassThrough.from([], { objectMode: true }); |
| 175 | + |
| 176 | + provider.reader.static.returns(staticReaderStream); |
| 177 | + provider.reader.dynamic.returns(dynamicReaderStream); |
| 178 | + |
| 179 | + await waitStreamEnd(provider.read(task)); |
| 180 | + |
| 181 | + assert.strictEqual(provider.reader.static.callCount, 0); |
| 182 | + }); |
| 183 | + }); |
121 | 184 | }); |
122 | 185 | }); |
0 commit comments