From 93b8945da37f2182659c7b360c2b6f7b405a7715 Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Tue, 21 Jul 2020 04:29:54 +0200 Subject: [PATCH] test.js: Fix test db path on Windows --- packages/cli/src/commands/test.js | 5 +- packages/cli/src/redwood-tools.js | 13 ++--- packages/internal/src/__tests__/paths.test.ts | 52 ++++++++++++++++++- packages/internal/src/paths.ts | 12 +++++ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/packages/cli/src/commands/test.js b/packages/cli/src/commands/test.js index f4924c6b95ec..81215ce02c62 100644 --- a/packages/cli/src/commands/test.js +++ b/packages/cli/src/commands/test.js @@ -1,6 +1,7 @@ import execa from 'execa' import terminalLink from 'terminal-link' import { getProject } from '@redwoodjs/structure' +import { toPosixPathOnWindows } from '@redwoodjs/internal' import { getPaths } from 'src/lib' import c from 'src/lib/colors' @@ -83,8 +84,8 @@ export const handler = async ({ side, watch, watchAll, collectCoverage }) => { * Migrate test database. This should be moved to somehow be done on a * per-side basis if possible. */ - const DATABASE_URL = - process.env.TEST_DATABASE_URL || `file:${CACHE_DIR}/test.db` + const cacheDirDb = `file:${toPosixPathOnWindows(CACHE_DIR)}/test.db` + const DATABASE_URL = process.env.TEST_DATABASE_URL || cacheDirDb await execa.command(`yarn rw db up`, { stdio: 'inherit', diff --git a/packages/cli/src/redwood-tools.js b/packages/cli/src/redwood-tools.js index fe10ff9712d6..65d3c895327d 100644 --- a/packages/cli/src/redwood-tools.js +++ b/packages/cli/src/redwood-tools.js @@ -3,7 +3,7 @@ import path from 'path' import fs from 'fs' import yargs from 'yargs' -import { getPaths } from '@redwoodjs/internal' +import { getPaths, toPosixPathOnWindows } from '@redwoodjs/internal' import execa from 'execa' import chokidar from 'chokidar' import _ from 'lodash' @@ -67,14 +67,9 @@ export const fixProjectBinaries = (PROJECT_PATH) => { export const copyFiles = async (src, dest) => { // TODO: Figure out if we need to only run based on certain events. - if (process.platform === 'win32') { - // rsync doesn't do Windows paths, so we need to change them to posix paths - const srcDrive = src[0].toLowerCase() - const destDrive = dest[0].toLowerCase() - - src = `/${srcDrive}/${src.substring(3).replace(/\\/g, '/')}` - dest = `/${destDrive}/${dest.substring(3).replace(/\\/g, '/')}` - } + // rsync doesn't do Windows paths, so we need to change them to posix paths + src = toPosixPathOnWindows(src) + dest = toPosixPathOnWindows(dest) await execa('rsync', ['-rtvu --delete', `'${src}'`, `'${dest}'`], { shell: true, diff --git a/packages/internal/src/__tests__/paths.test.ts b/packages/internal/src/__tests__/paths.test.ts index 5025f2e2f2dc..c54231a836d7 100644 --- a/packages/internal/src/__tests__/paths.test.ts +++ b/packages/internal/src/__tests__/paths.test.ts @@ -1,6 +1,6 @@ import path from 'path' -import { processPagesDir, resolveFile } from '../paths' +import { processPagesDir, resolveFile, toPosixPathOnWindows } from '../paths' describe('paths', () => { describe('processPagesDir', () => { @@ -21,4 +21,54 @@ describe('paths', () => { const p = resolveFile(path.join(__dirname, './fixtures/api/test/test')) expect(path.extname(p)).toEqual('.ts') }) + + describe('toPosixPathOnWindows', () => { + it('Returns unmodified input if not on Windows', () => { + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'NotWindows' + }); + + const testPath = 'X:\\some\\weird\\path' + const posixPath = toPosixPathOnWindows(testPath) + + Object.defineProperty(process, 'platform', { + value: originalPlatform + }); + + expect(posixPath).toEqual(testPath) + }) + + it('Only modifies absolute paths', () => { + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'win32' + }); + + const testPath = 'some\\relative\\path' + const posixPath = toPosixPathOnWindows(testPath) + + Object.defineProperty(process, 'platform', { + value: originalPlatform + }); + + expect(posixPath).toEqual(testPath) + }) + + it('Handles drive letters', () => { + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'win32' + }); + + const testPath = 'C:\\some\\full\\path\\to\\file.ext' + const posixPath = toPosixPathOnWindows(testPath) + + Object.defineProperty(process, 'platform', { + value: originalPlatform + }); + + expect(posixPath).toEqual('/c/some/full/path/to/file.ext') + }) + }) }) diff --git a/packages/internal/src/paths.ts b/packages/internal/src/paths.ts index 3ca1e58e83d6..718501d58b1d 100644 --- a/packages/internal/src/paths.ts +++ b/packages/internal/src/paths.ts @@ -193,3 +193,15 @@ export const processPagesDir = ( }) return deps } + +export const toPosixPathOnWindows = (path: string) => { + let posixPath = path + + if (process.platform === 'win32' && /^[A-Z]:\\/.test(path)) { + const drive = path[0].toLowerCase() + + posixPath = `/${drive}/${path.substring(3).replace(/\\/g, '/')}` + } + + return posixPath +}