From 494e0795830bf51e4eb3821ad6d1dd57f4a50626 Mon Sep 17 00:00:00 2001 From: Cristian Date: Thu, 21 Apr 2022 19:53:12 +0200 Subject: [PATCH 1/3] chore: Add test for file not found --- package.json | 4 +++- test/fromFile.test.js | 26 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 9a40083..1b991cc 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,8 @@ "mocha": "^9.1.2", "rdf-ext": "^1.3.5", "shelljs": "^0.8.4", - "stricter-standard": "^0.2.0" + "stricter-standard": "^0.2.0", + "assert-throws-async": "^3.0.0", + "get-stream": "^6.0.1" } } diff --git a/test/fromFile.test.js b/test/fromFile.test.js index 4be87d1..d720239 100644 --- a/test/fromFile.test.js +++ b/test/fromFile.test.js @@ -1,5 +1,7 @@ -const { strictEqual, throws } = require('assert') +const { strictEqual } = require('assert') const { resolve } = require('path') +const assertThrows = require('assert-throws-async') +const getStream = require('get-stream') const { isReadable } = require('isstream') const { describe, it } = require('mocha') const rdf = require('rdf-ext') @@ -9,32 +11,36 @@ const example = require('./support/example') describe('fromFile', () => { it('should create a quad stream', async () => { const stream = fromFile(resolve(__dirname, 'support/example.nt')) - stream.resume() - strictEqual(isReadable(stream), true) }) it('should forward options to parser', async () => { const stream = fromFile(resolve(__dirname, 'support/example.ttl'), { baseIRI: 'http://example.org/' }) const dataset = await rdf.dataset().import(stream) - strictEqual(dataset.toCanonical(), example().toCanonical()) }) - it('should throw an error if the file extension is unknown', () => { - throws(() => { + it('should throw an error if the file extension is unknown', async () => { + await assertThrows(async () => { fromFile('test.jpg') - }) + }, Error, /Unknown file extension/) }) - it('should throw an error if the media type is unknown', () => { - throws(() => { + it('should throw an error if the media type is unknown', async () => { + await assertThrows(async () => { fromFile('test.jpg', { extensions: { jpg: 'image/jpeg' } }) - }) + }, Error, /No parser available for media type/) + }) + + it('should throw an error if the file does not exist', async () => { + await assertThrows(async () => { + const stream = fromFile('void.ttl') + await getStream.array(stream) + }, Error, /ENOENT: no such file or directory/) }) }) From 298693f0421291df9bdc699bcd20077cca2b1a85 Mon Sep 17 00:00:00 2001 From: Cristian Date: Tue, 26 Apr 2022 10:25:45 +0200 Subject: [PATCH 2/3] chore: Add test for file not found --- package.json | 4 +--- test/fromFile.test.js | 51 ++++++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 1b991cc..9a40083 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,6 @@ "mocha": "^9.1.2", "rdf-ext": "^1.3.5", "shelljs": "^0.8.4", - "stricter-standard": "^0.2.0", - "assert-throws-async": "^3.0.0", - "get-stream": "^6.0.1" + "stricter-standard": "^0.2.0" } } diff --git a/test/fromFile.test.js b/test/fromFile.test.js index d720239..ae24576 100644 --- a/test/fromFile.test.js +++ b/test/fromFile.test.js @@ -1,7 +1,6 @@ const { strictEqual } = require('assert') +const assert = require('assert/strict') const { resolve } = require('path') -const assertThrows = require('assert-throws-async') -const getStream = require('get-stream') const { isReadable } = require('isstream') const { describe, it } = require('mocha') const rdf = require('rdf-ext') @@ -22,25 +21,43 @@ describe('fromFile', () => { }) it('should throw an error if the file extension is unknown', async () => { - await assertThrows(async () => { - fromFile('test.jpg') - }, Error, /Unknown file extension/) + await assert.rejects( + async () => { + fromFile('test.jpg') + }, + { + name: 'Error', + message: 'Unknown file extension: jpg' + } + ) }) it('should throw an error if the media type is unknown', async () => { - await assertThrows(async () => { - fromFile('test.jpg', { - extensions: { - jpg: 'image/jpeg' - } - }) - }, Error, /No parser available for media type/) + await assert.rejects( + async () => { + fromFile('test.jpg', { + extensions: { + jpg: 'image/jpeg' + } + }) + }, + { + name: 'Error', + message: 'No parser available for media type: image/jpeg' + } + ) }) it('should throw an error if the file does not exist', async () => { - await assertThrows(async () => { - const stream = fromFile('void.ttl') - await getStream.array(stream) - }, Error, /ENOENT: no such file or directory/) + await assert.rejects( + async () => { + const stream = fromFile('void.ttl') + stream.resume() + }, + { + name: 'Error', + message: 'ENOENT: no such file or directory' + } + ) }) -}) +}) \ No newline at end of file From 82b3601892959dcd182629c94034d7f99fb32220 Mon Sep 17 00:00:00 2001 From: Cristian Date: Tue, 26 Apr 2022 10:25:45 +0200 Subject: [PATCH 3/3] chore: Add test for file not found --- package.json | 4 +--- test/fromFile.test.js | 33 ++++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 1b991cc..9a40083 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,6 @@ "mocha": "^9.1.2", "rdf-ext": "^1.3.5", "shelljs": "^0.8.4", - "stricter-standard": "^0.2.0", - "assert-throws-async": "^3.0.0", - "get-stream": "^6.0.1" + "stricter-standard": "^0.2.0" } } diff --git a/test/fromFile.test.js b/test/fromFile.test.js index d720239..84779d3 100644 --- a/test/fromFile.test.js +++ b/test/fromFile.test.js @@ -1,10 +1,11 @@ const { strictEqual } = require('assert') +const assert = require('assert/strict') const { resolve } = require('path') -const assertThrows = require('assert-throws-async') -const getStream = require('get-stream') +const { promisify } = require('util') const { isReadable } = require('isstream') const { describe, it } = require('mocha') const rdf = require('rdf-ext') +const { finished } = require('readable-stream') const fromFile = require('../fromFile') const example = require('./support/example') @@ -21,26 +22,36 @@ describe('fromFile', () => { strictEqual(dataset.toCanonical(), example().toCanonical()) }) - it('should throw an error if the file extension is unknown', async () => { - await assertThrows(async () => { + it('should throw an error if the file extension is unknown', () => { + assert.throws(() => { fromFile('test.jpg') - }, Error, /Unknown file extension/) + }, { + name: 'Error', + message: 'Unknown file extension: jpg' + }) }) - it('should throw an error if the media type is unknown', async () => { - await assertThrows(async () => { + it('should throw an error if the media type is unknown', () => { + assert.throws(() => { fromFile('test.jpg', { extensions: { jpg: 'image/jpeg' } }) - }, Error, /No parser available for media type/) + }, { + name: 'Error', + message: 'No parser available for media type: image/jpeg' + }) }) it('should throw an error if the file does not exist', async () => { - await assertThrows(async () => { + await assert.rejects(async () => { const stream = fromFile('void.ttl') - await getStream.array(stream) - }, Error, /ENOENT: no such file or directory/) + stream.resume() + await promisify(finished)(stream) + }, { + name: 'Error', + message: "ENOENT: no such file or directory, open 'void.ttl'" + }) }) })