From 97c8cb501ef33d7a06dd5869c869dfeb7d38bc66 Mon Sep 17 00:00:00 2001 From: Ryan Chang Date: Fri, 27 Sep 2024 21:40:04 +0800 Subject: [PATCH 1/3] fix: file not exist error message --- src/blob.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blob.ts b/src/blob.ts index 30ec7c6..b6d598b 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -23,7 +23,7 @@ export class Blob { get streamable(): Readable { if (!fs.existsSync(this.absolutePath)) { - throw new Error(`File does not exist, path: ${this.absolutePath}.`) + throw new Error(`File does not exist, path: ${this.absolutePath}`) } return fs From 5251c8e933455262dbe7dcaea72fafe603c1eec2 Mon Sep 17 00:00:00 2001 From: Ryan Chang Date: Fri, 27 Sep 2024 22:05:37 +0800 Subject: [PATCH 2/3] test: add file/stream errors test for blob --- __tests__/blob.test.ts | 86 +++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/__tests__/blob.test.ts b/__tests__/blob.test.ts index dafebdb..cbb2436 100644 --- a/__tests__/blob.test.ts +++ b/__tests__/blob.test.ts @@ -2,7 +2,7 @@ import * as core from '@actions/core' import fs from 'node:fs' import { join } from 'node:path' import { Buffer } from 'node:buffer' -import { Readable } from 'node:stream' +import { Readable, PassThrough } from 'node:stream' import { describe, jest, beforeEach, it, expect } from '@jest/globals' import { Blob, getBlob } from '../src/blob' import * as cwd from '../src/utils/cwd' @@ -25,20 +25,6 @@ describe('Blob', () => { expect(blob.absolutePath).toBe(join(__dirname, '/my_path.txt')) }) - it('stream', async () => { - const blob = new Blob('/my_stream.txt') - jest - .spyOn(blob, 'streamable', 'get') - .mockReturnValue(Readable.from('Hello World')) - - const chunks: Buffer[] = [] - for await (const chunk of blob.streamable) { - chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) - } - const streamedContent = Buffer.concat(chunks).toString('utf8') - expect(streamedContent).toEqual('Hello World') - }) - it('getBlob', async () => { const blob = getBlob('fixtures/blob.json') @@ -46,11 +32,69 @@ describe('Blob', () => { expect(blob.absolutePath).toBe(join(__dirname, 'fixtures/blob.json')) }) - it('load', async () => { - const blob = getBlob('fixtures/blob.txt') - const fileAddition = await blob.load() - expect(fileAddition.contents).toEqual( - fs.readFileSync(join(__dirname, 'fixtures/blob.base64.txt')).toString() - ) + it('getBlob collection', async () => { + const blobs = getBlob(['fixtures/blob.json']) + expect(blobs.length).toBe(1) + const blob = blobs[0] + expect(blob.path).toBe('fixtures/blob.json') + expect(blob.absolutePath).toBe(join(__dirname, 'fixtures/blob.json')) + }) + + describe('stream', () => { + it('file does not exist', async () => { + const blob = new Blob('/my_stream.txt') + expect(() => blob.streamable).toThrow( + /^File does not exist, path: .+\/my_stream\.txt$/ + ) + }) + + it('file exists', async () => { + const blob = new Blob('/my_stream.txt') + jest + .spyOn(blob, 'streamable', 'get') + .mockReturnValue(Readable.from('Hello World')) + + const chunks: Buffer[] = [] + for await (const chunk of blob.streamable) { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) + } + const streamedContent = Buffer.concat(chunks).toString('utf8') + expect(streamedContent).toEqual('Hello World') + }) + }) + + describe('load', () => { + it('successfully', async () => { + const blob = getBlob('fixtures/blob.txt') + const fileAddition = await blob.load() + expect(fileAddition.contents).toEqual( + fs.readFileSync(join(__dirname, 'fixtures/blob.base64.txt')).toString() + ) + }) + + it('file with string', async () => { + const blob = getBlob('fixtures/error.txt') + const mockStream = new PassThrough() + jest.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream) + + const loadPromise = blob.load() + mockStream.emit('data', 'string data') + mockStream.end() + await expect(loadPromise).resolves.toEqual({ + contents: 'string data', + path: 'fixtures/error.txt', + }) + }) + + it('stream with error', async () => { + const blob = getBlob('fixtures/error.txt') + const mockStream = new PassThrough() + jest.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream) + + blob.load() + expect(() => mockStream.emit('error', new Error('stream error'))).toThrow( + /^Read file failed, error: stream error, path: .+\/fixtures\/error\.txt$/ + ) + }) }) }) From 3667b756f5aa7b8aec3a83490b2464bacb07f533 Mon Sep 17 00:00:00 2001 From: Ryan Chang Date: Fri, 27 Sep 2024 22:05:56 +0800 Subject: [PATCH 3/3] chore: update bundle dist --- dist/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index 8ec0a9a..f47999c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30375,7 +30375,7 @@ class Blob { } get streamable() { if (!fs.existsSync(this.absolutePath)) { - throw new Error(`File does not exist, path: ${this.absolutePath}.`); + throw new Error(`File does not exist, path: ${this.absolutePath}`); } return fs .createReadStream(this.absolutePath, { encoding: 'utf8' })