1
1
import { existsSync } from 'node:fs' ;
2
2
import path from 'node:path' ;
3
+ import { pathToFileURL } from 'node:url' ;
3
4
4
5
import { write , writeSync } from '..' ;
5
- import { read } from '../..' ;
6
+ import { read , readSync } from '../..' ;
6
7
7
8
let tmpDir : string ;
8
9
beforeEach ( ( ) => {
@@ -40,36 +41,37 @@ test('async write image to disk (jpeg)', async () => {
40
41
expect ( imgRead . colorModel ) . toBe ( 'RGBA' ) ;
41
42
} ) ;
42
43
43
- test ( 'write image to disk' , async ( ) => {
44
+ test ( 'sync write image to disk' , ( ) => {
44
45
const img = testUtils . load ( 'opencv/test.png' ) ;
45
46
const destination = path . join ( tmpDir , 'image.png' ) ;
46
47
writeSync ( destination , img ) ;
47
48
expect ( existsSync ( destination ) ) . toBe ( true ) ;
48
- const imgRead = await read ( destination ) ;
49
+ const imgRead = readSync ( destination ) ;
49
50
expect ( imgRead ) . toMatchImage ( img ) ;
50
51
} ) ;
51
52
52
- test ( 'write image to disk (jpeg)' , async ( ) => {
53
+ test ( 'sync write image to disk (jpeg)' , ( ) => {
53
54
const img = testUtils . load ( 'opencv/test.png' ) ;
54
55
const destination = path . join ( tmpDir , 'image.jpeg' ) ;
55
56
writeSync ( destination , img ) ;
56
57
expect ( existsSync ( destination ) ) . toBe ( true ) ;
57
- const imgRead = await read ( destination ) ;
58
+ const imgRead = readSync ( destination ) ;
58
59
expect ( imgRead . width ) . toBe ( img . width ) ;
59
60
expect ( imgRead . colorModel ) . toBe ( 'RGBA' ) ;
60
61
} ) ;
61
62
62
- test ( 'write mask image to disk' , async ( ) => {
63
+ test ( 'sync write mask image to disk' , ( ) => {
63
64
let img = testUtils . load ( 'opencv/test.png' ) ;
64
65
img = img . convertColor ( 'GREY' ) ;
65
66
let mask = img . threshold ( ) ;
66
67
let maskImage = mask . convertColor ( 'GREY' ) ;
67
68
const destination = path . join ( tmpDir , 'image.png' ) ;
68
69
writeSync ( destination , mask ) ;
69
70
expect ( existsSync ( destination ) ) . toBe ( true ) ;
70
- const imgRead = await read ( destination ) ;
71
+ const imgRead = readSync ( destination ) ;
71
72
expect ( imgRead ) . toMatchImage ( maskImage ) ;
72
73
} ) ;
74
+
73
75
test ( 'async write mask image to disk' , async ( ) => {
74
76
let img = testUtils . load ( 'opencv/test.png' ) ;
75
77
img = img . convertColor ( 'GREY' ) ;
@@ -82,6 +84,51 @@ test('async write mask image to disk', async () => {
82
84
expect ( imgRead ) . toMatchImage ( maskImage ) ;
83
85
} ) ;
84
86
87
+ test ( 'async write with URL' , async ( ) => {
88
+ const img = testUtils . load ( 'opencv/test.png' ) ;
89
+ const destination = pathToFileURL ( path . join ( tmpDir , 'image.png' ) ) ;
90
+ await write ( destination , img ) ;
91
+ expect ( existsSync ( destination ) ) . toBe ( true ) ;
92
+ const imgRead = await read ( destination ) ;
93
+ expect ( imgRead ) . toMatchImage ( img ) ;
94
+ } ) ;
95
+
96
+ test ( 'sync write with URL' , ( ) => {
97
+ const img = testUtils . load ( 'opencv/test.png' ) ;
98
+ const destination = pathToFileURL ( path . join ( tmpDir , 'image.png' ) ) ;
99
+ writeSync ( destination , img ) ;
100
+ expect ( existsSync ( destination ) ) . toBe ( true ) ;
101
+ const imgRead = readSync ( destination ) ;
102
+ expect ( imgRead ) . toMatchImage ( img ) ;
103
+ } ) ;
104
+
105
+ test ( 'async write with recursive option' , async ( ) => {
106
+ const img = testUtils . load ( 'opencv/test.png' ) ;
107
+ const destination = path . join ( tmpDir , 'subdir/123' , 'image.png' ) ;
108
+ await write ( destination , img , { recursive : true } ) ;
109
+ expect ( existsSync ( destination ) ) . toBe ( true ) ;
110
+ const imgRead = await read ( destination ) ;
111
+ expect ( imgRead ) . toMatchImage ( img ) ;
112
+ } ) ;
113
+
114
+ test ( 'sync write with recursive option' , ( ) => {
115
+ const img = testUtils . load ( 'opencv/test.png' ) ;
116
+ const destination = path . join ( tmpDir , 'subdir/123' , 'image.png' ) ;
117
+ writeSync ( destination , img , { recursive : true } ) ;
118
+ expect ( existsSync ( destination ) ) . toBe ( true ) ;
119
+ const imgRead = readSync ( destination ) ;
120
+ expect ( imgRead ) . toMatchImage ( img ) ;
121
+ } ) ;
122
+
123
+ test ( 'unknown format error' , ( ) => {
124
+ const img = testUtils . load ( 'opencv/test.png' ) ;
125
+ const destination = path . join ( tmpDir , 'image.png' ) ;
126
+ // @ts -expect-error test invalid format
127
+ expect ( ( ) => writeSync ( destination , img , { format : 'foo' } ) ) . toThrow (
128
+ / u n k n o w n f o r m a t : f o o / ,
129
+ ) ;
130
+ } ) ;
131
+
85
132
test ( 'image extension error' , async ( ) => {
86
133
const img = testUtils . load ( 'opencv/test.png' ) ;
87
134
const destination = path . join ( tmpDir , 'image.tiff' ) ;
0 commit comments