|
1 | | -const expect = require('expect.js'); |
2 | | -const {spy} = require('sinon'); |
| 1 | +const {createTestContext} = require('./__fixtues__/create-test-context'); |
| 2 | +const {io} = require('./__fixtues__/io'); |
3 | 3 |
|
4 | | -describe('bind-to-server', () => { |
5 | | - |
6 | | - let properties; |
| 4 | +const propertiesReader = require('../'); |
7 | 5 |
|
8 | | - const tempFile = require('./utils/temporary-file'); |
9 | | - const {givenFilePropertiesReader} = require('./utils/bdd'); |
| 6 | +describe('bind-to-server', () => { |
10 | 7 |
|
11 | | - function givenTheProperties (content) { |
12 | | - return properties = givenFilePropertiesReader(content); |
13 | | - } |
| 8 | + let context; |
| 9 | + let app; |
14 | 10 |
|
15 | | - beforeEach(() => { |
| 11 | + beforeEach(async () => { |
| 12 | + context = await createTestContext(); |
| 13 | + app = { |
| 14 | + set: jest.fn(), |
| 15 | + }; |
16 | 16 | }); |
| 17 | + afterEach(() => jest.restoreAllMocks()); |
17 | 18 |
|
18 | | - afterEach(() => tempFile.tearDown()); |
| 19 | + it('Creates directories when necessary - absolute paths', async () => { |
| 20 | + const dirPath = context.path('foo'); |
| 21 | + const file = ` |
| 22 | + some.property.dir = ${ dirPath } |
| 23 | + foo.bar = A Value |
| 24 | + `; |
19 | 25 |
|
20 | | - it('Creates directories when necessary - absolute paths', () => { |
21 | | - const dirPath = tempFile.pushDir('/tmp/' + Math.floor(Math.random() * 1e10).toString(16)); |
22 | | - const app = {set: spy()}; |
| 26 | + propertiesReader(await context.file('properties.ini', file)) |
| 27 | + .bindToExpress(app, null, true); |
23 | 28 |
|
24 | | - givenTheProperties(` |
| 29 | + expect(io.isdir(dirPath)).toBe(true); |
| 30 | + }); |
25 | 31 |
|
| 32 | + it('Does not create directories when already present', async () => { |
| 33 | + const dirPath = await context.dir('foo'); |
| 34 | + const file = ` |
26 | 35 | some.property.dir = ${ dirPath } |
27 | | -
|
28 | 36 | foo.bar = A Value |
| 37 | + `; |
29 | 38 |
|
30 | | - `).bindToExpress(app, null, true); |
| 39 | + propertiesReader(await context.file('properties.ini', file)) |
| 40 | + .bindToExpress(app, null, true); |
31 | 41 |
|
32 | | - expect(require('fs').statSync(dirPath).isDirectory()).to.be.ok(); |
| 42 | + expect(io.isdir(dirPath)).toBe(true); |
33 | 43 | }); |
34 | 44 |
|
35 | | - it('Creates directories when necessary - relative paths', () => { |
36 | | - const dirName = Math.floor(Math.random() * 1e10).toString(16); |
37 | | - const dirBase = process.cwd(); |
38 | | - const dirPath = tempFile.pushDir(dirBase + '/' + dirName); |
39 | | - const app = {set: spy()}; |
40 | | - |
41 | | - givenTheProperties(` |
| 45 | + it('Creates directories when necessary - relative paths', async () => { |
| 46 | + jest.spyOn(process, 'cwd').mockReturnValue(context.root); |
42 | 47 |
|
| 48 | + const dirName = 'bar'; |
| 49 | + const dirPath = context.path(dirName); |
| 50 | + const file = ` |
43 | 51 | some.property.dir = ${ dirName } |
44 | | -
|
45 | 52 | foo.bar = A Value |
| 53 | + `; |
46 | 54 |
|
47 | | - `).bindToExpress(app, dirBase, true); |
| 55 | + propertiesReader(await context.file('properties.ini', file)) |
| 56 | + .bindToExpress(app, null, true); |
48 | 57 |
|
49 | | - expect(require('fs').statSync(dirPath).isDirectory()).to.be.ok(); |
| 58 | + expect(io.isdir(dirPath)).toBe(true); |
50 | 59 | }); |
51 | 60 |
|
| 61 | + it('Creates directories when necessary - relative path to explicit working directory', async () => { |
| 62 | + const dirName = 'bar'; |
| 63 | + const dirPath = context.path(dirName); |
| 64 | + const file = ` |
| 65 | + some.property.dir = ${ dirName } |
| 66 | + foo.bar = A Value |
| 67 | + `; |
| 68 | + |
| 69 | + propertiesReader(await context.file('properties.ini', file)) |
| 70 | + .bindToExpress(app, context.root, true); |
| 71 | + |
| 72 | + expect(io.isdir(dirPath)).toBe(true); |
| 73 | + }); |
52 | 74 |
|
53 | 75 | }); |
54 | 76 |
|
0 commit comments