Skip to content

Commit

Permalink
Merge pull request #7 from license2e/add-tests
Browse files Browse the repository at this point in the history
Add tests for JSON merge
  • Loading branch information
license2e authored Oct 20, 2016
2 parents e9266f0 + 76f117f commit 9bb2db7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "refs",
"version": "0.9.1",
"version": "0.9.2",
"description": "Compile and merge YAML, JSON or INI config files together through file path references",
"main": "index.js",
"preferGlobal": true,
Expand Down
9 changes: 9 additions & 0 deletions test/data/file-merge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"test": {
"$merge": [{
"$ref": "./file.json"
}, {
"$ref": "./file-refs.json"
}]
}
}
57 changes: 57 additions & 0 deletions test/test-lib-json-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,63 @@ describe('JSON Tests', () => {
});
});

it('process: should throw error the file with malformed merge settings', (done) => {
fs.writeFileSync(JSON_MERGE_FILE, '{"$merge":[{"one":true},{"two":true}]}', 'utf-8');
const jsonProcessor = require('../lib/processor-json');

jsonProcessor.process(JSON_MERGE_FILE)
.then(() => {
done('Rejection failed.');
})
.catch((err) => {
should(err).be.eql('Malformed merge setting, please check the input file.');
done();
});
});

it('process: should throw error the file with malformed merge settings with ref settings', (done) => {
fs.writeFileSync(JSON_MERGE_FILE, '{"$merge":[{"one":true},{"two":true}]}', 'utf-8');
const jsonContent = fs.readFileSync(JSON_FILE.replace('/tmp', `${__dirname}/data`), 'utf-8');
const jsonRefContent = fs.readFileSync(JSON_REF_FILE.replace('/tmp', `${__dirname}/data`), 'utf-8');
const jsonMergeContent = fs.readFileSync(JSON_MERGE_FILE.replace('/tmp', `${__dirname}/data`), 'utf-8');
fs.writeFileSync(JSON_FILE, jsonContent, 'utf-8');
fs.writeFileSync(JSON_REF_FILE, jsonRefContent, 'utf-8');
const jsonMergeContentMalformed = JSON.stringify(Object.assign({}, JSON.parse(jsonMergeContent), JSON.parse('{"another":{"$merge":[{"one":true},{"two":true}]}}')));
fs.writeFileSync(JSON_MERGE_FILE, jsonMergeContentMalformed, 'utf-8');
const jsonProcessor = require('../lib/processor-json');

jsonProcessor.process(JSON_MERGE_FILE)
.then(() => {
done('Rejection failed.');
})
.catch((err) => {
should(err).be.eql('Malformed merge setting, please check the input file.');
done();
});
});

it('process: should process the file with merge settings', (done) => {
const jsonContent = fs.readFileSync(JSON_FILE.replace('/tmp', `${__dirname}/data`), 'utf-8');
const jsonRefContent = fs.readFileSync(JSON_REF_FILE.replace('/tmp', `${__dirname}/data`), 'utf-8');
const jsonMergeContent = fs.readFileSync(JSON_MERGE_FILE.replace('/tmp', `${__dirname}/data`), 'utf-8');
fs.writeFileSync(JSON_FILE, jsonContent, 'utf-8');
fs.writeFileSync(JSON_REF_FILE, jsonRefContent, 'utf-8');
fs.writeFileSync(JSON_MERGE_FILE, jsonMergeContent, 'utf-8');
const jsonProcessor = require('../lib/processor-json');

jsonProcessor.process(JSON_MERGE_FILE)
.then((results) => {
should(results).be.eql({
dataString: '{"test":{"test":true,"another":{"test":true}}}',
key: undefined,
});
done();
})
.catch((err) => {
done(err);
});
});

it('write: should throw error on write', (done) => {
td.replace(fs, 'writeFile', (outputFile, data, option, cb) => cb('An error occurred.'));
const jsonProcessor = require('../lib/processor-json');
Expand Down

0 comments on commit 9bb2db7

Please sign in to comment.