[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[nodeJS] 043 File Write (sync)

"043-1 Writing files with #writeSync:buffer:offset:length:position:"
| fs data text|
fs := NodeJS fs.
text := 'YYZ'.	
fs open: (TestPath, 'Sample43-1.txt') flags: #w with: [:error :fd| | jsObject |
   error isNil ifFalse: [^self error: '043-1' ,error].
   jsObject := {'new Buffer(3);'}.
   data := (NodeBuffer @ jsObject).   
   data write: text.   	   
   [fs writeSync: fd buffer: data offset: 0 length: (data length) position:0]
	on: Error
	do: [:ex| ^self error: '043-1' ,ex].
   fs closeSync:fd.
].


"043-2 Writing files with #writeSync:buffer:offset:length:"
| fs data text|
fs := NodeJS fs.
text := 'YYZ'.	
fs open: (TestPath, 'Sample43-2.txt') flags: #w with: [:error :fd| | jsObject | 
   error isNil ifFalse: [ ^self error: '043-2' ,error].
   jsObject := {'new Buffer(3);'}.
   data := (NodeBuffer @ jsObject).   
   data write: text.   	   
   [fs writeSync: fd buffer: data offset: 0 length: (data length)]
	on: Error
	do: [:ex| ^self error: '043-2' ,ex].
    fs closeSync:fd.
].


"043-3 Writing files with #writeSync:data:position:encoding:"
| fs |
fs := NodeJS fs.
fs open: (TestPath, 'Sample43-3.txt') flags: #w with: [:error :fd| | size | 
   error isNil ifFalse: [ ^self error: '043-3 ' ,error].
   [fs writeSync: fd data: 'Tom Sawyer' position: 0 encoding:#utf8]
	on: Error
	do: [:ex| ^self error: '043-3' ,ex].
    fs closeSync:fd.
].


"043-4 Writing files with #writeSync:data:position:"
| fs |
fs := NodeJS fs.
fs open: (TestPath, 'Sample43-4.txt') flags: #w with: [:error :fd| 
   error isNil ifFalse: [ ^self error: '043-4 ' ,error].
   [fs writeSync: fd data: 'Red Barchetta' position: 0 ]
	on: Error
	do: [:ex| ^self error: '043-4 ' ,ex].
    fs closeSync:fd.
].


"043-5 Writing files with #writeSync:data:"
| fs |
fs := NodeJS fs.
fs open: (TestPath, 'Sample43-5.txt') flags: #w with: [:error :fd| 
   error isNil ifFalse: [ ^self error: '043-5 ' ,error].
   [fs writeSync: fd data: 'Witch Hunt']
	on: Error
	do: [:ex| ^self error: '043-5 ' ,ex].
    fs closeSync:fd.
].


"043-6 Writing files with #writeFileSync:data:"
[NodeJS fs writeFileSync: (TestPath, 'Sample43-6.txt') data: 'Content generated by S8.']
   on: Error do: [:ex | ^self error: '043-6 ' ,ex ].


"043-7 Writing files with #writeFileSync:data:options:"
| options |
options := ((#encoding -> #utf8),(#mode -> 666),(#flag -> #w)).
[NodeJS fs writeFileSync: (TestPath, 'Sample43-7.txt') data: 'YYZ' options: options ]
   on: Error
   do: [:ex | ^self error: '043-7 ' ,ex].