Carrooi / Node-FsMock

[ABANDONED] Mock for fs module
MIT License
7 stars 2 forks source link

Not able to add raw data #7

Closed sdumetz closed 9 years ago

sdumetz commented 10 years ago

It might be a desired behavior or just me doing thing in a strange way, but I'd like to use fs-mock with some binary data (a tiny .tar archive).

The idea behind this is being allowed to test some function that performs operations on this archive without actually creating files on system.

I tried to make a readFile() of the archive then calling new FS() with data as a value for some file.

This got me a "Unknown Type" error.

Looking at the source, I understand well how it happens. However, I don't know if it's supposed to be a desired behaviour or if it's just that raw output isn't yet implemented and it may be useful if I tried to submit a pull request on this feature

davidkudera commented 10 years ago

Hi, can you please submit your code with which you are trying to create FS object?

sdumetz commented 10 years ago

It's something like this :

fs = require('fs);
FS=require('fs-mock');
fs.readFile('sample.tar',{flag:'r'},function(err,data){
    fsMock = new FS({
            'tmp':{
                'testFile':'someData',
                'sample':data
            })

}

Maybe I need to do some transformation over raw data returned by readFile, but I'd like to be able to read it again with a ̀̀̀`fsMock.readFile([...])`` later and get the same result.

davidkudera commented 10 years ago

I see.. This should definitely work and I probably just forgot to add it, because here https://github.com/sakren/node-fs-mock/blob/develop/src/fs.coffee#L106 it is tested if it is string and here https://github.com/sakren/node-fs-mock/blob/develop/src/fs.coffee#L139-L144 it is tested if it is string or instance of Buffer class.

So if you want to implement this and create pull request, I'll be really glad.

Temporary workaround should be this, but it also transform data from Buffer into string:

var fs = require('fs');
var mock = require('fs-mock');

fs.readFile('sample.tar', {flag: 'r', encoding: 'utf8'}, function(err, data) {
    // rest of your code
});

or without specified encoding:

new mock({
    tmp: {
        sample: data.toString('utf8')
    }
});
sdumetz commented 10 years ago

Thanks for this workaround. it works great. I'll try to submit a pull request to implement this