Skip to content

Remove mockfs use memfs#1574

Closed
ShinobuTakahashi wants to merge 7 commits intoremove-browserify-use-rollupfrom
remove-mockfs-use-memfs
Closed

Remove mockfs use memfs#1574
ShinobuTakahashi wants to merge 7 commits intoremove-browserify-use-rollupfrom
remove-mockfs-use-memfs

Conversation

@ShinobuTakahashi
Copy link
Copy Markdown
Contributor

@ShinobuTakahashi ShinobuTakahashi commented Jul 23, 2025

概要

#1561 の browserify -> rollup 変更に伴うテスト周りの修正。

  • akashic-cli-commons の mock-fsmemfs へ変更。
  • memfs で対応できない箇所は、実ファイルでテストを実施。

備考
#1561 へマージします。commons 以外の対応が完了しCIが通るまでマージしません
scan と lib-manage で一部 commons の NodeModules.ts を利用している箇所でテストが落ちている。別途対応。

mockfs(mockFsContent);

let pkgjsonPaths = await NodeModules.listModuleFiles(".", ["dummy@1", "dummy2", "dummy3"], logger);
let pkgjsonPaths = await NodeModules.listModuleFiles(contentPath, ["dummy", "dummy2", "dummy3"], logger);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummy@1 という名前がそもそもないので rollup で not found でエラーとなるので dummy に修正

import { Util } from "..";

describe("NodeModules", () => {
const mockFsContent = {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

うーん、実ファイルを展開するしかないという話をしたのは私で申し訳ないんですが、こうして見ると「相互依存度の高い」「(テスト用なので) 名前を見ても大して中身がわからない」「1-2行の」ファイル (=テストコードとセットで見ないと意味がないファイル) が量産されてつらいことになってしまいますね……。特にファイルが多いほかのモジュールでもやると大変そうです。

ちょっと方針転換して、この mock 定義をそのまま実ファイルシステムに展開するユーティリティを作る路線はどうでしょうか。

イメージ:

interface FSContentDescDir {
  [filename: string]: FSContentDescDir | string;
}

function setupFsContentImpl(baseDir: string, key: string, def: FSContentDescDir): void {
  if (typeof def === "string") {
    if (def.startsWith("simlink:")) {
      ...
    } else {
      ...
    }
  } else {
    const dir = join(baseDir, key)
    mkdir(dir);
    setupFsContent(dir, def);
  }
}

function setupFsContent(baseDir: string, def: FSContentDescDir): void {
   Object.entries(def).forEach(([key, val] => {
      setupFsContentImpl(baseDir, key, val);
   });
}

export function withFsContent<T>(def: FSContentDescDir, fun: () => Promise<T>): Promise<T> {
  const dir = mkdtemp();
  setupFsContent(dir, def);
  try {
    return await fun();
  } finally {
    rm(dir, { recursive: true, force: true });
  }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

議論: withFsContent のように実行までラップするより、vol.fromJSON() と同じ形 (vol.reset() 的な関数を afterEach() で呼ぶ) 方がほかテストと形が揃っていてわかりやすいかも。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。この PR をクローズし別途 PR を作成します。

@ShinobuTakahashi
Copy link
Copy Markdown
Contributor Author

実ファイルシステムを展開するユーティリティで対応するためこの PR をクローズします。

@ShinobuTakahashi ShinobuTakahashi deleted the remove-mockfs-use-memfs branch March 26, 2026 05:09
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

Successfully merging this pull request may close these issues.

2 participants