-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from steveukx/feature/tests-update
Tests updated to use jest
- Loading branch information
Showing
13 changed files
with
3,610 additions
and
758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.idea/ | ||
|
||
coverage/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const {join} = require('path'); | ||
const {realpathSync} = require('fs'); | ||
const {io} = require('./io'); | ||
|
||
module.exports.createTestContext = async function createTestContext () { | ||
const root = await io.mkdtemp(); | ||
|
||
const context = { | ||
path (...segments) { | ||
return join(root, ...segments); | ||
}, | ||
async dir (...paths) { | ||
if (!paths.length) { | ||
return root; | ||
} | ||
|
||
return await io.mkdir(context.path(...paths)); | ||
}, | ||
async file (path, content = `File content ${ path }`) { | ||
if (Array.isArray(path)) { | ||
await context.dir(path[0]); | ||
} | ||
|
||
const pathArray = Array.isArray(path) ? path : [path]; | ||
return await io.writeFile(context.path(...pathArray), content); | ||
}, | ||
async files (...paths) { | ||
for (const path of paths) { | ||
await context.file(path); | ||
} | ||
}, | ||
get root () { | ||
return root; | ||
}, | ||
get rootResolvedPath () { | ||
return realpathSync(root); | ||
}, | ||
}; | ||
|
||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const {existsSync, mkdir, mkdtemp, readFile, statSync, writeFile} = require('fs'); | ||
|
||
module.exports.io = { | ||
isdir (path) { | ||
try { | ||
return statSync(path).isDirectory(); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
}, | ||
mkdir (path) { | ||
return new Promise((done, fail) => { | ||
if (existsSync(path)) { | ||
return done(path); | ||
} | ||
|
||
mkdir(path, (err) => err ? fail(err) : done(path)); | ||
}); | ||
}, | ||
mkdtemp () { | ||
return new Promise((done, fail) => { | ||
mkdtemp((process.env.TMPDIR || '/tmp/') + 'properties-reader-test-', (err, path) => { | ||
err ? fail(err) : done(path); | ||
}); | ||
}); | ||
}, | ||
readFile (path, encoding = 'utf-8') { | ||
return new Promise((done, fail) => { | ||
readFile(path, encoding, (err, data) => { | ||
err ? fail(err) : done(data); | ||
}) | ||
}); | ||
}, | ||
writeFile (path, content, encoding = 'utf-8') { | ||
return new Promise((done, fail) => { | ||
writeFile(path, content, encoding, (err) => { | ||
err ? fail(err) : done(path); | ||
}) | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,76 @@ | ||
const expect = require('expect.js'); | ||
const {spy} = require('sinon'); | ||
const {createTestContext} = require('./__fixtues__/create-test-context'); | ||
const {io} = require('./__fixtues__/io'); | ||
|
||
describe('bind-to-server', () => { | ||
|
||
let properties; | ||
const propertiesReader = require('../'); | ||
|
||
const tempFile = require('./utils/temporary-file'); | ||
const {givenFilePropertiesReader} = require('./utils/bdd'); | ||
describe('bind-to-server', () => { | ||
|
||
function givenTheProperties (content) { | ||
return properties = givenFilePropertiesReader(content); | ||
} | ||
let context; | ||
let app; | ||
|
||
beforeEach(() => { | ||
beforeEach(async () => { | ||
context = await createTestContext(); | ||
app = { | ||
set: jest.fn(), | ||
}; | ||
}); | ||
afterEach(() => jest.restoreAllMocks()); | ||
|
||
afterEach(() => tempFile.tearDown()); | ||
it('Creates directories when necessary - absolute paths', async () => { | ||
const dirPath = context.path('foo'); | ||
const file = ` | ||
some.property.dir = ${ dirPath } | ||
foo.bar = A Value | ||
`; | ||
|
||
it('Creates directories when necessary - absolute paths', () => { | ||
const dirPath = tempFile.pushDir('/tmp/' + Math.floor(Math.random() * 1e10).toString(16)); | ||
const app = {set: spy()}; | ||
propertiesReader(await context.file('properties.ini', file)) | ||
.bindToExpress(app, null, true); | ||
|
||
givenTheProperties(` | ||
expect(io.isdir(dirPath)).toBe(true); | ||
}); | ||
|
||
it('Does not create directories when already present', async () => { | ||
const dirPath = await context.dir('foo'); | ||
const file = ` | ||
some.property.dir = ${ dirPath } | ||
foo.bar = A Value | ||
`; | ||
|
||
`).bindToExpress(app, null, true); | ||
propertiesReader(await context.file('properties.ini', file)) | ||
.bindToExpress(app, null, true); | ||
|
||
expect(require('fs').statSync(dirPath).isDirectory()).to.be.ok(); | ||
expect(io.isdir(dirPath)).toBe(true); | ||
}); | ||
|
||
it('Creates directories when necessary - relative paths', () => { | ||
const dirName = Math.floor(Math.random() * 1e10).toString(16); | ||
const dirBase = process.cwd(); | ||
const dirPath = tempFile.pushDir(dirBase + '/' + dirName); | ||
const app = {set: spy()}; | ||
|
||
givenTheProperties(` | ||
it('Creates directories when necessary - relative paths', async () => { | ||
jest.spyOn(process, 'cwd').mockReturnValue(context.root); | ||
|
||
const dirName = 'bar'; | ||
const dirPath = context.path(dirName); | ||
const file = ` | ||
some.property.dir = ${ dirName } | ||
foo.bar = A Value | ||
`; | ||
|
||
`).bindToExpress(app, dirBase, true); | ||
propertiesReader(await context.file('properties.ini', file)) | ||
.bindToExpress(app, null, true); | ||
|
||
expect(require('fs').statSync(dirPath).isDirectory()).to.be.ok(); | ||
expect(io.isdir(dirPath)).toBe(true); | ||
}); | ||
|
||
it('Creates directories when necessary - relative path to explicit working directory', async () => { | ||
const dirName = 'bar'; | ||
const dirPath = context.path(dirName); | ||
const file = ` | ||
some.property.dir = ${ dirName } | ||
foo.bar = A Value | ||
`; | ||
|
||
propertiesReader(await context.file('properties.ini', file)) | ||
.bindToExpress(app, context.root, true); | ||
|
||
expect(io.isdir(dirPath)).toBe(true); | ||
}); | ||
|
||
}); | ||
|
Oops, something went wrong.