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

Replace mockFs load method #47

Open
dicolasi opened this issue Apr 16, 2024 · 3 comments
Open

Replace mockFs load method #47

dicolasi opened this issue Apr 16, 2024 · 3 comments

Comments

@dicolasi
Copy link

dicolasi commented Apr 16, 2024

Hi all,
I am in the process of replacing mock-fs with mem-fs due to its lack of support for node 20.X. I am struggling to get mem-fs working in this simple case:

Here the original test case with mock-fs:

  it('should use local hosts if they exist', async () => {
    mockFs({
      'admin/exchange-hosts.yaml': mockFs.load('test/unit/input-files/exchange-hosts.yaml'),
      'exchange-config': mockFs.load('test/unit/input-files/gateway/exchange-config-integration')
    });
    await validateHostsByEnvironment({ exchangeConfigPath: 'exchange-config', env: Environment.production, validation });
    expect(validation.errors).to.be.empty;
    expect(validation.warnings).to.be.empty;
    expect(validation.info).to.contain('[prod/real.yaml] Hosts authorised');
    expect(getExchangeHostsRawStub.notCalled).to.be.true;
  });

and here the refactored one with mem-fs that does not work:

  it('should use local hosts if they exist', async () => {
    vol.fromNestedJSON({
      'admin/exchange-hosts.yaml': ufs.readFileSync('test/unit/input-files/exchange-hosts.yaml'),
      'exchange-config': await loadIntoMockFS('test/unit/input-files/gateway/exchange-config-integration'),
    }, appPath);
    await validateHostsByEnvironment({ exchangeConfigPath: 'exchange-config', env: Environment.production, validation });
    expect(validation.errors).to.be.empty;
    expect(validation.warnings).to.be.empty;
    expect(validation.info).to.contain('[prod/real.yaml] Hosts authorised');
    expect(getExchangeHostsRawStub.notCalled).to.be.true;
  });

the method loadIntoMockFS is here:

async function loadIntoMockFS(dirPath: string): Promise<NestedDirectoryJSON> {
  const entries = await fsPromises.readdir(dirPath, {withFileTypes: true});
  const fileTree: NestedDirectoryJSON = {};

  for (const entry of entries) {
    const entryPath = path.join(dirPath, entry.name);
    if (entry.isDirectory()) {
      fileTree[entry.name] = await loadIntoMockFS(entryPath);
    } else if (entry.isFile()) {
      fileTree[entry.name] = await fsPromises.readFile(entryPath, 'utf-8');
    }
  }

  return fileTree;
}

I am not sure I actually need the loadIntoMockFS method at all. The test fail since the folder exchange-config cannot be found (but if I print vol.tree I can definitely see it).

I am sure I am missing something stupid here :)

@SukkaW
Copy link

SukkaW commented Apr 22, 2024

This is actually a nice feature to have! I am having the exactly same use case.

I can implement this feature in a PR if maintainers approve it.

@dicolasi
Copy link
Author

@SukkaW that would be great!

@simonc
Copy link
Owner

simonc commented Apr 22, 2024

Hi 🙂 I'd be happy to review and approve a PR. Just to be sure though. The code in the issue is in JS while memFS uses Ruby. Du you use memFS with JS?

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

3 participants