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

StorybookのVRTにファイル一覧の同期チェックを追加 #2385

Merged
merged 20 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,12 @@ jobs:
- name: Collect patch for snapshots
if: needs.config.outputs.shouldUpdateSnapshots == 'true'
run: |
git add --intent-to-add tests/ # git diff に表示されるようにする
git diff tests/ # ロギング用
git diff --binary tests/ > patch-${{ matrix.os }}.diff
# ログ用
git status
sevenc-nanashi marked this conversation as resolved.
Show resolved Hide resolved

# git diff に表示されるようにする
git add --intent-to-add --all tests/
git diff --binary --cached tests/ > patch-${{ matrix.os }}.diff

- name: Upload patch to artifact
if: needs.config.outputs.shouldUpdateSnapshots == 'true'
Expand All @@ -184,14 +187,14 @@ jobs:
uses: actions/download-artifact@v4
with:
pattern: updated-snapshots-*
path: patches
path: /tmp/patches
merge-multiple: true

- name: Commit updated snapshots
id: commit-updated-snapshots
run: |
# パッチを適用
for patch in patches/*.diff; do
for patch in /tmp/patches/*.diff; do
git apply --allow-empty $patch
rm $patch
Copy link
Member

Choose a reason for hiding this comment

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

(細かいけど)

このpatchesの移動も不要だったりしそう?
/tmp/、windowsでの挙動がよーわからんので避けたいモチベがほんの少しあるかも、くらいですが。
(あと差分がきれい!)

Copy link
Member Author

Choose a reason for hiding this comment

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

個人的に、Gitのworkdirはあんまり汚したくないんですよね…特にコミットが絡んでくると尚更。

これはubuntu-latestで実行されるのでWindowsは無視していいと思います。

Copy link
Member

@Hiroshiba Hiroshiba Dec 3, 2024

Choose a reason for hiding this comment

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

なるほど。

tmpを使うのも万能じゃなくて、他でtmp使うものが同じファイル名で書き出してたときにわけのわからない挙動になりますね。
基本globalなものはすべて恐ろしく、避けたい。

あとtmpは結構特別な扱いになってることも多いので意外と厄介な印象です。
今はlinuxでしか使ってないけど、という感じ。気軽に使って痛い目見るやつ。
ぱっと見つけたmacの例 https://autopp-tech.hatenablog.com/entry/2020/07/12/220856

ただ作業スペースを汚したくないのもわかります・・・。
今回の場合、リポジトリルートにpatchesというファイルができてもバグりますね・・・。

voicevox-update-snapshots-patchesみたいな名前にし、ディレクトリ丸ごと.gitignoreに追加して使うというのはどうでしょう?
voicevoxプレフィックス付けてるのは他で衝突しないようにくらいの意図です)

Copy link
Member Author

Choose a reason for hiding this comment

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

うーん、多分プロジェクトルートに.actions-tmpみたいなフォルダを作ってignoreに入れて、それを共用で使うのが良さそうな気がします(.actions-tmp/patchesみたいな)

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
Member Author

Choose a reason for hiding this comment

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

RUNNER_TEMPを使うようにしました(なんかあったな〜って記憶はあったけど出てこなかった)

Copy link
Member

Choose a reason for hiding this comment

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

同じくです AIに思い出してもらってました
https://chatgpt.com/share/674f0852-bf00-8008-8028-d58103737724

done
Expand Down
49 changes: 42 additions & 7 deletions tests/e2e/storybook/スクリーンショット.spec.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// 起動中のStorybookで様々なStoryを表示し、スクリーンショットを撮って比較するVRT。
// テスト自体はend-to-endではないが、Playwrightを使う関係でe2eディレクトリ内でテストしている。
import fs from "fs/promises";
import path from "path";
import { test, expect, Locator } from "@playwright/test";
import z from "zod";

// Storybook 8.3.5時点でのindex.jsonのスキーマ。
// もしスキーマが変わってテストが通らなくなった場合は、このスキーマを修正する
// もしスキーマが変わってテストが通らなくなった場合は、このスキーマを修正すること
const storybookIndexSchema = z.object({
v: z.literal(5),
entries: z.record(
Expand All @@ -19,12 +21,19 @@ const storybookIndexSchema = z.object({
});
type StorybookIndex = z.infer<typeof storybookIndexSchema>;
type Story = StorybookIndex["entries"][string];
type Theme = "light" | "dark";

const toSnapshotFileName = (story: Story, theme: Theme) =>
`${story.id}-${theme}.png`;

// テスト対象のStory一覧を取得する。
// play-fnが付いているStoryはUnit Test用Storyとみなしてスクリーンショットを撮らない
const getStoriesToTest = (index: StorybookIndex) =>
Object.values(index.entries).filter(
(entry) => entry.type === "story" && !entry.tags.includes("play-fn"),
(entry) =>
entry.type === "story" &&
!entry.tags.includes("play-fn") &&
!entry.tags.includes("skip-screenshot"),
);

let index: StorybookIndex;
Expand Down Expand Up @@ -52,14 +61,11 @@ for (const story of currentStories) {
for (const [story, stories] of Object.entries(allStories)) {
test.describe(story, () => {
for (const story of stories) {
if (story.tags.includes("skip-screenshot")) {
continue;
}
test.describe(story.name, () => {
for (const [theme, name] of [
["light", "ライト"],
["dark", "ダーク"],
]) {
] as const) {
test(`テーマ:${name}`, async ({ page }) => {
test.skip(
process.platform !== "win32",
Expand Down Expand Up @@ -95,11 +101,40 @@ for (const [story, stories] of Object.entries(allStories)) {
elementToScreenshot = root;
}
await expect(elementToScreenshot).toHaveScreenshot(
`${story.id}-${theme}.png`,
toSnapshotFileName(story, theme),
);
});
}
});
}
});
}

test("スクリーンショットの一覧に過不足が無い", async () => {
test.skip(process.platform !== "win32", "Windows以外のためスキップします");
const screenshotFiles = await fs.readdir(test.info().snapshotDir);
const screenshotPaths = screenshotFiles.map((file) =>
path.join(test.info().snapshotDir, file),
);

const expectedScreenshots = currentStories.flatMap((story) =>
(["light", "dark"] as const).map((theme) =>
test.info().snapshotPath(toSnapshotFileName(story, theme)),
),
);

screenshotPaths.sort();
expectedScreenshots.sort();

// update-snapshotsが指定されていたら、余分なスクリーンショットを削除する。
// 指定されていなかったら、スクリーンショットの一覧が一致していることを確認する。
if (test.info().config.updateSnapshots === "all") {
for (const screenshot of screenshotPaths) {
if (!expectedScreenshots.includes(screenshot)) {
await fs.unlink(screenshot);
}
}
} else {
expect(screenshotPaths).toEqual(expectedScreenshots);
}
Copy link
Member

Choose a reason for hiding this comment

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

前のコメントから移動)

削除した旨をログで出しつつ、あとファイルの一番上で案内も! 🙏

});
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading