@@ -4,7 +4,11 @@ const os = require('os');
44const path = require ( 'path' ) ;
55const fs = require ( 'fs' ) ;
66const test = require ( 'tape' ) ;
7+ const { promisify} = require ( 'es6-promisify' ) ;
8+ const tryToCatch = require ( 'try-to-catch' ) ;
9+
710const writejson = require ( '..' ) ;
11+ const _writejson = promisify ( writejson ) ;
812
913const tmp = os . tmpdir ( ) ;
1014const NAME = path . join ( tmp , String ( Math . random ( ) ) ) ;
@@ -21,7 +25,7 @@ test('writejson: should write json data to file', (t) => {
2125 t . notOk ( error , 'no read error' ) ;
2226 t . deepEqual ( json , JSON . parse ( data ) , 'data should be equal' ) ;
2327
24- fs . unlink ( NAME , error => {
28+ fs . unlink ( NAME , ( error ) => {
2529 t . notOk ( error , 'no remove error' ) ;
2630 t . end ( ) ;
2731 } ) ;
@@ -51,7 +55,7 @@ test('writejson: should write json data to file with options', (t) => {
5155 t . equal ( resultStr , data , 'data should be equal' ) ;
5256 t . deepEqual ( JSON . parse ( data ) , result , 'objects should be equal' ) ;
5357
54- fs . unlink ( NAME , error => {
58+ fs . unlink ( NAME , ( error ) => {
5559 t . notOk ( error , 'no remove error' ) ;
5660 t . end ( ) ;
5761 } ) ;
@@ -60,7 +64,7 @@ test('writejson: should write json data to file with options', (t) => {
6064} ) ;
6165
6266test ( 'writejson: should write json data to file with default options' , ( t ) => {
63- const resultStr = JSON . stringify ( json , null , 4 ) + '\n'
67+ const resultStr = JSON . stringify ( json , null , 4 ) + '\n' ;
6468
6569 writejson ( NAME , json , error => {
6670 t . notOk ( error , 'no write error' ) ;
@@ -71,7 +75,7 @@ test('writejson: should write json data to file with default options', (t) => {
7175 t . equal ( resultStr , data , 'data should be equal' ) ;
7276 t . deepEqual ( JSON . parse ( data ) , json , 'objects should be equal' ) ;
7377
74- fs . unlink ( NAME , error => {
78+ fs . unlink ( NAME , ( error ) => {
7579 t . notOk ( error , 'no remove error' ) ;
7680 t . end ( ) ;
7781 } ) ;
@@ -80,12 +84,32 @@ test('writejson: should write json data to file with default options', (t) => {
8084} ) ;
8185
8286test ( 'writejson: write error' , ( t ) => {
83- writejson ( '/hello.json' , json , error => {
87+ writejson ( '/hello.json' , json , ( error ) => {
8488 t . ok ( error , 'should return error: ' + error . message ) ;
8589 t . end ( ) ;
8690 } ) ;
8791} ) ;
8892
93+ test ( 'writejson: write options' , async ( t ) => {
94+ const json = {
95+ hello : 'world' ,
96+ } ;
97+
98+ const options = {
99+ mode : 0o600
100+ } ;
101+
102+ await tryToCatch ( _writejson , NAME , json , options ) ;
103+
104+ const { mode} = fs . statSync ( NAME ) ;
105+ const expected = Number ( mode ) . toString ( 8 ) . slice ( 3 ) ;
106+
107+ fs . unlinkSync ( NAME ) ;
108+
109+ t . equal ( expected , '600' , 'should equal' ) ;
110+ t . end ( ) ;
111+ } ) ;
112+
89113test ( 'writejson.sync.try: write error' , ( t ) => {
90114 const error = writejson . sync . try ( '/hello.json' , json ) ;
91115
@@ -100,14 +124,14 @@ test('writejson: no args', (t) => {
100124
101125test ( 'writejson: no json' , ( t ) => {
102126 const fn = ( ) => writejson ( 'hello' ) ;
103-
127+
104128 t . throws ( fn , / j s o n s h o u l d b e o b j e c t ! / , 'json check' ) ;
105129 t . end ( ) ;
106130} ) ;
107131
108132test ( 'writejson: options not object' , ( t ) => {
109133 const fn = ( ) => writejson ( 'hello' , { } , 'options' , ( ) => { } ) ;
110-
134+
111135 t . throws ( fn , / o p t i o n s s h o u l d b e o b j e c t ! / , 'options check' ) ;
112136 t . end ( ) ;
113137} ) ;
0 commit comments