Use memfs for commons#1576
Conversation
| @@ -0,0 +1,48 @@ | |||
| import * as fs from "fs"; | |||
There was a problem hiding this comment.
他パッケージのテストでも使うので "TestUtil" としましたが、ファイル名に Test と入ってるのが違和感があり、"FsContentUtil" とかのファイル名の方が良いでしょうか?
There was a problem hiding this comment.
ファイル名はそのままでいいと思うのですが、モジュール本体に関係ないので __tests__/ 以下におきませんか。
他パッケージから __tests__/ 以下を import するのも変ではありますが、これは単に (テスト用ユーティリティのパッケージを分離する) 手間を省いただけなので。テストの手間を省く目的のために本体に影響が出るのは避けたいです。
他パッケージで commons の __tests__ 以下を参照する箇所では (本当はテスト用ユーティリティとして獨立させるべきところ手間を省いて commons のコードを参照している旨) コメントをつけておきたいです。
There was a problem hiding this comment.
ありがとうございます。 __tests__/helper/ 配下に移動しました。
| mockfs(mockFsContent); | ||
|
|
||
| let pkgjsonPaths = await NodeModules.listModuleFiles(".", ["dummy@1", "dummy2", "dummy3"], logger); | ||
| let pkgjsonPaths = await NodeModules.listModuleFiles(targetPath, ["dummy", "dummy2", "dummy3"], logger); |
There was a problem hiding this comment.
"dummy@1" は rollup では解決できないので "dummy" へ修正
| "devDependencies": { | ||
| "@akashic/eslint-config": "^3.0.1", | ||
| "@types/mock-fs": "4.13.4", | ||
| "@types/mem-fs": " 2.2.0", |
There was a problem hiding this comment.
memfs と mem-fs は別物では。memfs は @types 不要ではないかと思います。
There was a problem hiding this comment.
ありがとうございます。削除しました。
| @@ -0,0 +1,48 @@ | |||
| import * as fs from "fs"; | |||
There was a problem hiding this comment.
ファイル名はそのままでいいと思うのですが、モジュール本体に関係ないので __tests__/ 以下におきませんか。
他パッケージから __tests__/ 以下を import するのも変ではありますが、これは単に (テスト用ユーティリティのパッケージを分離する) 手間を省いただけなので。テストの手間を省く目的のために本体に影響が出るのは避けたいです。
他パッケージで commons の __tests__ 以下を参照する箇所では (本当はテスト用ユーティリティとして獨立させるべきところ手間を省いて commons のコードを参照している旨) コメントをつけておきたいです。
| return memfs.fs; | ||
| }); | ||
|
|
||
| // editorconfig の parse() で memfs でモックした .editorconfig が読み込めないため parse() をモックし |
There was a problem hiding this comment.
ありがとうございます。修正しました。
| setupFsContent(dir, def); | ||
| return { | ||
| "path": dir, | ||
| func: () => { |
There was a problem hiding this comment.
名前をつけて外部から呼ぶものを func と呼ぶのはさすがに変に思います。 dispose() とかじゃないんでしょうか。
There was a problem hiding this comment.
確かに仰る通りです。dispose へ修正しました。
| const dir = fs.mkdtempSync(target); | ||
| setupFsContent(dir, def); | ||
| return { | ||
| "path": dir, |
| } | ||
| }; | ||
|
|
||
| const baseDir = path.resolve(__dirname, "..", "__tests__", "output"); |
There was a problem hiding this comment.
これ mkdtemp(path.resolve(__dirname, "..", "__tests__", "fixture-")) とはできないでしょうか。
あまりないと思いますが、並列にテストすると崩壊するので原則的にはディレクトリを固定にしたくないように思います。またそうでなくても __tests__/output/ というは普通にも使いかねない名前だと思えるので、被りそうにない名前にはしておきたいです。
There was a problem hiding this comment.
fixture- に修正しました。TestUtil の方で mkdtemp() で fixture-xxxxx のディレクトリが作成される事を確認しました。
|
|
||
| const baseDir = path.resolve(__dirname, "..", "__tests__", "output"); | ||
| let logger: Logger; | ||
| let fsContentResult: testUtil.FsContentResult; |
There was a problem hiding this comment.
原則的に変数は「型名」でなく「意味」で名付けたいです。(たとえば (敵の攻撃力 - 自分の防御力) という計算の結果は、型名にしたがって num と呼ぶより意味で damage と呼ぶ方がわかりやすいように思います)
これはテスト用の fixture のファイルなので、たとえば fixtureContents とするのはどうでしょうか。
There was a problem hiding this comment.
ありがとうございます。修正しました。
| const baseDir = path.resolve(__dirname, "..", "__tests__", "output"); | ||
| let logger: Logger; | ||
| let fsContentResult: testUtil.FsContentResult; | ||
| let targetPath = ""; |
There was a problem hiding this comment.
"target" という新たな言葉を導入されていますが、上に合わせるなら fixtureContentsPath になるかと思います。
利用箇所も多くないので変数を作らず都度 fixtureContents.path としてもよさそうです。
There was a problem hiding this comment.
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(/^\//, ""); |
There was a problem hiding this comment.
空白が 2 つになっています+ , の後の空白が抜けています。他の行にもあるので見直しをお願いします。
There was a problem hiding this comment.
ありがとうございます。修正しました。
| let packageJsonFiles = NodeModules.listPackageJsonsFromScriptsPath(targetPath, filePaths); | ||
| // 本来はルート直下の ./node_modules のパスだが、テストで node_modules のパスがルート直下ではないためパスを生成 | ||
| packageJsonFiles = packageJsonFiles.map(p => path.resolve(targetPath, p)); | ||
| const moduleMainPaths = NodeModules.listModuleMainPaths(packageJsonFiles); |
There was a problem hiding this comment.
moduleMainScripts() のテストが moduleMainPaths() のテストに変わっていますが意図されたものでしょうか。前者が残っているなら前者も引き続きテストすべきに思えます。
There was a problem hiding this comment.
すみません、ミスです。 元の moduleMainScripts() に修正しました。
| 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(/^\//, ""); |
There was a problem hiding this comment.
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(/^\//, ""),
...
});There was a problem hiding this comment.
ありがとうございます。ご指摘いただいたよう直接書くよう修正しました。
| } | ||
| } | ||
|
|
||
| export function preperFsContent(def: FSContentDescDir, baseDir?: string): FsContentResult { |
There was a problem hiding this comment.
- prepare の typo に見えます。
- 実際省略するケースがないなら baseDir も必須にしてしまいませんか。(テスト用ユーティリティで汎用の便利挙動をつけておく動機が薄い)
There was a problem hiding this comment.
ありがとうございます。typo、baseDir を必須化に修正しました。
| [filename: string]: FSContentDescDir | string; | ||
| } | ||
|
|
||
| export interface FsContentResult { |
There was a problem hiding this comment.
〜Result は「〜の結果」に無理やり名前をつけた感じなので、名付けるなら「〜」の部分は関数名にしたいかなと思います。( FsContentResult だと「FsContent した結果」みたいな意味に感じられて違和感があります)
逆にすぱっと FsContent と言い切るという手もありますが、それにしては持ってるプロパティが簡素すぎるので、PrepareFsContentResult ですかね。
There was a problem hiding this comment.
ありがとうございます。最初に FsContent にしたのですが、仰る通りプロパティが少なく Content なのかと疑問に思ったので Result を付けた経緯です。
PrepareFsContentResult に修正しました。
| import * as os from "os"; | ||
| import path from "path"; | ||
|
|
||
| interface FSContentDescDir { |
There was a problem hiding this comment.
全体的に私がざっと例に出したてきとうな名前なのでいまいちな気はしてるんですが、特にこれは「変数名で def を使ってるのでそれに合わせたい」のと「FS の表記を揃えたい」ので FsContentDefinition ですかね。(逆に変数名を desc にすることもできると思います)
There was a problem hiding this comment.
変数名そのままで、 interface 名を FsContentDefinition に修正しました。
| await expect(readJSON("./foo/test.json")).rejects.toMatchObject({ code: "ENOENT" }); | ||
| }); | ||
| }); | ||
| }); No newline at end of file |
There was a problem hiding this comment.
いくつかのファイルで末尾改行がなくなっていそうです。
There was a problem hiding this comment.
ありがとうございます。修正しました。
概要
#1561 の browserify -> rollup 変更に伴うテスト周りの修正。
mock-fsをmemfsへ変更。#1574 (comment) でいただいたコメントを反映し、実ファイルを展開するユーティリティを追加。
備考
#1561 へマージします。commons 以外の対応が完了しCIが通るまでマージしません ローカルで commonsのテストが通る事は確認しています。
scan と lib-manage で一部 commons の NodeModules.ts を利用している箇所でテストが落ちている。別途対応(#1586)。