From 1f861a67720a18a6246f123eeaffafba77f0055a Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 2 Oct 2019 16:29:18 +0100 Subject: [PATCH] fix regex and add AspellWorker unit tests --- services/spelling/app/js/ASpellWorker.js | 2 +- .../test/unit/js/ASpellWorkerTests.js | 112 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 services/spelling/test/unit/js/ASpellWorkerTests.js diff --git a/services/spelling/app/js/ASpellWorker.js b/services/spelling/app/js/ASpellWorker.js index 6d9921ece4..c4ead15083 100644 --- a/services/spelling/app/js/ASpellWorker.js +++ b/services/spelling/app/js/ASpellWorker.js @@ -130,7 +130,7 @@ class ASpellWorker { this.pipe.stdout.setEncoding('utf8') // ensure utf8 output is handled correctly var output = '' - const endMarkerRegex = new RegExp('^[a-z][a-z]', 'm') + const endMarkerRegex = new RegExp('^[a-z][a-z]', 'gm') this.pipe.stdout.on('data', data => { // We receive the language code from Aspell as the end of data marker in // the data. The input is a utf8 encoded string. diff --git a/services/spelling/test/unit/js/ASpellWorkerTests.js b/services/spelling/test/unit/js/ASpellWorkerTests.js new file mode 100644 index 0000000000..fa66f2ac05 --- /dev/null +++ b/services/spelling/test/unit/js/ASpellWorkerTests.js @@ -0,0 +1,112 @@ +/* eslint-disable + handle-callback-err, + no-undef +*/ +// TODO: This file was created by bulk-decaffeinate. +// Sanity-check the conversion and remove this comment. +/* + * decaffeinate suggestions: + * DS102: Remove unnecessary code created because of implicit returns + * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md + */ +const sinon = require('sinon') +const chai = require('chai') +const { should, expect, assert } = chai +const SandboxedModule = require('sandboxed-module') +const EventEmitter = require('events') + +describe('ASpellWorker', function () { + beforeEach(function () { + this.child_process = {} + return (this.ASpellWorker = SandboxedModule.require('../../../app/js/ASpellWorker', { + requires: { + 'logger-sharelatex': { + log() { }, + info() { }, + err() { } + }, + 'metrics-sharelatex': { + gauge() { }, + inc() { } + }, + 'child_process': this.child_process + } + })) + }) + + describe("creating a worker", function () { + beforeEach(function () { + this.pipe = { + 'stdout': new EventEmitter(), + 'stderr': { on: sinon.stub() }, + 'stdin': {on: sinon.stub() }, + 'on': sinon.stub(), + 'pid': 12345 + } + this.child_process.spawn = sinon.stub().returns(this.pipe) + this.pipe.stdout.setEncoding = sinon.stub() + worker = new this.ASpellWorker('en') + + }) + + describe("with normal aspell output", function () { + beforeEach(function () { + this.callback = worker.callback = sinon.stub() + this.pipe.stdout.emit('data', '& hello\n') + this.pipe.stdout.emit('data', '& world\n') + this.pipe.stdout.emit('data', 'en\n') + this.pipe.stdout.emit('data', '& goodbye') + }) + + it('should call the callback', function() { + expect(this.callback.called).to.equal(true) + expect(this.callback.calledWith(null, "& hello\n& world\nen\n")).to.equal(true) + }) + }) + + describe("with the aspell end marker split across chunks", function () { + beforeEach(function () { + this.callback = worker.callback = sinon.stub() + this.pipe.stdout.emit('data', '& hello\n') + this.pipe.stdout.emit('data', '& world\ne') + this.pipe.stdout.emit('data', 'n\n') + this.pipe.stdout.emit('data', '& goodbye') + }) + + it('should call the callback', function() { + expect(this.callback.called).to.equal(true) + expect(this.callback.calledWith(null, "& hello\n& world\nen\n")).to.equal(true) + }) + }) + + describe("with the aspell end marker newline split across chunks", function () { + beforeEach(function () { + this.callback = worker.callback = sinon.stub() + this.pipe.stdout.emit('data', '& hello\n') + this.pipe.stdout.emit('data', '& world\n') + this.pipe.stdout.emit('data', 'en') + this.pipe.stdout.emit('data', '\n& goodbye') + }) + + it('should call the callback', function() { + expect(this.callback.called).to.equal(true) + expect(this.callback.calledWith(null, "& hello\n& world\nen")).to.equal(true) + }) + }) + + describe("with everything split across chunks", function () { + beforeEach(function () { + this.callback = worker.callback = sinon.stub() + '& hello\n& world\nen\n& goodbye'.split('').forEach(x => { + this.pipe.stdout.emit('data', x) + }) + }) + + it('should call the callback', function() { + expect(this.callback.called).to.equal(true) + expect(this.callback.calledWith(null, "& hello\n& world\nen")).to.equal(true) + }) + }) + + }) +})