|
1 | 1 | import FileRequest from '../src/fileRequest'
|
2 | 2 | import { promises as fs } from 'fs'
|
| 3 | +import path from 'path' |
| 4 | +import testDir from './testDir' |
3 | 5 |
|
4 | 6 | // Initialize Directory
|
5 |
| -const entryDir: string = '/tmp/gimonfu/fileRequest.test.ts/entry'; |
6 |
| -(async () => await fs.rmdir(entryDir, {recursive: true})); |
| 7 | +const entryDir: string = path.resolve(testDir, 'tmp', 'gimonfu', 'fileRequest.test.ts', 'entry'); |
| 8 | +(async () => await fs.rm(entryDir, {recursive: true, force: true})); |
7 | 9 | (async () => await fs.mkdir(entryDir, {recursive: true}));
|
8 | 10 |
|
9 | 11 | const fileRequest = new FileRequest(entryDir)
|
|
33 | 35 |
|
34 | 36 | test('customUrl2filePath', () => {
|
35 | 37 | const filePath = fileRequest.customUrl2filePath(article)
|
36 |
| - expect(filePath).toBe('/tmp/gimonfu/fileRequest.test.ts/entry/2020/05/12/today-blog.md') |
| 38 | + expect(filePath).toBe(path.resolve(testDir, 'tmp', 'gimonfu', 'fileRequest.test.ts', 'entry', '2020', '05', '12', 'today-blog.md')) |
37 | 39 | });
|
38 | 40 |
|
39 | 41 | test('filePath2customUrl', async () => {
|
40 |
| - const filePath = '/tmp/gimonfu/fileRequest.test.ts/entry/hoge/fuga.md' |
| 42 | + const filePath = path.resolve(testDir, 'tmp', 'gimonfu', 'fileRequest.test.ts', 'entry', 'hoge', 'fuga.md') |
41 | 43 | const customUrl = await (fileRequest as any).filePath2customUrl(filePath)
|
42 | 44 | expect(customUrl).toBe('hoge/fuga')
|
43 | 45 | })
|
44 | 46 |
|
45 | 47 | test('(invalid) filePath2customUrl', () => {
|
46 |
| - const filePath = '/out-of-entry-dir/tmp/gimonfu/fileRequest.test.ts/entry/hoge/fuga.md' |
| 48 | + const filePath = path.resolve(testDir, 'out-of-entry-dir', 'tmp', 'gimonfu', 'fileRequest.test.ts', 'entry', 'hoge', 'fuga.md') |
47 | 49 | expect(
|
48 | 50 | (fileRequest as any).filePath2customUrl(filePath)
|
49 |
| - ).rejects.toThrowError('Base directory /tmp/gimonfu/fileRequest.test.ts/entry does not contain markdown file path /out-of-entry-dir/tmp/gimonfu/fileRequest.test.ts/entry/hoge/fuga.md') //toMatch('does not contain markdown file path') |
| 51 | + ).rejects.toThrowError(`Base directory ${entryDir} does not contain markdown file path ${filePath}`) //toMatch('does not contain markdown file path') |
50 | 52 | })
|
51 | 53 |
|
52 | 54 | test('write/read', async () => {
|
53 | 55 | // articleオブジェクトをファイルに一度writeしてのちにreadしたとき、内容が一致する
|
54 | 56 | await fileRequest.write(article)
|
55 |
| - const readArticle = await fileRequest.read('/tmp/gimonfu/fileRequest.test.ts/entry/2020/05/12/today-blog.md', {}) |
| 57 | + const readArticle = await fileRequest.read(path.resolve(testDir, 'tmp', 'gimonfu', 'fileRequest.test.ts', 'entry', '2020', '05', '12', 'today-blog.md'), {}) |
56 | 58 | expect(readArticle).toEqual(article)
|
57 | 59 | })
|
0 commit comments