From 291a11aa601dfec24b777a7749ebb6e1f34c2e91 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 4 Nov 2023 20:14:20 +0100 Subject: [PATCH] fix(core): update --- test/test.streamfile.ts | 67 +++++++++++++++++++ test/testassets/temp/mytest.json | 8 +++ .../temp/test/testassets/mytest.json | 8 +++ ts/00_commitinfo_data.ts | 2 +- ts/classes.streamfile.ts | 8 ++- ts/index.ts | 1 + 6 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 test/testassets/temp/mytest.json create mode 100644 test/testassets/temp/test/testassets/mytest.json diff --git a/test/test.streamfile.ts b/test/test.streamfile.ts index e69de29..e862d79 100644 --- a/test/test.streamfile.ts +++ b/test/test.streamfile.ts @@ -0,0 +1,67 @@ +import * as path from 'path'; +import { expect, tap } from '@push.rocks/tapbundle'; +import * as smartfile from '../ts/index.js'; // adjust the import path as needed + +// Test assets path +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.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 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')); + // 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')) + ).toBeTrue(); +}); + +tap.test('StreamFile should write to a directory', async () => { + 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')) + ).toBeTrue(); +}); + +tap.test('StreamFile should return content as a buffer', async () => { + 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 contentString = await streamFile.getContentAsString(); + expect(typeof contentString).toBeTypeofString(); + // Verify the content matches what's expected + // This assumes the file contains a JSON object with a key 'key1' with value 'this works' + expect(JSON.parse(contentString).key1).toEqual('this works'); +}); + +// Start the test sequence +tap.start(); diff --git a/test/testassets/temp/mytest.json b/test/testassets/temp/mytest.json new file mode 100644 index 0000000..3439934 --- /dev/null +++ b/test/testassets/temp/mytest.json @@ -0,0 +1,8 @@ +{ + "key1": "this works", + "key2": "this works too", + "key3": { + "nestedkey1": "hello" + } +} + diff --git a/test/testassets/temp/test/testassets/mytest.json b/test/testassets/temp/test/testassets/mytest.json new file mode 100644 index 0000000..3439934 --- /dev/null +++ b/test/testassets/temp/test/testassets/mytest.json @@ -0,0 +1,8 @@ +{ + "key1": "this works", + "key2": "this works too", + "key3": { + "nestedkey1": "hello" + } +} + diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 9afd05b..e55835f 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartfile', - version: '10.0.37', + version: '10.0.38', description: 'offers smart ways to work with files in nodejs' } diff --git a/ts/classes.streamfile.ts b/ts/classes.streamfile.ts index 48254e0..c7765cd 100644 --- a/ts/classes.streamfile.ts +++ b/ts/classes.streamfile.ts @@ -1,5 +1,6 @@ import * as plugins from './smartfile.plugins.js'; -import * as fsStream from './fsstream.js'; +import * as smartfileFs from './fs.js'; +import * as smartfileFsStream from './fsstream.js'; import { Readable } from 'stream'; type StreamSource = () => Promise; @@ -21,7 +22,7 @@ export class StreamFile { // STATIC public static async fromPath(filePath: string): Promise { - const streamSource = () => Promise.resolve(fsStream.createReadStream(filePath)); + const streamSource = () => Promise.resolve(smartfileFsStream.createReadStream(filePath)); return new StreamFile(streamSource, filePath); } @@ -55,7 +56,7 @@ export class StreamFile { */ public async writeToDisk(filePathArg: string): Promise { const readStream = await this.createReadStream(); - const writeStream = fsStream.createWriteStream(filePathArg); + const writeStream = smartfileFsStream.createWriteStream(filePathArg); return new Promise((resolve, reject) => { readStream.pipe(writeStream); @@ -67,6 +68,7 @@ export class StreamFile { public async writeToDir(dirPathArg: string) { const filePath = plugins.path.join(dirPathArg, this.relativeFilePath); + await smartfileFs.ensureDir(plugins.path.parse(filePath).dir); return this.writeToDisk(filePath); } diff --git a/ts/index.ts b/ts/index.ts index 7c94907..d789379 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -5,6 +5,7 @@ import * as interpreterMod from './interpreter.js'; import * as memoryMod from './memory.js'; export * from './classes.smartfile.js'; +export * from './classes.streamfile.js'; export * from './classes.virtualdirectory.js'; export const fs = fsMod;