Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to add raw data #7

Closed
sdumetz opened this issue Apr 18, 2014 · 4 comments
Closed

Not able to add raw data #7

sdumetz opened this issue Apr 18, 2014 · 4 comments

Comments

@sdumetz
Copy link

sdumetz commented Apr 18, 2014

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
Copy link
Member

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

@sdumetz
Copy link
Author

sdumetz commented Apr 22, 2014

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
Copy link
Member

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
Copy link
Author

sdumetz commented Apr 22, 2014

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

sdumetz pushed a commit to sdumetz/node-fs-mock that referenced this issue Apr 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants