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: respect folders with dot at the start, when mocking #2244

Merged
merged 1 commit into from Oct 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})
})