From 67af289a9dca53b3c4c263db70f9e28eba2af7df Mon Sep 17 00:00:00 2001 From: Guillaume Grossetie Date: Fri, 24 Jan 2020 12:00:41 +0100 Subject: [PATCH] Use the new Timings API to remove Opal usage This new API was introduced in Asciidoctor.js 2.0.0. --- lib/invoker.js | 13 ++++++------- test/test.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/invoker.js b/lib/invoker.js index 6db111c..b3e2e03 100644 --- a/lib/invoker.js +++ b/lib/invoker.js @@ -1,4 +1,3 @@ -/* global Opal */ const fs = require('fs') const ospath = require('path') const asciidoctor = require('@asciidoctor/core')() @@ -61,10 +60,10 @@ CLI version ${pkg.version}` static convertFromStdin (options, args) { stdin.read((data) => { if (args['timings']) { - const timings = asciidoctor.Timings.$new() - const instanceOptions = Object.assign({}, options, { timings: timings }) + const timings = asciidoctor.Timings.create() + const instanceOptions = Object.assign({}, options, { timings }) Invoker.convert(asciidoctor.convert, data, instanceOptions) - timings.$print_report(Opal.gvars.stderr, '-') + timings.printReport(process.stderr, '-') } else { Invoker.convert(asciidoctor.convert, data, options) } @@ -94,10 +93,10 @@ CLI version ${pkg.version}` console.log(`converting file ${file}`) } if (timings) { - const timings = asciidoctor.Timings.$new() - const instanceOptions = Object.assign({}, options, { timings: timings }) + const timings = asciidoctor.Timings.create() + const instanceOptions = Object.assign({}, options, { timings }) Invoker.convertFile(file, instanceOptions) - timings.$print_report(Opal.gvars.stderr, file) + timings.printReport(process.stderr, file) } else { Invoker.convertFile(file, options) } diff --git a/test/test.js b/test/test.js index ce2391f..3f33815 100644 --- a/test/test.js +++ b/test/test.js @@ -151,6 +151,24 @@ describe('Help', () => { }) }) +describe('Print timings report', () => { + it('should print timings report', () => { + sinon.stub(process.stderr, 'write') + sinon.stub(process, 'exit') + try { + new Invoker(new Options().parse(['node', 'asciidoctor', `${__dirname}/fixtures/sample.adoc`, '--timings', '-o', '-'])).invoke() + expect(process.stderr.write.called).to.be.true() + expect(process.stderr.write.getCall(0).args[0]).to.equal(`Input file: ${__dirname}/fixtures/sample.adoc`) + expect(process.stderr.write.getCall(1).args[0]).to.include('Time to read and parse source:') + expect(process.stderr.write.getCall(2).args[0]).to.include('Time to convert document:') + expect(process.stderr.write.getCall(3).args[0]).to.include('Total time (read, parse and convert):') + } finally { + process.stderr.write.restore() + process.exit.restore() + } + }) +}) + describe('Version', () => { it('should print version if -v is specified as sole argument', () => { sinon.stub(console, 'log')