From 5567afa4deed58583a400a654042ac932433df68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20=C5=A0imi=C4=87?= Date: Tue, 9 Sep 2014 15:46:17 +0200 Subject: [PATCH] start throwing errors when file doesn't exist --- index.js | 3 +++ package.json | 1 + tests.js | 45 ++++++++++++++++++++++++++++----------------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 220b1bd..73bb83c 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +var fs = require('fs') + function Play(opts){ var opts = opts || {} @@ -9,6 +11,7 @@ function Play(opts){ this.play = function(what){ if (!what) return; + if (!fs.existsSync(what)) throw new Error("Couldn't find file: " + what) var players = this.players, self = this diff --git a/package.json b/package.json index e087a48..6952e11 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "devDependencies": { "expect.js": "^0.3.1", "mocha": "^1.21.4", + "mock-fs": "^2.3.1", "sinon": "^1.10.3" } } diff --git a/tests.js b/tests.js index 55e8564..a281955 100644 --- a/tests.js +++ b/tests.js @@ -1,15 +1,20 @@ var expect = require('expect.js') , sinon = require('sinon') + , mock = require('mock-fs') describe('cvlc has the maximum priority', function(){ - it('tries to play with cvlc first', function(){ - var command = "cvlc beep.mp3" - , spy = sinon.stub() + var command, spy, cli; - var cli = require('./')({ child_process: { - exec: spy - }}) + beforeEach(function(){ + command = "cvlc beep.mp3" + , spy = sinon.stub() + , cli = require('./')({ child_process: { exec: spy }}) + + mock({'./beep.mp3': ''}) + }) + + it('tries to play with cvlc first', function(){ spy.callsArg(1) cli.play("beep.mp3") @@ -20,31 +25,37 @@ describe('cvlc has the maximum priority', function(){ }) it("doesn't try to play anything if nothing is passed", function(){ - var command = "cvlc beep.mp3" - , spy = sinon.spy() - - var cli = require('./')({ child_process: spy}) cli.play() expect(spy.called).to.not.be(true) }) it("fallbacks to other players if it's not available", function(){ - var command = "cvlc beep.mp3" - , spy = sinon.stub() - - var cli = require('./')({ child_process: { - exec: spy - }}) spy.callsArg(1) spy.withArgs("cvlc beep.mp3").callsArgWith(1, "cvlc: command not found") cli.play("beep.mp3") expect(cli.player).to.be("mplayer") }) + + after(function(){ + mock.restore() + }) }) describe('error handling', function(){ - it("throws errors if file doesn't exist") + it("throws errors if the file doesn't exist", function(){ + var stub = sinon.stub() + , cli = require('./')({ child_process: {exec: stub}}) + + expect(cli.play).withArgs("beep.mp3").to.throwException() + }) + it("throws errors if suitable audio tool couldn't be found") }) + +describe("overridable options", function(){ + it("supports overrides for the list of players") + it("supports override for child_process") + it("supports override for player") +})