fix(fs): Improve fs and stream handling, enhance SmartFile/StreamFile, update tests and CI configs
This commit is contained in:
@@ -9,53 +9,83 @@ const testAssetsPath = './test/testassets/';
|
||||
// StreamFile tests
|
||||
// ---------------------------
|
||||
|
||||
tap.test('StreamFile.fromPath should create a StreamFile from a file path', async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromPath(path.join(testAssetsPath, 'mytest.json'));
|
||||
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
|
||||
const contentBuffer = await streamFile.getContentAsBuffer();
|
||||
expect(contentBuffer).toBeInstanceOf(Buffer);
|
||||
});
|
||||
tap.test(
|
||||
'StreamFile.fromPath should create a StreamFile from a file path',
|
||||
async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromPath(
|
||||
path.join(testAssetsPath, 'mytest.json'),
|
||||
);
|
||||
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
|
||||
const contentBuffer = await streamFile.getContentAsBuffer();
|
||||
expect(contentBuffer).toBeInstanceOf(Buffer);
|
||||
},
|
||||
);
|
||||
|
||||
tap.test('StreamFile.fromUrl should create a StreamFile from a URL', async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromUrl('http://example.com/somefile.json');
|
||||
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
|
||||
});
|
||||
tap.test(
|
||||
'StreamFile.fromUrl should create a StreamFile from a URL',
|
||||
async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromUrl(
|
||||
'http://example.com/somefile.json',
|
||||
);
|
||||
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
|
||||
},
|
||||
);
|
||||
|
||||
tap.test('StreamFile.fromBuffer should create a StreamFile from a Buffer', async () => {
|
||||
const buffer = Buffer.from('Some content');
|
||||
const streamFile = smartfile.StreamFile.fromBuffer(buffer, 'bufferfile.txt');
|
||||
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
|
||||
});
|
||||
tap.test(
|
||||
'StreamFile.fromBuffer should create a StreamFile from a Buffer',
|
||||
async () => {
|
||||
const buffer = Buffer.from('Some content');
|
||||
const streamFile = smartfile.StreamFile.fromBuffer(
|
||||
buffer,
|
||||
'bufferfile.txt',
|
||||
);
|
||||
expect(streamFile).toBeInstanceOf(smartfile.StreamFile);
|
||||
},
|
||||
);
|
||||
|
||||
tap.test('StreamFile should write the stream to disk', async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromPath(path.join(testAssetsPath, 'mytest.json'));
|
||||
await streamFile.writeToDisk(path.join(testAssetsPath, 'temp', 'mytest.json'));
|
||||
const streamFile = await smartfile.StreamFile.fromPath(
|
||||
path.join(testAssetsPath, 'mytest.json'),
|
||||
);
|
||||
await streamFile.writeToDisk(
|
||||
path.join(testAssetsPath, 'temp', 'mytest.json'),
|
||||
);
|
||||
// Verify the file was written
|
||||
expect(
|
||||
// We'll use the fileExists method from your smartfile library
|
||||
// Replace with the actual method you use to check file existence
|
||||
await smartfile.fs.fileExists(path.join(testAssetsPath, 'temp', 'mytest.json'))
|
||||
await smartfile.fs.fileExists(
|
||||
path.join(testAssetsPath, 'temp', 'mytest.json'),
|
||||
),
|
||||
).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('StreamFile should write to a directory', async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromPath(path.join(testAssetsPath, 'mytest.json'));
|
||||
const streamFile = await smartfile.StreamFile.fromPath(
|
||||
path.join(testAssetsPath, 'mytest.json'),
|
||||
);
|
||||
await streamFile.writeToDir(path.join(testAssetsPath, 'temp'));
|
||||
// Verify the file was written
|
||||
expect(
|
||||
await smartfile.fs.fileExists(path.join(testAssetsPath, 'temp', 'mytest.json'))
|
||||
await smartfile.fs.fileExists(
|
||||
path.join(testAssetsPath, 'temp', 'mytest.json'),
|
||||
),
|
||||
).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('StreamFile should return content as a buffer', async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromPath(path.join(testAssetsPath, 'mytest.json'));
|
||||
const streamFile = await smartfile.StreamFile.fromPath(
|
||||
path.join(testAssetsPath, 'mytest.json'),
|
||||
);
|
||||
const contentBuffer = await streamFile.getContentAsBuffer();
|
||||
expect(contentBuffer).toBeInstanceOf(Buffer);
|
||||
// Further checks on the content can be added here if necessary
|
||||
});
|
||||
|
||||
tap.test('StreamFile should return content as a string', async () => {
|
||||
const streamFile = await smartfile.StreamFile.fromPath(path.join(testAssetsPath, 'mytest.json'));
|
||||
const streamFile = await smartfile.StreamFile.fromPath(
|
||||
path.join(testAssetsPath, 'mytest.json'),
|
||||
);
|
||||
const contentString = await streamFile.getContentAsString();
|
||||
expect(contentString).toBeTypeofString();
|
||||
// Verify the content matches what's expected
|
||||
|
Reference in New Issue
Block a user