|
1 | 1 | import test from 'ava';
|
2 | 2 | import { promises as fs } from 'fs';
|
| 3 | +import mkdirp from 'mkdirp'; |
3 | 4 | import path from 'path';
|
4 |
| -import { renderString, renderTemplateFile } from '../src'; |
| 5 | +import { |
| 6 | + renderGlob, |
| 7 | + renderString, |
| 8 | + renderTemplateFile, |
| 9 | + renderToFolder |
| 10 | +} from '../src'; |
| 11 | +import { limitOpenFiles } from '../src/utils'; |
5 | 12 |
|
6 | 13 | test('Data is replaced when given string', t => {
|
7 | 14 | // Should return the same without regard of consistent spacing
|
@@ -67,3 +74,91 @@ test('Data is replaced when given file path', async t => {
|
67 | 74 |
|
68 | 75 | t.is(actual, expected);
|
69 | 76 | });
|
| 77 | + |
| 78 | +test('Renders from a glob', async t => { |
| 79 | + const actualFiles: { name: string; contents: string }[] = []; |
| 80 | + const expectedFiles = [ |
| 81 | + { |
| 82 | + name: './__tests__/helpers/templates/also-cool.json', |
| 83 | + contents: '{ "fullName": "Bob" }\n' |
| 84 | + }, |
| 85 | + { |
| 86 | + name: './__tests__/helpers/templates/cool.md', |
| 87 | + contents: '# Hello, Bob!\n' |
| 88 | + } |
| 89 | + ]; |
| 90 | + |
| 91 | + await renderGlob( |
| 92 | + './__tests__/helpers/templates/**/*.!(txt)', |
| 93 | + { name: 'Bob' }, |
| 94 | + (name, contents) => { |
| 95 | + actualFiles.push({ name, contents }); |
| 96 | + } |
| 97 | + ); |
| 98 | + |
| 99 | + t.is(actualFiles.length, 2); |
| 100 | + |
| 101 | + if (actualFiles[0].name === expectedFiles[0].name) { |
| 102 | + t.deepEqual(actualFiles, expectedFiles); |
| 103 | + } else { |
| 104 | + t.deepEqual(actualFiles.reverse(), expectedFiles); |
| 105 | + } |
| 106 | +}); |
| 107 | + |
| 108 | +test('Can render output to a file', async t => { |
| 109 | + const expectedFiles = [ |
| 110 | + { |
| 111 | + name: './__tests__/helpers/output/also-cool.json', |
| 112 | + contents: '{ "fullName": "Kai" }\n' |
| 113 | + }, |
| 114 | + { |
| 115 | + name: './__tests__/helpers/output/cool.md', |
| 116 | + contents: '# Hello, Kai!\n' |
| 117 | + } |
| 118 | + ]; |
| 119 | + |
| 120 | + await renderToFolder( |
| 121 | + './__tests__/helpers/templates/**/*.!(txt)', |
| 122 | + './__tests__/helpers/output', |
| 123 | + { name: 'Kai' } |
| 124 | + ); |
| 125 | + |
| 126 | + for (const { name, contents } of expectedFiles) { |
| 127 | + const actualContents = await fs.readFile(name, { encoding: 'utf-8' }); |
| 128 | + t.is(actualContents, contents); |
| 129 | + } |
| 130 | +}); |
| 131 | + |
| 132 | +test('Can render a ton of files', async t => { |
| 133 | + const expectedFiles = [] as { name: string; contents: string }[]; |
| 134 | + |
| 135 | + // Pre-test setup |
| 136 | + const templateFolder = './__tests__/helpers/large/'; |
| 137 | + const outputFolder = `${templateFolder}/output`; |
| 138 | + const template = 'Hello, {{ name }}'; |
| 139 | + |
| 140 | + await mkdirp(templateFolder); |
| 141 | + await Promise.all( |
| 142 | + Array.from({ length: 50000 }, (_, i) => { |
| 143 | + const basename = `${i}.template`; |
| 144 | + |
| 145 | + expectedFiles.push({ |
| 146 | + name: `${outputFolder}/${basename}`, |
| 147 | + contents: 'Hello, Test' |
| 148 | + }); |
| 149 | + |
| 150 | + return limitOpenFiles(() => |
| 151 | + fs.writeFile(`${templateFolder}/${basename}`, template) |
| 152 | + ); |
| 153 | + }) |
| 154 | + ); |
| 155 | + |
| 156 | + await renderToFolder(`${templateFolder}/*.template`, outputFolder, { |
| 157 | + name: 'Test' |
| 158 | + }); |
| 159 | + |
| 160 | + for (const { name, contents } of expectedFiles) { |
| 161 | + const actualContents = await fs.readFile(name, { encoding: 'utf-8' }); |
| 162 | + t.is(actualContents, contents); |
| 163 | + } |
| 164 | +}); |
0 commit comments