Skip to content

Commit 650edba

Browse files
authored
Merge pull request #25 from ryancyq/test/blob
test: more coverage for blob
2 parents cdd04ca + 3667b75 commit 650edba

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

__tests__/blob.test.ts

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as core from '@actions/core'
22
import fs from 'node:fs'
33
import { join } from 'node:path'
44
import { Buffer } from 'node:buffer'
5-
import { Readable } from 'node:stream'
5+
import { Readable, PassThrough } from 'node:stream'
66
import { describe, jest, beforeEach, it, expect } from '@jest/globals'
77
import { Blob, getBlob } from '../src/blob'
88
import * as cwd from '../src/utils/cwd'
@@ -25,32 +25,76 @@ describe('Blob', () => {
2525
expect(blob.absolutePath).toBe(join(__dirname, '/my_path.txt'))
2626
})
2727

28-
it('stream', async () => {
29-
const blob = new Blob('/my_stream.txt')
30-
jest
31-
.spyOn(blob, 'streamable', 'get')
32-
.mockReturnValue(Readable.from('Hello World'))
33-
34-
const chunks: Buffer[] = []
35-
for await (const chunk of blob.streamable) {
36-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
37-
}
38-
const streamedContent = Buffer.concat(chunks).toString('utf8')
39-
expect(streamedContent).toEqual('Hello World')
40-
})
41-
4228
it('getBlob', async () => {
4329
const blob = getBlob('fixtures/blob.json')
4430

4531
expect(blob.path).toBe('fixtures/blob.json')
4632
expect(blob.absolutePath).toBe(join(__dirname, 'fixtures/blob.json'))
4733
})
4834

49-
it('load', async () => {
50-
const blob = getBlob('fixtures/blob.txt')
51-
const fileAddition = await blob.load()
52-
expect(fileAddition.contents).toEqual(
53-
fs.readFileSync(join(__dirname, 'fixtures/blob.base64.txt')).toString()
54-
)
35+
it('getBlob collection', async () => {
36+
const blobs = getBlob(['fixtures/blob.json'])
37+
expect(blobs.length).toBe(1)
38+
const blob = blobs[0]
39+
expect(blob.path).toBe('fixtures/blob.json')
40+
expect(blob.absolutePath).toBe(join(__dirname, 'fixtures/blob.json'))
41+
})
42+
43+
describe('stream', () => {
44+
it('file does not exist', async () => {
45+
const blob = new Blob('/my_stream.txt')
46+
expect(() => blob.streamable).toThrow(
47+
/^File does not exist, path: .+\/my_stream\.txt$/
48+
)
49+
})
50+
51+
it('file exists', async () => {
52+
const blob = new Blob('/my_stream.txt')
53+
jest
54+
.spyOn(blob, 'streamable', 'get')
55+
.mockReturnValue(Readable.from('Hello World'))
56+
57+
const chunks: Buffer[] = []
58+
for await (const chunk of blob.streamable) {
59+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
60+
}
61+
const streamedContent = Buffer.concat(chunks).toString('utf8')
62+
expect(streamedContent).toEqual('Hello World')
63+
})
64+
})
65+
66+
describe('load', () => {
67+
it('successfully', async () => {
68+
const blob = getBlob('fixtures/blob.txt')
69+
const fileAddition = await blob.load()
70+
expect(fileAddition.contents).toEqual(
71+
fs.readFileSync(join(__dirname, 'fixtures/blob.base64.txt')).toString()
72+
)
73+
})
74+
75+
it('file with string', async () => {
76+
const blob = getBlob('fixtures/error.txt')
77+
const mockStream = new PassThrough()
78+
jest.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream)
79+
80+
const loadPromise = blob.load()
81+
mockStream.emit('data', 'string data')
82+
mockStream.end()
83+
await expect(loadPromise).resolves.toEqual({
84+
contents: 'string data',
85+
path: 'fixtures/error.txt',
86+
})
87+
})
88+
89+
it('stream with error', async () => {
90+
const blob = getBlob('fixtures/error.txt')
91+
const mockStream = new PassThrough()
92+
jest.spyOn(blob, 'streamable', 'get').mockReturnValue(mockStream)
93+
94+
blob.load()
95+
expect(() => mockStream.emit('error', new Error('stream error'))).toThrow(
96+
/^Read file failed, error: stream error, path: .+\/fixtures\/error\.txt$/
97+
)
98+
})
5599
})
56100
})

dist/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30375,7 +30375,7 @@ class Blob {
3037530375
}
3037630376
get streamable() {
3037730377
if (!fs.existsSync(this.absolutePath)) {
30378-
throw new Error(`File does not exist, path: ${this.absolutePath}.`);
30378+
throw new Error(`File does not exist, path: ${this.absolutePath}`);
3037930379
}
3038030380
return fs
3038130381
.createReadStream(this.absolutePath, { encoding: 'utf8' })

src/blob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class Blob {
2323

2424
get streamable(): Readable {
2525
if (!fs.existsSync(this.absolutePath)) {
26-
throw new Error(`File does not exist, path: ${this.absolutePath}.`)
26+
throw new Error(`File does not exist, path: ${this.absolutePath}`)
2727
}
2828

2929
return fs

0 commit comments

Comments
 (0)