Skip to content

Commit

Permalink
Merge pull request #25 from ryancyq/test/blob
Browse files Browse the repository at this point in the history
test: more coverage for blob
  • Loading branch information
ryancyq authored Sep 27, 2024
2 parents cdd04ca + 3667b75 commit 650edba
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
86 changes: 65 additions & 21 deletions __tests__/blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -25,32 +25,76 @@ 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')

expect(blob.path).toBe('fixtures/blob.json')
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$/
)
})
})
})
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
2 changes: 1 addition & 1 deletion src/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 650edba

Please sign in to comment.