1
0
Fork 0
liza/test/conf/ConfLoaderTest.ts

157 lines
4.2 KiB
TypeScript
Raw Normal View History

/**
* Tests ConfLoader
2019-11-11 16:57:24 -05:00
*
* Copyright (C) 2010-2019 R-T Specialty, LLC.
*
* This file is part of the Liza Data Collection Framework.
*
* liza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2019-11-11 16:57:24 -05:00
const chai = require( 'chai' );
const expect = chai.expect;
import { readFile } from "fs";
import { ConfLoader as Sut } from "../../src/conf/ConfLoader";
type FsLike = { readFile: typeof readFile };
const {
store: {
MemoryStore: Store,
},
} = require( '../../' );
chai.use( require( 'chai-as-promised' ) );
describe( 'ConfLoader', () =>
{
it( "loads Store'd configuration from file", () =>
{
const expected_path = "/foo/bar/baz.json";
const expected_data = '{ "foo": "bar" }';
2019-11-11 16:57:24 -05:00
const fs = <FsLike>{
readFile( path: string, encoding: string, callback: any )
{
expect( path ).to.equal( expected_path );
expect( encoding ).to.equal( 'utf8' );
callback( null, expected_data );
},
};
return expect(
2019-11-11 16:57:24 -05:00
new Sut( fs, Store )
.fromFile( expected_path )
.then( conf => conf.get( 'foo' ) )
).to.eventually.deep.equal( JSON.parse( expected_data ).foo );
} );
it( "fails on read error", () =>
{
const expected_err = Error( 'rejected' );
2019-11-11 16:57:24 -05:00
const fs = <FsLike>{
readFile( _: any, __: any, callback: any )
{
callback( expected_err, null );
},
};
2019-11-11 16:57:24 -05:00
return expect( new Sut( fs, Store ).fromFile( '' ) )
.to.eventually.be.rejectedWith( expected_err );
} );
it( "can override #parseConfData for custom parser", () =>
{
const result = { foo: {} };
const input = "foo";
2019-11-11 16:57:24 -05:00
const fs = <FsLike>{
readFile( _: any, __: any, callback: any )
{
callback( null, input );
},
};
2019-11-11 16:57:24 -05:00
const sut = new class extends Sut
{
2019-11-11 16:57:24 -05:00
parseConfData( given_input: string )
{
expect( given_input ).to.equal( input );
return Promise.resolve( result );
2019-11-11 16:57:24 -05:00
}
}( fs, Store );
return expect(
sut.fromFile( '' )
.then( conf => conf.get( 'foo' ) )
).to.eventually.equal( result.foo );
} );
it( 'rejects promise on parsing error', () =>
{
const expected_err = SyntaxError( 'test parsing error' );
2019-11-11 16:57:24 -05:00
const fs = <FsLike>{
readFile( _: any, __: any, callback: any )
{
// make async so that we clear the stack, and therefore
// try/catch
process.nextTick( () => callback( null, '' ) );
},
};
2019-11-11 16:57:24 -05:00
const sut = new class extends Sut
{
2019-11-11 16:57:24 -05:00
parseConfData( _given_input: string ): never
{
throw expected_err;
2019-11-11 16:57:24 -05:00
}
}( fs, Store );
return expect( sut.fromFile( '' ) )
.to.eventually.be.rejectedWith( expected_err );
} );
it( "rejects promise on Store ctor error", () =>
{
const expected_err = Error( 'test Store ctor error' );
2019-11-11 16:57:24 -05:00
const fs = <FsLike>{
readFile: ( _: any, __: any, callback: any ) =>
callback( null, '' ),
};
const badstore = () => { throw expected_err };
2019-11-11 16:57:24 -05:00
return expect( new Sut( fs, badstore ).fromFile( '' ) )
.to.eventually.be.rejectedWith( expected_err );
} );
it( "rejects promise on bad fs call", () =>
{
2019-11-11 16:57:24 -05:00
return expect( new Sut( <FsLike>{}, Store ).fromFile( '' ) )
.to.eventually.be.rejected;
} );
} );