Skip to content

Conversation

@ShinobuTakahashi
Copy link
Contributor

@ShinobuTakahashi ShinobuTakahashi commented Jul 30, 2025

概要

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

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

#1574 (comment) でいただいたコメントを反映し、実ファイルを展開するユーティリティを追加。

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

@ShinobuTakahashi ShinobuTakahashi changed the base branch from main to remove-browserify-use-rollup July 30, 2025 01:33
@@ -0,0 +1,48 @@
import * as fs from "fs";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

他パッケージのテストでも使うので "TestUtil" としましたが、ファイル名に Test と入ってるのが違和感があり、"FsContentUtil" とかのファイル名の方が良いでしょうか?

Copy link
Member

@xnv xnv Jul 30, 2025

Choose a reason for hiding this comment

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

ファイル名はそのままでいいと思うのですが、モジュール本体に関係ないので __tests__/ 以下におきませんか。

他パッケージから __tests__/ 以下を import するのも変ではありますが、これは単に (テスト用ユーティリティのパッケージを分離する) 手間を省いただけなので。テストの手間を省く目的のために本体に影響が出るのは避けたいです。

他パッケージで commons の __tests__ 以下を参照する箇所では (本当はテスト用ユーティリティとして獨立させるべきところ手間を省いて commons のコードを参照している旨) コメントをつけておきたいです。

Copy link
Contributor Author

@ShinobuTakahashi ShinobuTakahashi Jul 30, 2025

Choose a reason for hiding this comment

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

ありがとうございます。 __tests__/helper/ 配下に移動しました。

mockfs(mockFsContent);

let pkgjsonPaths = await NodeModules.listModuleFiles(".", ["dummy@1", "dummy2", "dummy3"], logger);
let pkgjsonPaths = await NodeModules.listModuleFiles(targetPath, ["dummy", "dummy2", "dummy3"], logger);
Copy link
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 では解決できないので "dummy" へ修正

"devDependencies": {
"@akashic/eslint-config": "^3.0.1",
"@types/mock-fs": "4.13.4",
"@types/mem-fs": " 2.2.0",
Copy link
Member

Choose a reason for hiding this comment

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

memfs と mem-fs は別物では。memfs は @types 不要ではないかと思います。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。削除しました。

@@ -0,0 +1,48 @@
import * as fs from "fs";
Copy link
Member

@xnv xnv Jul 30, 2025

Choose a reason for hiding this comment

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

ファイル名はそのままでいいと思うのですが、モジュール本体に関係ないので __tests__/ 以下におきませんか。

他パッケージから __tests__/ 以下を import するのも変ではありますが、これは単に (テスト用ユーティリティのパッケージを分離する) 手間を省いただけなので。テストの手間を省く目的のために本体に影響が出るのは避けたいです。

他パッケージで commons の __tests__ 以下を参照する箇所では (本当はテスト用ユーティリティとして獨立させるべきところ手間を省いて commons のコードを参照している旨) コメントをつけておきたいです。

return memfs.fs;
});

// editorconfig の parse() で memfs でモックした .editorconfig が読み込めないため parse() をモックし
Copy link
Member

Choose a reason for hiding this comment

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

コメントが途中で途切れていませんか。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

setupFsContent(dir, def);
return {
"path": dir,
func: () => {
Copy link
Member

Choose a reason for hiding this comment

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

名前をつけて外部から呼ぶものを func と呼ぶのはさすがに変に思います。 dispose() とかじゃないんでしょうか。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

確かに仰る通りです。dispose へ修正しました。

const dir = fs.mkdtempSync(target);
setupFsContent(dir, def);
return {
"path": dir,
Copy link
Member

Choose a reason for hiding this comment

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

" 不要そうです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

修正しました。

}
};

const baseDir = path.resolve(__dirname, "..", "__tests__", "output");
Copy link
Member

Choose a reason for hiding this comment

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

これ mkdtemp(path.resolve(__dirname, "..", "__tests__", "fixture-")) とはできないでしょうか。

あまりないと思いますが、並列にテストすると崩壊するので原則的にはディレクトリを固定にしたくないように思います。またそうでなくても __tests__/output/ というは普通にも使いかねない名前だと思えるので、被りそうにない名前にはしておきたいです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixture- に修正しました。TestUtil の方で mkdtemp()fixture-xxxxx のディレクトリが作成される事を確認しました。


const baseDir = path.resolve(__dirname, "..", "__tests__", "output");
let logger: Logger;
let fsContentResult: testUtil.FsContentResult;
Copy link
Member

Choose a reason for hiding this comment

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

原則的に変数は「型名」でなく「意味」で名付けたいです。(たとえば (敵の攻撃力 - 自分の防御力) という計算の結果は、型名にしたがって num と呼ぶより意味で damage と呼ぶ方がわかりやすいように思います)

これはテスト用の fixture のファイルなので、たとえば fixtureContents とするのはどうでしょうか。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

const baseDir = path.resolve(__dirname, "..", "__tests__", "output");
let logger: Logger;
let fsContentResult: testUtil.FsContentResult;
let targetPath = "";
Copy link
Member

Choose a reason for hiding this comment

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

"target" という新たな言葉を導入されていますが、上に合わせるなら fixtureContentsPath になるかと思います。
利用箇所も多くないので変数を作らず都度 fixtureContents.path としてもよさそうです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixtureContents.path を利用するよう修正しました。

const pkgJsonKey3 = path.resolve(targetPath, "node_modules/dummy3/package.json");

const expectObj: { [key: string]: string } = {};
expectObj[pkgJsonKey1] = path.resolve(targetPath,"node_modules/dummy/main.js").replace(/^\//, "");
Copy link
Member

Choose a reason for hiding this comment

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

空白が 2 つになっています+ , の後の空白が抜けています。他の行にもあるので見直しをお願いします。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

let packageJsonFiles = NodeModules.listPackageJsonsFromScriptsPath(targetPath, filePaths);
// 本来はルート直下の ./node_modules のパスだが、テストで node_modules のパスがルート直下ではないためパスを生成
packageJsonFiles = packageJsonFiles.map(p => path.resolve(targetPath, p));
const moduleMainPaths = NodeModules.listModuleMainPaths(packageJsonFiles);
Copy link
Member

Choose a reason for hiding this comment

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

moduleMainScripts() のテストが moduleMainPaths() のテストに変わっていますが意図されたものでしょうか。前者が残っているなら前者も引き続きテストすべきに思えます。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

すみません、ミスです。 元の moduleMainScripts() に修正しました。

Comment on lines 174 to 181
const pkgJsonKey1 = path.resolve(targetPath,"node_modules/dummy/package.json");
const gameJsonKey = path.resolve(targetPath, "node_modules/dummy/node_modules/dummyChild/package.json");
const pkgJsonKey3 = path.resolve(targetPath, "node_modules/dummy3/package.json");

const expectObj: { [key: string]: string } = {};
expectObj[pkgJsonKey1] = path.resolve(targetPath,"node_modules/dummy/main.js").replace(/^\//, "");
expectObj[gameJsonKey] = path.resolve(targetPath, "node_modules/dummy/node_modules/dummyChild/main.js").replace(/^\//, "");
expectObj[pkgJsonKey3] = path.resolve(targetPath, "node_modules/dummy3/index.js").replace(/^\//, "");
Copy link
Member

Choose a reason for hiding this comment

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

expectObj とか gameJsonKey とか名前をつけなきゃいけないの面倒そうですが、以下のように直接書けたりしないでしょうか。

expect(...).toEqual({
  [path.resolve(targetPath,"node_modules/dummy/package.json")]:
    path.resolve(targetPath,"node_modules/dummy/main.js").replace(/^\//, ""),
  [path.resolve(targetPath, "node_modules/dummy/node_modules/dummyChild/package.json")]:
    path.resolve(targetPath, "node_modules/dummy/node_modules/dummyChild/main.js").replace(/^\//, ""),
  ...
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。ご指摘いただいたよう直接書くよう修正しました。

}
}

export function preperFsContent(def: FSContentDescDir, baseDir?: string): FsContentResult {
Copy link
Member

Choose a reason for hiding this comment

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

  • prepare の typo に見えます。
  • 実際省略するケースがないなら baseDir も必須にしてしまいませんか。(テスト用ユーティリティで汎用の便利挙動をつけておく動機が薄い)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。typo、baseDir を必須化に修正しました。

[filename: string]: FSContentDescDir | string;
}

export interface FsContentResult {
Copy link
Member

Choose a reason for hiding this comment

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

〜Result は「〜の結果」に無理やり名前をつけた感じなので、名付けるなら「〜」の部分は関数名にしたいかなと思います。( FsContentResult だと「FsContent した結果」みたいな意味に感じられて違和感があります)

逆にすぱっと FsContent と言い切るという手もありますが、それにしては持ってるプロパティが簡素すぎるので、PrepareFsContentResult ですかね。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。最初に FsContent にしたのですが、仰る通りプロパティが少なく Content なのかと疑問に思ったので Result を付けた経緯です。
PrepareFsContentResult に修正しました。

import * as os from "os";
import path from "path";

interface FSContentDescDir {
Copy link
Member

@xnv xnv Aug 1, 2025

Choose a reason for hiding this comment

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

全体的に私がざっと例に出したてきとうな名前なのでいまいちな気はしてるんですが、特にこれは「変数名で def を使ってるのでそれに合わせたい」のと「FS の表記を揃えたい」ので FsContentDefinition ですかね。(逆に変数名を desc にすることもできると思います)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

変数名そのままで、 interface 名を FsContentDefinition に修正しました。

await expect(readJSON("./foo/test.json")).rejects.toMatchObject({ code: "ENOENT" });
});
});
}); No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

いくつかのファイルで末尾改行がなくなっていそうです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます。修正しました。

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.

4 participants