1
0
mirror of synced 2025-12-23 21:07:12 -05:00

Bring in data-directory, let's go async file reads (#16782)

* Bring in data-directory, let's go async file reads

* Lint fixes

* Update glossary.js
This commit is contained in:
Kevin Heis
2020-12-09 11:20:24 -08:00
committed by GitHub
parent b807035720
commit 1b424dfdc4
11 changed files with 166 additions and 42 deletions

View File

@@ -0,0 +1,15 @@
const filenameToKey = require('../../../lib/filename-to-key')
describe('filename-to-key', () => {
test('converts filenames to object keys', () => {
expect(filenameToKey('foo/bar/baz.txt')).toBe('foo.bar.baz')
})
test('ignores leading slash on filenames', () => {
expect(filenameToKey('/foo/bar/baz.txt')).toBe('foo.bar.baz')
})
test('supports MS Windows paths', () => {
expect(filenameToKey('path\\to\\file.txt')).toBe('path.to.file')
})
})

View File

@@ -0,0 +1 @@
I am a README. I am ignored by default.

View File

@@ -0,0 +1 @@
another_markup_language: 'yes'

View File

@@ -0,0 +1 @@
{"meaningOfLife": 42}

View File

@@ -0,0 +1 @@
I am markdown!

View File

@@ -0,0 +1,40 @@
const path = require('path')
const dataDirectory = require('../../../lib/data-directory')
const fixturesDir = path.join(__dirname, 'fixtures')
describe('data-directory', () => {
test('works', async () => {
const data = await dataDirectory(fixturesDir)
const expected = {
bar: { another_markup_language: 'yes' },
foo: { meaningOfLife: 42 },
nested: { baz: 'I am markdown!' }
}
expect(data).toEqual(expected)
})
test('option: preprocess function', async () => {
const preprocess = function (content) {
return content.replace('markdown', 'MARKDOWN')
}
const data = await dataDirectory(fixturesDir, { preprocess })
expect(data.nested.baz).toBe('I am MARKDOWN!')
})
test('option: extensions array', async () => {
const extensions = ['.yaml', 'markdown']
const data = await dataDirectory(fixturesDir, { extensions })
expect('bar' in data).toBe(true)
expect('foo' in data).toBe(false) // JSON file should be ignored
})
test('option: ignorePatterns', async () => {
const ignorePatterns = []
// README is ignored by default
expect('README' in await dataDirectory(fixturesDir)).toBe(false)
// README can be included by setting empty ignorePatterns array
expect('README' in await dataDirectory(fixturesDir, { ignorePatterns })).toBe(true)
})
})