Skip to content

Commit

Permalink
test.js: Fix test db path on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Jul 21, 2020
1 parent 124e154 commit 93b8945
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
5 changes: 3 additions & 2 deletions packages/cli/src/commands/test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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',
Expand Down
13 changes: 4 additions & 9 deletions packages/cli/src/redwood-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
52 changes: 51 additions & 1 deletion packages/internal/src/__tests__/paths.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'

import { processPagesDir, resolveFile } from '../paths'
import { processPagesDir, resolveFile, toPosixPathOnWindows } from '../paths'

describe('paths', () => {
describe('processPagesDir', () => {
Expand All @@ -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')
})
})
})
12 changes: 12 additions & 0 deletions packages/internal/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 93b8945

Please sign in to comment.