From 72456396e2837a2768ab83f086f341101f286f81 Mon Sep 17 00:00:00 2001 From: Dmytro Hryshyn Date: Wed, 17 Jan 2024 15:17:40 +0200 Subject: [PATCH] add windows unit tests --- .github/workflows/test.yml | 9 +- .../13/app-directory-boilerplate/test/test.ts | 132 ----------- .../test/test.win.ts | 214 ++++++++++++++++++ package.json | 1 + 4 files changed, 222 insertions(+), 134 deletions(-) create mode 100644 codemods/next/13/app-directory-boilerplate/test/test.win.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fe30c6ec..6f0a3657 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [macos-latest, ubuntu-latest] + os: [macos-latest, ubuntu-latest, windows-latest] steps: - name: Check out Git repository @@ -34,4 +34,9 @@ jobs: run: pnpm install - name: Run codemod unit tests - run: pnpm test + run: | + if [ "${{ matrix.os }}" == "windows-latest" ]; then + pnpm test:win + else + pnpm test + fi diff --git a/codemods/next/13/app-directory-boilerplate/test/test.ts b/codemods/next/13/app-directory-boilerplate/test/test.ts index 7cfb64ce..1a76bb30 100644 --- a/codemods/next/13/app-directory-boilerplate/test/test.ts +++ b/codemods/next/13/app-directory-boilerplate/test/test.ts @@ -210,136 +210,6 @@ describe('next 13 app-directory-boilerplate', function () { ); }); - // Windows - // it.only('should build correct files', async function () { - // const externalFileCommands = await transform({ - // 'C:\\project\\pages\\index.jsx': INDEX_CONTENT, - // 'C:\\project\\pages\\_app.jsx': 'any', - // 'C:\\project\\pages\\app.jsx': 'any', - // 'C:\\project\\pages\\_document.jsx': 'any', - // 'C:\\project\\pages\\_error.jsx': 'any', - // 'C:\\project\\pages\\_404.jsx': 'any', - // 'C:\\project\\pages\\[a]\\[b].tsx': A_B_CONTENT, - // 'C:\\project\\pages\\[a]\\c.tsx': A_C_CONTENT, - // 'C:\\project\\pages\\a\\index.tsx': 'any', - // }); - - // console.log(JSON.stringify(externalFileCommands, null, 2), '???commands') - // deepStrictEqual(externalFileCommands.length, 18); - - // ok( - // externalFileCommands.some( - // (command) => - // command.kind === 'deleteFile' && - // command.path === 'C:\\project\\pages\\_app.jsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => - // command.kind === 'deleteFile' && - // command.path === 'C:\\project\\pages\\_document.jsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => command.path === 'C:\\project\\app\\layout.tsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => command.path === 'C:\\project\\app\\error.tsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => command.path === 'C:\\project\\app\\not-found.tsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => command.path === 'C:\\project\\app\\page.tsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => - // command.path === 'C:\\project\\app\\[a]\\[b]\\page.tsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => command.path === 'C:\\project\\app\\[a]\\c\\page.tsx', - // ), - // ); - - // ok( - // externalFileCommands.some( - // (command) => command.path === 'C:\\project\\app\\a\\page.tsx', - // ), - // ); - - // ok( - // externalFileCommands.some((command) => { - // return ( - // command.kind === 'upsertFile' && - // command.path === 'C:\\project\\app\\components.tsx' && - // command.data.replace(/\W/gm, '') === - // ` - // 'use client'; - // // This file has been sourced from: C\\project\\pages\\index.jsx - - // export default function Index({}) { - // return null; - // } - // ;`.replace(/\W/gm, '') - // ); - // }), - // ); - - // ok( - // externalFileCommands.some((command) => { - // return ( - // command.kind === 'upsertFile' && - // command.path === 'C:\\project\\app\\[a]\\c\\page.tsx' && - // command.data.replace(/\W/gm, '') === - // ` - // // This file has been sourced from: C"\\project\\pages\\[a]\\c.tsx - // import Components from "./components"; - // // TODO reimplement getServerSideProps with custom logic - // const getServerSideProps = () => { - // }; - // export default async function Page(props: any) { - // return ; - // } - // `.replace(/\W/gm, '') - // ); - // }), - // ); - - // ok( - // externalFileCommands.some((command) => { - // return ( - // command.kind === 'upsertFile' && - // command.path === - // 'C:\\project\\app\\[a]\\[b]\\components.tsx' && - // command.data.replace(/\W/gm, '') === - // ` - // 'use client'; - // // This file has been sourced from: C:\\project\\pages\\[a]\\[b].tsx - // `.replace(/\W/gm, '') - // ); - // }), - // ); - // }); - it('migrated page should keep only data-fetching hooks and wrapped client component', async function () { const INDEX_CONTENT = `' const Index = () => ''; @@ -818,8 +688,6 @@ describe('next 13 app-directory-boilerplate', function () { '/opt/project/pages/a/b/c.tsx': index, }); - console.log([upsertPageCommand, _, deleteFileCommand], '???'); - deepStrictEqual(upsertPageCommand?.kind, 'upsertFile'); deepStrictEqual( upsertPageCommand?.path, diff --git a/codemods/next/13/app-directory-boilerplate/test/test.win.ts b/codemods/next/13/app-directory-boilerplate/test/test.win.ts new file mode 100644 index 00000000..692b027c --- /dev/null +++ b/codemods/next/13/app-directory-boilerplate/test/test.win.ts @@ -0,0 +1,214 @@ +import { deepStrictEqual, ok } from 'node:assert'; +import { describe, it } from 'vitest'; +import { DirectoryJSON, Volume, createFsFromVolume } from 'memfs'; +import { buildApi, executeFilemod } from '@intuita-inc/filemod'; +import { repomod } from '../src/index.js'; +import tsmorph from 'ts-morph'; +import { fromMarkdown } from 'mdast-util-from-markdown'; +import { toMarkdown } from 'mdast-util-to-markdown'; +import { mdxjs } from 'micromark-extension-mdxjs'; +import { mdxFromMarkdown, mdxToMarkdown } from 'mdast-util-mdx'; +import { visit } from 'unist-util-visit'; +import { + buildUnifiedFileSystem, + buildPathAPI, +} from '@codemod-registry/utilities'; + +const INDEX_CONTENT = ` +import A from './testQWE'; + +export default function Index({}) { + return null; +} + +export const getStaticProps = async ({}) => { + return { + props: {}, + revalidate: 10, + } +} +`; + +const A_B_CONTENT = ` +import { X } from "../../testABC"; +import { Y } from "./testDEF"; + +export const getStaticPaths = () => { + +} +`; + +const A_C_CONTENT = ` +export const getServerSideProps = () => { + +} +`; + +const transform = async (json: DirectoryJSON) => { + const volume = Volume.fromJSON(json); + + const fs = createFsFromVolume(volume); + + const unifiedFileSystem = buildUnifiedFileSystem(fs); + const pathApi = buildPathAPI('/'); + + const parseMdx = (data: string) => + fromMarkdown(data, { + extensions: [mdxjs()], + mdastExtensions: [mdxFromMarkdown()], + }); + + type Root = ReturnType; + + const stringifyMdx = (tree: Root) => + toMarkdown(tree, { extensions: [mdxToMarkdown()] }); + + const api = buildApi<{ + tsmorph: typeof tsmorph; + parseMdx: typeof parseMdx; + stringifyMdx: typeof stringifyMdx; + visitMdxAst: typeof visit; + }>( + unifiedFileSystem, + () => ({ + tsmorph, + parseMdx, + stringifyMdx, + visitMdxAst: visit, + }), + pathApi, + ); + + return executeFilemod(api, repomod, '/', {}, {}); +}; + +describe('next 13 app-directory-boilerplate', () => { + it('should build correct files', async function () { + const externalFileCommands = await transform({ + 'C:\\project\\pages\\index.jsx': INDEX_CONTENT, + 'C:\\project\\pages\\_app.jsx': 'any', + 'C:\\project\\pages\\app.jsx': 'any', + 'C:\\project\\pages\\_document.jsx': 'any', + 'C:\\project\\pages\\_error.jsx': 'any', + 'C:\\project\\pages\\_404.jsx': 'any', + 'C:\\project\\pages\\[a]\\[b].tsx': A_B_CONTENT, + 'C:\\project\\pages\\[a]\\c.tsx': A_C_CONTENT, + 'C:\\project\\pages\\a\\index.tsx': 'any', + }); + + deepStrictEqual(externalFileCommands.length, 18); + + ok( + externalFileCommands.some( + (command) => + command.kind === 'deleteFile' && + command.path === 'C:\\project\\pages\\_app.jsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => + command.kind === 'deleteFile' && + command.path === 'C:\\project\\pages\\_document.jsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => command.path === 'C:\\project\\app\\layout.tsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => command.path === 'C:\\project\\app\\error.tsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => command.path === 'C:\\project\\app\\not-found.tsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => command.path === 'C:\\project\\app\\page.tsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => + command.path === 'C:\\project\\app\\[a]\\[b]\\page.tsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => + command.path === 'C:\\project\\app\\[a]\\c\\page.tsx', + ), + ); + + ok( + externalFileCommands.some( + (command) => command.path === 'C:\\project\\app\\a\\page.tsx', + ), + ); + + ok( + externalFileCommands.some((command) => { + return ( + command.kind === 'upsertFile' && + command.path === 'C:\\project\\app\\components.tsx' && + command.data.replace(/\W/gm, '') === + ` + 'use client'; + // This file has been sourced from: C\\project\\pages\\index.jsx + + export default function Index({}) { + return null; + } + ;`.replace(/\W/gm, '') + ); + }), + ); + + ok( + externalFileCommands.some((command) => { + return ( + command.kind === 'upsertFile' && + command.path === 'C:\\project\\app\\[a]\\c\\page.tsx' && + command.data.replace(/\W/gm, '') === + ` + // This file has been sourced from: C"\\project\\pages\\[a]\\c.tsx + import Components from "./components"; + // TODO reimplement getServerSideProps with custom logic + const getServerSideProps = () => { + }; + export default async function Page(props: any) { + return ; + } + `.replace(/\W/gm, '') + ); + }), + ); + + ok( + externalFileCommands.some((command) => { + return ( + command.kind === 'upsertFile' && + command.path === + 'C:\\project\\app\\[a]\\[b]\\components.tsx' && + command.data.replace(/\W/gm, '') === + ` + 'use client'; + // This file has been sourced from: C:\\project\\pages\\[a]\\[b].tsx + `.replace(/\W/gm, '') + ); + }), + ); + }); +}); diff --git a/package.json b/package.json index b7fa1179..663e5a97 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "lint:eslint": "eslint --fix .", "lint:prettier": "prettier --write .", "test": "vitest run", + "test:win": "vitest run test.win.ts", "coverage": "vitest run --coverage" }, "devDependencies": {