From 3baccd63cc586168cd79e4803d84078b926a5e66 Mon Sep 17 00:00:00 2001 From: Anik Ghosh <52236930+anikghosh256@users.noreply.github.com> Date: Tue, 27 Aug 2024 20:26:45 +0600 Subject: [PATCH] fix: resolve esm file path issue and '<>' (example tests) on windows (#678) #676 --------- Co-authored-by: Misha Kaletsky --- .github/workflows/ci.yml | 7 +++++++ examples/0.5-vanilla-esm/migrate.mjs | 3 ++- examples/2-es-modules/umzug.mjs | 3 ++- src/umzug.ts | 4 +++- test/examples.test.ts | 11 ++++++++++- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b02eff0..48089838 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,13 @@ jobs: - run: pnpm test -- --coverage - name: Coverage uses: codecov/codecov-action@v3 + run_windows: + runs-on: windows-latest + steps: + - uses: actions/checkout@v4 + - run: corepack enable + - run: pnpm install + - run: pnpm test create_tgz: runs-on: ubuntu-latest steps: diff --git a/examples/0.5-vanilla-esm/migrate.mjs b/examples/0.5-vanilla-esm/migrate.mjs index 03691b5f..9c12ac15 100644 --- a/examples/0.5-vanilla-esm/migrate.mjs +++ b/examples/0.5-vanilla-esm/migrate.mjs @@ -1,6 +1,7 @@ import { Umzug, JSONStorage } from 'umzug'; +import { fileURLToPath } from 'url' -const __dirname = new URL('.', import.meta.url).pathname.replace(/\/$/, ''); +const __dirname = fileURLToPath(new URL('.', import.meta.url)).replace(/\/$/, '') export const migrator = new Umzug({ migrations: { diff --git a/examples/2-es-modules/umzug.mjs b/examples/2-es-modules/umzug.mjs index 56b7954e..23ec4f65 100644 --- a/examples/2-es-modules/umzug.mjs +++ b/examples/2-es-modules/umzug.mjs @@ -1,6 +1,7 @@ import { Umzug, SequelizeStorage } from 'umzug'; import { Sequelize, DataTypes } from 'sequelize'; import * as path from 'path'; +import os from 'os'; const sequelize = new Sequelize({ dialect: 'sqlite', @@ -10,7 +11,7 @@ const sequelize = new Sequelize({ export const migrator = new Umzug({ migrations: { - glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace('file://', '')) }], + glob: ['migrations/*.{js,cjs,mjs}', { cwd: path.dirname(import.meta.url.replace(os.platform() === 'win32' ? 'file:///' : 'file://', '')) }], }, context: { sequelize, DataTypes }, storage: new SequelizeStorage({ diff --git a/src/umzug.ts b/src/umzug.ts index 4f4ecc0d..a520c325 100644 --- a/src/umzug.ts +++ b/src/umzug.ts @@ -3,6 +3,7 @@ import {glob} from 'fast-glob' import * as fs from 'fs' import * as path from 'path' import * as errorCause from 'pony-cause' +import {pathToFileURL} from 'url' import type {CommandLineParserOptions} from './cli' import {UmzugCLI} from './cli' import type {UmzugStorage} from './storage' @@ -132,7 +133,8 @@ export class Umzug extends emittery require(filepath) as RunnableMigration } else if (jsExt === '.js' || jsExt === '.mjs') { - loadModule = async () => import(filepath) as Promise> + const fileUrl = pathToFileURL(filepath).href + loadModule = async () => import(fileUrl) as Promise> } else { loadModule = async () => { throw new MissingResolverError(filepath) diff --git a/test/examples.test.ts b/test/examples.test.ts index 8451b53a..f1e13506 100644 --- a/test/examples.test.ts +++ b/test/examples.test.ts @@ -23,6 +23,11 @@ const cleanup = (cwd: string) => { ) } +// Convert path to use forward slashes and normalize it +const normalizePath = (str: string) => { + return str.replace(/\\+/g, '/') // Convert backslashes to forward slashes +} + examples.forEach(ex => { test(`example ${ex}`, async () => { const dir = path.join(examplesDir, ex) @@ -32,6 +37,9 @@ examples.forEach(ex => { cleanup(dir) + const cwd = path.resolve(process.cwd()) // Get absolute path + const normalizedCwd = normalizePath(cwd) // Normalize cwd path + const stdout = bash .split('\n') .map(line => line.split('#')[0].trim()) @@ -41,7 +49,8 @@ examples.forEach(ex => { let output = execa.sync('sh', ['-c', `${cmd} 2>&1`], {cwd: dir}).stdout output = stripAnsi(output) output = cmd.startsWith('npm') || cmd.endsWith('--help') ? '...' : output // npm commands and `--help` are formatted inconsistently and aren't v relevant - output = output.split(process.cwd()).join('<>') // cwd varies by machine + output = normalizePath(output) + output = output.split(normalizedCwd).join('<>') // cwd varies by machine output = output.replaceAll(/durationSeconds: .*/g, 'durationSeconds: ???') // migrations durations vary by a few milliseconds output = output.replaceAll(/\d{4}.\d{2}.\d{2}T\d{2}.\d{2}.\d{2}/g, '<>') // the river of time flows only in one direction return [`\`${cmd}\` output:`, output]