From 24ef94f232f212fe30a7360e06d897ea15a265e2 Mon Sep 17 00:00:00 2001 From: Lee Dohm Date: Sat, 13 May 2017 20:19:22 -0700 Subject: [PATCH] Add tests for .getPluginConfig --- test/fixtures/config/basic.yml | 3 ++ test/fixtures/config/empty.yml | 0 test/fixtures/config/malformed.yml | 1 + test/robot.js | 77 ++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 test/fixtures/config/basic.yml create mode 100644 test/fixtures/config/empty.yml create mode 100644 test/fixtures/config/malformed.yml diff --git a/test/fixtures/config/basic.yml b/test/fixtures/config/basic.yml new file mode 100644 index 00000000..35df5c33 --- /dev/null +++ b/test/fixtures/config/basic.yml @@ -0,0 +1,3 @@ +foo: 5 +bar: 7 +baz: 11 diff --git a/test/fixtures/config/empty.yml b/test/fixtures/config/empty.yml new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/config/malformed.yml b/test/fixtures/config/malformed.yml new file mode 100644 index 00000000..59c227c5 --- /dev/null +++ b/test/fixtures/config/malformed.yml @@ -0,0 +1 @@ +@ diff --git a/test/robot.js b/test/robot.js index 55da6353..7bf23561 100644 --- a/test/robot.js +++ b/test/robot.js @@ -1,3 +1,5 @@ +const fs = require('fs'); +const path = require('path'); const expect = require('expect'); const Context = require('../lib/context'); const createRobot = require('../lib/robot'); @@ -7,6 +9,12 @@ const nullLogger = {}; nullLogger[level] = function () { }; }); +function readConfig(fileName) { + const configPath = path.join(__dirname, 'fixtures', 'config', fileName); + const content = fs.readFileSync(configPath, {encoding: 'utf8'}); + return {content: Buffer.from(content).toString('base64')}; +} + describe('Robot', function () { let webhook; let robot; @@ -63,4 +71,73 @@ describe('Robot', function () { expect(spy).toNotHaveBeenCalled(); }); }); + + describe('getPluginConfig', function () { + let github; + + beforeEach(function () { + github = { + repos: { + getContent: spy + } + }; + }); + + it('gets a valid configuration', async function () { + spy.andReturn(Promise.resolve(readConfig('basic.yml'))); + const config = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml'); + + expect(spy).toHaveBeenCalled(); + expect(spy.calls[0].arguments[0]).toEqual({ + owner: 'owner', + repo: 'repo', + path: '.github/test-file.yml' + }); + expect(config).toEqual({ + foo: 5, + bar: 7, + baz: 11 + }); + }); + + it('throws when the file is missing', async function () { + spy.andReturn(Promise.reject(new Error('An error occurred'))); + + let e; + let contents; + try { + await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml'); + } catch (err) { + e = err; + } + + expect(contents).toNotExist(); + expect(e).toExist(); + expect(e.message).toEqual('An error occurred'); + }); + + it('throws when the configuration file is malformed', async function () { + spy.andReturn(Promise.resolve(readConfig('malformed.yml'))); + + let e; + let contents; + try { + contents = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml'); + } catch (err) { + e = err; + } + + expect(contents).toNotExist(); + expect(e).toExist(); + expect(e.message).toMatch(/^end of the stream or a document separator/); + }); + + it('returns an empty object when the file is empty', async function () { + spy.andReturn(readConfig('empty.yml')); + + const contents = await robot.getPluginConfig(github, 'owner', 'repo', 'test-file.yml'); + + expect(contents).toEqual({}); + }); + }); });