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

fix: resolve esm file path issue and '<<cwd>>' (example tests) on windows #678

Merged
merged 7 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion examples/0.5-vanilla-esm/migrate.mjs
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion examples/2-es-modules/umzug.mjs
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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({
Expand Down
4 changes: 3 additions & 1 deletion src/umzug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -132,7 +133,8 @@ export class Umzug<Ctx extends object = object> extends emittery<UmzugEvents<Ctx
// eslint-disable-next-line @typescript-eslint/no-var-requires
loadModule = async () => require(filepath) as RunnableMigration<unknown>
} else if (jsExt === '.js' || jsExt === '.mjs') {
loadModule = async () => import(filepath) as Promise<RunnableMigration<unknown>>
const fileUrl = pathToFileURL(filepath).href
loadModule = async () => import(fileUrl) as Promise<RunnableMigration<unknown>>
} else {
loadModule = async () => {
throw new MissingResolverError(filepath)
Expand Down
11 changes: 10 additions & 1 deletion test/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -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>>') // cwd varies by machine
output = normalizePath(output)
output = output.split(normalizedCwd).join('<<cwd>>') // 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, '<<timestamp>>') // the river of time flows only in one direction
return [`\`${cmd}\` output:`, output]
Expand Down
Loading