tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
911 stars 86 forks source link

/undefined path being created when passing '/' #204

Open feliupe opened 7 years ago

feliupe commented 7 years ago

If you run mock({'/': {}}) on linux '/undefined' will be created in the mocked filesystem.

I think the problem is here, it returns [] if you input '/':

When it returns to FileSystem.create :

FileSystem.create = function(paths, options) {
  var system = new FileSystem(options);
  console.log(paths, Object.keys(paths).length)
  for (var filepath in paths) {
    var parts = getPathParts(filepath);
    var directory = system._root;
    var i, ii, name, candidate;
    for (i = 0, ii = parts.length - 1; i < ii; ++i) {
      name = parts[i];
      candidate = directory.getItem(name);
      if (!candidate) {
        directory = directory.addItem(name, new Directory());
      } else if (candidate instanceof Directory) {
        directory = candidate;
      } else {
        throw new Error('Failed to create directory: ' + filepath);
      }
    }
    console.log(parts[i], parts)

    populate(directory, parts[i], paths[filepath]);
  }

  return system;
};

It enters the loop and get undefined in parts[i].

So, this is a bug?