@@ -2,7 +2,7 @@ import * as core from '@actions/core'
22import fs from 'node:fs'
33import { join } from 'node:path'
44import { Buffer } from 'node:buffer'
5- import { Readable } from 'node:stream'
5+ import { Readable , PassThrough } from 'node:stream'
66import { describe , jest , beforeEach , it , expect } from '@jest/globals'
77import { Blob , getBlob } from '../src/blob'
88import * 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+ / ^ F i l e d o e s n o t e x i s t , p a t h : .+ \/ m y _ s t r e a m \. t x t $ /
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+ / ^ R e a d f i l e f a i l e d , e r r o r : s t r e a m e r r o r , p a t h : .+ \/ f i x t u r e s \/ e r r o r \. t x t $ /
97+ )
98+ } )
5599 } )
56100} )
0 commit comments