Skip to content

Commit

Permalink
fix: respect folders with dot at the start, when mocking (#2244)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 31, 2022
1 parent 55fa650 commit 7f55c54
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/ui/client/auto-imports.d.ts
Expand Up @@ -41,6 +41,8 @@ declare global {
const nextTick: typeof import('vue')['nextTick']
const onActivated: typeof import('vue')['onActivated']
const onBeforeMount: typeof import('vue')['onBeforeMount']
const onBeforeRouteLeave: typeof import('vue-router')['onBeforeRouteLeave']
const onBeforeRouteUpdate: typeof import('vue-router')['onBeforeRouteUpdate']
const onBeforeUnmount: typeof import('vue')['onBeforeUnmount']
const onBeforeUpdate: typeof import('vue')['onBeforeUpdate']
const onClickOutside: typeof import('@vueuse/core')['onClickOutside']
Expand Down Expand Up @@ -72,6 +74,7 @@ declare global {
const refThrottled: typeof import('@vueuse/core')['refThrottled']
const refWithControl: typeof import('@vueuse/core')['refWithControl']
const resolveComponent: typeof import('vue')['resolveComponent']
const resolveDirective: typeof import('vue')['resolveDirective']
const resolveRef: typeof import('@vueuse/core')['resolveRef']
const resolveUnref: typeof import('@vueuse/core')['resolveUnref']
const shallowReactive: typeof import('vue')['shallowReactive']
Expand Down Expand Up @@ -164,6 +167,7 @@ declare global {
const useIntervalFn: typeof import('@vueuse/core')['useIntervalFn']
const useKeyModifier: typeof import('@vueuse/core')['useKeyModifier']
const useLastChanged: typeof import('@vueuse/core')['useLastChanged']
const useLink: typeof import('vue-router')['useLink']
const useLocalStorage: typeof import('@vueuse/core')['useLocalStorage']
const useMagicKeys: typeof import('@vueuse/core')['useMagicKeys']
const useManualRefHistory: typeof import('@vueuse/core')['useManualRefHistory']
Expand Down Expand Up @@ -205,6 +209,7 @@ declare global {
const useSessionStorage: typeof import('@vueuse/core')['useSessionStorage']
const useShare: typeof import('@vueuse/core')['useShare']
const useSlots: typeof import('vue')['useSlots']
const useSorted: typeof import('@vueuse/core')['useSorted']
const useSpeechRecognition: typeof import('@vueuse/core')['useSpeechRecognition']
const useSpeechSynthesis: typeof import('@vueuse/core')['useSpeechSynthesis']
const useStepper: typeof import('@vueuse/core')['useStepper']
Expand Down
16 changes: 3 additions & 13 deletions packages/vite-node/src/utils.ts
Expand Up @@ -58,21 +58,11 @@ export function pathFromRoot(root: string, filename: string) {
return filename

const relativePath = relative(root, filename)
// foo.js -> /foo.js
if (!relativePath.startsWith('/') && !relativePath.startsWith('.'))
return `/${relativePath}`

let index = 0
for (const char of relativePath) {
// ../../foo.js
// ^ returns from here -> /foo.js
if (char !== '.' && char !== '/')
return relativePath.slice(index - 1)
const segments = relativePath.split('/')
const startIndex = segments.findIndex(segment => segment !== '..' && segment !== '.')

index++
}

return relativePath
return `/${segments.slice(startIndex).join('/')}`
}

export function toFilePath(id: string, root: string): string {
Expand Down
40 changes: 40 additions & 0 deletions test/vite-node/test/utils.test.ts
@@ -0,0 +1,40 @@
import { describe, expect, test } from 'vitest'
import { pathFromRoot } from 'vite-node/utils'

describe('vite-node utils', () => {
test('usual path from root returns correct path', () => {
const root = '/Users/name/project'
const filename = '/Users/name/project/test.ts'
expect(pathFromRoot(root, filename)).toBe('/test.ts')
})

test('correct path when file and directory share a name', () => {
const root = '/Users/name/project/test'
const filename = '/Users/name/project/test/test/test.ts'
expect(pathFromRoot(root, filename)).toBe('/test/test.ts')
})

test('correct path for node builtins', () => {
const root = '/Users/name/project'
const filename = 'fs'
expect(pathFromRoot(root, filename)).toBe('fs')
})

test('correct path when relative path has back symbols', () => {
const root = '/Users/name/project'
const filename = '/Users/name/project/../test/test.ts'
expect(pathFromRoot(root, filename)).toBe('/test/test.ts')
})

test('correct path when name has a dot at the start', () => {
const root = '/Users/name/project'
const filename = '/Users/name/project/.test.ts'
expect(pathFromRoot(root, filename)).toBe('/.test.ts')
})

test('correct path when subfolder has a dot at the start', () => {
const root = '/Users/name/project'
const filename = '/Users/name/project/../.test/test.ts'
expect(pathFromRoot(root, filename)).toBe('/.test/test.ts')
})
})

0 comments on commit 7f55c54

Please sign in to comment.